Advertisement
Guest User

Genetic algorithm

a guest
Jan 23rd, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <time.h>
  6.  
  7. using namespace std;
  8.  
  9. //typedef vector<vector<int> > matrix;
  10. //typedef vector<int> vector, array;
  11.  
  12.  
  13. int IndividsCount;
  14. int CitiesCount;
  15.  
  16. class individ;
  17.  
  18. class city
  19. {
  20.     public:
  21.     int x,y,id;
  22.     city(){}
  23.     city(int x1, int y1, int id1)
  24.     {
  25.         x = x1;
  26.         y = y1;
  27.         id = id1;
  28.     }
  29.  
  30.     void SearchClosest(individ& , vector<city>&, vector<city> );
  31.     city operator=(city Second)
  32.     {
  33.         x = Second.x;
  34.         y = Second.y;
  35.         id = Second.id;
  36.     }
  37.  
  38. };
  39.  
  40. class individ
  41. {
  42.     public:
  43.  
  44.         vector<city> Genes;
  45.         float Way;
  46.         individ()
  47.         {
  48.  
  49.         }
  50.         float WayLength()
  51.         {
  52.             Way = 0;
  53.             for(size_t i = 0; i<CitiesCount-1; i++)
  54.             {
  55.                 Way+= sqrt( abs(Genes[i].x - Genes[i+1].x) + abs(Genes[i].y - Genes[i+1].y) );
  56.             }
  57.             return Way;
  58.         }
  59.         template <typename T>
  60.         individ operator= (T second)
  61.         {
  62.             if(Genes.size() > 0)
  63.             {
  64.                 Genes.clear();
  65.                 vector<city>().swap(Genes); //Чистим память
  66.             }
  67.             for(size_t i = 0; i<CitiesCount; i++)
  68.             {
  69.                 Genes.push_back(second.Genes[i]);
  70.             }
  71.  
  72.         }
  73. };
  74.  
  75. void city::SearchClosest(individ& _individ, vector<city>& Used, vector<city> _cities)
  76.     {
  77.         float way;
  78.     float min = 1000000000;
  79.         int minId;
  80.         for(size_t i = 0; i<CitiesCount; i++)
  81.         {
  82.             bool isUsed = false;
  83.             for(size_t j = 0; j<Used.size() ; j++)
  84.                 if(_cities[i].id == Used[j].id) isUsed = true;
  85.             if(isUsed) continue;
  86.             if(id == _cities[i].id) continue; //Необязатльная проверка(вроде)
  87.             way = sqrt( abs(x - _cities[i].x) + abs(y - _cities[i].y) );
  88.             if(way < min)
  89.             {
  90.                 min = way;
  91.                 minId = i;
  92.             }
  93.  
  94.         }
  95.         _individ.Genes.push_back(_cities[minId]);
  96.         Used.push_back(_cities[minId]);
  97.         if(Used.size() == CitiesCount) return;
  98.         else
  99.             _cities[minId].SearchClosest(_individ, Used, _cities);
  100.     }
  101.  
  102.  
  103. void Cross (individ parent1 , individ parent2, individ& child1 , individ& child2)  // replace двух случайных городов (a и b) в обоих потомках
  104. {
  105.     int RandCity1 = rand()%CitiesCount;
  106.     int RandCity2 = rand()%CitiesCount;
  107.  
  108.     while(RandCity1 == RandCity2)
  109.         RandCity2 = rand()%CitiesCount;
  110.     child1 = parent1;
  111.     child2 = parent2;
  112.     city a,b;
  113.     int aID, bID;
  114.     for(size_t i = 0; i<CitiesCount; i++)
  115.     {
  116.         if(child1.Genes[i].id == RandCity1) {a = child1.Genes[i]; aID = i;}
  117.         if(child1.Genes[i].id == RandCity2) {b = child1.Genes[i]; bID = i;}
  118.     }
  119.     city temp = a;
  120.     child1.Genes[aID] = b;
  121.     child1.Genes[bID] = temp;
  122.     for(size_t i = 0; i<CitiesCount; i++)
  123.     {
  124.         if(child2.Genes[i].id == RandCity1) {a = child2.Genes[i]; aID = i;}
  125.         if(child2.Genes[i].id == RandCity2) {b = child2.Genes[i]; bID = i;}
  126.     }
  127.     temp = a;
  128.     child2.Genes[aID] = b;
  129.     child2.Genes[bID] = temp;
  130.  
  131. }
  132.  
  133. class population
  134. {
  135.     public:
  136.     vector<individ> individs;
  137.  
  138.     population(){}
  139.  
  140.     void RandomFirstPopulation(vector<city> cities)
  141.     {
  142.         for(size_t i = 0; i<IndividsCount; i++)
  143.         {
  144.             individ Create;
  145.             int k = CitiesCount;
  146.             vector<city> TempCities = cities;
  147.             for(size_t j = 0; j<CitiesCount; j++)
  148.             {
  149.                 int RandIndex = rand()%k;
  150.                 Create.Genes.push_back(TempCities[RandIndex]);
  151.                 TempCities.erase(TempCities.begin() + RandIndex);
  152.                 k--;
  153.         }
  154.             individs.push_back(Create);
  155.  
  156.         }
  157.     }
  158.  
  159.     void LogicFirstPopulation(vector<city> cities)
  160.     {
  161.         individ Create;
  162.         vector<city> UsedCities;
  163.         int RandIndex = rand()%CitiesCount;
  164.         Create.Genes.push_back(cities[RandIndex]);
  165.         UsedCities.push_back(cities[RandIndex]);
  166.         cities[RandIndex].SearchClosest(Create, UsedCities, cities);
  167.         individs.push_back(Create);
  168.  
  169.     }
  170.  
  171.     void Mutation(float probability)
  172.     {
  173.  
  174.         for(size_t i = 0; i<IndividsCount; i++)
  175.         {
  176.             if(rand()%IndividsCount < probability*IndividsCount)
  177.             {
  178.                 int RandID1 = rand()%CitiesCount;
  179.                 int RandID2 = rand()%CitiesCount;
  180.                 while(RandID2 == RandID1)
  181.                     RandID2 = rand()%CitiesCount;
  182.                 city temp;
  183.                 temp = individs[i].Genes[RandID1];
  184.                 individs[i].Genes[RandID1] = individs[i].Genes[RandID2];
  185.                 individs[i].Genes[RandID2] = temp;
  186.             }
  187.         }
  188.     }
  189.  
  190.     void Selection(int T, population& NewPopulation)
  191.     {
  192.         while(NewPopulation.individs.size() < IndividsCount)
  193.         {
  194.             vector<int> CompetitorsIDs;
  195.             float LeastCompetitorWay = 1999999999;
  196.             int LCWid;
  197.             for(size_t i = 0; i<T; i++)
  198.             {
  199.                 int Random;
  200.                 bool isUnique = false;
  201.                 while(!isUnique)
  202.                 {
  203.                     Random = rand()%IndividsCount;
  204.                     isUnique = true;
  205.                     for(size_t j=0; j<CompetitorsIDs.size(); j++)
  206.                     {
  207.                         if(Random == CompetitorsIDs[j])
  208.                         {
  209.                             isUnique = false;
  210.                         }
  211.  
  212.                     }
  213.                 }
  214.                 CompetitorsIDs.push_back(Random);
  215.                 if(individs[Random].WayLength() < LeastCompetitorWay)
  216.                 {
  217.                     LeastCompetitorWay = individs[Random].WayLength();
  218.                     LCWid = Random; // id особи с наименьшим расстоянием
  219.                 }
  220.             }
  221.             NewPopulation.individs.push_back(individs[LCWid]);
  222.  
  223.         }
  224.  
  225.     }
  226. void Crossing(population& NewPopulation, float Probability)
  227.     {
  228.         while(NewPopulation.individs.size() < IndividsCount)
  229.         {
  230.             int ParentID1 = rand()%IndividsCount;
  231.             int ParentID2 = rand()%IndividsCount;
  232.             while(ParentID1 == ParentID2)
  233.                 ParentID2 = rand()%IndividsCount;
  234.             if(rand()%100 < Probability*100)
  235.             {
  236.                 individ _child1;
  237.                 individ _child2;
  238.                 Cross(individs[ParentID1], individs[ParentID2], _child1, _child2); //Функция описана на 103 строке
  239.                 NewPopulation.individs.push_back(_child1);
  240.                 NewPopulation.individs.push_back(_child2);
  241.             }
  242.         }
  243.  
  244.     }
  245.  
  246.     void Out()
  247.     {
  248.         for(size_t i = 0; i<IndividsCount; i++)
  249.         {
  250.             cout<<i<<" особь: ";
  251.             for(size_t j = 0; j<CitiesCount; j++)
  252.             {
  253.                 cout<<individs[i].Genes[j].id<<" ";
  254.             }
  255.             cout<<"Way = "<<individs[i].WayLength()<<endl;
  256.         }
  257.     }
  258.  
  259. };
  260.  
  261.  
  262. int main()
  263. {
  264.     srand(time(NULL));
  265.  
  266.     cout<<"Введите количество особей"<<endl;
  267.     cin>>IndividsCount;
  268.     cout<<"Введите количсетво городов"<<endl;
  269.     cin>>CitiesCount;
  270.  
  271.     vector<city> MainCities(CitiesCount);
  272.     for(size_t i = 0; i<CitiesCount; i++)
  273.     {
  274.         int variable;
  275.         MainCities[i].id = i;
  276.         cout<<"Введите абциссу(X) "<<i<<" города"<<endl;
  277.         cin>>variable;
  278.         MainCities[i].x = variable;
  279.         cout<<"Введите ординату(Y) "<<i<<" города"<<endl;
  280.         cin>>variable;
  281.         MainCities[i].y = variable;
  282.  
  283.     }
  284.     population _FirstPopulation;
  285.     _FirstPopulation.RandomFirstPopulation(MainCities);
  286.     _FirstPopulation.Out();
  287.     population _NewPopulation;
  288.     _FirstPopulation.Selection(2,_NewPopulation);
  289.     cout<<endl;
  290.     _NewPopulation.Out();
  291.     cout<<endl;
  292.     _NewPopulation.Mutation(1/IndividsCount);
  293.     _NewPopulation.Out();
  294.     cout<<endl;
  295.     population ThirdPopulation;
  296.     _NewPopulation.Crossing(ThirdPopulation, 0.6);
  297.     ThirdPopulation.Out();
  298. return 0;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement