okWarrior_EA/references/MQL5/Experts/Evalutionary/NetGenetic.mqh

653 行
47 KiB
MQL5

<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| NetGenetic.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 "..\NeuroNet_DNG\NeuroNet.mqh"
//---
#define MaxMutation 0.5f
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CNetGenetic : public CNet
{
protected:
uint i_PopulationSize;
vectorf v_Probability;
vectorf v_Rewards;
matrixf m_Weights;
matrixf m_WeightsConv;
//---
virtual bool CreatePopulation(void);
virtual int GetAction(CBufferFloat * probability);
virtual bool GetWeights(uint layer);
float NextGenerationWeight(matrixf &array, uint shift, vectorf &probability);
float GenerateWeight(uint total);
public:
CNetGenetic();
~CNetGenetic();
//---
virtual bool Create(CArrayObj *Description, uint population_size);
virtual bool SetPopulationSize(uint size);
virtual bool feedForward(CArrayFloat *inputVals, int window = 1, bool tem = true);
virtual bool Rewards(CArrayFloat *rewards);
virtual bool NextGeneration(float quantile, float mutation, float &average, float &mamximum);
virtual bool Load(string file_name, uint population_size, bool common = true);
virtual bool SaveModel(string file_name, int model, bool common = true);
//---
virtual bool CopyModel(CArrayLayer *source, uint model);
virtual bool Detach(void);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CNetGenetic::CNetGenetic() : i_PopulationSize(100)
{
SetPopulationSize(i_PopulationSize);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CNetGenetic::~CNetGenetic()
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CNetGenetic::Create(CArrayObj *Description, uint population_size)
{
if(CheckPointer(Description) == POINTER_INVALID)
return false;
//---
if(!SetPopulationSize(population_size))
return false;
CNet::Create(Description);
return CreatePopulation();
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CNetGenetic::SetPopulationSize(uint size)
{
i_PopulationSize = size;
v_Probability = vectorf::Zeros(i_PopulationSize);
v_Rewards = vectorf::Zeros(i_PopulationSize);
//---
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CNetGenetic::Load(string file_name, uint population_size, bool common = true)
{
float temp;
datetime dt;
if(!CNet::Load(file_name, temp, temp, temp, dt, common))
return false;
//---
if(!SetPopulationSize(population_size))
return false;
return CreatePopulation();
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CNetGenetic::CreatePopulation(void)
{
if(!layers || layers.Total() < 2)
return false;
//---
CLayer *layer = layers.At(0);
if(!layer || !layer.At(0))
return false;
//---
CNeuronBaseOCL *neuron_ocl = layer.At(0);
int prev_count = neuron_ocl.Neurons();
for(int i = 1; i < layers.Total(); i++)
{
layer = layers.At(i);
if(!layer || !layer.At(0))
return false;
//---
neuron_ocl = layer.At(0);
CLayerDescription *desc = neuron_ocl.GetLayerInfo();
int outputs = neuron_ocl.getConnections();
//---
for(uint n = layer.Total(); n < i_PopulationSize; n++)
{
CNeuronConvOCL *neuron_conv_ocl = NULL;
CNeuronProofOCL *neuron_proof_ocl = NULL;
CNeuronAttentionOCL *neuron_attention_ocl = NULL;
CNeuronMLMHAttentionOCL *neuron_mlattention_ocl = NULL;
CNeuronDropoutOCL *dropout = NULL;
CNeuronBatchNormOCL *batch = NULL;
CVAE *vae = NULL;
CNeuronLSTMOCL *lstm = NULL;
switch(layer.At(0).Type())
{
case defNeuron:
case defNeuronBaseOCL:
neuron_ocl = new CNeuronBaseOCL();
if(CheckPointer(neuron_ocl) == POINTER_INVALID)
return false;
if(!neuron_ocl.Init(outputs, n, opencl, desc.count, desc.optimization, desc.batch))
{
delete neuron_ocl;
return false;
}
neuron_ocl.SetActivationFunction(desc.activation);
if(!layer.Add(neuron_ocl))
{
delete neuron_ocl;
return false;
}
neuron_ocl = NULL;
break;
//---
case defNeuronConvOCL:
neuron_conv_ocl = new CNeuronConvOCL();
if(CheckPointer(neuron_conv_ocl) == POINTER_INVALID)
return false;
if(!neuron_conv_ocl.Init(outputs, n, opencl, desc.window, desc.step, desc.window_out, desc.count, desc.optimization, desc.batch))
{
delete neuron_conv_ocl;
return false;
}
neuron_conv_ocl.SetActivationFunction(desc.activation);
if(!layer.Add(neuron_conv_ocl))
{
delete neuron_conv_ocl;
return false;
}
neuron_conv_ocl = NULL;
break;
//---
case defNeuronProofOCL:
neuron_proof_ocl = new CNeuronProofOCL();
if(!neuron_proof_ocl)
return false;
if(!neuron_proof_ocl.Init(outputs, n, opencl, desc.window, desc.step, desc.count, desc.optimization, desc.batch))
{
delete neuron_proof_ocl;
return false;
}
neuron_proof_ocl.SetActivationFunction(desc.activation);
if(!layer.Add(neuron_proof_ocl))
{
delete neuron_proof_ocl;
return false;
}
neuron_proof_ocl = NULL;
break;
//---
case defNeuronAttentionOCL:
neuron_attention_ocl = new CNeuronAttentionOCL();
if(CheckPointer(neuron_attention_ocl) == POINTER_INVALID)
return false;
if(!neuron_attention_ocl.Init(outputs, n, opencl, desc.window, desc.count, desc.optimization, desc.batch))
{
delete neuron_attention_ocl;
return false;
}
neuron_attention_ocl.SetActivationFunction(desc.activation);
if(!layer.Add(neuron_attention_ocl))
{
delete neuron_attention_ocl;
return false;
}
neuron_attention_ocl = NULL;
break;
//---
case defNeuronMHAttentionOCL:
neuron_attention_ocl = new CNeuronMHAttentionOCL();
if(CheckPointer(neuron_attention_ocl) == POINTER_INVALID)
return false;
if(!neuron_attention_ocl.Init(outputs, n, opencl, desc.window, desc.count, desc.optimization, desc.batch))
{
delete neuron_attention_ocl;
return false;
}
neuron_attention_ocl.SetActivationFunction(desc.activation);
if(!layer.Add(neuron_attention_ocl))
{
delete neuron_attention_ocl;
return false;
}
neuron_attention_ocl = NULL;
break;
//---
case defNeuronMLMHAttentionOCL:
neuron_mlattention_ocl = new CNeuronMLMHAttentionOCL();
if(CheckPointer(neuron_mlattention_ocl) == POINTER_INVALID)
return false;
if(!neuron_mlattention_ocl.Init(outputs, n, opencl, desc.window, desc.window_out, desc.step, desc.count, desc.layers, desc.optimization, desc.batch))
{
delete neuron_mlattention_ocl;
return false;
}
neuron_mlattention_ocl.SetActivationFunction(desc.activation);
if(!layer.Add(neuron_mlattention_ocl))
{
delete neuron_mlattention_ocl;
return false;
}
neuron_mlattention_ocl = NULL;
break;
//---
case defNeuronDropoutOCL:
dropout = new CNeuronDropoutOCL();
if(CheckPointer(dropout) == POINTER_INVALID)
return false;
if(!dropout.Init(outputs, n, opencl, desc.count, desc.probability, desc.optimization, desc.batch))
{
delete dropout;
return false;
}
if(!layer.Add(dropout))
{
delete dropout;
return false;
}
dropout = NULL;
break;
//---
case defNeuronBatchNormOCL:
batch = new CNeuronBatchNormOCL();
if(CheckPointer(batch) == POINTER_INVALID)
return false;
if(!batch.Init(outputs, n, opencl, desc.count, desc.batch, desc.optimization))
{
delete batch;
return false;
}
batch.SetActivationFunction(desc.activation);
if(!layer.Add(batch))
{
delete batch;
return false;
}
batch = NULL;
break;
//---
case defNeuronVAEOCL:
vae = new CVAE();
if(!vae)
return false;
if(!vae.Init(outputs, n, opencl, desc.count, desc.optimization, desc.batch))
{
delete vae;
return false;
}
if(!layer.Add(vae))
{
delete vae;
return false;
}
vae = NULL;
break;
case defNeuronLSTMOCL:
lstm = new CNeuronLSTMOCL();
if(!lstm)
return false;
if(!lstm.Init(outputs, n, opencl, desc.count, desc.optimization, desc.batch))
{
delete lstm;
return false;
}
if(!lstm.SetInputs(prev_count))
{
delete lstm;
return false;
}
if(!layer.Add(lstm))
{
delete lstm;
return false;
}
lstm = NULL;
break;
//---
case defNeuronSoftMaxOCL:
neuron_ocl = new CNeuronSoftMaxOCL();
if(!neuron_ocl)
return false;
if(!neuron_ocl.Init(outputs, n, opencl, desc.count, desc.optimization, desc.batch))
{
delete neuron_ocl;
return false;
}
if(!layer.Add(neuron_ocl))
{
delete neuron_ocl;
return false;