kbklpl21

Untitled

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