kbklpl21

Untitled

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