kbklpl21

Untitled

Apr 9th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 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. #include <climits>
  9.  
  10. using namespace std;
  11.  
  12. long int **read_matrix(ifstream &input, long int n);
  13. double ran01(long int *idum);
  14. long int *generate_random_vector(long int n);
  15. unsigned long int compute_evaluation_function(long int n, long int **d, long int **f, long int *p);
  16. void first_2_opt_symmetric (long int n, long int **d, long int **f, long int *p);
  17. void swap(long int pos_1, long int pos_2, long int *);
  18. void perturbation(long int pert_size, long int n, long int **d, long int **f, long int *p);
  19. long int actual_solution_value;
  20. long int new_eval;
  21. long int current_eval;
  22.  
  23. #define IA 16807
  24. #define IM 2147483647
  25. #define AM (1.0/IM)
  26. #define IQ 127773
  27. #define IR 2836
  28.  
  29. int main()
  30. {
  31. string name;
  32. long int optimum;
  33. ifstream infile;
  34. long int n;
  35. long int **d = NULL;
  36. long int **f = NULL;
  37. long int *p = NULL;
  38. long int initial_eval;
  39. long int *ILS_best_p = NULL;
  40. long int new_vs_current;
  41. new_vs_current = INT_MAX;
  42. long int current_best_eval;
  43. current_best_eval = INT_MAX;
  44. long int new_current_eval;
  45. long int i;
  46. long int j;
  47. long int k;
  48.  
  49. infile.open("nug12.dat");
  50. infile >> name;
  51. cout << "Name: " << name << "\n";
  52.  
  53. infile >> n;
  54. cout << "Rows and Columns: " << n << "\n";
  55.  
  56. ILS_best_p = new long int[n];
  57.  
  58. infile >> optimum;
  59. cout << "Optimum solution: " << optimum << "\n";
  60. d=read_matrix(infile, n);
  61. f=read_matrix(infile, n);
  62. cout << endl;
  63.  
  64. p = generate_random_vector(n);
  65. compute_evaluation_function(n, d, f, p);
  66. cout << endl;
  67. first_2_opt_symmetric (n, d, f, p);
  68. cout << endl;
  69. initial_eval = compute_evaluation_function(n, d, f, p);
  70. cout << endl << endl;
  71.  
  72. for (i = 0; i < 10000; i++) //ILS loop
  73. {
  74. if (new_eval < current_eval)
  75. {
  76. new_vs_current = new_eval;
  77. cout << "iteration 1st = " << i+1 << " " << endl;
  78. }
  79. else if (current_eval < new_eval)
  80. {
  81. new_vs_current = current_eval;
  82. cout << "iteration 2nd = " << i+1 << " " << endl;
  83. }
  84. cout << "iteration = " << i+1 << " " << endl;
  85. cout << " Vector before perturbation = ";
  86. for (j = 0; j < n; j++)
  87. {
  88. cout << p[j] << " ";
  89. }
  90. cout << endl << endl;
  91. perturbation(3, n, d, f, p);
  92. cout << endl << endl;
  93.  
  94. cout << " Vector after perturbation = ";
  95. for (j = 0; j < n; j++)
  96. {
  97. cout << p[j] << " ";
  98. }
  99. cout << endl << endl;
  100.  
  101. first_2_opt_symmetric (n, d, f, p);
  102.  
  103. new_current_eval = compute_evaluation_function(n, d, f, p);
  104.  
  105. cout << endl;
  106.  
  107. cout << " Vector after local search = ";
  108. for (j = 0; j < n; j++)
  109. {
  110. cout << p[j] << " ";
  111. }
  112. cout << endl << endl;
  113.  
  114. if (new_current_eval < current_best_eval)
  115. {
  116. new_eval = new_current_eval;
  117. cout << "iteration 3rd = " << i+1 << " " << endl;
  118. }
  119. else if (current_best_eval <= new_current_eval)
  120. {
  121. new_eval = current_best_eval;
  122. cout << "iteration 4th = " << i+1 << " " << endl;
  123. }
  124.  
  125. if (new_eval < new_vs_current)
  126. {
  127. current_best_eval = new_eval;
  128. cout << "iteration 5th = " << i+1 << " " << endl;
  129. for(k = 0; k < n; k++)
  130. {
  131. ILS_best_p[k] = p[k];
  132. }
  133. }
  134. else if (new_vs_current < new_eval)
  135. {
  136. current_best_eval = new_vs_current;
  137. cout << "iteration 6th = " << i+1 << " " << endl;
  138. for(k = 0; k < n; k++)
  139. {
  140. ILS_best_p[k] = p[k];
  141. }
  142. }
  143. }
  144. cout << endl << "current best eval = " << current_best_eval << endl;
  145.  
  146. cout << "ILS best vector = ";
  147. for (i = 0; i < n; i++)
  148. {
  149. cout << ILS_best_p[i] << " ";
  150. }
  151. for ( i = 0 ; i < n ; i++ )
  152. {
  153. delete [] d[i];
  154. delete [] f[i];
  155. }
  156. delete [] d;
  157. delete [] f;
  158. delete [] p;
  159. delete [] ILS_best_p;
  160.  
  161. return 0;
  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 = NULL;
  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. delete [] x;
  325. }
  326.  
  327. void swap(long int pos_1, long int pos_2, long int *p)
  328. {
  329.  
  330. /*
  331. FUNCTION: swap position of two items in a vector
  332. INPUT: position 1, position 2, pointer to vector
  333. OUTPUT: none
  334. (SIDE)EFFECTS: none
  335. COMMENTS: none
  336. */
  337.  
  338. long int tmp;
  339.  
  340. tmp = p[pos_1];
  341. p[pos_1] = p[pos_2];
  342. p[pos_2] = tmp;
  343. }
  344.  
  345. void perturbation(long int pert_size, long int n, long int **d, long int **f, long int *p)
  346. {
  347. /*
  348. FUNCTION: apply random perturbation
  349. INPUT: pointer to solution, perturbation scheme, perturbation strength, change in perturbation, end perturbation size
  350. OUTPUT: none
  351. (SIDE)EFFECTS: new solution formed from old solution
  352. COMMENTS: none
  353. */
  354.  
  355. long int *hold_value = new long int[n]; //allocate memory for items to be chosen in move/*
  356. int *chosen = new int[pert_size]; //*allocate temporary memory to determine items to be moved */*
  357. long int i;
  358.  
  359. for(i = 0; i < n; i++)
  360. {
  361. hold_value[i] = p[i]; //initialize hold_value with the same value from local search/*
  362. }
  363. int j = n - 1;
  364. int rand_number;
  365. int rand_no;
  366.  
  367. for(i = 1; i <= pert_size; i++)
  368. {
  369. rand_number = rand();
  370. rand_no = rand_number % j; //choose random number from 1 to size - 1/
  371. chosen[i - 1] = rand_no + i; //copy the value to chosen[i]
  372. j--;
  373. }
  374. long int temp;
  375.  
  376. for(i = 0; i < pert_size; i++)
  377. {
  378. temp = chosen[i];
  379. 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/
  380. }
  381.  
  382. long int temp1;
  383. long int temp2;
  384. temp1 = p[hold_value[1]];//
  385. temp2 = p[hold_value[0]];
  386.  
  387. for(i = 0; i < pert_size - 1; i++)
  388. {
  389. p[hold_value[i + 1]] = temp2;
  390. temp2 = temp1;
  391. temp1 = p[hold_value[i + 2]];
  392. }
  393.  
  394. p[hold_value[0]] = temp2;
  395.  
  396. actual_solution_value = compute_evaluation_function(n, d, f, p);
  397. new_eval = actual_solution_value;
  398. current_eval = new_eval;
  399.  
  400. delete [] hold_value;
  401. delete [] chosen;
  402. }
Advertisement
Add Comment
Please, Sign In to add comment