Advertisement
AlexMatveev

Untitled

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