//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit3.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
 RunGrid->Cells[0][0] = "Ep";
 RunGrid->Cells[1][0] = "Pat";
 RunGrid->Cells[2][0] = "Output";
 RunGrid->Cells[3][0] = "Error";
 GridIdx = 1;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::OnStudyDataGet(TMessage& Msg)
{
  double* MidTerm = (double*)Msg.LParam;

  for ( int inc = 0 ; inc < 29 ; inc ++ )
    RunGrid->Cells[inc][GridIdx] = MidTerm[inc];
  Addition.clear();
  Addition.assign(&MidTerm[4],&MidTerm[29]);
  GridIdx++;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::OnStudyEnd(TMessage&  Msg)
{
  Button2->Enabled = true;
//  Addition.clear();
  MLP->GetAddition(Add);
//  Addition = MLP->GetAddition();
  MLP->Terminate();
}

//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton9Click(TObject *Sender)
{
  TSpeedButton * Btn = dynamic_cast<TSpeedButton*>(Sender);
  if ( Btn->Caption == "1" )
    {
      Btn->Caption = "0";
      Btn->Down = true;
    }
  else
    if ( Btn->Caption == "0" )
      {
        Btn->Caption = "1";
        Btn->Down = false;
      }
//Currency
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
  if ( MLP != NULL )
    delete MLP;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  for ( int i = 0 ; i  < RunGrid->RowCount ; i ++ )
    for ( int j = 0 ; j < RunGrid->ColCount ; j ++ )
      RunGrid->Cells[i][j] = "";

  RunGrid->Cells[0][0] = "Ep";
  RunGrid->Cells[1][0] = "Pat";
  RunGrid->Cells[2][0] = "Output";
  RunGrid->Cells[3][0] = "Error";
      
  Button2->Enabled = false;
  double	 Input0[25]  = {0,1,1,1,0, 1,0,0,0,1, 1,0,0,0,1, 1,0,0,0,1 ,0,1,1,1,0};
  double	 Input1[25]  = {0,0,1,0,0, 0,1,1,0,0, 0,0,1,0,0, 0,0,1,0,0, 0,1,1,1,0};
  double	 Input2[25]  = {0,1,1,1,0, 1,0,0,0,1, 0,0,1,1,0, 0,1,0,0,0, 1,1,1,1,1};
  double	 Input3[25]  = {0,1,1,1,0, 0,0,0,0,1, 0,1,1,1,0, 0,0,0,0,1, 0,1,1,1,0};
  double	 Input4[25]  = {0,1,0,1,0, 0,1,0,1,0, 0,1,1,1,1, 0,0,0,1,0, 0,0,0,1,0};
  double	 Input5[25]  = {0,1,1,1,0, 0,1,0,0,0, 0,1,1,1,0, 0,0,0,1,0, 0,1,1,1,0};
  double	 Input6[25]  = {0,1,1,1,0, 0,1,0,0,0, 0,1,1,1,0, 0,1,0,1,0, 0,1,1,1,0};
  double	 Input7[25]  = {0,1,1,1,0, 0,0,0,1,0, 0,0,0,1,0, 0,0,0,1,0, 0,0,0,1,0};
  double	 Input8[25]  = {0,1,1,1,0, 1,0,0,0,1, 0,1,1,1,0, 1,0,0,0,1, 0,1,1,1,0};
  double	 Input9[25]  = {0,1,1,1,0, 0,1,0,1,0, 0,1,1,1,0, 0,0,0,1,0, 0,0,0,1,0};
  double	    man[25]  = {1,1,1,0,1, 0,0,1,0,1, 1,1,1,1,1, 1,0,1,0,0, 1,0,1,1,1};
  double	 Target[11]   = {0,10,20,30,40,50,60,70,80,90,100};
  unsigned int Run_Count = Edit1->Text.ToIntDef(100);
  double	Study_Addition = Edit2->Text.ToDouble();

  vector < vector < double	 > > InputMatrix(11,vector<double	>(25));
  vector<double	> VTarget(25);

  VTarget.assign(&Target[0],&Target[11]);

  InputMatrix[0].assign(&Input0[0],&Input0[25]);
  InputMatrix[1].assign(&Input1[0],&Input1[25]);
  InputMatrix[2].assign(&Input2[0],&Input2[25]);
  InputMatrix[3].assign(&Input3[0],&Input3[25]);
  InputMatrix[4].assign(&Input4[0],&Input4[25]);
  InputMatrix[5].assign(&Input5[0],&Input5[25]);
  InputMatrix[6].assign(&Input6[0],&Input6[25]);
  InputMatrix[7].assign(&Input7[0],&Input7[25]);
  InputMatrix[8].assign(&Input8[0],&Input8[25]);
  InputMatrix[9].assign(&Input9[0],&Input9[25]);
  InputMatrix[10].assign(&man[0],&man[25]);

  RunGrid->RowCount = Run_Count * 12 +1;
  if ( MLP != NULL )
    delete MLP;
  MLP = NULL;

  MLP = new ThreadMLP(Handle);
  MLP->StudyAddition(Study_Addition);
  MLP->Run_Count(Run_Count);
  MLP->InputMatrix(InputMatrix);
  MLP->Target(VTarget);
  MLP->Resume();
  Label3->Caption = "¹Ì½Ç½Ã";
  GridIdx = 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  vector<double	> VInput(25);
  double Input[25];
  double Result;
  for( int i=0 ; i < this->ComponentCount ; i++ )
    {
       if( dynamic_cast<TSpeedButton*>(this->Components[i]) )
         {
           TSpeedButton * Btn = dynamic_cast<TSpeedButton*>(this->Components[i]);
           Input[Btn->Tag] = Btn->Caption.ToDouble();
         }
    }
  VInput.assign(&Input[0],&Input[25]);
  size_t ISize = VInput.size();
  Label6->Caption = Vector_Multiple(VInput,Add);
//  Label3->Caption = Vector_Multiple(Addition,VInput);
}
//---------------------------------------------------------------------------
double	 TForm1::Vector_Multiple (const vector<double	> lhs , const vector<double	> rhs )
{
  size_t lhsSize = lhs.size();
  size_t rhsSize = rhs.size();

  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;
};

double __fastcall TForm1::PerceptronTest( const vector<double> Data)
{
  return Vector_Multiple(Addition , Data );
}

void __fastcall TForm1::OnTss(TMessage& Msg)
{
  double Tss = (double)Msg.LParam;

    RunGrid->Cells[0][GridIdx] = "TSS Value";
    RunGrid->Cells[1][GridIdx] = Tss;

  GridIdx++;
}

