kbklpl21

Untitled

Apr 8th, 2020
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <ctime>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11. long int **read_matrix(ifstream &input, long int n);
  12. double ran01(long int *idum);
  13. long int *generate_random_vector(long int n);
  14. unsigned long int compute_evaluation_function(long int n, long int **d, long int **f, long int *p);
  15. void first_2_opt_symmetric (long int n, long int **d, long int **f, long int *p);
  16. void swap(long int pos_1, long int pos_2, long int *);
  17. void perturbation(long int pert_size, long int n, long int **d, long int **f, long int *p);
  18. long int actual_solution_value;
  19. long int new_eval;
  20. long int current_eval;
  21.  
  22. #define IA 16807
  23. #define IM 2147483647
  24. #define AM (1.0/IM)
  25. #define IQ 127773
  26. #define IR 2836
  27.  
  28. int main()
  29. {
  30. string name;
  31. long int optimum;
  32. ifstream infile;
  33. long int **matrix;
  34. long int *v;
  35. long int *x;
  36. long int *hold_value;
  37. int *chosen;
  38. long int n;
  39. long int **d;
  40. long int **f;
  41. long int *p;
  42. long int initial_eval;
  43. long int *ILS_best_p;
  44. ILS_best_p = new long int[n];
  45. long int new_vs_current;
  46. new_vs_current = INT_MAX;
  47. long int current_best_eval;
  48. current_best_eval = INT_MAX;
  49. //long int *array = new long int[n];
  50. long int new_current_eval;
  51. long int i;
  52. long int j;
  53. long int k;
  54.  
  55. infile.open("nug12.dat");
  56. infile >> name;
  57. cout << "Name: " << name << "\n";
  58.  
  59. infile >> n;
  60. cout << "Rows and Columns: " << n << "\n";
  61.  
  62. infile >> optimum;
  63. cout << "Optimum solution: " << optimum << "\n";
  64. d=read_matrix(infile, n);
  65. f=read_matrix(infile, n);
  66. cout << endl;
  67.  
  68. p=generate_random_vector(n);
  69. compute_evaluation_function(n, d, f, p);
  70. cout << endl;
  71. first_2_opt_symmetric (n, d, f, p);
  72. cout << endl;
  73. initial_eval = compute_evaluation_function(n, d, f, p);
  74. cout << endl << endl;
  75.  
  76. for (i = 0; i < 10000; i++) //ILS loop
  77. {
  78. if (new_eval < current_eval)
  79. {
  80. new_vs_current = new_eval;
  81. cout << "iteration 1st = " << i+1 << " " << endl;
  82. }
  83. else if (current_eval < new_eval)
  84. {
  85. new_vs_current = current_eval;
  86. cout << "iteration 2nd = " << i+1 << " " << endl;
  87. }
  88. cout << "iteration = " << i+1 << " " << endl;
  89. cout << " Vector before perturbation = ";
  90. for (j = 0; j < n; j++)
  91. {
  92. cout << p[j] << " ";
  93. }
  94. cout << endl << endl;
  95. perturbation(3, n, d, f, p);
  96. cout << endl << endl;
  97.  
  98. cout << " Vector after perturbation = ";
  99. for (j = 0; j < n; j++)
  100. {
  101. cout << p[j] << " ";
  102. }
  103. cout << endl << endl;
  104.  
  105. first_2_opt_symmetric (n, d, f, p);
  106.  
  107. new_current_eval = compute_evaluation_function(n, d, f, p);
  108.  
  109. cout << endl;
  110.  
  111. cout << " Vector after local search = ";
  112. for (j = 0; j < n; j++)
  113. {
  114. cout << p[j] << " ";
  115. }
  116. cout << endl << endl;
  117.  
  118. if (new_current_eval < current_best_eval)
  119. {
  120. new_eval = new_current_eval;
  121. cout << "iteration 3rd = " << i+1 << " " << endl;
  122. }
  123. else if (current_best_eval <= new_current_eval)
  124. {
  125. new_eval = current_best_eval;
  126. cout << "iteration 4th = " << i+1 << " " << endl;
  127. }
  128.  
  129. if (new_eval < new_vs_current)
  130. {
  131. current_best_eval = new_eval;
  132. cout << "iteration 5th = " << i+1 << " " << endl;
  133. for(k = 0; k < n; k++)
  134. {
  135. ILS_best_p[k] = p[k];
  136. }
  137. }
  138. else if (new_vs_current < new_eval)
  139. {
  140. current_best_eval = new_vs_current;
  141. cout << "iteration 6th = " << i+1 << " " << endl;
  142. for(k = 0; k < n; k++)
  143. {
  144. ILS_best_p[k] = p[k];
  145. }
  146. }
  147. }
  148. cout << endl << "current best eval = " << current_best_eval << endl;
  149.  
  150. cout << "ILS best vector = ";
  151. for (i = 0; i < n; i++)
  152. {
  153. cout << ILS_best_p[i] << " ";
  154. }
  155. return 0;
  156. delete [] ILS_best_p;
  157. delete [] matrix;
  158. delete [] v;
  159. delete [] x;
  160. delete [] hold_value;
  161. delete [] chosen;
  162. }
  163.  
  164. long int **read_matrix(ifstream &input, long int n)
  165. {
  166.  
  167. /*
  168. FUNCTION: read instance matrices
  169. INPUT: instance file name, instance size
  170. OUTPUT: d-matrix and f-matrix
  171. (SIDE)EFFECTS: none
  172. COMMENTS: read the distance matrix and flow matrix
  173. */
  174.  
  175.  
  176. long int i, j;
  177. long int **matrix = new long int *[n];
  178. for (i = 0; i < n; i++)
  179. {
  180. matrix[i] = new long int[n];
  181. for (j = 0; j < n; j++)
  182. {
  183. if( !(input >> matrix[i][j]) )
  184. {
  185. cerr << "Error reading at " << i << j << endl;
  186. exit(1);
  187. }
  188. }
  189. }
  190. return matrix;
  191. }
  192.  
  193. double ran01(long int *idum)
  194. {
  195.  
  196. /*
  197. FUNCTION: returns a pseudo-random number
  198. INPUT: a pointer to the seed variable
  199. OUTPUT: a pseudo-random number uniformly distributed in [0,1]
  200. (SIDE)EFFECTS: changes the value of seed
  201. */
  202.  
  203.  
  204. long k;
  205. double ans;
  206.  
  207. k =(*idum)/IQ;
  208. *idum = IA * (*idum - k * IQ) - IR * k;
  209. if (*idum < 0 )
  210. {
  211. *idum += IM;
  212. }
  213. ans = AM * (*idum);
  214. return ans;
  215. }
  216.  
  217. long int *generate_random_vector(long int n)
  218. {
  219.  
  220. /*
  221. FUNCTION: generates a random vector
  222. INPUT: vector dimension
  223. OUTPUT: returns pointer to vector, free memory when vector is not needed anymore
  224. (SIDE)EFFECTS: none
  225. */
  226.  
  227.  
  228. long int i, j, help;
  229. long int *v;
  230. srand(static_cast<size_t>(time(0)));
  231. long int seed=rand();
  232.  
  233. v = new long int[ n ];
  234.  
  235. for ( i = 0 ; i < n; i++ )
  236. {
  237. v[i] = i;
  238. }
  239.  
  240. for ( i = 0 ; i < n-1 ; i++)
  241. {
  242. j = (long int) ( ran01( &seed ) * (n - i));
  243. assert( i + j < n );
  244. help = v[i];
  245. v[i] = v[i+j];
  246. v[i+j] = help;
  247. }
  248. return v;
  249. }
  250.  
  251. unsigned long int compute_evaluation_function(long int n, long int **d, long int **f, long int *p)
  252. {
  253.  
  254. /*
  255. FUNCTION: compute evaluation function
  256. INPUT: pointer to solution
  257. OUTPUT: evaluation function
  258. (SIDE)EFFECTS: none
  259. COMMENTS: none
  260. */
  261.  
  262. long int i, j;
  263. unsigned long obj_f_value;
  264.  
  265. obj_f_value = 0;
  266. for(i = 0; i < n; i++)
  267. {
  268. for(j = 0; j < n; j++)
  269. {
  270. obj_f_value += d[i][j] * f[p[i]][p[j]];
  271. }
  272. }
  273. cout << obj_f_value;
  274. return obj_f_value;
  275. }
  276.  
  277. void first_2_opt_symmetric (long int n, long int **d, long int **f, long int *p)
  278. {
  279. /*
  280. FUNCTION: first improvement 2-opt local search for symmetric instances
  281. INPUT: pointer to some initial assignment
  282. OUTPUT: none
  283. (SIDE)EFFECTS: initial assignment is locally optimized, dlb is used to increase the search speed, the dlb value is changed to false if item change it's location during perturbation
  284. COMMENT: neighborhood is scanned in random order, use of register for faster computation
  285. */
  286.  
  287. long int improvement = 1;
  288. long int improve_item = 0;
  289. long int u, v, i, j, k;
  290. long int tmp;
  291. long int *x;
  292. improvement = 1;
  293. x = generate_random_vector(n);
  294.  
  295. while (improvement)
  296. {
  297. improvement = 0;
  298. for ( i = 0 ; i < n ; i++ )
  299. {
  300. u = x[i];
  301. improve_item = 0;
  302. for ( j = 0 ; j < n ; j++ )
  303. {
  304. v = x[j];
  305. if (u == v)
  306. continue;
  307. tmp = 0;
  308. for ( k = 0 ; k < n ; k++ )
  309. {
  310. if ( (k != u) && (k != v) )
  311. {
  312. tmp += (d[u][k] - d[v][k]) * (f[p[v]][p[k]] - f[p[u]][p[k]]);
  313. }
  314. }
  315. if (tmp < 0)
  316. {
  317. improvement = 1;
  318. improve_item = 1;
  319. swap (u, v, p);
  320. }
  321. }
  322. }
  323. }
  324. }
  325.  
  326. void swap(long int pos_1, long int pos_2, long int *p)
  327. {
  328.  
  329. /*
  330. FUNCTION: swap position of two items in a vector
  331. INPUT: position 1, position 2, pointer to vector
  332. OUTPUT: none
  333. (SIDE)EFFECTS: none
  334. COMMENTS: none
  335. */
  336.  
  337. long int tmp;
  338.  
  339. tmp = p[pos_1];
  340. p[pos_1] = p[pos_2];
  341. p[pos_2] = tmp;
  342. }
  343.  
  344. void perturbation(long int pert_size, long int n, long int **d, long int **f, long int *p)
  345. {
  346. /*
  347. FUNCTION: apply random perturbation
  348. INPUT: pointer to solution, perturbation scheme, perturbation strength, change in perturbation, end perturbation size
  349. OUTPUT: none
  350. (SIDE)EFFECTS: new solution formed from old solution
  351. COMMENTS: none
  352. */
  353.  
  354. long int *hold_value = new long int[n]; //allocate memory for items to be chosen in move/*
  355. int *chosen = new int[pert_size]; //*allocate temporary memory to determine items to be moved */*
  356. long int i;
  357.  
  358. for(i = 0; i < n; i++)
  359. {
  360. hold_value[i] = p[i]; //initialize hold_value with the same value from local search/*
  361. }
  362. int j = n - 1;
  363. int rand_number;
  364. int rand_no;
  365.  
  366. for(i = 1; i <= pert_size; i++)
  367. {
  368. rand_number = rand();
  369. rand_no = rand_number % j; //choose random number from 1 to size - 1/
  370. chosen[i - 1] = rand_no + i; //copy the value to chosen[i]
  371. j--;
  372. }
  373. long int temp;
  374.  
  375. for(i = 0; i < pert_size; i++)
  376. {
  377. temp = chosen[i];
  378. swap(i, temp, hold_value); //swap chosen[i] and hold_value[i]; n first item in hold_value[i] is the index array that will be included in perturbation/
  379. }
  380.  
  381. long int temp1;
  382. long int temp2;
  383. temp1 = p[hold_value[1]];//
  384. temp2 = p[hold_value[0]];
  385.  
  386. for(i = 0; i < pert_size - 1; i++)
  387. {
  388. p[hold_value[i + 1]] = temp2;
  389. temp2 = temp1;
  390. temp1 = p[hold_value[i + 2]];
  391. }
  392.  
  393. p[hold_value[0]] = temp2;
  394.  
  395. actual_solution_value = compute_evaluation_function(n, d, f, p);
  396. new_eval = actual_solution_value;
  397. current_eval = new_eval;
  398. }
Advertisement
Add Comment
Please, Sign In to add comment