Thread: ordering arrays
View Single Post
Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

Re: ordering arrays

 
0
  #5
Nov 20th, 2008
sorry i have been looking at code solid for the last few days so my brain is wasted!

right the 16 rows in the weights array all have a fitness rating or error, which is stored in the array error

here is the code

  1. for(int i = 0; i < 16; i++)
  2. {
  3. for(int j = 0; j < 60; j++)
  4. {
  5. weights[i][j] = -1 + rand() / ( RAND_MAX / ( 1 + 1 ) + 0.1 );
  6. }
  7. }
  8.  
  9.  
  10. //---- test strings for fitness --------------------------
  11.  
  12.  
  13. for(int i = 0; i < 16; i++)
  14. {
  15. toterr = 0;
  16. for (pat = 1; pat < 4; pat++)
  17. {
  18. patpixselect();
  19.  
  20. cout << endl << "pattern: " << pat << endl;
  21.  
  22. cout << endl << patpix[0] << endl;
  23. getch();
  24. OUTA = 1/(1 + pow(e, -((weights[i][0] * patpix[0]) + (weights[i][1] * patpix[1])
  25. + (weights[i][2] * patpix[2] ) + (weights[i][3] * patpix[3]) + (weights[i][4] * patpix[4])
  26. + ( weights[i][5] * patpix[5]) + (weights[i][6] * patpix[6]) + (weights[i][7] * patpix[7])
  27. + (weights[i][8] * patpix[8]))));
  28. cout << "OUTA " << OUTA << endl;
  29.  
  30. OUTB = 1/(1 + pow(e, -((weights[i][9] * patpix[0]) + (weights[i][10] * patpix[1])
  31. + (weights[i][11] * patpix[2] ) + (weights[i][12] * patpix[3]) + (weights[i][13] * patpix[4])
  32. + ( weights[i][14] * patpix[5]) + (weights[i][15] * patpix[6]) + (weights[i][16] * patpix[7])
  33. + (weights[i][17] * patpix[8]))));
  34. cout << "OUTB " << OUTB << endl;
  35.  
  36. OUTC = 1/(1 + pow(e, -((weights[i][18] * patpix[0]) + (weights[i][19] * patpix[1])
  37. + (weights[i][20] * patpix[2] ) + (weights[i][21] * patpix[3]) + (weights[i][22] * patpix[4])
  38. + ( weights[i][23] * patpix[5]) + (weights[i][24] * patpix[6]) + (weights[i][25] * patpix[7])
  39. + (weights[i][26] * patpix[8]))));
  40. cout << "OUTC " << OUTC << endl;
  41.  
  42. OUTD = 1/(1 + pow(e, -((weights[i][27] * patpix[0]) + (weights[i][28] * patpix[1])
  43. + (weights[i][29] * patpix[2] ) + (weights[i][30] * patpix[3]) + (weights[i][31] * patpix[4])
  44. + ( weights[i][32] * patpix[5]) + (weights[i][33] * patpix[6]) + (weights[i][34] * patpix[7])
  45. + (weights[i][35] * patpix[8]))));
  46. cout << "OUTD " << OUTD << endl;
  47.  
  48. OUTE = 1/(1 + pow(e, -((weights[i][36] * patpix[0]) + (weights[i][37] * patpix[1])
  49. + (weights[i][38] * patpix[2] ) + (weights[i][39] * patpix[3]) + (weights[i][40] * patpix[4])
  50. + ( weights[i][41] * patpix[5]) + (weights[i][42] * patpix[6]) + (weights[i][43] * patpix[7])
  51. + (weights[i][44] * patpix[8]))));
  52. cout << "OUTE " << OUTE << endl;
  53.  
  54. OUTX = 1/(1 + pow(e, -((weights[i][45] * OUTA) + (weights[i][46] * OUTB)
  55. + (weights[i][47] * OUTC ) + (weights[i][48] * OUTD) + (weights[i][49] * OUTE))));
  56. cout << "OUTX " << OUTX << endl;
  57.  
  58. OUTY = 1/(1 + pow(e, -((weights[i][50] * OUTA) + (weights[i][51] * OUTB)
  59. + (weights[i][52] * OUTC ) + (weights[i][53] * OUTD) + (weights[i][54] * OUTE))));
  60. cout << "OUTY " << OUTY << endl;
  61.  
  62. OUTZ = 1/(1 + pow(e, -((weights[i][55] * OUTA) + (weights[i][56] * OUTB)
  63. + (weights[i][57] * OUTC ) + (weights[i][58] * OUTD) + (weights[i][59] * OUTE))));
  64. cout << "OUTZ " << OUTZ << endl;
  65.  
  66. // ------------- calculate errors -(fitness)--------------------------------------------------
  67.  
  68. cout << endl << endl << "target 1 is " << target1 << endl
  69. << "target 2 is " << target2 << endl << "target3 is " << target3;
  70.  
  71.  
  72.  
  73. ERRX = (OUTX)*(1 - OUTX)*(target1 - OUTX ); // error of out put x
  74. ERRX = pow(ERRX,2);
  75. ERRX = sqrt(ERRX);
  76.  
  77. ERRY = (OUTY)*(1 - OUTY)*(target2 - OUTY ); // err of output Y
  78. ERRY = pow(ERRY,2);
  79. ERRY = sqrt(ERRY);
  80.  
  81. ERRZ = (OUTZ)*(1 - OUTZ)*(target3 - OUTZ ); // err of output z
  82. ERRZ = pow(ERRZ,2);
  83. ERRZ = sqrt(ERRZ);
  84.  
  85. err = ( ERRX + ERRY + ERRZ ); // total error of 1 pattern for 1 string
  86. cout << endl << "total error for 1 pattern for 1 string " << err << endl;
  87.  
  88.  
  89. toterr += err; // --- error for all patterns for 1 string
  90. cout << endl << "total error for all patterns for 1 string " << toterr << endl;
  91. }
  92.  
  93. error[i] = toterr; // put each error for each string in array to match with each string.
  94. cout << endl << "total error for string " << i << " is " << error[i] << endl ;
  95. }
  96.  
  97. getch();
  98. return 0;

basically there will be 16 numbers in the array known as error, and i need a way of putting them into order of lowest to highest based on the error.

if it helps im training a neural network to recognise 3 patterns using a genetic algorithm, so the part im on right now is sorting out the best half of the strings ( or the best half of the 16 in the array weights) so i can pair them up, swap elements around and create another 8 strings to replace the weakest half of the set. which i will need help with too!
Reply With Quote