Hexadroid

Untitled

Sep 18th, 2020
787
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma hdrstop
  2. #pragma argsused
  3.  
  4. #ifdef _WIN32
  5. #include <tchar.h>
  6. #else
  7.   typedef char _TCHAR;
  8.   #define _tmain main
  9. #endif
  10.  
  11. #include <algorithm>
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <iostream>
  15.  
  16. using std::cin;
  17. using std::cout;
  18. using std::endl;
  19.  
  20.  
  21.     const long double cyclesi = 1000;
  22.     const long double learningrate = 0.0000000001;  //higher values can cause Inf or Nan error
  23.     const int training_inputs=1020;
  24.  
  25.     long double training_set_inputs[training_inputs][3];
  26.     long double training_set_outputs[training_inputs], new_situation[training_inputs][3];
  27.  
  28.     long double funcSynapticWeight[4];
  29.     long double funcOutput5, funcDOToutput5, funcoutput;
  30.     long double Bfuncoutput[training_inputs], BfuncPart[training_inputs], Bfunc_DerivativePart[training_inputs], BfuncerrorPart[training_inputs], BfuncadjustmentPart[training_inputs], BfuncadjustSynapticWeight[4];
  31.  
  32.     long double oldoutput, cool, indicator,i=1,ErrorT;
  33.     int m,q,qq, create;
  34.  
  35.     int f,g,h,y;
  36.  
  37.  
  38. long double outt(long double (*array)[training_inputs][3], int b)
  39. {
  40.     long double calc;
  41.  
  42.         calc = 60*(*array)[b][0] + 37*(*array)[b][1];
  43.      //  calc= 2*powl((*array)[b][0],3.0)  -  5.5*pow((*array)[b][1],2)  +  4.77*(*array)[b][2];
  44.  
  45.     return calc;
  46. }
  47.  
  48.  
  49.            // func function
  50. long double s(long double x)
  51. {
  52.     return (x>=0)?(x):(0);
  53. }
  54.  
  55.             //func derivative
  56. long double d(long double x)
  57. {
  58.     return (x>=0)?(1):(0);
  59. }
  60.  
  61.  
  62.  int _tmain(int argc, _TCHAR* argv[])
  63. {
  64.  
  65.     srand (time(NULL));
  66.  
  67.  
  68.     new_situation[0][0]=63.00;new_situation[0][1]=35.00;new_situation[0][2]=62.00;
  69.     cool = outt(&new_situation,0);
  70.  
  71.        printf("\n the output should be approx %.2Lf\n",cool);
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  for (create = 0; create < training_inputs; create++) {
  80.  
  81.                 bool redo=false;
  82.  
  83.                 do
  84.                 {
  85.                 training_set_inputs[create][0]= (rand() % 60) + 20.00L;
  86.                 training_set_inputs[create][1]= (rand() % 60) + 20.00L;
  87.                 training_set_inputs[create][2]= (rand() % 60) + 20.00L;
  88.  
  89.  
  90.                 training_set_outputs[create] = outt(&training_set_inputs, create);
  91.                    if (training_set_outputs[create]==0) redo=true;
  92.  
  93.  
  94.                 }  while (redo==true);
  95.  
  96.  
  97.  
  98.  
  99.             }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.     funcSynapticWeight[1]=((rand()%1000)/1000.00)+0.01;
  107.     funcSynapticWeight[2]=((rand()%1000)/1000.00)+0.01;
  108.     funcSynapticWeight[3]=((rand()%1000)/1000.00)+0.01;
  109.  
  110.  
  111.  
  112.     printf("SynapticWeight[1]=%.2Lf , SynapticWeight[2]=%.2Lf, SynapticWeight[3]=%.2Lf\n\n", funcSynapticWeight[1], funcSynapticWeight[2], funcSynapticWeight[3]);
  113.  
  114.  
  115.     while (i++) {
  116.  
  117.  
  118.         //for( q = 1; q <= training_inputs; q++)
  119.         q = rand() % training_inputs + 1;
  120.          {
  121.  
  122.         Bfuncoutput[q] = training_set_inputs[q-1][0]*funcSynapticWeight[1]+training_set_inputs[q-1][1]*funcSynapticWeight[2]+training_set_inputs[q-1][2]*funcSynapticWeight[3];
  123.  
  124.         BfuncPart[q] = s(Bfuncoutput[q]);
  125.  
  126.         //Bfunc_DerivativePart[q] = d(BfuncPart[q]);
  127.  
  128.         BfuncerrorPart[q] = 2*(training_set_outputs[q-1] - BfuncPart[q]);
  129.  
  130.         ErrorT += pow(training_set_outputs[q-1] - BfuncPart[q],2);
  131.  
  132.         BfuncadjustmentPart[q] =  BfuncerrorPart[q] * Bfunc_DerivativePart[q];
  133.  
  134.         }
  135.  
  136.  
  137.  
  138.         for( q = 1; q <= 3; q++) {
  139.  
  140.  
  141.             BfuncadjustSynapticWeight[q]=0;
  142.  
  143.  
  144.             for( qq = 1; qq <= training_inputs; qq++) {  BfuncadjustSynapticWeight[q] += (s(training_set_inputs[qq-1][q-1])*BfuncadjustmentPart[qq])/(training_inputs);
  145.  
  146.             //new method
  147.             funcSynapticWeight[q]=funcSynapticWeight[q]+learningrate*training_set_inputs[qq-1][q-1]*BfuncerrorPart[qq];
  148.             }
  149.  
  150.             //old method
  151.             //funcSynapticWeight[q]=funcSynapticWeight[q]+0.000000001*BfuncadjustSynapticWeight[q];
  152.  
  153.  
  154.         }
  155.  
  156.  
  157.  
  158.         if ((floor(i/cyclesi*100))!=indicator)
  159.                 {
  160.  
  161.  
  162.  
  163.     funcOutput5=new_situation[0][0]*funcSynapticWeight[1]+new_situation[0][1]*funcSynapticWeight[2]+new_situation[0][2]*funcSynapticWeight[3];
  164.  
  165.     funcDOToutput5 = s(funcOutput5);
  166.  
  167.     funcoutput = funcOutput5;
  168.  
  169.  
  170.  
  171.     printf("Step %.0Lf ",indicator); printf("Error : %.2Lf \n", ErrorT);
  172.     ErrorT=0;
  173.  
  174.     printf("\n( %.0Lf iterations) has output %.2Lf  (targetvalue = %.2Lf)",i, funcoutput,cool);
  175.  
  176.                 }
  177.  
  178.  
  179.  
  180.         indicator = floor(i/cyclesi*100);
  181.  
  182.  
  183.     }
  184.  
  185.  
  186.  
  187.     cin.get();
  188.  
  189.  
  190.  
  191.  
  192.  
  193.     return 0;
  194. }
RAW Paste Data