Advertisement
Guest User

8953549

a guest
Nov 11th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <ctime>
  6. #include <math.h>
  7. #include <random>
  8. #include <chrono>
  9.  
  10.  
  11. using namespace std;
  12.  
  13. const int u = 100;
  14. const int lambda = 5 * u;
  15. const float pi = 3.14159;
  16.  
  17.  
  18. float random_generator(float a, float b)
  19. {
  20. float n = (b - a)*((float)rand() / RAND_MAX) + a;
  21. return n;
  22. }
  23.  
  24. void generation(float w[][7], int a, int b, const int pop) //generowanie zbioru liczb losowych z podanego przedziału
  25. {
  26. for (int i = 0; i < pop; i++)
  27. {
  28. for (int j = 0; j < 6; j++)
  29. {
  30. if (j < 3)
  31. {
  32. w[i][j] = random_generator(a, b);
  33. }
  34. else
  35. {
  36. w[i][j] = random_generator(0, b);
  37. }
  38. }
  39. }
  40. }
  41.  
  42. void data(float w[101][2]) //przepisanie danych z pliku
  43. {
  44.  
  45. fstream plik;
  46. plik.open("C:\\Users\\Lenovo\\Desktop\\AIdata17.dat", ios::in);
  47. if (plik.good() == true)
  48. {
  49. while (!plik.eof())
  50. {
  51. for (int i = 0; i <= 100; i++)
  52. {
  53. for (int j = 0; j <= 1; j++)
  54. {
  55. plik >> w[i][j];
  56. //cout << w[i][j] << "\t";
  57. }
  58. //cout << endl;
  59. }
  60. }
  61. plik.close();
  62. }
  63. }
  64.  
  65.  
  66. int main() {
  67.  
  68. srand(time(NULL));
  69. float population[u][7] = { 0 };
  70. float lambda_population[lambda][7] = { 0 };
  71. float arguments[101][2];
  72. int random;
  73. float r,r1,r2;
  74. float tau1 = 1 / sqrt(2 * 6);
  75. float tau2 = 1 / sqrt(2 * sqrt(6));
  76. float error_parents[u] = { 0 };
  77. float error_offsprings[lambda] = { 0 };
  78.  
  79. float merge_pop[u + lambda][7];
  80. int iteration = 0;
  81.  
  82. unsigned seed = chrono::system_clock::now().time_since_epoch().count();
  83. default_random_engine generator(seed);
  84. normal_distribution <float> distribution(0,1);
  85.  
  86. data(arguments);
  87.  
  88. generation(population, -10, 10, u);
  89. float diff=0;
  90.  
  91. float best_parent=1;
  92. float best_offspring=0;
  93. //random = rand() % 100;
  94. //cout<<random<<endl;
  95. while (abs(best_parent - best_offspring) > 0.00005)
  96. {
  97. for (int i = 0; i < u; i++)
  98. {
  99. population[i][6] = 0;
  100. }
  101. for (int i = 0; i < lambda; i++)
  102. {
  103. lambda_population[i][6] = 0;
  104. }
  105. iteration++;
  106.  
  107.  
  108. for (int i = 0; i < lambda; i++)
  109. {
  110. random = rand() % 100;
  111. for (int j = 0; j < 6; j++)
  112. {
  113. if (j < 3)
  114. {
  115. r = distribution(generator);
  116. lambda_population[i][j] = population[random][j] + population[random][j + 3] * r;
  117.  
  118. }
  119. else
  120. {
  121. r1 = distribution(generator);
  122. r2 = distribution(generator);
  123. lambda_population[i][j] = population[random][j] * exp(tau1*r1)*exp(tau2*r2);
  124. //cout << i<<". "<<lambda_population[i][1] << endl;
  125.  
  126. }
  127.  
  128.  
  129. }
  130.  
  131. }
  132.  
  133. //MSE of parents
  134. for (int i = 0; i < u; i++)
  135. {
  136. diff = 0;
  137. for (int j = 0; j <= 100; j++)
  138. {
  139. float temp = 0;
  140. temp = population[i][0] * (pow(arguments[j][0], 2) - population[i][1] * cos(pi*population[i][2] * arguments[j][0]));
  141. diff += pow((arguments[i][1] - temp), 2);
  142.  
  143.  
  144. }
  145.  
  146. population[i][6] = diff / 101;
  147. error_parents[i] = diff / 101;
  148. cout << error_parents[i]<< endl;
  149. }
  150.  
  151.  
  152. //MSE of offsprings
  153. for (int i = 0; i < lambda; i++)
  154. { diff = 0;
  155. for (int j = 0; j <= 100; j++)
  156. {
  157. float temp = 0;
  158. temp = lambda_population[i][0] * (pow(arguments[j][0], 2) - lambda_population[i][1] * cos(pi*lambda_population[i][2] * arguments[j][0]));
  159. diff += pow((arguments[i][1] - temp), 2);
  160.  
  161. }
  162. lambda_population[i][6] = diff / 101;
  163. error_offsprings[i] = diff / 101;
  164. //cout << i << " " << lambda_population[i][2]<<endl;
  165. }
  166.  
  167.  
  168. sort(error_parents, error_parents + u);
  169. sort(error_offsprings, error_offsprings + lambda);
  170.  
  171.  
  172. best_parent = error_parents[0];
  173. best_offspring = error_offsprings[0];
  174.  
  175.  
  176.  
  177. //merging of 2 matrices
  178. for (int i = 0; i < (u+lambda); i++)
  179. {
  180. for (int j = 0; j < 7; j++)
  181. {
  182. if (i < u)
  183. merge_pop[i][j] = population[i][j];
  184. else
  185. merge_pop[i][j] = lambda_population[i][j];
  186. }
  187. }
  188.  
  189.  
  190.  
  191.  
  192. float temp[7];
  193.  
  194. //sortowanie bąbelkowe - wg kolumny 7
  195.  
  196. for (int i = 0; i<(lambda+u); i++) {
  197.  
  198. for (int j = i + 1; j<(lambda+u); j++)
  199.  
  200. {
  201.  
  202. if (merge_pop[j][6] < merge_pop[i][6]) {
  203. for (int t = 0; t < 7; t++) {
  204. temp[t] = merge_pop[i][t];
  205.  
  206. merge_pop[i][t] = merge_pop[j][t];
  207.  
  208. merge_pop[j][t] = temp[t];
  209. }
  210. }
  211.  
  212. }
  213. }
  214.  
  215.  
  216. for (int i = 0; i < u; i++)
  217. {
  218. for (int j = 0; j < 7; j++)
  219. {
  220. population[i][j] = merge_pop[i][j];
  221. }
  222. }
  223. cout << iteration << endl;
  224.  
  225.  
  226. }
  227.  
  228.  
  229.  
  230.  
  231. return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement