Warrior_EA/references/MQL5/Experts/GCRL/Trajectory.mqh

414 lines
24 KiB
MQL5

<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Trajectory.mqh |
//| Copyright DNG<EFBFBD> |
//| https://www.mql5.com/en/users/dng |
//+------------------------------------------------------------------+
#property copyright "Copyright DNG<00>"
#property link "https://www.mql5.com/en/users/dng"
#property version "1.00"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "..\RL\FQF.mqh"
//---
#define HistoryBars 20 //Depth of history
#define BarDescr 12 //Elements for 1 bar description
#define AccountDescr 8 //Account description
#define NActions 4 //Number of possible Actions
#define NSkills 20 //Number of skills
#define Buffer_Size 6500
#define FileName "GCRL"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
struct SState
{
float state[HistoryBars * BarDescr];
float account[AccountDescr-1];
//---
SState(void);
//---
bool Save(int file_handle);
bool Load(int file_handle);
//--- overloading
void operator=(const SState &obj) { ArrayCopy(state, obj.state); ArrayCopy(account, obj.account); }
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
SState::SState(void)
{
ArrayInitialize(state, 0);
ArrayInitialize(account, 0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool SState::Save(int file_handle)
{
if(file_handle == INVALID_HANDLE)
return false;
//---
int total = ArraySize(state);
if(FileWriteInteger(file_handle, total) < sizeof(int))
return false;
for(int i = 0; i < total; i++)
if(FileWriteFloat(file_handle, state[i]) < sizeof(float))
return false;
//---
total = ArraySize(account);
if(FileWriteInteger(file_handle, total) < sizeof(int))
return false;
for(int i = 0; i < total; i++)
if(FileWriteFloat(file_handle, account[i]) < sizeof(float))
return false;
//---
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool SState::Load(int file_handle)
{
if(file_handle == INVALID_HANDLE)
return false;
if(FileIsEnding(file_handle))
return false;
//---
int total = FileReadInteger(file_handle);
if(total != ArraySize(state))
return false;
//---
for(int i = 0; i < total; i++)
{
if(FileIsEnding(file_handle))
return false;
state[i] = FileReadFloat(file_handle);
}
//---
total = FileReadInteger(file_handle);
if(total != ArraySize(account))
return false;
//---
for(int i = 0; i < total; i++)
{
if(FileIsEnding(file_handle))
return false;
account[i] = FileReadFloat(file_handle);
}
//---
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
struct STrajectory
{
SState States[Buffer_Size];
int Actions[Buffer_Size];
float Revards[Buffer_Size];
int Total;
float DiscountFactor;
bool CumCounted;
//---
STrajectory(void);
//---
bool Add(SState &state, int action, float reward);
void CumRevards(void);
//---
bool Save(int file_handle);
bool Load(int file_handle);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
STrajectory::STrajectory(void) : Total(0),
DiscountFactor(0.9f),
CumCounted(false)
{
ArrayInitialize(Actions, -1);
ArrayInitialize(Revards, 0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool STrajectory::Save(int file_handle)
{
if(file_handle == INVALID_HANDLE)
return false;
//---
if(!CumCounted)
CumRevards();
if(FileWriteInteger(file_handle, Total) < sizeof(int))
return false;
for(int i = 0; i < Total; i++)
{
if(!States[i].Save(file_handle))
return false;
if(FileWriteInteger(file_handle, Actions[i]) < sizeof(int))
return false;
if(FileWriteFloat(file_handle, Revards[i]) < sizeof(float))
return false;
}
//---
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool STrajectory::Load(int file_handle)
{
if(file_handle == INVALID_HANDLE)
return false;
//---
Total = FileReadInteger(file_handle);
if(Total >= ArraySize(Actions))
return false;
CumCounted = true;
//---
for(int i = 0; i < Total; i++)
{
if(!States[i].Load(file_handle))
return false;
Actions[i] = FileReadInteger(file_handle);
Revards[i] = FileReadFloat(file_handle);
}
//---
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void STrajectory::CumRevards(void)
{
if(CumCounted)
return;
//---
for(int i = Total - 2; i >= 0; i--)
Revards[i] += Revards[i + 1] * DiscountFactor;
CumCounted = true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool STrajectory::Add(SState &state, int action, float reward)
{
if(Total + 1 >= ArraySize(Actions))
return false;
States[Total] = state;
Actions[Total] = action;
if(Total > 0)
Revards[Total - 1] = reward;
Total++;
//---
return true;
}
//+------------------------------------------------------------------+
//| |