PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/pa/ant/doc/exemplo_do_ACO/ls.c

http://orgarq.googlecode.com/
C | 1532 lines | 1203 code | 116 blank | 213 comment | 339 complexity | 457408800477431cdeac3455dbe413b1 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. AAAA CCCC OOOO TTTTTT SSSSS PPPPP
  3. AA AA CC OO OO TT SS PP PP
  4. AAAAAA CC OO OO TT SSSS PPPPP
  5. AA AA CC OO OO TT SS PP
  6. AA AA CCCC OOOO TT SSSSS PP
  7. ######################################################
  8. ########## ACO algorithms for the TSP ##########
  9. ######################################################
  10. Version: 1.0
  11. File: ls.c
  12. Author: Thomas Stuetzle
  13. Purpose: implementation of local search routines
  14. Check: README and gpl.txt
  15. Copyright (C) 1999 Thomas Stuetzle
  16. */
  17. /***************************************************************************
  18. Program's name: acotsp
  19. Ant Colony Optimization algorithms (AS, ACS, EAS, RAS, MMAS, BWAS) for the
  20. symmetric TSP
  21. Copyright (C) 2004 Thomas Stuetzle
  22. This program is free software; you can redistribute it and/or modify
  23. it under the terms of the GNU General Public License as published by
  24. the Free Software Foundation; either version 2 of the License, or
  25. (at your option) any later version.
  26. This program is distributed in the hope that it will be useful,
  27. but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. GNU General Public License for more details.
  30. You should have received a copy of the GNU General Public License
  31. along with this program; if not, write to the Free Software
  32. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  33. email: stuetzle no@spam informatik.tu-darmstadt.de
  34. mail address: Universitaet Darmstadt
  35. Fachbereich Informatik
  36. Hochschulstr. 10
  37. D-64283 Darmstadt
  38. Germany
  39. ***************************************************************************/
  40. #include <stdio.h>
  41. #include <assert.h>
  42. #include <stdlib.h>
  43. #include <limits.h>
  44. #include "InOut.h"
  45. #include "TSP.h"
  46. #include "ants.h"
  47. #include "utilities.h"
  48. long int ls_flag; /* indicates whether and which local search is used */
  49. long int nn_ls; /* maximal depth of nearest neighbour lists used in the
  50. local search */
  51. long int dlb_flag = TRUE; /* flag indicating whether don't look bits are used. I recommend
  52. to always use it if local search is applied */
  53. long int * generate_random_permutation( long int n )
  54. /*
  55. FUNCTION: generate a random permutation of the integers 0 .. n-1
  56. INPUT: length of the array
  57. OUTPUT: pointer to the random permutation
  58. (SIDE)EFFECTS: the array holding the random permutation is allocated in this
  59. function. Don't forget to free again the memory!
  60. COMMENTS: only needed by the local search procedures
  61. */
  62. {
  63. long int i, help, node, tot_assigned = 0;
  64. double rnd;
  65. long int *r;
  66. r = malloc(n * sizeof(int));
  67. for ( i = 0 ; i < n; i++)
  68. r[i] = i;
  69. for ( i = 0 ; i < n ; i++ ) {
  70. /* find (randomly) an index for a free unit */
  71. rnd = ran01 ( &seed );
  72. node = (long int) (rnd * (n - tot_assigned));
  73. assert( i + node < n );
  74. help = r[i];
  75. r[i] = r[i+node];
  76. r[i+node] = help;
  77. tot_assigned++;
  78. }
  79. return r;
  80. }
  81. void two_opt_first( long int *tour )
  82. /*
  83. FUNCTION: 2-opt a tour
  84. INPUT: pointer to the tour that undergoes local optimization
  85. OUTPUT: none
  86. (SIDE)EFFECTS: tour is 2-opt
  87. COMMENTS: the neighbourhood is scanned in random order (this need
  88. not be the best possible choice). Concerning the speed-ups used
  89. here consult, for example, Chapter 8 of
  90. Holger H. Hoos and Thomas Stuetzle,
  91. Stochastic Local Search---Foundations and Applications,
  92. Morgan Kaufmann Publishers, 2004.
  93. or some of the papers online available from David S. Johnson.
  94. */
  95. {
  96. long int c1, c2; /* cities considered for an exchange */
  97. long int s_c1, s_c2; /* successor cities of c1 and c2 */
  98. long int p_c1, p_c2; /* predecessor cities of c1 and c2 */
  99. long int pos_c1, pos_c2; /* positions of cities c1, c2 */
  100. long int i, j, h, l;
  101. long int improvement_flag, improve_node, help, n_improves = 0, n_exchanges=0;
  102. long int h1=0, h2=0, h3=0, h4=0;
  103. long int radius; /* radius of nn-search */
  104. long int gain = 0;
  105. long int *random_vector;
  106. long int *pos; /* positions of cities in tour */
  107. long int *dlb; /* vector containing don't look bits */
  108. pos = malloc(n * sizeof(long int));
  109. dlb = malloc(n * sizeof(long int));
  110. for ( i = 0 ; i < n ; i++ ) {
  111. pos[tour[i]] = i;
  112. dlb[i] = FALSE;
  113. }
  114. improvement_flag = TRUE;
  115. random_vector = generate_random_permutation( n );
  116. while ( improvement_flag ) {
  117. improvement_flag = FALSE;
  118. for (l = 0 ; l < n; l++) {
  119. c1 = random_vector[l];
  120. DEBUG ( assert ( c1 < n && c1 >= 0); )
  121. if ( dlb_flag && dlb[c1] )
  122. continue;
  123. improve_node = FALSE;
  124. pos_c1 = pos[c1];
  125. s_c1 = tour[pos_c1+1];
  126. radius = instance.distance[c1][s_c1];
  127. /* First search for c1's nearest neighbours, use successor of c1 */
  128. for ( h = 0 ; h < nn_ls ; h++ ) {
  129. c2 = instance.nn_list[c1][h]; /* exchange partner, determine its position */
  130. if ( radius > instance.distance[c1][c2] ) {
  131. s_c2 = tour[pos[c2]+1];
  132. gain = - radius + instance.distance[c1][c2] +
  133. instance.distance[s_c1][s_c2] - instance.distance[c2][s_c2];
  134. if ( gain < 0 ) {
  135. h1 = c1; h2 = s_c1; h3 = c2; h4 = s_c2;
  136. improve_node = TRUE;
  137. goto exchange2opt;
  138. }
  139. }
  140. else
  141. break;
  142. }
  143. /* Search one for next c1's h-nearest neighbours, use predecessor c1 */
  144. if (pos_c1 > 0)
  145. p_c1 = tour[pos_c1-1];
  146. else
  147. p_c1 = tour[n-1];
  148. radius = instance.distance[p_c1][c1];
  149. for ( h = 0 ; h < nn_ls ; h++ ) {
  150. c2 = instance.nn_list[c1][h]; /* exchange partner, determine its position */
  151. if ( radius > instance.distance[c1][c2] ) {
  152. pos_c2 = pos[c2];
  153. if (pos_c2 > 0)
  154. p_c2 = tour[pos_c2-1];
  155. else
  156. p_c2 = tour[n-1];
  157. if ( p_c2 == c1 )
  158. continue;
  159. if ( p_c1 == c2 )
  160. continue;
  161. gain = - radius + instance.distance[c1][c2] +
  162. instance.distance[p_c1][p_c2] - instance.distance[p_c2][c2];
  163. if ( gain < 0 ) {
  164. h1 = p_c1; h2 = c1; h3 = p_c2; h4 = c2;
  165. improve_node = TRUE;
  166. goto exchange2opt;
  167. }
  168. }
  169. else
  170. break;
  171. }
  172. if (improve_node) {
  173. exchange2opt:
  174. n_exchanges++;
  175. improvement_flag = TRUE;
  176. dlb[h1] = FALSE; dlb[h2] = FALSE;
  177. dlb[h3] = FALSE; dlb[h4] = FALSE;
  178. /* Now perform move */
  179. if ( pos[h3] < pos[h1] ) {
  180. help = h1; h1 = h3; h3 = help;
  181. help = h2; h2 = h4; h4 = help;
  182. }
  183. if ( pos[h3] - pos[h2] < n / 2 + 1) {
  184. /* reverse inner part from pos[h2] to pos[h3] */
  185. i = pos[h2]; j = pos[h3];
  186. while (i < j) {
  187. c1 = tour[i];
  188. c2 = tour[j];
  189. tour[i] = c2;
  190. tour[j] = c1;
  191. pos[c1] = j;
  192. pos[c2] = i;
  193. i++; j--;
  194. }
  195. }
  196. else {
  197. /* reverse outer part from pos[h4] to pos[h1] */
  198. i = pos[h1]; j = pos[h4];
  199. if ( j > i )
  200. help = n - (j - i) + 1;
  201. else
  202. help = (i - j) + 1;
  203. help = help / 2;
  204. for ( h = 0 ; h < help ; h++ ) {
  205. c1 = tour[i];
  206. c2 = tour[j];
  207. tour[i] = c2;
  208. tour[j] = c1;
  209. pos[c1] = j;
  210. pos[c2] = i;
  211. i--; j++;
  212. if ( i < 0 )
  213. i = n-1;
  214. if ( j >= n )
  215. j = 0;
  216. }
  217. tour[n] = tour[0];
  218. }
  219. } else {
  220. dlb[c1] = TRUE;
  221. }
  222. }
  223. if ( improvement_flag ) {
  224. n_improves++;
  225. }
  226. }
  227. free( random_vector );
  228. free( dlb );
  229. free( pos );
  230. }
  231. void two_h_opt_first( long int *tour )
  232. /*
  233. FUNCTION: 2-h-opt a tour
  234. INPUT: pointer to the tour that undergoes local optimization
  235. OUTPUT: none
  236. (SIDE)EFFECTS: tour is 2-h-opt
  237. COMMENTS: for details on 2-h-opt see J. L. Bentley. Fast algorithms for geometric
  238. traveling salesman problems. ORSA Journal on Computing,
  239. 4(4):387--411, 1992.
  240. The neighbourhood is scanned in random order (this need
  241. not be the best possible choice). Concerning the speed-ups used
  242. here consult, for example, Chapter 8 of
  243. Holger H. Hoos and Thomas Stuetzle,
  244. Stochastic Local Search---Foundations and Applications,
  245. Morgan Kaufmann Publishers, 2004.
  246. or some of the papers online available from David S. Johnson.
  247. */
  248. {
  249. long int c1, c2; /* cities considered for an exchange */
  250. long int s_c1, s_c2; /* successors of c1 and c2 */
  251. long int p_c1, p_c2; /* predecessors of c1 and c2 */
  252. long int pos_c1, pos_c2; /* positions of cities c1, c2 */
  253. long int i, j, h, l;
  254. long int improvement_flag, improve_node;
  255. long int h1=0, h2=0, h3=0, h4=0, h5=0, help;
  256. long int radius; /* radius of nn-search */
  257. long int gain = 0;
  258. long int *random_vector;
  259. long int two_move, node_move;
  260. long int *pos; /* positions of cities in tour */
  261. long int *dlb; /* vector containing don't look bits */
  262. pos = malloc(n * sizeof(long int));
  263. dlb = malloc(n * sizeof(long int));
  264. for ( i = 0 ; i < n ; i++ ) {
  265. pos[tour[i]] = i;
  266. dlb[i] = FALSE;
  267. }
  268. improvement_flag = TRUE;
  269. random_vector = generate_random_permutation( n );
  270. while ( improvement_flag ) {
  271. improvement_flag = FALSE; two_move = FALSE; node_move = FALSE;
  272. for (l = 0 ; l < n; l++) {
  273. c1 = random_vector[l];
  274. DEBUG ( assert ( c1 < n && c1 >= 0); )
  275. if ( dlb_flag && dlb[c1] )
  276. continue;
  277. improve_node = FALSE;
  278. pos_c1 = pos[c1];
  279. s_c1 = tour[pos_c1+1];
  280. radius = instance.distance[c1][s_c1];
  281. /* First search for c1's nearest neighbours, use successor of c1 */
  282. for ( h = 0 ; h < nn_ls ; h++ ) {
  283. c2 = instance.nn_list[c1][h]; /* exchange partner, determine its position */
  284. if ( radius > instance.distance[c1][c2] ) {
  285. pos_c2 = pos[c2];
  286. s_c2 = tour[pos_c2+1];
  287. gain = - radius + instance.distance[c1][c2] +
  288. instance.distance[s_c1][s_c2] - instance.distance[c2][s_c2];
  289. if ( gain < 0 ) {
  290. h1 = c1; h2 = s_c1; h3 = c2; h4 = s_c2;
  291. improve_node = TRUE; two_move = TRUE; node_move = FALSE;
  292. goto exchange;
  293. }
  294. if (pos_c2 > 0)
  295. p_c2 = tour[pos_c2-1];
  296. else
  297. p_c2 = tour[n-1];
  298. gain = - radius + instance.distance[c1][c2] + instance.distance[c2][s_c1]
  299. + instance.distance[p_c2][s_c2] - instance.distance[c2][s_c2]
  300. - instance.distance[p_c2][c2];
  301. if ( c2 == s_c1 )
  302. gain = 0;
  303. if ( p_c2 == s_c1 )
  304. gain = 0;
  305. gain = 0;
  306. if ( gain < 0 ) {
  307. h1 = c1; h2 = s_c1; h3 = c2; h4 = p_c2; h5 = s_c2;
  308. improve_node = TRUE; node_move = TRUE; two_move = FALSE;
  309. goto exchange;
  310. }
  311. }
  312. else
  313. break;
  314. }
  315. /* Second search for c1's nearest neighbours, use predecessor c1 */
  316. if (pos_c1 > 0)
  317. p_c1 = tour[pos_c1-1];
  318. else
  319. p_c1 = tour[n-1];
  320. radius = instance.distance[p_c1][c1];
  321. for ( h = 0 ; h < nn_ls ; h++ ) {
  322. c2 = instance.nn_list[c1][h]; /* exchange partner, determine its position */
  323. if ( radius > instance.distance[c1][c2] ) {
  324. pos_c2 = pos[c2];
  325. if (pos_c2 > 0)
  326. p_c2 = tour[pos_c2-1];
  327. else
  328. p_c2 = tour[n-1];
  329. if ( p_c2 == c1 )
  330. continue;
  331. if ( p_c1 == c2 )
  332. continue;
  333. gain = - radius + instance.distance[c1][c2] +
  334. instance.distance[p_c1][p_c2] - instance.distance[p_c2][c2];
  335. if ( gain < 0 ) {
  336. h1 = p_c1; h2 = c1; h3 = p_c2; h4 = c2;
  337. improve_node = TRUE; two_move = TRUE; node_move = FALSE;
  338. goto exchange;
  339. }
  340. s_c2 = tour[pos[c2]+1];
  341. gain = - radius + instance.distance[c2][c1] + instance.distance[p_c1][c2]
  342. + instance.distance[p_c2][s_c2] - instance.distance[c2][s_c2]
  343. - instance.distance[p_c2][c2];
  344. if ( p_c1 == c2 )
  345. gain = 0;
  346. if ( p_c1 == s_c2 )
  347. gain = 0;
  348. if ( gain < 0 ) {
  349. h1 = p_c1; h2 = c1; h3 = c2; h4 = p_c2; h5 = s_c2;
  350. improve_node = TRUE; node_move = TRUE; two_move = FALSE;
  351. goto exchange;
  352. }
  353. }
  354. else
  355. break;
  356. }
  357. exchange:
  358. if (improve_node) {
  359. if ( two_move ) {
  360. improvement_flag = TRUE;
  361. dlb[h1] = FALSE; dlb[h2] = FALSE;
  362. dlb[h3] = FALSE; dlb[h4] = FALSE;
  363. /* Now perform move */
  364. if ( pos[h3] < pos[h1] ) {
  365. help = h1; h1 = h3; h3 = help;
  366. help = h2; h2 = h4; h4 = help;
  367. }
  368. if ( pos[h3] - pos[h2] < n / 2 + 1) {
  369. /* reverse inner part from pos[h2] to pos[h3] */
  370. i = pos[h2]; j = pos[h3];
  371. while (i < j) {
  372. c1 = tour[i];
  373. c2 = tour[j];
  374. tour[i] = c2;
  375. tour[j] = c1;
  376. pos[c1] = j;
  377. pos[c2] = i;
  378. i++; j--;
  379. }
  380. }
  381. else {
  382. /* reverse outer part from pos[h4] to pos[h1] */
  383. i = pos[h1]; j = pos[h4];
  384. if ( j > i )
  385. help = n - (j - i) + 1;
  386. else
  387. help = (i - j) + 1;
  388. help = help / 2;
  389. for ( h = 0 ; h < help ; h++ ) {
  390. c1 = tour[i];
  391. c2 = tour[j];
  392. tour[i] = c2;
  393. tour[j] = c1;
  394. pos[c1] = j;
  395. pos[c2] = i;
  396. i--; j++;
  397. if ( i < 0 )
  398. i = n-1;
  399. if ( j >= n )
  400. j = 0;
  401. }
  402. tour[n] = tour[0];
  403. }
  404. } else if ( node_move ) {
  405. improvement_flag = TRUE;
  406. dlb[h1] = FALSE; dlb[h2] = FALSE; dlb[h3] = FALSE;
  407. dlb[h4] = FALSE; dlb[h5] = FALSE;
  408. /* Now perform move */
  409. if ( pos[h3] < pos[h1] ) {
  410. help = pos[h1] - pos[h3];
  411. i = pos[h3];
  412. for ( h = 0 ; h < help ; h++ ) {
  413. c1 = tour[i+1];
  414. tour[i] = c1;
  415. pos[c1] = i;
  416. i++;
  417. }
  418. tour[i] = h3;
  419. pos[h3] = i;
  420. tour[n] = tour[0];
  421. } else {
  422. /* pos[h3] > pos[h1] */
  423. help = pos[h3] - pos[h1];
  424. /* if ( help < n / 2 + 1) { */
  425. i = pos[h3];
  426. for ( h = 0 ; h < help - 1 ; h++ ) {
  427. c1 = tour[i-1];
  428. tour[i] = c1;
  429. pos[c1] = i;
  430. i--;
  431. }
  432. tour[i] = h3;
  433. pos[h3] = i;
  434. tour[n] = tour[0];
  435. /* } */
  436. }
  437. } else {
  438. fprintf(stderr,"this should never occur, 2-h-opt!!\n");
  439. exit(0);
  440. }
  441. two_move = FALSE; node_move = FALSE;
  442. } else {
  443. dlb[c1] = TRUE;
  444. }
  445. }
  446. }
  447. free( random_vector );
  448. free( dlb );
  449. free( pos );
  450. }
  451. void three_opt_first( long int *tour )
  452. /*
  453. FUNCTION: 3-opt the tour
  454. INPUT: pointer to the tour that is to optimize
  455. OUTPUT: none
  456. (SIDE)EFFECTS: tour is 3-opt
  457. COMMENT: this is certainly not the best possible implementation of a 3-opt
  458. local search algorithm. In addition, it is very lengthy; the main
  459. reason herefore is that awkward way of making an exchange, where
  460. it is tried to copy only the shortest possible part of a tour.
  461. Whoever improves the code regarding speed or solution quality, please
  462. drop me the code at stuetzle no@spam informatik.tu-darmstadt.de
  463. The neighbourhood is scanned in random order (this need
  464. not be the best possible choice). Concerning the speed-ups used
  465. here consult, for example, Chapter 8 of
  466. Holger H. Hoos and Thomas Stuetzle,
  467. Stochastic Local Search---Foundations and Applications,
  468. Morgan Kaufmann Publishers, 2004.
  469. or some of the papers online available from David S. Johnson.
  470. */
  471. {
  472. /* In case a 2-opt move should be performed, we only need to store opt2_move = TRUE,
  473. as h1, .. h4 are used in such a way that they store the indices of the correct move */
  474. long int c1, c2, c3; /* cities considered for an exchange */
  475. long int s_c1, s_c2, s_c3; /* successors of these cities */
  476. long int p_c1, p_c2, p_c3; /* predecessors of these cities */
  477. long int pos_c1, pos_c2, pos_c3; /* positions of cities c1, c2, c3 */
  478. long int i, j, h, g, l;
  479. long int improvement_flag, help;
  480. long int h1=0, h2=0, h3=0, h4=0, h5=0, h6=0; /* memorize cities involved in a move */
  481. long int diffs, diffp;
  482. long int between = FALSE;
  483. long int opt2_flag; /* = TRUE: perform 2-opt move, otherwise none or 3-opt move */
  484. long int move_flag; /*
  485. move_flag = 0 --> no 3-opt move
  486. move_flag = 1 --> between_move (c3 between c1 and c2)
  487. move_flag = 2 --> not_between with successors of c2 and c3
  488. move_flag = 3 --> not_between with predecessors of c2 and c3
  489. move_flag = 4 --> cyclic move
  490. */
  491. long int gain, move_value, radius, add1, add2;
  492. long int decrease_breaks; /* Stores decrease by breaking two edges (a,b) (c,d) */
  493. long int val[3];
  494. long int n1, n2, n3;
  495. long int *pos; /* positions of cities in tour */
  496. long int *dlb; /* vector containing don't look bits */
  497. long int *h_tour; /* help vector for performing exchange move */
  498. long int *hh_tour; /* help vector for performing exchange move */
  499. long int *random_vector;
  500. pos = malloc(n * sizeof(long int));
  501. dlb = malloc(n * sizeof(long int));
  502. h_tour = malloc(n * sizeof(long int));
  503. hh_tour = malloc(n * sizeof(long int));
  504. for ( i = 0 ; i < n ; i++ ) {
  505. pos[tour[i]] = i;
  506. dlb[i] = FALSE;
  507. }
  508. improvement_flag = TRUE;
  509. random_vector = generate_random_permutation( n );
  510. while ( improvement_flag ) {
  511. move_value = 0;
  512. improvement_flag = FALSE;
  513. for ( l = 0 ; l < n ; l++ ) {
  514. c1 = random_vector[l];
  515. if ( dlb_flag && dlb[c1] )
  516. continue;
  517. opt2_flag = FALSE;
  518. move_flag = 0;
  519. pos_c1 = pos[c1];
  520. s_c1 = tour[pos_c1+1];
  521. if (pos_c1 > 0)
  522. p_c1 = tour[pos_c1-1];
  523. else
  524. p_c1 = tour[n-1];
  525. h = 0; /* Search for one of the h-nearest neighbours */
  526. while ( h < nn_ls ) {
  527. c2 = instance.nn_list[c1][h]; /* second city, determine its position */
  528. pos_c2 = pos[c2];
  529. s_c2 = tour[pos_c2+1];
  530. if (pos_c2 > 0)
  531. p_c2 = tour[pos_c2-1];
  532. else
  533. p_c2 = tour[n-1];
  534. diffs = 0; diffp = 0;
  535. radius = instance.distance[c1][s_c1];
  536. add1 = instance.distance[c1][c2];
  537. /* Here a fixed radius neighbour search is performed */
  538. if ( radius > add1 ) {
  539. decrease_breaks = - radius - instance.distance[c2][s_c2];
  540. diffs = decrease_breaks + add1 + instance.distance[s_c1][s_c2];
  541. diffp = - radius - instance.distance[c2][p_c2] +
  542. instance.distance[c1][p_c2] + instance.distance[s_c1][c2];
  543. }
  544. else
  545. break;
  546. if ( p_c2 == c1 ) /* in case p_c2 == c1 no exchange is possible */
  547. diffp = 0;
  548. if ( (diffs < move_value) || (diffp < move_value) ) {
  549. improvement_flag = TRUE;
  550. if (diffs <= diffp) {
  551. h1 = c1; h2 = s_c1; h3 = c2; h4 = s_c2;
  552. move_value = diffs;
  553. opt2_flag = TRUE; move_flag = 0;
  554. /* goto exchange; */
  555. } else {
  556. h1 = c1; h2 = s_c1; h3 = p_c2; h4 = c2;
  557. move_value = diffp;
  558. opt2_flag = TRUE; move_flag = 0;
  559. /* goto exchange; */
  560. }
  561. }
  562. /* Now perform the innermost search */
  563. g = 0;
  564. while (g < nn_ls) {
  565. c3 = instance.nn_list[s_c1][g];
  566. pos_c3 = pos[c3];
  567. s_c3 = tour[pos_c3+1];
  568. if (pos_c3 > 0)
  569. p_c3 = tour[pos_c3-1];
  570. else
  571. p_c3 = tour[n-1];
  572. if ( c3 == c1 ) {
  573. g++;
  574. continue;
  575. }
  576. else {
  577. add2 = instance.distance[s_c1][c3];
  578. /* Perform fixed radius neighbour search for innermost search */
  579. if ( decrease_breaks + add1 < add2 ) {
  580. if ( pos_c2 > pos_c1 ) {
  581. if ( pos_c3 <= pos_c2 && pos_c3 > pos_c1 )
  582. between = TRUE;
  583. else
  584. between = FALSE;
  585. }
  586. else if ( pos_c2 < pos_c1 )
  587. if ( pos_c3 > pos_c1 || pos_c3 < pos_c2 )
  588. between = TRUE;
  589. else
  590. between = FALSE;
  591. else {
  592. printf(" Strange !!, pos_1 %ld == pos_2 %ld, \n",pos_c1,pos_c2);
  593. }
  594. if ( between ) {
  595. /* We have to add edges (c1,c2), (c3,s_c1), (p_c3,s_c2) to get
  596. valid tour; it's the only possibility */
  597. gain = decrease_breaks - instance.distance[c3][p_c3] +
  598. add1 + add2 +
  599. instance.distance[p_c3][s_c2];
  600. /* check for improvement by move */
  601. if ( gain < move_value ) {
  602. improvement_flag = TRUE; /* g = neigh_ls + 1; */
  603. move_value = gain;
  604. opt2_flag = FALSE;
  605. move_flag = 1;
  606. /* store nodes involved in move */
  607. h1 = c1; h2 = s_c1; h3 = c2; h4 = s_c2; h5 = p_c3; h6 = c3;
  608. goto exchange;
  609. }
  610. }
  611. else { /* not between(pos_c1,pos_c2,pos_c3) */
  612. /* We have to add edges (c1,c2), (s_c1,c3), (s_c2,s_c3) */
  613. gain = decrease_breaks - instance.distance[c3][s_c3] +
  614. add1 + add2 +
  615. instance.distance[s_c2][s_c3];
  616. if ( pos_c2 == pos_c3 ) {
  617. gain = 20000;
  618. }
  619. /* check for improvement by move */
  620. if ( gain < move_value ) {
  621. improvement_flag = TRUE; /* g = neigh_ls + 1; */
  622. move_value = gain;
  623. opt2_flag = FALSE;
  624. move_flag = 2;
  625. /* store nodes involved in move */
  626. h1 = c1; h2 = s_c1; h3 = c2; h4 = s_c2; h5 = c3; h6 = s_c3;
  627. goto exchange;
  628. }
  629. /* or add edges (c1,c2), (s_c1,c3), (p_c2,p_c3) */
  630. gain = - radius - instance.distance[p_c2][c2]
  631. - instance.distance[p_c3][c3] +
  632. add1 + add2 +
  633. instance.distance[p_c2][p_c3];
  634. if ( c3 == c2 || c2 == c1 || c1 == c3 || p_c2 == c1 ) {
  635. gain = 2000000;
  636. }
  637. if ( gain < move_value ) {
  638. improvement_flag = TRUE;
  639. move_value = gain;
  640. opt2_flag = FALSE;
  641. move_flag = 3;
  642. h1 = c1; h2 = s_c1; h3 = p_c2; h4 = c2; h5 = p_c3; h6 = c3;
  643. goto exchange;
  644. }
  645. /* Or perform the 3-opt move where no subtour inversion is necessary
  646. i.e. delete edges (c1,s_c1), (c2,p_c2), (c3,s_c3) and
  647. add edges (c1,c2), (c3,s_c1), (p_c2,s_c3) */
  648. gain = - radius - instance.distance[p_c2][c2] -
  649. instance.distance[c3][s_c3]
  650. + add1 + add2 + instance.distance[p_c2][s_c3];
  651. /* check for improvement */
  652. if ( gain < move_value ) {
  653. improvement_flag = TRUE;
  654. move_value = gain;
  655. opt2_flag = FALSE;
  656. move_flag = 4;
  657. improvement_flag = TRUE;
  658. /* store nodes involved in move */
  659. h1 = c1; h2 = s_c1; h3 = p_c2; h4 = c2; h5 = c3; h6 = s_c3;
  660. goto exchange;
  661. }
  662. }
  663. }
  664. else
  665. g = nn_ls + 1;
  666. }
  667. g++;
  668. }
  669. h++;
  670. }
  671. if ( move_flag || opt2_flag ) {
  672. exchange:
  673. move_value = 0;
  674. /* Now make the exchange */
  675. if ( move_flag ) {
  676. dlb[h1] = FALSE; dlb[h2] = FALSE; dlb[h3] = FALSE;
  677. dlb[h4] = FALSE; dlb[h5] = FALSE; dlb[h6] = FALSE;
  678. pos_c1 = pos[h1]; pos_c2 = pos[h3]; pos_c3 = pos[h5];
  679. if ( move_flag == 4 ) {
  680. if ( pos_c2 > pos_c1 )
  681. n1 = pos_c2 - pos_c1;
  682. else
  683. n1 = n - (pos_c1 - pos_c2);
  684. if ( pos_c3 > pos_c2 )
  685. n2 = pos_c3 - pos_c2;
  686. else
  687. n2 = n - (pos_c2 - pos_c3);
  688. if ( pos_c1 > pos_c3 )
  689. n3 = pos_c1 - pos_c3;
  690. else
  691. n3 = n - (pos_c3 - pos_c1);
  692. /* n1: length h2 - h3, n2: length h4 - h5, n3: length h6 - h1 */
  693. val[0] = n1; val[1] = n2; val[2] = n3;
  694. /* Now order the partial tours */
  695. h = 0;
  696. help = LONG_MIN;
  697. for ( g = 0; g <= 2; g++) {
  698. if ( help < val[g] ) {
  699. help = val[g];
  700. h = g;
  701. }
  702. }
  703. /* order partial tours according length */
  704. if ( h == 0 ) {
  705. /* copy part from pos[h4] to pos[h5]
  706. direkt kopiert: Teil von pos[h6] to pos[h1], it
  707. remains the part from pos[h2] to pos[h3] */
  708. j = pos[h4];
  709. h = pos[h5];
  710. i = 0;
  711. h_tour[i] = tour[j];
  712. n1 = 1;
  713. while ( j != h) {
  714. i++;
  715. j++;
  716. if ( j >= n )
  717. j = 0;
  718. h_tour[i] = tour[j];
  719. n1++;
  720. }
  721. /* First copy partial tour 3 in new position */
  722. j = pos[h4];
  723. i = pos[h6];
  724. tour[j] = tour[i];
  725. pos[tour[i]] = j;
  726. while ( i != pos_c1) {
  727. i++;
  728. if ( i >= n )
  729. i = 0;
  730. j++;
  731. if ( j >= n )
  732. j = 0;
  733. tour[j] = tour[i];
  734. pos[tour[i]] = j;
  735. }
  736. /* Now copy stored part from h_tour */
  737. j++;
  738. if ( j >= n )
  739. j = 0;
  740. for ( i = 0; i<n1 ; i++ ) {
  741. tour[j] = h_tour[i];
  742. pos[h_tour[i]] = j;
  743. j++;
  744. if ( j >= n )
  745. j = 0;
  746. }
  747. tour[n] = tour[0];
  748. }
  749. else if ( h == 1 ) {
  750. /* copy part from pos[h6] to pos[h1]
  751. direkt kopiert: Teil von pos[h2] to pos[h3], it
  752. remains the part from pos[h4] to pos[h5] */
  753. j = pos[h6];
  754. h = pos[h1];
  755. i = 0;
  756. h_tour[i] = tour[j];
  757. n1 = 1;
  758. while ( j != h) {
  759. i++;
  760. j++;
  761. if ( j >= n )
  762. j = 0;
  763. h_tour[i] = tour[j];
  764. n1++;
  765. }
  766. /* First copy partial tour 3 in new position */
  767. j = pos[h6];
  768. i = pos[h2];
  769. tour[j] = tour[i];
  770. pos[tour[i]] = j;
  771. while ( i != pos_c2) {
  772. i++;
  773. if ( i >= n )
  774. i = 0;
  775. j++;
  776. if ( j >= n )
  777. j = 0;
  778. tour[j] = tour[i];
  779. pos[tour[i]] = j;
  780. }
  781. /* Now copy stored part from h_tour */
  782. j++;
  783. if ( j >= n )
  784. j = 0;
  785. for ( i = 0; i<n1 ; i++ ) {
  786. tour[j] = h_tour[i];
  787. pos[h_tour[i]] = j;
  788. j++;
  789. if ( j >= n )
  790. j = 0;
  791. }
  792. tour[n] = tour[0];
  793. }
  794. else if ( h == 2 ) {
  795. /* copy part from pos[h2] to pos[h3]
  796. direkt kopiert: Teil von pos[h4] to pos[h5], it
  797. remains the part from pos[h6] to pos[h1] */
  798. j = pos[h2];
  799. h = pos[h3];
  800. i = 0;
  801. h_tour[i] = tour[j];
  802. n1 = 1;
  803. while ( j != h) {
  804. i++;
  805. j++;
  806. if ( j >= n )
  807. j = 0;
  808. h_tour[i] = tour[j];
  809. n1++;
  810. }
  811. /* First copy partial tour 3 in new position */
  812. j = pos[h2];
  813. i = pos[h4];
  814. tour[j] = tour[i];
  815. pos[tour[i]] = j;
  816. while ( i != pos_c3) {
  817. i++;
  818. if ( i >= n )
  819. i = 0;
  820. j++;
  821. if ( j >= n )
  822. j = 0;
  823. tour[j] = tour[i];
  824. pos[tour[i]] = j;
  825. }
  826. /* Now copy stored part from h_tour */
  827. j++;
  828. if ( j >= n )
  829. j = 0;
  830. for ( i = 0; i<n1 ; i++ ) {
  831. tour[j] = h_tour[i];
  832. pos[h_tour[i]] = j;
  833. j++;
  834. if ( j >= n )
  835. j = 0;
  836. }
  837. tour[n] = tour[0];
  838. }
  839. }
  840. else if ( move_flag == 1 ) {
  841. if ( pos_c3 < pos_c2 )
  842. n1 = pos_c2 - pos_c3;
  843. else
  844. n1 = n - (pos_c3 - pos_c2);
  845. if ( pos_c3 > pos_c1 )
  846. n2 = pos_c3 - pos_c1 + 1;
  847. else
  848. n2 = n - (pos_c1 - pos_c3 + 1);
  849. if ( pos_c2 > pos_c1 )
  850. n3 = n - (pos_c2 - pos_c1 + 1);
  851. else
  852. n3 = pos_c1 - pos_c2 + 1;
  853. /* n1: length h6 - h3, n2: length h5 - h2, n2: length h1 - h3 */
  854. val[0] = n1; val[1] = n2; val[2] = n3;
  855. /* Now order the partial tours */
  856. h = 0;
  857. help = LONG_MIN;
  858. for ( g = 0; g <= 2; g++) {
  859. if ( help < val[g] ) {
  860. help = val[g];
  861. h = g;
  862. }
  863. }
  864. /* order partial tours according length */
  865. if ( h == 0 ) {
  866. /* copy part from pos[h5] to pos[h2]
  867. (inverted) and from pos[h4] to pos[h1] (inverted)
  868. it remains the part from pos[h6] to pos[h3] */
  869. j = pos[h5];
  870. h = pos[h2];
  871. i = 0;
  872. h_tour[i] = tour[j];
  873. n1 = 1;
  874. while ( j != h ) {
  875. i++;
  876. j--;
  877. if ( j < 0 )
  878. j = n-1;
  879. h_tour[i] = tour[j];
  880. n1++;
  881. }
  882. j = pos[h1];
  883. h = pos[h4];
  884. i = 0;
  885. hh_tour[i] = tour[j];
  886. n2 = 1;
  887. while ( j != h) {
  888. i++;
  889. j--;
  890. if ( j < 0 )
  891. j = n-1;
  892. hh_tour[i] = tour[j];
  893. n2++;
  894. }
  895. j = pos[h4];
  896. for ( i = 0; i< n2 ; i++ ) {
  897. tour[j] = hh_tour[i];
  898. pos[hh_tour[i]] = j;
  899. j++;
  900. if (j >= n)
  901. j = 0;
  902. }
  903. /* Now copy stored part from h_tour */
  904. for ( i = 0; i< n1 ; i++ ) {
  905. tour[j] = h_tour[i];
  906. pos[h_tour[i]] = j;
  907. j++;
  908. if ( j >= n )
  909. j = 0;
  910. }
  911. tour[n] = tour[0];
  912. }
  913. else if ( h == 1 ) {
  914. /* copy part from h3 to h6 (wird inverted) erstellen : */
  915. j = pos[h3];
  916. h = pos[h6];
  917. i = 0;
  918. h_tour[i] = tour[j];
  919. n1 = 1;
  920. while ( j != h) {
  921. i++;
  922. j--;
  923. if ( j < 0 )
  924. j = n-1;
  925. h_tour[i] = tour[j];
  926. n1++;
  927. }
  928. j = pos[h6];
  929. i = pos[h4];
  930. tour[j] = tour[i];
  931. pos[tour[i]] = j;
  932. while ( i != pos_c1) {
  933. i++;
  934. j++;
  935. if ( j >= n)
  936. j = 0;
  937. if ( i >= n)
  938. i = 0;
  939. tour[j] = tour[i];
  940. pos[tour[i]] = j;
  941. }
  942. /* Now copy stored part from h_tour */
  943. j++;
  944. if ( j >= n )
  945. j = 0;
  946. i = 0;
  947. tour[j] = h_tour[i];
  948. pos[h_tour[i]] = j;
  949. while ( j != pos_c1 ) {
  950. j++;
  951. if ( j >= n )
  952. j = 0;
  953. i++;
  954. tour[j] = h_tour[i];
  955. pos[h_tour[i]] = j;
  956. }
  957. tour[n] = tour[0];
  958. }
  959. else if ( h == 2 ) {
  960. /* copy part from pos[h2] to pos[h5] and
  961. from pos[h3] to pos[h6] (inverted), it
  962. remains the part from pos[h4] to pos[h1] */
  963. j = pos[h2];
  964. h = pos[h5];
  965. i = 0;
  966. h_tour[i] = tour[j];
  967. n1 = 1;
  968. while ( j != h ) {
  969. i++;
  970. j++;
  971. if ( j >= n )
  972. j = 0;
  973. h_tour[i] = tour[j];
  974. n1++;
  975. }
  976. j = pos_c2;
  977. h = pos[h6];
  978. i = 0;
  979. hh_tour[i] = tour[j];
  980. n2 = 1;
  981. while ( j != h) {
  982. i++;
  983. j--;
  984. if ( j < 0 )
  985. j = n-1;
  986. hh_tour[i] = tour[j];
  987. n2++;
  988. }
  989. j = pos[h2];
  990. for ( i = 0; i< n2 ; i++ ) {
  991. tour[j] = hh_tour[i];
  992. pos[hh_tour[i]] = j;
  993. j++;
  994. if ( j >= n)
  995. j = 0;
  996. }
  997. /* Now copy stored part from h_tour */
  998. for ( i = 0; i< n1 ; i++ ) {
  999. tour[j] = h_tour[i];
  1000. pos[h_tour[i]] = j;
  1001. j++;
  1002. if ( j >= n )
  1003. j = 0;
  1004. }
  1005. tour[n] = tour[0];
  1006. }
  1007. }
  1008. else if ( move_flag == 2 ) {
  1009. if ( pos_c3 < pos_c1 )
  1010. n1 = pos_c1 - pos_c3;
  1011. else
  1012. n1 = n - (pos_c3 - pos_c1);
  1013. if ( pos_c3 > pos_c2 )
  1014. n2 = pos_c3 - pos_c2;
  1015. else
  1016. n2 = n - (pos_c2 - pos_c3);
  1017. if ( pos_c2 > pos_c1 )
  1018. n3 = pos_c2 - pos_c1;
  1019. else
  1020. n3 = n - (pos_c1 - pos_c2);
  1021. val[0] = n1; val[1] = n2; val[2] = n3;
  1022. /* Determine which is the longest part */
  1023. h = 0;
  1024. help = LONG_MIN;
  1025. for ( g = 0; g <= 2; g++) {
  1026. if ( help < val[g] ) {
  1027. help = val[g];
  1028. h = g;
  1029. }
  1030. }
  1031. /* order partial tours according length */
  1032. if ( h == 0 ) {
  1033. /* copy part from pos[h3] to pos[h2]
  1034. (inverted) and from pos[h5] to pos[h4], it
  1035. remains the part from pos[h6] to pos[h1] */
  1036. j = pos[h3];
  1037. h = pos[h2];
  1038. i = 0;
  1039. h_tour[i] = tour[j];
  1040. n1 = 1;
  1041. while ( j != h ) {
  1042. i++;
  1043. j--;
  1044. if ( j < 0 )
  1045. j = n-1;
  1046. h_tour[i] = tour[j];
  1047. n1++;
  1048. }
  1049. j = pos[h5];
  1050. h = pos[h4];
  1051. i = 0;
  1052. hh_tour[i] = tour[j];
  1053. n2 = 1;
  1054. while ( j != h ) {
  1055. i++;
  1056. j--;
  1057. if ( j < 0 )
  1058. j = n-1;
  1059. hh_tour[i] = tour[j];
  1060. n2++;
  1061. }
  1062. j = pos[h2];
  1063. for ( i = 0; i<n1 ; i++ ) {
  1064. tour[j] = h_tour[i];
  1065. pos[h_tour[i]] = j;
  1066. j++;
  1067. if ( j >= n )
  1068. j = 0;
  1069. }
  1070. for ( i = 0; i < n2 ; i++ ) {
  1071. tour[j] = hh_tour[i];
  1072. pos[hh_tour[i]] = j;
  1073. j++;
  1074. if ( j >= n )
  1075. j = 0;
  1076. }
  1077. tour[n] = tour[0];
  1078. /* getchar(); */
  1079. }
  1080. else if ( h == 1 ) {
  1081. /* copy part from pos[h2] to pos[h3] and
  1082. from pos[h1] to pos[h6] (inverted), it
  1083. remains the part from pos[h4] to pos[h5] */
  1084. j = pos[h2];
  1085. h = pos[h3];
  1086. i = 0;
  1087. h_tour[i] = tour[j];
  1088. n1 = 1;
  1089. while ( j != h ) {
  1090. i++;
  1091. j++;
  1092. if ( j >= n )
  1093. j = 0;
  1094. h_tour[i] = tour[j];
  1095. n1++;
  1096. }
  1097. j = pos[h1];
  1098. h = pos[h6];
  1099. i = 0;
  1100. hh_tour[i] = tour[j];
  1101. n2 = 1;
  1102. while ( j != h ) {
  1103. i++;
  1104. j--;
  1105. if ( j < 0 )
  1106. j = n-1;
  1107. hh_tour[i] = tour[j];
  1108. n2++;
  1109. }
  1110. j = pos[h6];
  1111. for ( i = 0; i<n1 ; i++ ) {
  1112. tour[j] = h_tour[i];
  1113. pos[h_tour[i]] = j;
  1114. j++;
  1115. if ( j >= n )
  1116. j = 0;
  1117. }
  1118. for ( i = 0; i < n2 ; i++ ) {
  1119. tour[j] = hh_tour[i];
  1120. pos[hh_tour[i]] = j;
  1121. j++;
  1122. if ( j >= n )
  1123. j = 0;
  1124. }
  1125. tour[n] = tour[0];
  1126. }
  1127. else if ( h == 2 ) {
  1128. /* copy part from pos[h1] to pos[h6]
  1129. (inverted) and from pos[h4] to pos[h5],
  1130. it remains the part from pos[h2] to
  1131. pos[h3] */
  1132. j = pos[h1];
  1133. h = pos[h6];
  1134. i = 0;
  1135. h_tour[i] = tour[j];
  1136. n1 = 1;
  1137. while ( j != h ) {
  1138. i++;
  1139. j--;
  1140. if ( j < 0 )
  1141. j = n-1;
  1142. h_tour[i] = tour[j];
  1143. n1++;
  1144. }
  1145. j = pos[h4];
  1146. h = pos[h5];
  1147. i = 0;
  1148. hh_tour[i] = tour[j];
  1149. n2 = 1;
  1150. while ( j != h ) {
  1151. i++;
  1152. j++;
  1153. if ( j >= n )
  1154. j = 0;
  1155. hh_tour[i] = tour[j];
  1156. n2++;
  1157. }
  1158. j = pos[h4];
  1159. /* Now copy stored part from h_tour */
  1160. for ( i = 0; i<n1 ; i++ ) {
  1161. tour[j] = h_tour[i];
  1162. pos[h_tour[i]] = j;
  1163. j++;
  1164. if ( j >= n )
  1165. j = 0;
  1166. }
  1167. /* Now copy stored part from h_tour */
  1168. for ( i = 0; i < n2 ; i++ ) {
  1169. tour[j] = hh_tour[i];
  1170. pos[hh_tour[i]] = j;
  1171. j++;
  1172. if ( j >= n )
  1173. j = 0;
  1174. }
  1175. tour[n] = tour[0];
  1176. }
  1177. }
  1178. else if ( move_flag == 3 ) {
  1179. if ( pos_c3 < pos_c1 )
  1180. n1 = pos_c1 - pos_c3;
  1181. else
  1182. n1 = n - (pos_c3 - pos_c1);
  1183. if ( pos_c3 > pos_c2 )
  1184. n2 = pos_c3 - pos_c2;
  1185. else
  1186. n2 = n - (pos_c2 - pos_c3);
  1187. if ( pos_c2 > pos_c1 )
  1188. n3 = pos_c2 - pos_c1;
  1189. else
  1190. n3 = n - (pos_c1 - pos_c2);
  1191. /* n1: length h6 - h1, n2: length h4 - h5, n2: length h2 - h3 */
  1192. val[0] = n1; val[1] = n2; val[2] = n3;
  1193. /* Determine which is the longest part */
  1194. h = 0;
  1195. help = LONG_MIN;
  1196. for ( g = 0; g <= 2; g++) {
  1197. if ( help < val[g] ) {
  1198. help = val[g];
  1199. h = g;
  1200. }
  1201. }
  1202. /* order partial tours according length */
  1203. if ( h == 0 ) {
  1204. /* copy part from pos[h2] to pos[h3]
  1205. (inverted) and from pos[h4] to pos[h5]
  1206. it remains the part from pos[h6] to pos[h1] */
  1207. j = pos[h3];
  1208. h = pos[h2];
  1209. i = 0;
  1210. h_tour[i] = tour[j];
  1211. n1 = 1;
  1212. while ( j != h ) {
  1213. i++;
  1214. j--;
  1215. if ( j < 0 )
  1216. j = n-1;
  1217. h_tour[i] = tour[j];
  1218. n1++;
  1219. }
  1220. j = pos[h2];
  1221. h = pos[h5];
  1222. i = pos[h4];
  1223. tour[j] = h4;
  1224. pos[h4] = j;
  1225. while ( i != h ) {
  1226. i++;
  1227. if ( i >= n )
  1228. i = 0;
  1229. j++;
  1230. if ( j >= n )
  1231. j = 0;
  1232. tour[j] = tour[i];
  1233. pos[tour[i]] = j;
  1234. }
  1235. j++;
  1236. if ( j >= n )
  1237. j = 0;
  1238. for ( i = 0; i < n1 ; i++ ) {
  1239. tour[j] = h_tour[i];
  1240. pos[h_tour[i]] = j;
  1241. j++;
  1242. if ( j >= n )
  1243. j = 0;
  1244. }
  1245. tour[n] = tour[0];
  1246. }
  1247. else if ( h == 1 ) {
  1248. /* copy part from pos[h3] to pos[h2]
  1249. (inverted) and from pos[h6] to pos[h1],
  1250. it remains the part from pos[h4] to pos[h5] */
  1251. j = pos[h3];
  1252. h = pos[h2];
  1253. i = 0;
  1254. h_tour[i] = tour[j];
  1255. n1 = 1;
  1256. while ( j != h ) {
  1257. i++;
  1258. j--;
  1259. if ( j < 0 )
  1260. j = n-1;
  1261. h_tour[i] = tour[j];
  1262. n1++;
  1263. }
  1264. j = pos[h6];
  1265. h = pos[h1];
  1266. i = 0;
  1267. hh_tour[i] = tour[j];
  1268. n2 = 1;
  1269. while ( j != h ) {
  1270. i++;
  1271. j++;
  1272. if ( j >= n )
  1273. j = 0;
  1274. hh_tour[i] = tour[j];
  1275. n2++;
  1276. }
  1277. j = pos[h6];
  1278. for ( i = 0; i<n1 ; i++ ) {
  1279. tour[j] = h_tour[i];
  1280. pos[h_tour[i]] = j;
  1281. j++;
  1282. if ( j >= n )
  1283. j = 0;
  1284. }
  1285. for ( i = 0 ; i < n2 ; i++ ) {
  1286. tour[j] = hh_tour[i];
  1287. pos[hh_tour[i]] = j;
  1288. j++;
  1289. if ( j >= n )
  1290. j = 0;
  1291. }
  1292. tour[n] = tour[0];
  1293. }
  1294. else if ( h == 2 ) {
  1295. /* copy part from pos[h4] to pos[h5]
  1296. (inverted) and from pos[h6] to pos[h1] (inverted)
  1297. it remains the part from pos[h2] to pos[h3] */
  1298. j = pos[h5];
  1299. h = pos[h4];
  1300. i = 0;
  1301. h_tour[i] = tour[j];
  1302. n1 = 1;
  1303. while ( j != h ) {
  1304. i++;
  1305. j--;
  1306. if ( j < 0 )
  1307. j = n-1;
  1308. h_tour[i] = tour[j];
  1309. n1++;
  1310. }
  1311. j = pos[h1];
  1312. h = pos[h6];
  1313. i = 0;
  1314. hh_tour[i] = tour[j];
  1315. n2 = 1;
  1316. while ( j != h ) {
  1317. i++;
  1318. j--;
  1319. if ( j < 0 )
  1320. j = n-1;
  1321. hh_tour[i] = tour[j];
  1322. n2++;
  1323. }
  1324. j = pos[h4];
  1325. /* Now copy stored part from h_tour */
  1326. for ( i = 0; i< n1 ; i++ ) {
  1327. tour[j] = h_tour[i];
  1328. pos[h_tour[i]] = j;
  1329. j++;
  1330. if ( j >= n )
  1331. j = 0;
  1332. }
  1333. /* Now copy stored part from h_tour */
  1334. for ( i = 0; i< n2 ; i++ ) {
  1335. tour[j] = hh_tour[i];
  1336. pos[hh_tour[i]] = j;
  1337. j++;
  1338. if ( j >= n )
  1339. j = 0;
  1340. }
  1341. tour[n] = tour[0];
  1342. }
  1343. }
  1344. else {
  1345. printf(" Some very strange error must have occurred !!!\n\n");
  1346. exit(0);
  1347. }
  1348. }
  1349. if (opt2_flag) {
  1350. /* Now perform move */
  1351. dlb[h1] = FALSE; dlb[h2] = FALSE;
  1352. dlb[h3] = FALSE; dlb[h4] = FALSE;
  1353. if ( pos[h3] < pos[h1] ) {
  1354. help = h1; h1 = h3; h3 = help;
  1355. help = h2; h2 = h4; h4 = help;
  1356. }
  1357. if ( pos[h3]-pos[h2] < n / 2 + 1) {
  1358. /* reverse inner part from pos[h2] to pos[h3] */
  1359. i = pos[h2]; j = pos[h3];
  1360. while (i < j) {
  1361. c1 = tour[i];
  1362. c2 = tour[j];
  1363. tour[i] = c2;
  1364. tour[j] = c1;
  1365. pos[c1] = j;
  1366. pos[c2] = i;
  1367. i++; j--;
  1368. }
  1369. }
  1370. else {
  1371. /* reverse outer part from pos[h4] to pos[h1] */
  1372. i = pos[h1]; j = pos[h4];
  1373. if ( j > i )
  1374. help = n - (j - i) + 1;
  1375. else
  1376. help = (i - j) + 1;
  1377. help = help / 2;
  1378. for ( h = 0 ; h < help ; h++ ) {
  1379. c1 = tour[i];
  1380. c2 = tour[j];
  1381. tour[i] = c2;
  1382. tour[j] = c1;
  1383. pos[c1] = j;
  1384. pos[c2] = i;
  1385. i--; j++;
  1386. if ( i < 0 )
  1387. i = n - 1;
  1388. if ( j >= n )
  1389. j = 0;
  1390. }
  1391. tour[n] = tour[0];
  1392. }
  1393. }
  1394. }
  1395. else {
  1396. dlb[c1] = TRUE;
  1397. }
  1398. }
  1399. }
  1400. free( random_vector );
  1401. free( h_tour );
  1402. free( hh_tour );
  1403. free( pos );
  1404. free( dlb );
  1405. }