Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <vector>
- #include <iomanip>
- #include <ctime>
- #include <algorithm>
- double function(double x, double y)
- {
- double f;
- f = exp(-pow(x,2) - pow(y,2));
- return f;
- }
- int main()
- {
- setlocale(0, "rus");
- std::vector <double> firstchromosome;
- std::vector <double> secondchromosome;
- std::vector <double> FIT;
- srand(time(0));
- for (size_t i = 0; i < 4; ++i)
- {
- firstchromosome.push_back(((double)rand() / RAND_MAX)*4-2);
- secondchromosome.push_back (((double)rand() / RAND_MAX)*4-2);
- FIT.push_back(function(firstchromosome[i], secondchromosome[i]));
- }
- std::cout << "| N | x | y | FIT |Максимальный результат|Средний результат|" << std::endl;
- std::cout << "|" << std::setw(3) << std::setprecision(3) << 0;
- for (size_t i = 0; i < 4; i++) {
- std::cout << "| " << std::fixed << std::setprecision(3) << std::setw(10) << firstchromosome[i] << "| ";
- std::cout << std::setw(10) << secondchromosome[i] << "|";
- std::cout << std::setw(10) << FIT[i] << "| ";
- if (i == 0)
- {
- std::cout << std::setw(21) << *std::max_element(FIT.begin(), FIT.end()) << "|";
- std::cout << std::setw(17) << (FIT[0] + FIT[1] + FIT[2] + FIT[3]) / 4 << "|";
- }
- else
- {
- std::cout << std::setw(22) << "|";
- std::cout << std::setw(18) << "|";
- }
- std::cout<< std::endl << "|" << std::setw(5) << std::fixed << std::setprecision(3);
- }
- std::vector <double> firstchromosome2;
- std::vector <double> secondchromosome2;
- std::vector <double> FIT2;
- for (size_t k = 1; k < 100; ++k) {
- double sum = FIT[0] + FIT[1] + FIT[2] + FIT[3];
- double persent1 = FIT[0]*100 / sum; //процентное соотношение каждой точки
- double persent2 = FIT[1]*100 / sum;
- double persent3 = FIT[2]*100 / sum;
- double persent4 = FIT[3]*100 / sum;
- for (size_t i = 0; i < 2; ++i)
- {
- size_t j = rand() % 100 + 1;
- if (j <= persent1)
- {
- firstchromosome2.push_back(firstchromosome[0]);
- secondchromosome2.push_back(secondchromosome[0]);
- }
- if (j > persent1 && j <= persent2+persent1)
- {
- firstchromosome2.push_back(firstchromosome[1]);
- secondchromosome2.push_back(secondchromosome[1]);
- }
- if (j > persent1 && j > persent2+persent1 && j <= persent3+persent1+persent2)
- {
- firstchromosome2.push_back(firstchromosome[2]);
- secondchromosome2.push_back(secondchromosome[2]);
- }
- if (j > persent1 && j > persent2 + persent1 && j > persent3 + persent1 + persent2)
- {
- firstchromosome2.push_back(firstchromosome[3]);
- secondchromosome2.push_back(secondchromosome[3]);
- }
- }
- firstchromosome2.push_back(firstchromosome2[0]);
- secondchromosome2.push_back(secondchromosome2[1]);
- firstchromosome2.push_back(firstchromosome2[1]);
- secondchromosome2.push_back(secondchromosome2[0]);
- for (size_t i = 0; i < 4; ++i)
- {
- double mutation = rand() % 100 + 1;
- if (mutation <= 25)
- {
- mutation = rand()%2;
- if (mutation == 0)
- {
- mutation = (((double)rand() / RAND_MAX)/10 - 0.1);
- firstchromosome2[i] = firstchromosome2[i] + mutation;
- }
- else {
- mutation = (((double)rand() / RAND_MAX) / 10 - 0.1);
- secondchromosome2[i] = secondchromosome2[i] + mutation;
- }
- }
- }
- for (size_t i = 0; i < 4; i++)
- {
- FIT2.push_back(function(firstchromosome2[i], secondchromosome2[i]));
- }
- std::cout << std::setw(3) << std::fixed << std::setprecision(3) << k;
- for (size_t i = 0; i < 4; ++i)
- {
- std::cout << "| " << std::fixed << std::setprecision(3) << std::setw(10) << firstchromosome[i] << "| ";
- std::cout << std::setw(10) << secondchromosome[i] << "|";
- std::cout << std::setw(10) << FIT[i] << "| ";
- if (i == 0)
- {
- std::cout << std::setw(21) << *std::max_element(FIT.begin(), FIT.end()) << "|";
- std::cout << std::setw(17) << (FIT[0] + FIT[1] + FIT[2] + FIT[3]) / 4 << "|";
- }
- else
- {
- std::cout << std::setw(22) << "|";
- std::cout << std::setw(18) << "|";
- }
- std::cout << std::endl << "|" << std::setw(5) << std::fixed << std::setprecision(3);
- }
- firstchromosome = firstchromosome2;
- secondchromosome = secondchromosome2;
- FIT = FIT2;
- firstchromosome2.clear();
- secondchromosome2.clear();
- FIT2.clear();
- }
- std::vector<double>::iterator it = std::max_element(FIT.begin(), FIT.end());
- size_t index = std::distance(FIT.begin(), it);
- std::cout<< " Лучшее значение FIT функции 100 популяции " << *it << " при х = " << firstchromosome[index] << " и у = " << secondchromosome[index] << "|";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement