• 沒有找到結果。

本研究參考成本擾動法(NM),藉由擾動成本跳脫區域最佳解的觀念,並提 出不同擾動成本的方式,改進了結果不能重現的缺點。另外研究中所提出的確定 性成本擾動法針對成本擾動比率值Cαi、增加成本比率值Hβi、成本擾動比率值長 度 K 、增加成本比率值長度 L 等參數,以及起始解解法、核心交換法及擾動公式 等執行架構組件的設計與測試,所建議DNM 法的組件方式和參數組合範圍如下 表所示。在所建議的組件組合方式與數值範圍下得到的之解題績效為:16 個測 試例題在總擾動次數 30 的總平均誤差為 1.01%、標準差為 1.22%;總擾動次數 45 的總平均誤差為 0.88%、標準差為 1.11%;總擾動次數 60 的總平均誤差為 0.80%、標準差為 1.16%。若從整個測試過程中所能找到 16 個例題的最佳個案來 看:DNM 法共找到 13 題的已知最佳解,而平均個案誤差為 0.19%。

組件項目與參數組合 建議之方式與範圍

起始構建法 最遠插入法(FI)

擾動成本之核心交換法 Or-H 原始成本之核心交換法 2-Opt

成本擾動公式 (3-13)式

成本擾動比率值(Cα) (30~50)、(30~50)、(40~60) 增加成本比率值(Hβ) (10~30)、(10~30)、(15~35) 成本擾動比率值長度與增加成本比率值長度(K,L) (10,3)、(15,3)、(20,3)

) ,

(Cα Hβ 的下降型態 (3-14)、(3-15)式 在測試過程中發現:在測試的範圍內,總執行次數越大則總平均誤差有下降 趨勢,惟時間上會有增加的情況。確定性成本擾動法(DNM)的執行架構在各例題 的求解上,隨著節點數增加,呈現多項式時間的成長。DNM法經由國際標竿例 題之測試,結果顯示有不錯的解題能力,可突破傳統交換型啟發式解法的求解屏 障,在求解時間上隨著節點增加亦有合理之成長,表示DNM法有相當之應用潛 力,期望此方法能做為求解TSP或相關車輛路線問題之有效工具。

然而,本研究對於DNM法應用在求解TSP上只是提出一個初步的可行架構,

且在組件的組合方式之探討和參數範圍的測試上仍有許多地方需要改進和加 強。以下提出幾點建議,作為未來後續研究參考之用。

1. 在起始解部分,本研究僅考慮最遠插入法(FI),未來可考慮其他有效率之起始 解構建方法,如鄰點法(Neighbor Procedure)、節省法(Saving Method)、貪心

法(Greedy Algorithm)和最小擴張樹法(Minimal Spanning Tree Approach)等方 法。

2. 在核心交換法部分,本研究僅考慮2-Opt和Or-H兩種方法,未來可嘗試不同的 交換法,如3-Opt、Lin-Kernighan交換法和GENIUS等方法。

3. 在參數測試上,可進行更大規模和更詳細之測試;值得注意的是隨著上述的 新的組件的應用,在參數建議範圍上亦應隨著調整。

4. 本研究所提出之方法亦可結合其他著名之啟發式解法,如Tabu、SA和TA等方 法之觀念和優點以加強解題績效。

參考文獻

1. 王國琛(2000),(指導教授:韓復華),「巨集啟發式解法在求解大規模旅行 推銷員問題之應用」,國立交通大學運輸工程與管理學系,八十九年度大專 生參與專題研究計畫研究成果報告 (NSC89-2815-C009-014-E)。

2. 林修竹(1999),(指導教授:韓復華),「包容性啟發式解法在VRPTW 問題 上之應用」,交通大學運輸工程與管理學系碩士論文。

3. 卓裕仁(2001),(指導教授:韓復華),「以巨集啟發式方法求解多車種與週 期性車輛路線問題之研究」,交通大學運輸工程與管理學系所博士論文。

4. 陳國清 (1996),(指導教授:韓復華),「成本擾動法(NM)與兩極跳躍法(FF) 在TSP 問題應用之研究」,國立交通大學,運輸工程與管理學系畢業專題 研究報告。

5. 陳建緯(2001),(指導教授:韓復華),「大規模旅行推銷員問題之研究:鄰 域搜尋法與巨集啟發式解法之應用」,交通大學運輸工程與管理研究所碩士 論文。

6. 韓復華、張靖、卓裕仁 (1996),「車輛路線問題研究:SA、TA、NM、SSS 與交換型啟發式方法之綜合應用分析」,國立交通大學運輸工程與管理學 系,八十五年度國科會專題研究計畫成果報告(NSC-85-2211-E-009-023)。

7. 韓復華、張靖、卓裕仁 (1997),「路線與排程問題研究:結合交換型解法 與AI 演算法之應用」,國立交通大學運輸工程與管理學系,八十六年度國 科會專題研究計畫成果報告(NSC-86-2621-E-009-001)。

8. 楊智凱 (1995),(指導教授:韓復華),「以門檻接受法改善TSP 與VRP 路 網成本之研究」,國立交通大學,土木研究所運工管組碩士論文。

9. 韓復華、楊智凱 (1996) 「門檻接受法在TSP 問題上之應用」, 運輸計劃季 刊, Vol. 25, No. 2, pp. 163-188.。

10. Althofer, I. & K.U. Koschnick (1991), “On the Convergence of Threshold Accepting,” Applied Mathematics and Optimization, Vol.24, pp.183-195.

11. Bodin, L., B.L. Golden, A. Assad & M. Ball (1983), “Routing and Scheduling of Vehicle and Crew: The State of Art,” Special Issue of Computers and Operations Research, Vol.10, No.2, pp.63-211.

12. Charon, I. & O. Hudry (1993), “The Noising Method: a New Method for Combinatorial Optimization,” Operations Research Letters, Vol. 14, pp.133-137.

13. Charon, I. & O. Hudry (2000), “Application of Noising Method to the Traveling Salesman Problem,” European Journal of Operational Research, Vol. 125, pp.266-277.

14. Cook, S.A. (1971), “The Complexity of Theorem-Proving Procedures,” Proc.

Third Annual ACM Symposium on the Theory of Computing, pp.151-158.

15. Dantzig, G.B., D.R. Fulkerson, & S.M. Johnson (1954), Solution of a large-scale Traveling Salesman problem, Operations Research, 2(4), pp.393-410.

16. Dueck, G. & T. Scheuer (1990), “Threshold Accepting: A General Purpose Optimization Algorithm Appearing Superior to Simulated Annealing,” Journal of Computational Physics, Vol.90, pp.161-175.

17. Dueck, G. (1993), “New Optimization Heuristics: the Great Deluge Algorithm and the Record-to-Record Travel,” Journal of Computational Physics, Vol.104, pp.86-92.

18. Flood, M.M. (1956), “The Traveling Salesman Problem, ” Operations Research, Vol.4, pp.61-75.

19. Gu, J. & X. Huang (1994), “Efficient Local Search With Search Space Smoothing: A Case Study of the Traveling Salesman Problem (TSP),” IEEE Transaction on Systems, Man and Cybernetics, Vol. 24, No. 5, pp.728-736.

20. Heider, C.H. (1972), “A Computationally Simplified Pair Exchange Algorithm for the Quadratic Assignment Problem,” Pap. No.101 Center for Naval Analysis, Arlington, Virginia

21. Lawler, E.L. (1963), “The Quadratic Assignment Problem,” Management Science, Vol.9, pp.586-599.

22. Miller, C.E., A.W. Tucker & R.A. Zemlin (1960), Integer Programming Formulation of Traveling Salesman Problems, Journal of the ACM, 7(4), pp.326-329

23. Kirkpatrick, S., C.D. Gelatt & M.P. Vecchi (1983), “Optimization by Simulated Annealing”, Science, Vol.220, pp.671-680.

24. Koopmans, T.C. & M. Beckman (1957), “Assignment Problems and the Location of Economic Activities,” Econometrica , Vol.25, pp.53-76.

25. Lin, S. (1965), “Computer Solutions of the Traveling Salesman Problem,” Bell System Technology Journal, Vol.44, pp.2245-2269.

26. Lin, S. & B.W. Kernighan (1973), "An Effective Heuristic Algorithm for the Traveling Salesman Problem," Operations Research, Vol.21, pp.498-516.

27. Norback, J.P. & R.F. Love (1977), “Geometric Approaches to Solving the Traveling Salesman Problem”, Management Science, Vol.23, pp.1208-1223.

28. Norback, J.P. & R.F. Love (1979), “Heuristic for the Hamiltonian path problem in Euclidean two space”, Journal of Operations Research Society, Vol. 30, pp.

363-368.

29. Or, I. (1976), “Traveling salesman-type Combinatorial Problems and Their Relation to the Logistics of Regional Blood Banking”, Ph.D. Dissertation, Northwestern University, Evanston, IL.

30. Reinelt, G. (1991), “TSPLIB:A Traveling Salesman Problem Library”, ORSA Journal on Computing, Vol.3, No.4, pp.376-384.

31. Rosenkrantz, D.J., R.E. Stearns & P.N. Lewis (1977), “An analysis of Several Heuristics for the Traveling Salesman Problem”, SLAM Journal on Computing, Vol.6, pp.563-581.

附錄Ⅰ:本研究在 16 個測試例題的最佳個案結果

本研究求得各例題之最佳路線圖如下,其中gr24.tsp、swiss42.tsp、gr48.tsp、

hk48.tsp、brazil58.tsp、gr120.tsp 等例題由於成本資料屬於 Matrix 型態,所以無 法繪製其路線圖,僅將其路線的總成本和路線的節點邊編號依序列出。

1. gr24.tsp

* 成本:1272

* 路線:1 12 4 23 9 13 14 20 2 15 19 22 18 17 10 5 21 8 24 6 7 3 11 16 1

* 路線圖:此例題成本資料屬於Matrix 型態,所以無法繪製其路線圖。

2. swiss42.tsp * 成本:1273

* 路線:1 2 7 5 4 28 3 29 30 31 39 23 40 22 25 41 24 42 10 9 11 26 12 13 19 27 6 14 20 15 17 16 38 8 18 32 37 36 21 34 35 33 1

* 路線圖:此例題成本資料屬於Matrix 型態,所以無法繪製其路線圖。

3. gr48.tsp

* 成本:5046

* 路線:1 13 48 16 11 36 26 6 14 9 32 27 17 21 22 8 33 5 31 12 10 15 24 37 47 43 45 2 40 39 42 35 20 38 30 4 19 3 25 23 34 18 46 41 44 28 7 29 1

* 路線圖:此例題成本資料屬於Matrix 型態,所以無法繪製其路線圖。

4. hk48.tsp

* 成本:11461

* 路線:1 2 48 15 43 21 33 30 23 9 10 40 36 34 6 8 47 7 38 14 18 12 22 13 28 32 25 3 5 29 26 41 24 35 17 31 20 11 16 42 4 46 45 39 44 27 37 19 1

* 路線圖:此例題成本資料屬於Matrix 型態,所以無法繪製其路線圖。

5. eil51.tsp * 成本:426

* 路線:1 22 8 26 31 28 3 36 35 20 2 29 21 16 50 34 30 9 49 10 39 33 45 15 44 42 19 40 41 13 25 14 24 43 7 23 48 6 27 51 46 12 47 18 4 7 37 5 38 11 32 1

* 路線圖:

0 10 20 30 40 50 60 70 80

0 10 20 30 40 50 60 70

6. brazil58.tsp * 成本:25395

* 路線:1 18 44 58 24 57 12 23 5 27 43 49 47 51 52 10 35 41 2 54 55 48 3 29 33 45 56 46 34 15 37 14 28 6 19 26 17 36 21 39 11 7 31 38 42 16 22 8 4 50 53 20 32 9 25 40 13 30 1

* 路線圖:此例題成本資料屬於Matrix 型態,所以無法繪製其路線圖。

7. st70.tsp * 成本:675

* 路線:1 36 29 13 70 35 69 31 38 59 22 66 63 57 15 24 19 7 2 4 18 42 32 3 8 26 55 49 28 14 20 30 44 68 27 46 25 45 39 61 40 9 17 43 41 6 53 5 10 52 60 12 21 34 33 62 54 67 48 11 64 65 56 51 50 58 37 47 16 23 1

* 路線圖:

0 10 20 30 40 50 60 70 80 90 100

0 10 20 30 40 50 60 70 80 90 100

8. pr76.tsp

* 成本:108159

* 路線:1 23 22 21 25 24 46 45 44 48 47 69 68 70 67 50 49 51 66 65 71 72 73 64 63 62 61 41 60 59 58 57 56 55 52 53 54 42 43 28 27 26 20 19 31 30 29 32 33 35 34 40 39 38 36 37 18 17 16 15 74 14 13 12 11 10 9 8 7 6 5 4 3 2 75 76 1

* 路線圖:

0 2000 4000 6000 8000 10000 12000 14000

0 2000 4000 6000 8000 10000 12000 14000 16000 18000 20000

9. kroA100.tsp * 成本:21828

* 路線:1 63 6 49 90 19 75 92 8 42 89 31 80 56 97 4 65 26 66 70 22 94 16 88 53 79 18 24 38 99 36 84 10 72 21 74 59 17 15 11 32 45 91 98 23 77 60 62 35 86 27 12 20 57 9 7 55 83 34 29 46 43 3 14 71 41 100 48 30 39 96 78 52 5 37 33 76 13 95 82 85 68 73 50 44 2 54 40 64 69 81 25 87 51 61 58 67 28 93 47 1

* 路線圖:

0 200 400 600 800 1000 1200 1400 1600 1800 2000

0 500 1000 1500 2000 2500 3000 3500 4000

10. kroC100.tsp * 成本:20749

* 路線:1 85 27 15 13 79 64 20 42 55 67 47 31 65 80 77 30 68 35 2 54 6 75 22 8 17 25 90 34 58 98 88 28 39 38 71 56 43 5 86 72 83 62 50 95 94 91 76 70 23 21 89 41 59 73 3 69 60 4 93 99 19 92 10 14 36 57 74 100 33 45 81 97 96 87 52 11 84 48 66 44 63 51 16 37 9 78 82 7 26 61 32 24 46 29 18 49 12 40 53 1

* 路線圖:

0 200 400 600 800 1000 1200 1400 1600 1800 2000

0 500 1000 1500 2000 2500 3000 3500 4000

11. lin105.tsp * 成本:14379

* 路線:1 2 6 7 10 11 15 103 21 22 29 30 31 32 33 28 23 20 12 19 24 27 16 17 18 25 26 36 37 42 41 43 46 52 53 58 57 54 51 47 44 104 40 49 45 48 50 55 56 59 105 62 63 70 69 74 75 81 73 76 80 86 79 77 72 64 67 68 71 78 82 83 84 85 91 92 96 97 101 102 93 89 90 98 99 100 95 94 88 87 66 65 61 60 39 38 35 34 14 13 4 5 9 8 3 1

* 路線圖:

0 200 400 600 800 1000 1200 1400

0 500 1000 1500 2000 2500 3000 3500

12.gr120.tsp * 成本:6951

* 路線:1 76 59 15 30 29 120 32 92 28 45 78 86 94 81 22 66 31 117 85 18 19 25 108 43 79 52 33 100 58 91 68 65 69 113 107 20 46 50 44 75 14 87 74 105 40 72 38 7 56 41 42 98 17 118 49 13 51 11 23 9 103 119 3 82 2 115 21 93 109 88 97 12 95 77 64 53 80 27 5 63 39 83 57 73 114 106 67 37 62 99 104 10 35 84 6 36 112 110 101 102 48 89 55 47 71 26 4 34 116 70 8 54 90 96 111 24 60 16 61 1

* 路線圖:此例題成本資料屬於Matrix 型態,所以無法繪製其路線圖。

13. u159.tsp

* 成本:42080

* 路線:1 3 2 4 5 6 153 152 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 37 36 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 97 98 99 100 101 102 103 104 105 106 107 95 96 87 88 94 89 90 91 92 93 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 130 124 129 125 126 127 128 131 132 134 133 135 136 137 138 140 139 141 142 143 144 145 146 147 148 149 150 151 154 155 156 157 158 159 1

* 路線圖:

2000 2500 3000 3500 4000 4500 5000 5500 6000 6500 7000

3000 4000 5000 6000 7000 8000 9000

14. ts225.tsp

* 成本:126643

* 路線:1 2 3 4 5 6 7 131 8 9 10 11 12 13 14 15 16 136 137 138 139 140 39 40 41 42 43 44 145 144 143 142 141 17 18 19 20 21 22 23 24 25 146 147 148 149 150 50 171 172 173 49 48 47 46 45 166 167 168 169 170 69 70 71 72 73 74 174 175 75 196 197 198 199 200 99 100 221 222 223 224 225 125 124 123 122 121 120 220 119 118 117 116 115 114 113 112 111 110 215 214 213 212 211 88 89 90 91 92 93 216 217 218 219 98 97 96 95 94 195 194 193 192 191 68 67 66 65 64 63 186 187 188 189 190 87 86 85 84 83 82 206 207 208 209 210 109 108 107 106 105 104 103 102 101 205 204 203 202 201 76 180 179 77 78 79 80 81 185 184 183 182 181 58 59 60 61 62 165 164 163 162 161 38 37 36 35 34 33 135 134 133 132 28 29 30 31 32 156 157 158 159 160 57 56 55 54 53 52 178 177 176 51 155 154 153 152 151 26 27 130 129 128 127 126 1

* 路線圖:

3000 5000 7000 9000 11000 13000 15000 17000

3000 5000 7000 9000 11000 13000 15000 17000

15. lin318.tsp * 成本:42458

* 路線:1 2 6 7 10 11 15 21 22 29 103 12 20 23 28 30 31 32 33 27 24 19 16 17 18 25 26 36 37 42 41 43 46 52 53 58 57 54 51 47 44 104 40 316 317 49 45 48 50 55 56 59 105 62 63 70 69 74 75 81 73 76 80 86 79 77 72 64 67 68 71 78 82 83 84 85 91 92 96 97 101 102 93 89 90 98 99 95 94 100 207 206 202 201 197 196 88 87 190 189 188 187 183 176 173 172 65 66 61 60 164 210 167 168 169 177 182 184 191 185 181 178 174 175 179 180 186 195 194 198 203 204 200 199 205 312 311 307 306 302 301 193 192 295 294 293 292 288 281 278 277 170 171 166 165 269 266 265 260 258 255 259 250 143 144 140 139 242 241 240 239 232 231 313 230 233 238 243 237 234 229 226 227 228 235 236 247 246 314 254 257 251 252 253 256 262 263 268 261 264 267 315 272 273 280 279 284 285 291 290 286 283 274 282 287 289 296 303 299 300 308 309 310 305 304 298 297 276 275 271 270 318 249 248 245 244 224 223 214 215 219 218 213 222 225 119 118 221 220 217 216 212 211 109 110 114 113 108 117 125 128 133 138 132 129 124 121 122 123 130 131 141 142 147 146 148 151 157 158 163 162 159 156 152 149 209 150 153 155 160 161 154 145 38 39 35 34 137 136 135 134 127 126 208 120 14 13 116 115 112 111 107 106 4 5 9 8 3 1

* 路線圖:

-500 0 500 1000 1500 2000 2500 3000 3500 4000 4500

0 500 1000 1500 2000 2500 3000 3500

16. pcb442.tsp * 成本:51750

* 路線:1 442 34 66 67 68 69 70 71 72 73 74 75 76 99 106 105 117 128 140 139 153 393 163 176 188 200 213 222 230 212 229 221 403 211 175 187 399 186 173 174 396 152 392 138 116 387 389 151 162 150 137 127 386 115 104 441 102 103 114 126 136 149 161 172 185 400 401 406 405 227 234 239 238 266 269 273 276 279 281 282 428 341 342 308 309 310 311 285 284 283 280 440 426 277 274 270 271 267 240 235 228 407 241 242 243 244 245 246 247 248 249 250 415 425 421 424 288 289 290 291 292 318 317 316 315 314 313 287 286 312 340 339 346 347 348 433 349 350 351 352 343 353 354 355 434 356 357 358 435 359 430 360 361 344 362 363 364 365 366 367 345 368 369 370 371 372 373 374 375 338 337 427 336 335 307 334 432 333 332 331 306 305 330 329 431 326 327 328 304 303 302 301 300 325 429 324 299 298 297 323 322 321 320 319 293 294 295 296 278 417 418 253 252 251 231 223 214 402 202 201 189 177 397 164 165 154 141 129 118 107 100 80 79 47 48 49 81 82 83 439 84 85 381 382 109 385 121 110 122 132 391 144 145 157 168 181 194 205 217 404 413 412 409 408 233 225 216 204 192 193 180 167 156 143 131 120 388 390 108 119 130 142 155 166 394 395 179 178 398 191 190 203 215 224 232 254 255 256 257 258 259 260 419 422 261 262 263 236 264 416 268 420 272 438 423 275 437 265 237 414 410 411 226 220 210 199 184 171 160 148 135 125 113 383 384 98 97 65 33 377 376 32 31 64 96 380 379 95 63 30 29 62 94 436 112 124 134 147 159 170 183 198 209 219 218 208 207 206 195 196 197 182 169 158 146 133 123 111 101 93 92 91 90 89 57 58 59 60 61 28 27 26 25 24 23 56 88 378 87 86 53 54 55 22 21 20 19 52 51 18 17 50 16 15 14 13 46 78 77 45 12 11 44 43 10 9 42 41 8 7 40 39 6 5 38 37 4 3 3635 2 1

* 路線圖:

0 500 1000 1500 2000 2500 3000 3500 4000

0 500 1000 1500 2000 2500 3000

附錄Ⅱ:Visual C++和 Mathematica5.0 程式碼

Visual C++

#include <iostream>

#include <ctime>

#include <cstdlib>

#include <iomanip>

#include <fstream>

using namespace std;

#define INFI 99999

struct data { long dist; double rdist;char adj;};

void paraMain( double, double, int, int );

void nesrestInsertion( int, data **&, int *&, int *&, long & );

void nesrInit( int, data **&, int, int *&, int *&, long & );

int closeSele( int, data **, int *, int * );

void insertion( int, data **&, int, int *&, int *&, long & );

void addIN( int, int, int *& );

void removeOUT( int, int, int *& );

void insertAfter( int, int, int *&, int );

void furthestInsertion( int, data **&, int *&, int *&, long & );

void furthInit( int, data **&, int, int *&, int *&, long & );

int farthSele( int, data **, int *, int * );

void twoOpt( int, data **&, int *&, long &, long );

void revOrder( int, int *&, int, int );

void perturtation( int, data **&, int *&, double, double );

void orOpt( int, data **&, int *& );

void calAdj( int, data **, int * );

long calDist( int, data ** );

void coreExchange( int, data **, int *&, double, double, int, int );

int counts = 0;long totalDist = 0;long tempDist = 0;long origDist = 0;int distMax = 0;ofstream dataOut( "parameterData.txt", ios::out );int main()

{ifstream para;para.open( "data/parameter.txt", ios::in );double c1[ 10 ];double h1[ 10 ];

int c1Length = 10;int h1Length = 3;int i;for ( i = 0; i < 10; i++ ) {para >> c1[ i ];h1[ i ] = c1[ i ];}

int j;for( i = 0; i < 10; i++ ) {for ( j = 0; j < 10; j++ ) {paraMain( c1[ i ], h1[ j ], c1Length, h1Length );

cout << "c1-" << c1[ i ]<< "(" << c1Length<< ")\th1-" << h1[ j ]<< "(" << h1Length<< ")\tOVER" <<

endl;}}dataOut.close();return 0;}

void paraMain( double c, double h, int cLength, int hLength )

{const int probSize = 48;clock_t start, end;double elapsed;ifstream fin;fin.open( "data/gr48.txt",

ios::in );ofstream fout;// fout.open( "Ssym_439.txt", ios::out ); data **node = new data *[ probSize + 1 ]; for ( int z= 0; z <= probSize; z++ ) node[ z ] = new data[ probSize + 1 ];int i, j;for ( i = 1; i <=

probSize; i++ ) {for( j = 1; j <= probSize; j++ ) {fin >> node[ i ][ j ].dist; if ( node[ i ][ j ].dist >

distMax )distMax = node[ i ][ j ].dist;node[ i ][ j ].adj = '0';}}

fin.close();

int *inSubtour = new int[ probSize + 1 ]; for ( i = 0; i <= probSize; i++ )inSubtour[ i ] = 0;

int *outSubtour = new int[ probSize + 1 ]; for ( i = 0; i <= probSize; i++ )outSubtour[ i ] = i;/*for ( i = 0; i <= probSize; i++ )cout << setw( 3 ) << inSubtour[ i ] << " ";cout << endl;for ( i = 0; i <= probSize;

i++ )cout << setw( 3 ) << outSubtour[ i ] << " ";cout << endl;*/cout << endl;start =

clock();furthestInsertion( probSize, node, inSubtour, outSubtour, totalDist );tempDist = totalDist;do {if ( tempDist < totalDist )totalDist = tempDist;twoOpt( probSize, node, inSubtour,

tempDist,totalDist );//cout << "temp distance is : " << tempDist << endl;} while ( tempDist <

totalDist );// cout << "total distance after 2-opt improvement is : " << totalDist << endl;

// cout << "==========================" <<endl;coreExchange( probSize, node, inSubtour, c, h, cLength, hLength );// cout << "================================" << endl;

//cout << "total distance after algorithm is : " << totalDist << endl;end = clock(); elapsed = ( (double) (end - start)) / CLOCKS_PER_SEC;dataOut << "c1-" << c<< "(" << cLength<< ")\th1-" << h

<< "(" << hLength<< ")\tSOLUTION : " << totalDist << endl;for ( i = 0; i <= probSize; i++ )dataOut

<< setw( 3 ) << inSubtour[ i ] << " ";dataOut << endl;dataOut << "The CPU time of this algorithm is :

" << elapsed << endl << endl;/*for ( int x = 1; x <= probSize; x++ ) {for ( int y = 1; y <= probSize;

y++ )fout << setw( 4 ) << node[ x ][ y ].adj << " ";fout << endl;}*/ // cout << "\ncounts : " <<

counts << endl;}

// ============================ 最遠點插入 ======================== //

void furthestInsertion( int n, data **&work, int *&IN, int *&OUT, long &DIST ) {int insertNode;int ct = 1;int start = 1; addIN( n, start, IN );removeOUT( n, start, OUT );

// cout << "initial node is : " << start << endl; furthInit( n, work, start, IN, OUT, DIST ); do { insertNode = farthSele( n, work, IN, OUT ); // cout << "insert node is : " << insertNode << endl;

insertion( n, work, insertNode, IN, OUT, DIST );ct++;/*for ( int x = 1; x <= n; x++ ) {for ( int y = 1; y

<= n; y++ )cout << setw( 3 ) << work[ x ][ y ].adj << " ";cout << endl;}*/} while ( OUT[ 1 ] !=

0 );IN[ 0 ] = IN[ n ];/* for ( int i = 1; i <= n; i++ )cout << setw( 3 ) << IN[ i ] << " ";cout << endl;

for ( i = 1; i <= n; i++ )cout << setw( 3 ) << OUT[ i ] << " ";cout << endl;cout << "cout : " << ct <<

endl;*/ // cout << "Total distance after Furthest Insertion is : " << DIST << endl;}

// ================= 選擇初始最大的 subtour ============================ //

void furthInit( int n, data **&work, int st, int *&IN, int *&OUT, long &DIST ){int tempNode;long tempDist = 0;for( int i = 1; i <=n; i++ ) {if ( OUT[ i ] != 0 ) {if ( work[ st ][ OUT[ i ] ].dist * 2

>tempDist ) {tempDist = work[ st ][ OUT[ i ] ].dist * 2;tempNode = OUT[ i ];}}else break;}addIN( n, tempNode, IN );removeOUT( n, tempNode, OUT );DIST += tempDist;// cout << "insert node is : "

<< tempNode << endl;work[ st ][ tempNode ].adj = '1';work[ tempNode ][ st ].adj = '1';/*for ( i = 1; i

<= n; i++ )cout << setw( 3 ) << IN[ i ] << " ";cout << endl;*/}

// ======================== 選擇離 subtour 最遠的點========================== //

int farthSele( int n, data **work, int *IN, int *OUT )

{int tempNode;long tempDist = 0;for ( int i = 1; i <= n; i++ ) {if ( IN[ i ] != 0 ) {for ( int j = 1; j <= n;

j++ ) {if ( OUT[ j ] != 0 ) {if ( work[ IN[ i ] ][ OUT[ j ] ].dist > tempDist ) {tempDist =

work[ IN[ i ] ][ OUT[ j ] ].dist;tempNode = OUT[ j ];}}else break;}}else break;}return tempNode;}

// =============================== 插入所選擇的點==========================//

void insertion( int n, data **&work, int insert, int *&IN, int *&OUT, long &DIST )

{int tempI;int tempJ;long tempDist = INFI;for ( int i = 1; i <= n; i++ ) {if ( IN[ i ] != 0 ) {for ( int j = 1;

j <= n; j++ ) {if ( IN[ j ] != 0 ) {if ( i != j ) {if ( work[ IN[ i ] ][ IN[ j ] ].adj == '1' ) {if

( work[ IN[ i ] ][ insert ].dist+ work[ insert ][ IN[ j ] ].dist- work[ IN[ i ] ][ IN[ j ] ].dist < tempDist ) {tempDist = work[ IN[ i ] ][ insert ].dist+ work[ insert ][ IN[ j ] ].dist-

work[ IN[ i ] ][ IN[ j ] ].dist;tempI = IN[ i ];tempJ = IN[ j ];}}}}else break;}}else break;}

insertAfter( n, insert, IN, tempI );removeOUT( n, insert, OUT );DIST +=

tempDist;work[ tempI ][ insert ].adj = '1';work[ insert ][ tempJ ].adj = '1';work[ tempI ][ tempJ ].adj = '0';/*for ( i = 1; i <= n; i++ )cout << setw( 3 ) << IN[ i ] << " ";cout << endl;*/}

void addIN( int n, int add, int *&IN )

{for ( int i = 1; i <= n; i++ ) {if ( IN[ i ] == 0 ) { IN[ i ] = add;IN[ 0 ] = add;break;}}}

void removeOUT( int n, int out, int *&OUT )

{int i, j;for ( i = 1; i <= n; i++ ) {if ( OUT[ i ] == out ) {//cout << "i : " << i << endl;j = i;}/*for ( int j = n; j >= 1; j-- ) {if ( OUT[ j ] != 0 ) {OUT[ i ] = OUT[ j ];OUT[ j ] = 0;break;}}*/}do {OUT[ j ] = OUT[ j+1 ];j++;} while ( j <= n );int z;for ( z = n; z >= 1; z-- ) {if ( OUT[ z ] != 0 ) {//cout << "z : " <<

z << endl;OUT[ z ] = 0;break;}}/* for ( i = 0; i <= n; i++ )cout << setw( 3 ) << OUT[ i ] << " ";cout

<< endl;*/}

void insertAfter( int n, int add, int *&IN, int I )

{for ( int i = 1; i <= n; i++ ) {if ( IN[ i ] == I ) {IN[ 0 ] = IN[ i + 1 ];IN[ i + 1 ] = add;

I = IN[ i + 1 ];add = IN[ 0 ];}}}

// ======================= 2-opt 改善 =================================== //

void twoOpt( int n, data **&work, int *&IN, long &DIST, long level ) {long tempDist;long temp = INFI;int x, y;

for ( int i = 0; i <= n - 3; i++ ) {for ( int j = i + 2; j <= n - 1; j++ ) {if ( i == 0 && j == n - 1 )continue;tempDist = work[ IN[ i ] ][ IN[ j ] ].dist+ work[ IN[i+1] ][ IN[j+1] ].dist- work[ IN[ i ] ][ IN[i+1] ].dist- work[ IN[ j ] ][ IN[j+1] ].dist;if ( tempDist == 0 )

continue;else {if ( tempDist < temp ) {temp = tempDist;x = i;y = j;counts++;}}}}if ( DIST + temp <

level ) {swap( IN[ x + 1 ], IN[ y ] ); //if ( ( y - (x+1) ) <= ( (n+(x-1)) - (y+2) ) ) {

if ( x + 2 < y - 1 ) revOrder( n, IN, x+2, y-1 );IN[ 0 ] = IN[ n ]; /*else {if ( ( y+2 ) < ( n+(x-1) ) ) revOrder( n, IN, y+2, n+(x-1) ); IN[ 0 ] = IN[ n ]; }}*/calAdj( n, work, IN );DIST = calDist( n,work );}

void revOrder( int n, int *&work, int st, int end )

{int X, Y;for ( int i = 0; i <= n - 2; i++ ) {if ( st+i < end-i ) {if ( (st+i) % n == 0 )X = ( st + i );

elseX = (st+i) % n;if ( (end-i) % n == 0 )Y = (end - i);elseY = (end-i) % n;swap ( work[ X ], work[ Y ] );}else break;}}

void calAdj( int n, data **work, int *IN )

{for ( int i = 1; i <= n; i++ ) {for ( int j = 1; j <= n; j++ )work[ i ][ j ].adj = '0';}for ( i = 0; i <= n - 1;

i++ )work[ IN[ i ] ][ IN[ i+1 ] ].adj = '1';}

long calDist( int n, data **work )

{long DIST = 0;for ( int x = 1; x <= n; x++ ) {for ( int y = 1; y <= n; y++ ) {if ( work[ x ][ y ].adj ==

'1' )DIST += work[ x ][ y ].dist;}}return DIST;}

// ============================= 擾動 ================================= //

void perturtation( int n, data **&work, int *&IN, double c1, double h1 ){int i, j;for ( i = 1; i <= n; i++ ) {for( j = 1; j <= n; j++ ) {work[ i ][ j ].rdist = static_cast<double>( work[ i ][ j ].dist ) / distMax;}}for ( i = 1; i <= n; i++ ) {for ( j = 1; j <= n; j++ ) {if ( work[ i ][ j ].rdist < c1 && i != j )

{work[ i ][ j ].rdist += h1;}//cout << work[ i ][ j ].rdist << " ";}// cout << endl;}orOpt( n, work, IN );}

// ========================= or-opt 改善 ============================== //

void orOpt( int n, data **&work, int *&IN ) {// double tempDist;

// =============================== p = 3 ==========================//

int i, j, ii;for ( i = 1; i <= n-3; i++ ) {for ( j = 1; j <= n; j++ ) {if ( j == i-1 || j == i || j == i+1 ||

j == i+2 )continue;else { if ( j == n ) {if ( ( work[ IN[ j ] ][ IN[ i ] ].rdist+

work[ IN[i+2] ][ IN[ 1 ] ].rdist- work[ IN[ j ] ][ IN[ 1 ] ].rdist- work[ IN[i-1] ][ IN[ i ] ].rdist- work[ IN[i+2] ][ IN[i+3] ].rdist+ work[ IN[i-1] ][ IN[i+3] ].rdist ) < -0.000001 ) {int *tempI = new int[ 3 + 1 ];tempI[ 1 ] = IN[ i ];tempI[ 2 ] = IN[ i+1 ];tempI[ 3 ] = IN[ i+2 ];

for ( ii = i+3; ii <= j; ii++ )IN[ ii-3 ] = IN[ ii ];IN[ j-2 ] = tempI[ 1 ];IN[ j-1 ] = tempI[ 2 ];IN[ j ] = tempI[ 3 ];IN[ 0 ] = IN[ j ];delete[] tempI;}} else {if ( ( work[ IN[ j ] ][ IN[ i ] ].rdist+

work[ IN[i+2] ][ IN[j+1] ].rdist- work[ IN[ j ] ][ IN[j+1] ].rdist- work[ IN[i-1] ][ IN[ i ] ].rdist-

work[ IN[i+2] ][ IN[i+3] ].rdist+ work[ IN[i-1] ][ IN[i+3] ].rdist ) < -0.000001 ) {if ( i < j ) {int *tempI

= new int[ 3 + 1 ];tempI[ 1 ] = IN[ i ];tempI[ 2 ] = IN[ i+1 ];tempI[ 3 ] = IN[ i+2 ]; for ( ii = i+3; ii

<= j; ii++ )IN[ ii-3 ] = IN[ ii ];IN[ j-2 ] = tempI[ 1 ];IN[ j-1 ] = tempI[ 2 ];IN[ j ] =

tempI[ 3 ];delete[] tempI;} else {int *tempI = new int[ 3 + 1 ];tempI[ 1 ] = IN[ i ];tempI[ 2 ] = IN[ i+1 ];tempI[ 3 ] = IN[ i+2 ];for ( ii = i-1; ii >= j+1; ii-- )IN[ ii+3 ] = IN[ ii ];IN[ j+1 ] = tempI[ 1 ];IN[ j+2 ] = tempI[ 2 ];IN[ j+3 ] = tempI[ 3 ];delete[] tempI;}}}}}}

// ================================ p = 2 ==================================//

for ( i = 1; i <= n-2; i++ ) {for ( j = 1; j <= n; j++ ) {if ( j == i-1 || j == i || j == i+1 )continue;else {if ( j == n ) {if ( ( work[ IN[ j ] ][ IN[ i ] ].rdist + work[ IN[i+1] ][ IN[ 1 ] ].rdist -

work[ IN[ j ] ][ IN[ 1 ] ].rdist- work[ IN[i-1] ][ IN[ i ] ].rdist- work[ IN[i+1] ][ IN[i+2] ].rdist+

work[ IN[i-1] ][ IN[i+2] ].rdist ) < -0.000001 ) {int *tempI = new int[ 2 + 1 ];tempI[ 1 ] = IN[ i ];

tempI[ 2 ] = IN[ i+1 ];for ( ii = i+2; ii <= j; ii++ )IN[ ii-2 ] = IN[ ii ]; IN[ j-1 ] = tempI[ 1 ];IN[ j ] = tempI[ 2 ];IN[ 0 ] = IN[ j ];delete[] tempI;}} else {if ( ( work[ IN[ j ] ][ IN[ i ] ].rdist+

work[ IN[i+1] ][ IN[j+1] ].rdist- work[ IN[ j ] ][ IN[j+1] ].rdist- work[ IN[i-1] ][ IN[ i ] ].rdist-

work[ IN[i+1] ][ IN[i+2] ].rdist+ work[ IN[i-1] ][ IN[i+2] ].rdist ) < -0.000001 ) {if ( i < j ) {int *tempI

= new int[ 2 + 1 ];tempI[ 1 ] = IN[ i ];tempI[ 2 ] = IN[ i+1 ];for ( ii = i+2; ii <= j; ii++ )IN[ ii-2 ] = IN[ ii ];IN[ j-1 ] = tempI[ 1 ];IN[ j ] = tempI[ 2 ];delete[] tempI;} else {int *tempI = new int[ 2 + 1 ];tempI[ 1 ] = IN[ i ];tempI[ 2 ] = IN[ i+1 ]; for ( ii = i-1; ii >= j+1; ii-- )IN[ ii+2 ] = IN[ ii ];

IN[ j+1 ] = tempI[ 1 ];IN[ j+2 ] = tempI[ 2 ];delete[] tempI;}}}}}}

// ==================================== p = 1===============================//

for ( i = 1; i <= n-1; i++ ) {for ( j = 1; j <= n; j++ ) {if ( j == i-1 || j == i )continue;

else {if ( j == n ) {if ( ( work[ IN[ j ] ][ IN[ i ] ].rdist+ work[ IN[ i ] ][ IN[ 1 ] ].rdist-

work[ IN[ j ] ][ IN[ 1 ] ].rdist- work[ IN[i-1] ][ IN[ i ] ].rdist- work[ IN[ i ] ][ IN[i+1] ].rdist+

work[ IN[i-1] ][ IN[i+1] ].rdist ) < -0.000001 ) {int *tempI = new int[ 1 + 1 ];tempI[ 1 ] = IN[ i ];for ( ii = i+1; ii <= j; ii++ )IN[ ii-1 ] = IN[ ii ];IN[ j ] = tempI[ 1 ];IN[ 0 ] = IN[ j ];delete[] tempI;}} else {if ( ( work[ IN[ j ] ][ IN[ i ] ].rdist+ work[ IN[ i ] ][ IN[j+1] ].rdist- work[ IN[ j ] ][ IN[j+1] ].rdist- work[ IN[i-1] ][ IN[ i ] ].rdist- work[ IN[ i ] ][ IN[i+1] ].rdist+

work[ IN[i-1] ][ IN[i+1] ].rdist ) < -0.000001 ) {if ( i < j ) {int *tempI = new int[ 1 + 1 ];tempI[ 1 ] = {int *tempI = new int[ 1 + 1 ];tempI[ 1 ] = IN[ i ]; for ( ii = i-1; ii >= j+1; ii-- )IN[ ii+1 ] = IN[ ii ];IN[ j+1 ] = tempI[ 1 ];delete[] tempI;}}}}}}

calAdj( n, work, IN );

totalDist = calDist( n, work );// cout << "total distance after perturtation is : " << totalDist <<

endl;}

void coreExchange( int n, data **work, int *&IN, double c, double h, int cLength, int hLength ) {double c1, h1;int i, j;for ( i = 0; i < cLength; i++ ) {origDist = totalDist;int *origIN = new int[ n + 1 ];int x;for ( x = 0; x <= n; x++ )origIN[ x ] = IN[ x ];/*for ( x = 0; x <= n; x++ )cout << setw( 3 ) <<

IN[ x ] << " ";cout << endl;for ( x = 0; x <= n; x++ )cout << setw( 3 ) << origIN[ x ] << " ";cout <<

endl;*/for ( j = 0; j < hLength; j++ ) {c1 = c * ( 1 - static_cast<double>(i)/(cLength) );h1 = h * ( 1 - static_cast<double>(j)/(hLength-1) );/*cout << "c1 : " << c1 << "\n"<< "h1 : " << h1 << endl;*/

perturtation( n, work, IN, c1, h1 );/*int x;for ( x = 0; x <= n; x++ )cout << setw( 3 ) << IN[ x ] << "

";cout << endl;*/tempDist = totalDist;do {if ( tempDist < totalDist )totalDist = tempDist;

twoOpt( n, work, IN, tempDist, totalDist );//cout << "temp distance is : " << tempDist << endl;} while ( tempDist < totalDist );//cout << "total distance after 2-opt improvement is : " << totalDist << endl;/*

for ( x = 0; x <= n; x++ )cout << setw( 3 ) << IN[ x ] << " ";cout << endl;*/}/*cout << "origDist :

"<< origDist << "\n"<< "totalDist : " << totalDist << endl;for ( x = 0; x <= n; x++ )cout << setw( 3 )<<

IN[ x ] << " ";cout << endl;for ( x = 0; x <= n; x++ )cout << setw( 3 ) << origIN[ x ] << " ";cout <<

endl;*/if ( origDist < totalDist ) {totalDist = origDist;for ( int x = 0; x <= n; x++ )IN[ x ] = origIN[ x ];}

else continue;delete[] origIN;}}

Mathematica5.0

ClearAll

<< LinearAlgebra`MatrixManipulation`

n = 70;ReadList["C:\\論文資料\\st70.txt", Number];a = Partition[ReadList["C:\\論文資料\\st70.txt", Number], 3];dat = MatrixForm[a];b = Array[v1, {n, n}];For[i = 1, i ≤ n, i++, For[j = 1, j ≤ n, j++, If[i ≠ j,v1[i, j] = Round[Sqrt[(dat[[1, i, 2]] dat[[1, j, 2]])^2 + (dat[[1, i, 3]] - dat[[1, j, 3]])^2]], v1[i, j]

= 0]]];dist = b;dis1 = dist;For[i = 1, i ≤ n, i++, For[j = 1, j ≤ n, j++, If[dis1[[i, j]] == 0, dis1[[i, j]] = -∞,]]];Do[dis1[[i, 1]] = -∞, {i, n}];seq = {1};a = Take[dis1, 1];p = Position[a, Max[a]];b = p[[1, 1]];

c = p[[1, 2]];seq = Append[seq, c];Do[dis1[[i, c]] = -∞, {i, n}];a = AppendColumns[Take[dis1, {1, 1}], Take[dis1, {c, c}]];seq = {1, c, 1};For[e = 3, e ≤ n, e++,p = Position[a, Max[a]];b = p[[1, 1]];c = p[[1, 2]];Do[dis1[[i, c]] = -∞, {i, n}]; seq1 = Insert[seq, c, 2];improbj = 0;For[i = 1, i ≤ e, i++,

improbj = improbj + dist[[seq1[[i]], seq1[[i + 1]]]];]For[j = 3, j ≤ e, j++,seq2 = Insert[seq, c,

j];improbj1 = 0;For[i = 1, i ≤ e, i++,improbj1 = improbj1 + dist[[seq2[[i]], seq2[[i + 1]]]];]If[improbj ≤ improbj1, improbj = improbj; seq1 = seq1, improbj = improbj1; seq1 = seq2];];seq = seq1;a =

Take[dis1, {seq[[1]], seq[[1]]}];For[i = 1, i ≤ e - 1, i++, a = AppendColumns[a, Take[dis1, {seq[[i + 1]], seq[[i + 1]]}]]]; ];

(*=== === === === === === === == Farthest Insert === === === === === = = = == === === ==*) impr0 = Array[v0, {n, n}];For[j = 1, j ≤ n, j++, For[i = 1, i ≤ n, i++, v0[i, j] = Min[]]];iteration = 0;For[k = 0; i = 1, i < n, i++, k = k + dist[[seq[[i]], seq[[i + 1]]]];]; k = k + dist[[seq[[n]], seq[[1]]]];

For[z = -1, z < 0,For[i = 1, i ≤ n - 2, i++,For[j = i + 2, j ≤ n, j++,temp = dist[[seq[[i]], seq[[j]]]] + dist[[seq[[i + 1]], seq[[j + 1]]]] - dist[[seq[[i]], seq[[i + 1]]]] - dist[[seq[[j]], seq[[j + 1]]]]; v0[i, j] = temp]];If[Min[impr0] < 0, z = Min[impr0];k = k + z; iteration = iteration + 1;p = Position[impr0, Min[impr0]];l = p[[1, 1]];m = p[[1, 2]];a = Take[seq, {l + 1, m}];c = Length[a];b = Drop[seq, {l + 1, m}];For[j = 1, j ≤ c, j++,b = Insert[b, a[[j]], l + 1]];seq = b;, z = 1]];

(*=== === === === === === === ==Two - Opt === === === ==== = = = === = == ===*) LocalSolution = seq;LocalOpt = k;

(*=== === === === === === === === Setting soltion === === === = == === === === = = = ===*) impr4 = Array[v4, {n, n}];For[j = 1, j ≤ n, j++, For[i = 1, i ≤ n, i++, v4[i, j] = Min[]]];

(*=== === === === === Matrix setting === === === = == === === === = = = === = ===*) Tim = Timing[For[K = 0,K ≤ 14, K++, C1 = 0.156; C1 = (C1)*(1 - (K)/15); For[L = 0, L ≤ 2, L++, H1

= 0.156; H1 = H1*(1 - (L)/2);dist2 = dist;a = Max[dist2];dist2 = dist2/a;For[i = 1, i ≤ n, i++, For[j = 1, j

≤ n, j++, If[dist2[[i, j]] ≠0, If[dist2[[i, j]] ≥ C1, dist2[[i, j]] = dist2[[i, j]], dist2[[i, j]] = (dist2[[i, j]] + H1),]]]];

(*=== === === === === === = ===Change date === === == = = = === = == === === === ==*) For[k =0; i = 1, i < n, i++, k = k + dist2[[seq[[i]], seq[[i + 1]]]];]; k = k + dist2[[seq[[n]], seq[[1]]]];

For[i = 1, i ≤ n - 3, i++, For[j = 1, j ≤ n, j++, If[i == 1, temp = dist2[[seq[[j]], seq[[i]]]] + dist2[[seq[[

i + 2]], seq[[j + 1]]]] - dist2[[seq[[j]], seq[[j + 1]]]] - dist2[[seq[[n]], seq[[i]]]] - dist2[[seq[[i + 2]], seq[[

i + 3]]]] + dist2[[seq[[n]], seq[[i + 3]]]], temp = dist2[[seq[[j]], seq[[i]]]] + dist2[[seq[[i + 2]], seq[[j +

1]]]] - dist2[[seq[[j]],seq[[j + 1]]]] - dist2[[seq[[i - 1]], seq[[i]]]] - dist2[[seq[[i + 2]], seq[[i + 3]]]] + dist2[[seq[[i - 1]], seq[[i + 3]]]]];If[i == 1; j == n, temp = 0,];If[j == i - 1, temp = 0,];If[j == i, temp = 0,];If[j == i + 1, temp = 0,];If[j == i + 2, temp = 0,];If[temp < -0.000001, a = Take[seq, {i, i + 2}]; b = Drop[seq, {i, i + 2}];If[j < i, For[t =1, t ≤ 3, t++, b = Insert[b, a[[4 - t]], j + 1]], For[t = 1, t ≤ 3, t++, b = Insert[b, a[[4 - t]], j - 2]]]; seq = b; seq[[n + 1]] = seq[[1]]; k = k + temp,];]];

iteration = iteration + 1;

(*=== === === === === ==== === Or - Opt(p = 3 Insert) === === === === === === ==*) For[i = 1, i ≤ n - 2, i++,For[j = 1, j ≤ n, j++,If[i == 1, temp = dist2[[seq[[j]], seq[[i]]]] + dist2[[seq[[i + 1]], seq[[j + 1]]]] - dist2[[seq[[j]], seq[[j + 1]]]] - dist2[[seq[[n]], seq[[i]]]] - dist2[[seq[[i + 1]], seq[[i + 2]]]] + dist2[[seq[[n]], seq[[i + 2]]]], temp = dist2[[seq[[j]], seq[[i]]]] + dist2[[seq[[i + 1]],seq[[j + 1]]]]

- dist2[[seq[[j]], seq[[j + 1]]]] - dist2[[seq[[i - 1]], seq[[i]]]] - dist2[[seq[[i + 1]], seq[[i + 2]]]] + dist2[[seq[[i - 1]], seq[[i + 2]]]]];If[i == 1; j == n, temp = 0, temp = temp];If[j == i - 1, temp = 0,];

If[j == i, temp = 0,];If[j == i + 1, temp = 0,];If[temp < -0.000001, a = Take[seq, {i, i + 1}]; b = Drop[seq, {i, i + 1}];If[j < i,For[t = 1, t ≤ 2, t++,b = Insert[b, a[[3 - t]], j + 1]], For[t = 1, t ≤ 2, t++,b = Insert[b, a[[3 - t]], j - 1]]]; seq = b; seq[[n + 1]] = seq[[1]]; k = k + temp,];]];

iteration = iteration + 1;

(*=== === === === = === === === === === Or - Opt(p = 2 Insert) === === === === =====*) For[i = 1, i ≤ n - 1, i++,For[j = 1, j ≤ n, j++,If[i == 1, temp =dist2[[seq[[j]], seq[[i]]]] + dist2[[seq[[i]], seq[[j + 1]]]] - dist2[[seq[[j]], seq[[j + 1]]]] - dist2[[seq[[n]], seq[[i]]]] - dist2[[seq[[i]], seq[[i + 1]]]] + dist2[[seq[[n]], seq[[i + 1]]]], temp = dist2[[seq[[j]], seq[[i]]]] + dist2[[seq[[i]], seq[[j + 1]]]] -

dist2[[seq[[j]], seq[[j + 1]]]] - dist2[[seq[[i - 1]], seq[[i]]]] - dist2[[seq[[i]], seq[[i + 1]]]] + dist2[[seq[[i - 1]], seq[[i + 1]]]]];If[i == 1; j == n, temp = 0,];If[j == i - 1, temp = 0,];

If[j == i, temp = 0,];If[temp < -0.000001, a = Take[seq, {i, i}]; b = Drop[seq, {i, i}];If[j < i, b = Insert[b, a[[1]], j + 1],b = Insert[b, a[[1]], j]]; seq = b; seq[[n + 1]] = seq[[1]]; k = k + temp,];]];

iteration = iteration + 1;

(*=== === === === === === Or - Opt(p = 1 Insert) === === === === ===== ==*)

For[k = 0; i = 1, i < n, i++, k = k + dist[[seq[[i]], seq[[i + 1]]]];]; k = k + dist[[seq[[n]], seq[[1]]]];

For[z = -1, z < 0,For[i = 1, i ≤ n - 2, i++,For[j = i + 2, j ≤ n, j++,temp = dist[[seq[[i]], seq[[j]]]] + dist[[seq[[i + 1]], seq[[j + 1]]]] - dist[[seq[[i]], seq[[i + 1]]]] - dist[[seq[[j]], seq[[j + 1]]]];

v4[i, j] = temp]];If[Min[impr4] < 0, z = Min[impr4];k = k + z; iteration = iteration + 1;p = Position[impr4, Min[impr4]];l = p[[1, 1]];m = p[[1, 2]];a = Take[seq, {l + 1, m}];c = Length[a];

b = Drop[seq, {l + 1, m}];For[j = 1, j ≤ c, j++,b = Insert[b, a[[j]], l + 1]];seq = b;, z = 1]];

(*=== === === === === ==== == = Two - Opt === === === === === ==== === ==*) ]; If[LocalOpt > k, LocalOpt = k; LocalSolution = seq, k = LocalOpt; seq = LocalSolution];];];

Print[Tim];Print[""];Print["Total iteration times is: ", iteration];Print["The local optimal solutin is: ", LocalOpt];Print[" And the sequence is: ", LocalSolution]

相關文件