//---------------------------------------------------------------------------
#pragma hdrstop
#include <vcl/SysUtils.hpp>
#include "Unit2.h"
//---------------------------------------------------------------------------
double	 TMultiLayerPerceptron::Vector_Multiple (const vector<double	> lhs , const vector<double	> rhs )
{
  double multiple = 0.0;
  if ( lhs.size() != rhs.size() )
    return -0.1;

  for ( size_t Itr = 0 ; Itr < lhs.size() ; Itr ++ )
    multiple += lhs[Itr] * rhs[Itr];

  return multiple;
};
//---------------------------------------------------------------------------
void __fastcall TMultiLayerPerceptron::Execute(void)
{
  Study();
}
//---------------------------------------------------------------------------
double	 TMultiLayerPerceptron::TSS(const vector<double	> V)
{
  double tss= 0.0;
    for ( size_t inc = 0 ; inc < V.size() ; inc ++ )
      tss += (V[inc] * V[inc]);
  return tss;
}
//---------------------------------------------------------------------------
void  TMultiLayerPerceptron::Study( void )
{
  VOutput.clear();
  VAdd.clear();
  VDelta.clear();
  VLDelta.clear();
  double MidData[29];
  // Data 저장소 초기화
  for ( size_t i = 0 ; i < VInputMatrix[0].size() ; i ++ )
    VAdd.push_back(0.0);

  for (size_t i = 0 ; i < VTarget.size() ; i ++ )
    {
      VOutput.push_back(0.0);
      VDelta.push_back(0.0);
      VLDelta.push_back(0.0);
    }
  // Data 저장소의 확보

  for (unsigned int inc = 0  ; FRun_Count != inc ;inc++ )
    {
      for ( size_t i = 0 ; i < VInputMatrix.size() ; i ++ )
        {
          VOutput[i] = Vector_Multiple(VInputMatrix[i],VAdd);
          //Output = 가중치 * 입력치

          VDelta[i] = VTarget[i] - VOutput[i];
          //Delta = 목표치 - 실 출력치

          for ( size_t inc = 0 ; inc < VInputMatrix[i].size() ; inc ++ )
            VLDelta[inc] = FStudyAddition * VDelta[i] * VInputMatrix[i][inc];
          //VDelta = 학습가중치 * Delta * 입력치

          for ( size_t Itr = 0 ; Itr < VAdd.size() ; Itr ++ )
            VAdd[Itr] += VLDelta[Itr];
            //가중치 = 가중치 + VDelta
            //VAdd = VAdd + VDelta
          MidData[0] = inc;
          MidData[1] = i;
          MidData[2] = VOutput[i];
          MidData[3] = VDelta[i];
          size_t size = VAdd.size();
          for ( size_t Itr = 0 ; Itr < VAdd.size() ; Itr ++ )
            MidData[3+Itr] = VAdd[Itr];

          SendMessage(FHandle ,WM_TERM_VALUE,0,(long)MidData);
        }
      Tss = TSS(VDelta);
      SendMessage(FHandle ,WM_TSS,0,(long)Tss);
      if ( Tss == 0.0 ) break;
    }
  SendMessage(FHandle ,WM_TERM_END,0,0);
}
//---------------------------------------------------------------------------
double	  TMultiLayerPerceptron::Test( vector<double	> Data )
{
  return Vector_Multiple(Data,VAdd);
}
//---------------------------------------------------------------------------
#pragma package(smart_init)
 