okWarrior_EA/references/MQL5/Experts/AutoBots/Study.mq5

349 lines
27 KiB
MQL5

<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| Study.mq5 |
//| 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"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#define Study
#include "Trajectory.mqh"
//+------------------------------------------------------------------+
//| Input parameters |
//+------------------------------------------------------------------+
input int Iterations = 10000;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
STrajectory Buffer[];
CNet Actor;
CNet StateEncoder;
CNet Autobot;
CNet Goal;
//---
float dError;
datetime dtStudied;
//---
CBufferFloat bState;
CBufferFloat bAccount;
CBufferFloat bGoal;
CBufferFloat bGradient;
CBufferFloat bOnes;
CBufferFloat *Result;
vector<float> check;
vector<float> STD_Actor;
vector<float> STD_Goal;
//---
COpenCLMy *OpenCL;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
ResetLastError();
if(!LoadTotalBase())
{
PrintFormat("Error of load study data: %d", GetLastError());
return INIT_FAILED;
}
//--- load models
float temp;
if(!Autobot.Load(FileName + "Traj.nnw", temp, temp, temp, dtStudied, true))
{
Print("Cann't load Autobot model");
return INIT_FAILED;
}
if(!StateEncoder.Load(FileName + "StEnc.nnw", temp, temp, temp, dtStudied, true) ||
!Goal.Load(FileName + "Goal.nnw", temp, temp, temp, dtStudied, true) ||
!Actor.Load(FileName + "Act.nnw", temp, temp, temp, dtStudied, true))
{
CArrayObj *actor = new CArrayObj();
CArrayObj *goal = new CArrayObj();
CArrayObj *encoder = new CArrayObj();
if(!CreateDescriptions(actor, goal, encoder))
{
delete actor;
delete goal;
delete encoder;
return INIT_FAILED;
}
if(!Actor.Create(actor) || !StateEncoder.Create(encoder) || !Goal.Create(goal))
{
delete actor;
delete goal;
delete encoder;
return INIT_FAILED;
}
delete actor;
delete goal;
delete encoder;
//---
}
//---
OpenCL = Actor.GetOpenCL();
StateEncoder.SetOpenCL(OpenCL);
Autobot.SetOpenCL(OpenCL);
Goal.SetOpenCL(OpenCL);
Autobot.TrainMode(false);
//---
Actor.getResults(Result);
if(Result.Total() != NActions)
{
PrintFormat("The scope of the actor does not match the actions count (%d <> %d)", NActions, Result.Total());
return INIT_FAILED;
}
//---
Autobot.getResults(Result);
if(Result.Total() != PrecoderBars * 3)
{
PrintFormat("The scope of the Autobot does not match precoder bars (%d <> %d)", PrecoderBars * 3, Result.Total());
return INIT_FAILED;
}
//---
Autobot.GetLayerOutput(0, Result);
if(Result.Total() != (HistoryBars * BarDescr))
{
PrintFormat("Input size of Autobot doesn't match state description (%d <> %d)", Result.Total(), (HistoryBars * BarDescr));
return INIT_FAILED;
}
//---
StateEncoder.GetLayerOutput(0, Result);
if(Result.Total() != PrecoderBars * 3)
{
PrintFormat("Input size of State Encoder doesn't match precoder bars (%d <> %d)", Result.Total(), PrecoderBars * 3);
return INIT_FAILED;
}
//---
StateEncoder.getResults(Result);
int latent_state = Result.Total();
Actor.GetLayerOutput(0, Result);
if(Result.Total() != latent_state)
{
PrintFormat("Input size of Actor doesn't match output State Encoder (%d <> %d)", Result.Total(), latent_state);
return INIT_FAILED;
}
//---
Goal.GetLayerOutput(0, Result);
latent_state = Result.Total();
Autobot.getResults(Result);
if(Result.Total() != latent_state)
{
PrintFormat("Input size of Goal doesn't match output Autobot (%d <> %d)", Result.Total(), latent_state);
return INIT_FAILED;
}
//---
Goal.getResults(Result);
if(Result.Total() != NRewards)
{
PrintFormat("The scope of Goal doesn't match rewards count (%d <> %d)", Result.Total(), NRewards);
return INIT_FAILED;
}
//---
if(!bOnes.BufferInit(EmbeddingSize, 1) ||
!bGradient.BufferInit(MathMax(EmbeddingSize, AccountDescr), 0) ||
!bOnes.BufferCreate(OpenCL) ||
!bGradient.BufferCreate(OpenCL))
{
PrintFormat("Error of create buffers: %d", GetLastError());
return INIT_FAILED;
}
//---
if(!EventChartCustom(ChartID(), 1, 0, 0, "Init"))
{
PrintFormat("Error of create study event: %d", GetLastError());
return INIT_FAILED;
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
if(!(reason == REASON_INITFAILED || reason == REASON_RECOMPILE))
{
Actor.Save(FileName + "Act.nnw", 0, 0, 0, TimeCurrent(), true);
StateEncoder.Save(FileName + "StEnc.nnw", 0, 0, 0, TimeCurrent(), true);
Goal.Save(FileName + "Goal.nnw", 0, 0, 0, TimeCurrent(), true);
}
delete Result;