Advertisement
AlexMatveev

Untitled

May 15th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 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 950
  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. unsigned int function(const std::vector<int> &sol, const std::vector<std::vector<int>> &matrix,
  35. std::vector<int> &best, std::vector<int> &bestValue, std::vector<int> &second, std::vector<int> &secondValue){
  36. int result = 0;
  37. for(int i = 0; i < FACILITY_COUNT; i++)
  38. if(sol[i] == 1)
  39. result += FIXED_COST;
  40. for(int j = 0; j < CLIENT_COUNT; j++){
  41. int min = INT_MAX;
  42. for(int i = 0; i < FACILITY_COUNT; i++)
  43. if(sol[i] == 1){
  44. int a = matrix[i][j];
  45. if(a < min){
  46. second[j] = best[j];
  47. secondValue[j] = bestValue[j];
  48. best[j] = i;
  49. bestValue[j] = a;
  50. min = a;
  51. }
  52. }
  53. result += min;
  54. }
  55. return result;
  56. }
  57.  
  58. void readMatrix(std::vector<std::vector<int>> &matrix, char* filename){
  59. int u, v, c;
  60. char line[LINE_SIZE];
  61. std::fstream fs;
  62. matrix.resize(FACILITY_COUNT);
  63. for(int i = 0; i < FACILITY_COUNT; i++)
  64. matrix[i].resize(CLIENT_COUNT);
  65. for(int i = 0; i < FACILITY_COUNT; i++)
  66. for(int j = 0; j< CLIENT_COUNT; j++)
  67. matrix[i][j] = INT_MAX;
  68. fs.open(filename,std::fstream::in);
  69. for(int i = 0; i<4; i++)
  70. fs.getline(line,LINE_SIZE);
  71. while(!fs.eof()){
  72. fs >> u >> v >> c;
  73. matrix[u-1][v-1] = c;
  74. }
  75. fs.close();
  76. }
  77.  
  78. void generateFirstPopulation(std::vector<std::vector<int>> &population){
  79. population.resize(POPULATION_SIZE);
  80. for(int i = 0; i < POPULATION_SIZE; i++){
  81. population[i].resize(FACILITY_COUNT);
  82. for(int j = 0; j < FACILITY_COUNT; j++){
  83.  
  84. int res = rand()%1000;
  85. if (res > 500)
  86. population[i][j] = 1;
  87. else
  88. population[i][j] = 0;
  89. }
  90. }
  91. }
  92.  
  93. void mutation(std::vector<int> &child){
  94. for(int i = 0; i < FACILITY_COUNT; i++){
  95. int res = rand()%1000;
  96. if (res > PROPABILITY_OF_MUTATION){
  97. if(child[i] == 0)
  98. child[i] = 1;
  99. else
  100. child[i] = 0;
  101. }
  102.  
  103. }
  104. }
  105.  
  106. std::vector<int> selectionAndCrossover(std::vector<std::vector<int>> &population, const std::vector<std::vector<int>> &matrix){
  107. std::vector<std::vector<int>> poolOfParents;
  108. for(int i = 0; i < POPULATION_SIZE; i++){
  109. int res = rand()%1000;
  110. if (res > PROPABILITY_TO_BE_IN_POOL_OF_PARENTS)
  111. poolOfParents.push_back(population[i]);
  112. }
  113. std::vector<int> first;
  114. std::vector<int> second;
  115. std::vector<int> child;
  116. child.resize(FACILITY_COUNT);
  117. int firstRec = INT_MAX;
  118. int secondRec = INT_MAX;
  119. for(int i = 0; i < poolOfParents.size(); i++){
  120. int res = function(poolOfParents[i], matrix);
  121. if (res < firstRec){
  122. secondRec = firstRec;
  123. second = first;
  124. firstRec = res;
  125. first = poolOfParents[i];
  126. }
  127. else
  128. if (res < secondRec){
  129. secondRec = res;
  130. second = poolOfParents[i];
  131. }
  132. }
  133.  
  134. for(int i = 0; i < FACILITY_COUNT; i++){
  135. if(first[i] == 0 && second[i] == 0){
  136. child[i] = 0;
  137. continue;
  138. }
  139. if(first[i] == 1 && second[i] == 1){
  140. child[i] = 1;
  141. continue;
  142. }
  143. int res = rand()%1000;
  144. if (res > 500)
  145. child[i] = 1;
  146. else
  147. child[i] = 0;
  148. }
  149. return child;
  150. }
  151.  
  152. void localFall(std::vector<int> &child, const std::vector<std::vector<int>> &matrix){
  153.  
  154. std::vector<int> localBest = child;
  155. std::vector<int> current;
  156. std::vector<int> best;
  157. best.resize(CLIENT_COUNT);
  158. std::vector<int> bestValue;
  159. bestValue.resize(CLIENT_COUNT);
  160. std::vector<int> second;
  161. second.resize(CLIENT_COUNT);
  162. std::vector<int> secondValue;
  163. secondValue.resize(CLIENT_COUNT);
  164.  
  165. //int bestScore = function(child, matrix,best,bestValue,second,secondValue);
  166. int bestScore = function(child, matrix);
  167. int localScore = bestScore;
  168. int a = 0;
  169. while(1){
  170. int bestScore = function(child, matrix,best,bestValue,second,secondValue);
  171. // std::cout<<a++<<std::endl;
  172. int flag = 0;
  173. for(int i = 0; i < FACILITY_COUNT; i++){
  174. current = child;
  175. if(current[i]==0){
  176. localScore += 3000;
  177. for(int j = 0; j < CLIENT_COUNT; j++)
  178. if (matrix[i][j] < bestValue[j]){
  179. secondValue[j] = bestValue[j];
  180. second[j] = best[j];
  181. bestValue[j] = matrix[i][j];
  182. best[j] = i;
  183. localScore -= secondValue[j];
  184. localScore += bestValue[j];
  185. }
  186. current[i] = 1;
  187. //localScore = function(current,matrix);
  188. if(localScore < bestScore){
  189. flag = 1;
  190. bestScore = localScore;
  191. localBest = current;
  192. }
  193. for(int j = i + 1; j < FACILITY_COUNT; j++){
  194. if(current[i]==1){
  195. current[i] = 0;
  196. // localScore = function(current,matrix);
  197. localScore -= 3000;
  198. for(int k = 0; k < CLIENT_COUNT; k++){
  199. if(best[k] == i){
  200. localScore -= bestValue[j];
  201. localScore += secondValue[j];
  202. }
  203. }
  204. if(localScore < bestScore){
  205. bestScore = localScore;
  206. localBest = current;
  207. flag = 1;
  208. }
  209. current[i] = 1;
  210. }
  211. }
  212. }
  213. else{
  214. current[i] = 0;
  215. //localScore = function(current,matrix);
  216. localScore -= 3000;
  217. for(int k = 0; k < CLIENT_COUNT; k++){
  218. if(best[k] == i){
  219. localScore -= bestValue[k];
  220. localScore += secondValue[k];
  221. }
  222. }
  223. if(localScore < bestScore){
  224. bestScore = localScore;
  225. localBest = current;
  226. flag = 1;
  227. }
  228. for(int j = i + 1; j < FACILITY_COUNT; j++){
  229. if(current[i]==0){
  230. current[i] = 1;
  231. // localScore = function(current,matrix);
  232. localScore += 3000;
  233. for(int j = 0; j < CLIENT_COUNT; j++)
  234. if (matrix[i][j] < bestValue[j]){
  235. localScore += matrix[i][j];
  236. localScore -= bestValue[j];
  237. }
  238. if(localScore < bestScore){
  239. bestScore = localScore;
  240. localBest = current;
  241. flag = 1;
  242. }
  243. current[i] = 0;
  244. }
  245. }
  246. }
  247. child = localBest;
  248. }
  249. if(flag == 0)
  250. break;
  251. }
  252. }
  253.  
  254. void updatePopulation(std::vector<std::vector<int>> &population, std::vector<int> child, const std::vector<std::vector<int>> &matrix){
  255. std::vector<std::vector<int>>::iterator numberOfWorst;
  256. int max = function(population[0],matrix);
  257. for(std::vector<std::vector<int>>::iterator i =population.begin(); i < population.end(); i++)
  258. if (function(*i, matrix)>=max)
  259. numberOfWorst = i;
  260. population.erase(numberOfWorst);
  261. population.push_back(child);
  262. }
  263.  
  264. std::vector<int> bestInPopulation(std::vector<std::vector<int>> &population, const std::vector<std::vector<int>> &matrix){
  265. std::vector<std::vector<int>>::iterator numberOfBest;
  266. unsigned int min = function(population[0],matrix);
  267. for(std::vector<std::vector<int>>::iterator i = population.begin(); i < population.end(); i++)
  268. if (function(*i, matrix)<=min){
  269. min = function(*i, matrix);
  270. numberOfBest = i;
  271. }
  272. return *numberOfBest;
  273. }
  274.  
  275. int main(){
  276. std::vector<std::vector<int>> population;
  277. std::vector<std::vector<int>> matrix;
  278. std::vector<int> child;
  279. srand(time(NULL));
  280. readMatrix(matrix,"test.txt");
  281. generateFirstPopulation(population);
  282. std::cout<<function(bestInPopulation(population,matrix),matrix)<<std::endl;
  283. for(int i = 0; i < N; i++){
  284. child = selectionAndCrossover(population, matrix);
  285. mutation(child);
  286. localFall(child,matrix);
  287. updatePopulation(population,child,matrix);
  288. int bestScore = function(bestInPopulation(population,matrix),matrix);
  289. std::cout<<"i "<< i<< " Score "<< bestScore<<std::endl;
  290. }
  291. std::vector<int> best = bestInPopulation(population,matrix);
  292. for(int i = 0; i < FACILITY_COUNT; i++)
  293. if(best[i]==1)
  294. std::cout<<i<<std::endl;
  295. return 0;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement