ответвлён от dng/NeuroNetworksBook
//+------------------------------------------------------------------+
| |
//| positionencoder.mqh |
| |
//| Copyright 2021, MetaQuotes Ltd. |
| |
//| https://www.mql5.com |
| |
//+------------------------------------------------------------------+
| |
#property copyright "Copyright 2021, MetaQuotes Ltd."
| |
#property link "https://www.mql5.com"
| |
//+------------------------------------------------------------------+
| |
//| Подключаем библиотеки |
| |
//+------------------------------------------------------------------+
| |
#include "bufferdouble.mqh"
| |
//+------------------------------------------------------------------+
| |
//| Class CPositionEncoder |
| |
//| Назначение: Класс позиционного кодированиия |
| |
//+------------------------------------------------------------------+
| |
class CPositionEncoder : public CBufferDouble
| |
{
| |
protected:
| |
ulong m_iCount;
| |
ulong m_iDimension;
| |
public:
| |
CPositionEncoder(void) {};
| |
~CPositionEncoder(void) {};
| |
//---
| |
virtual bool InitEncoder(ulong count, ulong dimension); // Метод инициализации
| |
virtual bool AddEncoder(CBufferDouble *&buffer); // Метод добавление позиционного коирования к исходным данным
| |
//--- Метод работы с файлами
| |
virtual bool Save(const int file_handle);
| |
virtual bool Load(const int file_handle);
| |
};
| |
//+------------------------------------------------------------------+
| |
//| Метод инициализации |
| |
//+------------------------------------------------------------------+
| |
bool CPositionEncoder::InitEncoder(ulong count, ulong dimension)
| |
{
| |
//--- Сожраняем константы
| |
m_iCount = count;
| |
m_iDimension = dimension;
| |
//--- Подготовка буфера
| |
if(!m_mMatrix.Resize(count,dimension))
| |
return false;
| |
//--- Заполнение буфера метками позиционного кодирования
| |
for(int r = 0; r < (int)count; r++)
| |
{
| |
for(int c = 0; c < (int)dimension; c++)
| |
{
| |
double value = r / pow(1000, 2 * c / dimension);
| |
switch(c % 2)
| |
{
| |
case 0:
| |
m_mMatrix[r,c] = MathSin(value);
| |
break;
| |
case 1:
| |
m_mMatrix[r,c] = MathCos(value);
| |
break;
| |
}
| |
}
| |
}
| |
//--- Создание буфера в контексте OpenCL
| |
if(m_cOpenCL)
| |
BufferCreate(m_cOpenCL);
| |
//---
| |
return true;
| |
}
| |
//+------------------------------------------------------------------+
| |
//| Метод добавление позиционного колирования к исходным данным |
| |
//+------------------------------------------------------------------+
| |
bool CPositionEncoder::AddEncoder(CBufferDouble *&buffer)
| |
{
| |
//--- Блок контролей
| |
if(!buffer)
| |
return false;
| |
if(buffer.m_mMatrix.Rows() != m_mMatrix.Rows() ||
| |
buffer.m_mMatrix.Cols() != m_mMatrix.Cols())
| |
if(!InitEncoder(buffer.m_mMatrix.Rows(),buffer.m_mMatrix.Cols()))
| |
return false;
| |
//--- Добавляем метки к буферу исходных данных
| |
buffer.m_mMatrix += m_mMatrix;
| |
//---
| |
return true;
| |
}
| |
//+------------------------------------------------------------------+
| |
//| Метод сохранения элементов класса в файл |
| |
//+------------------------------------------------------------------+
| |
bool CPositionEncoder::Save(const int file_handle)
| |
{
| |
//--- Блок контролей
| |
if(file_handle == INVALID_HANDLE)
| |
return false;
| |
//--- Сохранение констант
| |
if(!FileWriteInteger(file_handle, (int)m_iCount) ||
| |
!FileWriteInteger(file_handle, (int)m_iDimension))
| |
return false;
| |
//---
| |
return true;
| |
}
| |
//+------------------------------------------------------------------+
| |
//| Метод восстановления класса из сохранённых данных |
| |
//+------------------------------------------------------------------+
| |
bool CPositionEncoder::Load(const int file_handle)
| |
{
| |
//--- Блок контролей
| |
if(file_handle == INVALID_HANDLE)
| |
return false;
| |
//--- Считываем константы
| |
m_iCount = (uint)FileReadInteger(file_handle);
| |
m_iDimension = (uint)FileReadInteger(file_handle);
| |
//---
| |
return InitEncoder(m_iCount, m_iDimension);
| |
}
| |
//+------------------------------------------------------------------+
|