Original_NNB/MQL5/Include/NeuroNetworksBook/realization/positionencoder.mqh

123 lines
9.3 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:15:14 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| positionencoder.mqh |
//| Copyright 2021, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link "https://www.mql5.com"
//+------------------------------------------------------------------+
//| >4:;NG05< 181;8>B5:8 |
//+------------------------------------------------------------------+
#include "bufferdouble.mqh"
//+------------------------------------------------------------------+
//| Class CPositionEncoder |
//| 07=0G5=85: ;0AA ?>78F8>==>3> :>48@>20=88O |
//+------------------------------------------------------------------+
class CPositionEncoder : public CBufferDouble
{
protected:
uint m_iCount;
uint m_iDimension;
public:
CPositionEncoder(void) {};
~CPositionEncoder(void) {};
//---
virtual bool InitEncoder(uint count, uint dimension); // 5B>4 8=8F80;870F88
virtual bool AddEncoder(CBufferDouble *&buffer); // 5B>4 4>102;5=85 ?>78F8>==>3> :>8@>20=8O : 8AE>4=K< 40==K<
//--- 5B>4 @01>BK A D09;0<8
virtual bool Save(const int file_handle);
virtual bool Load(const int file_handle);
};
//+------------------------------------------------------------------+
//| 5B>4 8=8F80;870F88 |
//+------------------------------------------------------------------+
bool CPositionEncoder::InitEncoder(uint count, uint dimension)
{
//--- !>6@0=O5< 5:>=AB0=BK
m_iCount = count;
m_iDimension = dimension;
m_data_total = (int)(count * dimension);
//--- >43>B>2:0 1CD5@0
if(ArrayResize(m_data, m_data_total) < m_data_total)
{
m_data_total = 0;
return false;
}
m_data_max = m_data_total;
//--- 0?>;=5=85 1CD5@0 <5B:0<8 ?>78F8>==>3> :>48@>20=8O
for(int i = 0; i < (int)count; i++)
{
for(int d = 0; d < (int)dimension; d++)
{
int pos = i * (int)dimension + d;
double value = i / pow(1000, 2 * d / dimension);
switch(d % 2)
{
case 0:
m_data[pos]
= MathSin(value);
break;
case 1:
m_data[pos] = MathCos(value);
break;
}
}
}
//--- !>740=85 1CD5@0 2 :>=B5:AB5 OpenCL
if(CheckPointer(m_cOpenCL) != POINTER_INVALID)
BufferCreate(m_cOpenCL);
//---
return true;
}
//+------------------------------------------------------------------+
//| 5B>4 4>102;5=85 ?>78F8>==>3> :>;8@>20=8O : 8AE>4=K< 40==K< |
//+------------------------------------------------------------------+
bool CPositionEncoder::AddEncoder(CBufferDouble *&buffer)
{
//--- ;>: :>=B@>;59
if(CheckPointer(buffer) == POINTER_INVALID)
return false;
if(buffer.Total() > m_data_total)
if(!InitEncoder(1,buffer.Total()))
return false;
//--- >102;O5< <5B:8 : 1CD5@C 8AE>4=KE 40==KE
double result[];
int total = buffer.Total();
if(ArrayResize(result, total) < total)
return false;
for(int i = 0; i < total; i++)
buffer.m_data[i] += m_data[i];
//---
return true;
}
//+------------------------------------------------------------------+
//| 5B>4 A>E@0=5=8O M;5<5=B>2 :;0AA0 2 D09; |
//+------------------------------------------------------------------+
bool CPositionEncoder::Save(const int file_handle)
{
//--- ;>: :>=B@>;59
if(file_handle == INVALID_HANDLE)
return false;
//--- !>E@0=5=85 :>=AB0=B
if(!FileWriteInteger(file_handle, (int)m_iCount) ||
!FileWriteInteger(file_handle, (int)m_iDimension))
return false;
//---
return true;
}
//+------------------------------------------------------------------+
//| 5B>4 2>AAB0=>2;5=8O :;0AA0 87 A>E@0=Q==KE 40==KE |
//+------------------------------------------------------------------+
bool CPositionEncoder::Load(const int file_handle)
{
//--- ;>: :>=B@>;59
if(file_handle == INVALID_HANDLE)
return false;
//--- !G8BK205< :>=AB0=BK
m_iCount = (uint)FileReadInteger(file_handle);
m_iDimension = (uint)FileReadInteger(file_handle);
//---
return InitEncoder(m_iCount, m_iDimension);
}
//+------------------------------------------------------------------+