Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <random>
  7. #include <ctime>
  8. #include <math.h>
  9.  
  10. using namespace std;
  11.  
  12. typedef struct vector_X {
  13. vector <double> var;
  14. vector <double> strat;
  15. double func;
  16. }vec_x;
  17.  
  18. double fit_function(vector <vec_x> &popul, int N, int j) {
  19. popul[j].func = 0;
  20. for (int i = 0; i < N; i++) {
  21. popul[j].func += (-1 * popul[j].var[i]) * sin(sqrt(abs(popul[j].var[i])));
  22. }
  23. popul[j].func += 418.9829*N;
  24. return popul[j].func;
  25. }
  26.  
  27. void print_popul(vector <vec_x> &popul, int N, int size_pop) {
  28. for (int i = 0; i < popul.size(); i++)
  29. {
  30. cout << endl;
  31. cout << i + 1 << " individual of population: ";
  32. for (int j = 0; j < N; j++)
  33. {
  34. cout << popul[i].var[j] << " ";
  35. }
  36. cout << endl;
  37. cout << "Fitness function : " << popul[i].func << endl;
  38. }
  39. }
  40.  
  41. vector <double> random_vars(vector <vec_x> &popul, double upp_b, double bott_l, int N, int j) { //upper bound, bottom line
  42. double rand_x;
  43. rand_x = bott_l + static_cast <double> (rand()) / (static_cast <double> (RAND_MAX / (upp_b - bott_l)));
  44. for (int i = 0; i < N; i++) {
  45. popul[j].var.push_back(rand_x);
  46. }
  47. return popul[j].var;
  48. }
  49.  
  50. double random_guass() {
  51. double mean = 0.0;
  52. double stdev = 1.0;
  53. double rg;
  54. double u1, u2, r;
  55. do
  56. {
  57. u1 = 2.0*rand() / RAND_MAX - 1;
  58. u2 = 2.0*rand() / RAND_MAX - 1;
  59. r = u1*u1 + u2*u2;
  60. } while (r >= 1.0);
  61.  
  62. double d = sqrt(-2.0*log(r) / r);
  63. rg = mean + (u2*r)*stdev;
  64. return rg;
  65. }
  66.  
  67. vector <double> mut_child(vector <vec_x> &child, int N, int j) {
  68. double rg = random_guass();
  69. for (int i = 0; i < N; i++)
  70. {
  71. child[j].var[i] = child[j].var[i] + child[j].strat[i] * rg;
  72.  
  73. if (child[j].var[i] > 500) child[j].var[i] = 500;
  74. if (child[j].var[i] < -500) child[j].var[i] = -500;
  75. }
  76. return child[j].var;
  77. }
  78.  
  79. vector <double> mut_strat(vector <vec_x> &child, int N, int j) {
  80. double tau, tau_p;
  81. double rg = random_guass();
  82. tau = pow(sqrt(2.0 * N), -1.0);
  83. tau_p = pow(sqrt(2.0 * sqrt(N)), -1.0);
  84. for (int i = 0; i < N; i++) {
  85. child[j].strat[i] = child[j].strat[i] * exp(tau_p*rg + tau*rg);
  86. }
  87. return child[j].strat;
  88. }
  89.  
  90. vector <double> mutation(vector <vec_x> &child, int N, int j) {
  91. child[j].var = mut_child(child, N, j);
  92. child[j].strat = mut_strat(child, N, j);
  93. return child[j].var;
  94. }
  95.  
  96. void search_x(int max_gen, vector <vec_x> &child, vector <vec_x> &popul, vector <vec_x> &last_x, int N, int m_ch, int size_pop) {
  97. for (int gen = 0; gen < max_gen; gen++) {
  98. for (int j = 0; j < m_ch; j++) {
  99. child[j].var = mutation(popul, N, j);
  100. child[j].func = fit_function(child, N, j);
  101. }
  102. sort(child.begin(), child.end(), [](const vec_x& a, const vec_x& b)
  103. {
  104. return a.func < b.func;
  105. });
  106. last_x[gen] = child[0];
  107. }
  108. sort(last_x.begin(), last_x.end(), [](const vec_x& a, const vec_x& b)
  109. {
  110. return a.func < b.func;
  111. });
  112. /*cout << endl << "Final population:" << endl;
  113. print_popul(last_x, N, size_pop);*/
  114. cout << "The best solution:" << endl;
  115. cout << " values of variables : ";
  116. for (int j = 0; j < N; j++)
  117. {
  118. cout << last_x[0].var[j] << " ";
  119. }
  120. cout << endl << " value of function : " << last_x[0].func << endl;
  121. }
  122.  
  123. int main()
  124. {
  125. cout << "\t\tEvolutionary strategy" << endl;
  126.  
  127. int N = 5;
  128. double bott_l = -500.0;
  129. double upp_b = 500.0;
  130. double q = (upp_b - bott_l) * 0.05;
  131.  
  132. int m_ch = 20;
  133. int size_pop = 30;
  134. int max_gen = 1000;
  135. double rand_var;
  136.  
  137. vector <vec_x> popul(size_pop);
  138. vector <vec_x> child(m_ch);
  139. vector <vec_x> last_x(max_gen);
  140.  
  141. for (int j = 0; j < size_pop; j++) {
  142. popul[j].var = random_vars(popul, upp_b, bott_l, N, j);
  143. popul[j].strat = random_vars(popul, q, 0.0, N, j);
  144. popul[j].func = fit_function(popul, N, j);
  145. }
  146.  
  147. sort(popul.begin(), popul.end(), [](const vec_x& a, const vec_x& b)
  148. {
  149. return a.func < b.func;
  150. });
  151.  
  152. print_popul(popul, N, size_pop);
  153.  
  154. search_x(max_gen, child, popul, last_x, N, m_ch, size_pop);
  155.  
  156. system("pause");
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement