Advertisement
AlexMatveev

Untitled

May 7th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <iostream>
  4. #include <time.h>
  5. #include <iterator>
  6.  
  7. #define LINE_SIZE 40
  8. #define FACILITY_COUNT 100
  9. #define CLIENT_COUNT 100
  10. #define POPULATION_SIZE 30
  11. #define FIXED_COST 3000
  12. #define PROPABILITY_TO_BE_IN_POOL_OF_PARENTS 500
  13. #define PROPABILITY_OF_MUTATION 900
  14.  
  15. int function(std::vector<int> sol, std::vector<std::vector<int>> matrix){
  16.     int result = 0;
  17.     for(int i = 0; i < FACILITY_COUNT; i++)
  18.         if(sol[i] == 1)
  19.             result += FIXED_COST;
  20.     for(int j = 0; j < CLIENT_COUNT; j++){
  21.         int min = INT_MAX;
  22.         for(int i = 0; i < FACILITY_COUNT; i++)
  23.             if(matrix[i][j] < min)
  24.                 min = matrix[i][j];
  25.         result += min;
  26.     }
  27.     return result;
  28. }
  29.  
  30. void readMatrix(std::vector<std::vector<int>> &matrix, char* filename){
  31.     int u, v, c;
  32.     char line[LINE_SIZE];
  33.     std::fstream fs;
  34.     matrix.resize(FACILITY_COUNT);
  35.     for(int i = 0; i < FACILITY_COUNT; i++)
  36.         matrix[i].resize(CLIENT_COUNT);
  37.     for(int i = 0; i < FACILITY_COUNT; i++)
  38.         for(int j = 0; j< CLIENT_COUNT; j++)
  39.             matrix[i][j] = INT_MAX;
  40.     fs.open(filename,std::fstream::in);
  41.     for(int i = 0; i<4; i++)
  42.         fs.getline(line,LINE_SIZE);
  43.     while(!fs.eof()){
  44.         fs >> u >> v >> c;
  45.         matrix[u-1][v-1] = c;
  46.     }
  47.     fs.close();
  48. }
  49.  
  50. void generateFirstPopulation(std::vector<std::vector<int>> &population){
  51.     population.resize(POPULATION_SIZE);
  52.     for(int i = 0; i < POPULATION_SIZE; i++){
  53.         population[i].resize(FACILITY_COUNT);
  54.             for(int j = 0; j < FACILITY_COUNT; j++){
  55.                
  56.                 int res = rand()%1000;
  57.                 if (res > 500)
  58.                     population[i][j] = 1;
  59.                 else
  60.                     population[i][j] = 0;
  61.             }
  62.     }
  63. }
  64.  
  65. void mutation(std::vector<int> &child){
  66.     for(int i = 0; i < FACILITY_COUNT; i++){
  67.         int res = rand()%1000;
  68.         if (res > PROPABILITY_OF_MUTATION){
  69.             if(child[i] == 0)
  70.                 child[i] = 1;
  71.             else
  72.                 child[i] = 0;
  73.         }
  74.  
  75.     }
  76. }
  77.  
  78. std::vector<int> selectionAndCrossover(std::vector<std::vector<int>> &population, std::vector<std::vector<int>> matrix){
  79.     std::vector<std::vector<int>> poolOfParents;
  80.     for(int i = 0; i < POPULATION_SIZE; i++){
  81.         int res = rand()%1000;
  82.         if (res > PROPABILITY_TO_BE_IN_POOL_OF_PARENTS)
  83.             poolOfParents.push_back(population[i]);
  84.     }
  85.     std::vector<int> first;
  86.     std::vector<int> second;
  87.     std::vector<int> child;
  88.     child.resize(FACILITY_COUNT);
  89.     int firstRec = INT_MAX;
  90.     int secondRec = INT_MAX;
  91.     for(int i = 0; i < poolOfParents.size(); i++){
  92.         int res = function(poolOfParents[i], matrix);
  93.         if (res < firstRec){
  94.             secondRec = firstRec;
  95.             second = first;
  96.             firstRec = res;
  97.             first = poolOfParents[i];
  98.         }
  99.         else
  100.             if (res < secondRec){
  101.                 secondRec = res;
  102.                 second = poolOfParents[i];
  103.             }
  104.     }
  105.  
  106.     for(int i = 0; i < FACILITY_COUNT; i++){
  107.         if(first[i] == 0 && second[i] == 0){
  108.             child[i] = 0;
  109.             continue;
  110.         }
  111.         if(first[i] == 1 && second[i] == 1){
  112.             child[i] = 1;
  113.             continue;
  114.         }
  115.         int res = rand()%1000;
  116.         if (res > 500)
  117.             child[i] = 1;
  118.         else
  119.             child[i] = 0;
  120.     }
  121.     return child;
  122. }
  123.  
  124. void localFall(std::vector<int> &child, std::vector<std::vector<int>> matrix){
  125.     int bestScore = function(child, matrix);
  126.     std::vector<int> localBest = child, current;
  127.     int localScore;
  128.     for(int i = 0; i < FACILITY_COUNT; i++){
  129.         current = child;
  130.         if(current[i]==0){
  131.             current[i] = 1;
  132.             localScore = function(current,matrix);
  133.             if(localScore < bestScore)
  134.                 localBest = current;
  135.             for(int j = i + 1; j < FACILITY_COUNT; j++){
  136.                 if(current[i]==1)
  137.                     current[i] = 0;
  138.                 localScore = function(current,matrix);
  139.                 if(localScore < bestScore)
  140.                     localBest = current;
  141.             }
  142.         }
  143.         else{
  144.             current[i] = 0;
  145.             localScore = function(current,matrix);
  146.             if(localScore < bestScore)
  147.                 localBest = current;
  148.             for(int j = i + 1; j < FACILITY_COUNT; j++){
  149.                 if(current[i]==0)
  150.                     current[i] = 1;
  151.                 localScore = function(current,matrix);
  152.                 if(localScore < bestScore)
  153.                     localBest = current;
  154.             }
  155.         }
  156.         child = localBest;
  157.     }
  158. }
  159.  
  160. void updatePopulation(std::vector<std::vector<int>> &population, std::vector<int> child, std::vector<std::vector<int>> matrix){
  161.     std::vector<std::vector<int>>::iterator numberOfWorst;
  162.     int max = function(population[0],matrix);
  163.     for(std::vector<std::vector<int>>::iterator i =population.begin(); i < population.end(); i++)
  164.         if (function(*i, matrix)>max)
  165.             numberOfWorst = i;
  166.     population.erase(numberOfWorst);
  167.     population.push_back(child);
  168. }
  169.  
  170. std::vector<int> bestInPopulation(std::vector<std::vector<int>> population, std::vector<std::vector<int>> matrix){
  171.     std::vector<std::vector<int>>::iterator numberOfWorst;
  172.     int min = function(population[0],matrix);
  173.     for(std::vector<std::vector<int>>::iterator i = population.begin(); i < population.end(); i++)
  174.         if (function(*i, matrix)<min)
  175.             numberOfWorst = i;
  176.     return *numberOfWorst;
  177. }
  178.  
  179. int main(){
  180.     std::vector<std::vector<int>> population;
  181.     std::vector<std::vector<int>> matrix;
  182.     std::vector<int> child;
  183.     srand(time(NULL));
  184.     readMatrix(matrix,"test.txt");
  185.     generateFirstPopulation(population);
  186.     std::cout<<function(bestInPopulation(population,matrix),matrix)<<std::endl;
  187.     for(int i = 0; i < 10; i++){
  188.         child = selectionAndCrossover(population, matrix);
  189.         mutation(child);
  190.         localFall(child,matrix);
  191.         updatePopulation(population,child,matrix);
  192.         std::cout<<function(bestInPopulation(population,matrix),matrix);
  193.     }
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement