Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <functional>
  4. #include <vector>
  5. #include <iostream>
  6. #include <random>
  7. #include <iomanip>
  8. using namespace std;
  9.  
  10.  
  11. double MathFunction(double x, double y) {
  12. return 1 / sqrt(0.01 + x * x + y * y);
  13. }
  14.  
  15. double randomPoint(int a, int b) {
  16. random_device rd;
  17. mt19937 gen(rd());
  18. uniform_real_distribution<> dis(a, b);
  19. return dis(gen);
  20. }
  21.  
  22. void generateIndividual(pair<double,double>& individual,int x1,int x2,int y1,int y2){
  23. individual.first = randomPoint(x1,x2);
  24. individual.second = randomPoint(y1,y2);
  25. }
  26.  
  27. pair<double, double> generateIndividual(int x1, int x2, int y1, int y2) {
  28. pair<double, double> individual;
  29. individual.first = randomPoint(x1, x2);
  30. individual.second = randomPoint(y1, y2);
  31. return individual;
  32. }
  33.  
  34. double maxValue(const vector<pair<double, double>>& population) {
  35. vector<double> result;
  36. result.reserve(population.size());
  37. for (auto& i : population) {
  38. result.push_back(MathFunction(i.first, i.second));
  39. }
  40. return *max_element(result.begin(), result.end());
  41. }
  42.  
  43. double averageValue(const vector<pair<double, double>>& population) {
  44. vector<double> result;
  45. result.reserve(population.size());
  46. for (auto& i : population) {
  47. result.push_back(MathFunction(i.first, i.second));
  48. }
  49. double sum = 0;
  50. for (auto& i : result) {
  51. sum += i;
  52. }
  53. return sum / result.size();
  54. }
  55.  
  56. void print(vector<pair<double, double>> population, size_t i) {
  57. cout << setw(4) << i << setw(11) << fixed << setprecision(6) << population[0].first << setw(11) << population[0].second << setw(11) << MathFunction(population[0].first, population[0].second) << setw(13) << maxValue(population) << setw(13) << averageValue(population) << endl;
  58. for (size_t j = 1; j < population.size(); ++j) {
  59. cout << setw(15) << fixed << setprecision(6) << population[j].first << setw(11) << population[j].second << setw(11) << MathFunction(population[j].first, population[j].second) << endl;
  60. }
  61. cout << endl;
  62. }
  63.  
  64. vector<size_t> Roulette(vector<pair<double, double>>& points) {
  65. double sum = 0;
  66. for (auto value : points) {
  67. sum += MathFunction(value.first, value.second);
  68. }
  69.  
  70. vector<size_t> answer;
  71.  
  72. for (auto value : points) {
  73. answer.push_back(MathFunction(value.first, value.second) / sum * 100);
  74. }
  75. return answer;
  76. }
  77.  
  78. size_t CheckIndex(vector<size_t>& prob, double p) {
  79. size_t a = 0;
  80. for (size_t i = 0; i < prob.size(); i++) {
  81. a += prob[i];
  82. if (a > p) return i;
  83. }
  84. return -1;
  85. }
  86.  
  87. bool func_sort(pair<double, double> pair1, pair<double, double> pair2) {
  88. return (MathFunction(pair1.first, pair1.second) > MathFunction(pair2.first, pair2.second));
  89. }
  90.  
  91. vector<pair<double, double>> Crossover(vector<size_t> prob,
  92. vector<pair<double, double>>& points){
  93.  
  94. vector<pair<double, double>> answer;
  95. for (auto value : points) {
  96. random_device rd;
  97. mt19937 gen(rd());
  98. uniform_real_distribution<double> probability(0, 100);
  99. auto p = static_cast<double>(100 - probability(gen));
  100.  
  101. size_t index = CheckIndex(prob, p);
  102. if (index != -1) {
  103. answer.push_back(make_pair(points[index].first, value.second));
  104. answer.push_back(make_pair(value.first, points[index].second));
  105. }
  106.  
  107. }
  108.  
  109.  
  110. sort(answer.begin(), answer.end(), func_sort);
  111.  
  112. answer.resize(4);
  113. return answer;
  114. }
  115.  
  116. void Mutation(vector<pair<double, double>>& points) {
  117. random_device rd;
  118. mt19937 gen(rd());
  119. uniform_real_distribution<double> probability(0, 100);
  120. auto p = static_cast<size_t>(probability(gen));
  121.  
  122. for (auto value : points) {
  123. if (p > 25) {
  124. uniform_real_distribution<double> probability(0, 100);
  125. auto p = static_cast<size_t>(probability(gen));
  126. if (p > 50) {
  127. value.first += 0.1;
  128. value.second += 0.1;
  129. }
  130. else {
  131. value.first -= 0.1;
  132. value.second -= 0.1;
  133. }
  134. }
  135. }
  136. }
  137.  
  138. void genetic(int x1, int x2, int y1, int y2) {
  139. pair<double, double> individual;
  140. vector<pair<double, double>> population;
  141. // Генерация начальной популяции (4 рандомных особи)
  142. for (size_t i = 0; i < 4; ++i) {
  143. individual = generateIndividual(x1, x2, y1, y2);
  144. population.push_back(individual);
  145. }
  146. cout << setw(4) << "N" << setw(11) << "X" << setw(11) << "Y" << setw(11) << " FIT " << setw(13) << "MAX " << setw(13) << "Average" << endl;
  147.  
  148. for (size_t i = 0; i < 10; ++i) {
  149. print(population, i);
  150. population = Crossover(Roulette(population), population);
  151. Mutation(population);
  152. }
  153. }
  154.  
  155. int main() {
  156. genetic(-1, 1, -1, 1);
  157.  
  158. return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement