Abstract
This chapter gives utility codes used by various procedures presented in the book. It also gives some programs for testing many of the metaheuristics discussed in the book.
Electronic Supplementary Material The online version of this article (https://doi.org/10.1007/978-3-031-13714-3_12) contains supplementary material, which is available to authorized users.
You have full access to this open access chapter, Download chapter PDF
This appendix provides the codes of utility procedures (random number generation, TSP data structures, KD-tree) appearing in various algorithms discussed in this book. Then, several codes are provided for testing complete metaheuristics.
These codes have been simplified in such a way that a user who is not familiar with coding or the use of program libraries can quickly execute them. Their programming style is therefore not so exemplary!
1 Random Numbers
Code 12.1 random_generators.py Implementation of a pseudo-random generator due to l’Ecuyer [1] as well as utility functions to generate integers between two bounds, random permutations and random symmetric matrices
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaaa_HTML.png)
2 TSP Utilities
Code 12.2 tsp_utilities.py Utility functions for the traveling salesman: computation of the length of a tour when a solution is provided in the form of an array giving the order in which the cities are visited; transformation of a solution from one form to another (order, successors, predecessors); comparison of tours
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaab_HTML.png)
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaac_HTML.png)
3 TSP Lin and Kernighan Improvement Procedure
Code 12.3 tsp_ LK.pyEjection chain for the TSP
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaad_HTML.png)
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaae_HTML.png)
4 KD-Tree Insertion and Inspection
Code 12.4 kd_tree_add_scan.pyCodes to define the general structure of the nodes of a KD-tree, to add an element to a KD-tree, and to inspect the whole tree. The inspection procedure just prints out the elements
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaaf_HTML.png)
5 KD-Tree Delete
Code 12.5 kd_tree_delete.pyCode for removing a node in a KD-tree
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaag_HTML.png)
6 KD-Tree Update Pareto Set
Code 12.6 kd_tree_update_pareto.pyCode for updating a Pareto set represented by a KD-tree
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaah_HTML.png)
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaai_HTML.png)
7 TSP 2-Opt and 3-Opt Test Program
Code 12.7 test_tsp_2_and_3opt.pyThis code first generates a symmetric matrix with random distances and starts with a random solution. The latter is improved with a local search applying the first-move improving policy with the 2-opt neighborhood. This method is relatively rapid for instances with up to a few thousand cities. Then, all sub-paths of 100 successive cities in this solution are improved with a 3-opt neighborhood. This method runs in almost linear time, but only produces good solutions if the starting solution is adequate. The solution is then improved with a full 3-opt neighborhood. Its complexity is considerably higher; the computational time becomes significant beyond a few hundred cities. Ultimately, the solution is improved with the 2-opt neighborhood, but applying the best move policy at each iteration
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaaj_HTML.png)
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaak_HTML.png)
8 Multi-objective TSP Test Program
Code 12.8 test_tsp_3opt_pareto.py A program for testing a local Pareto search for a TSP with a randomly generated distance matrix. For a 20-city and 3-objective instance, this program generates an approximation to the Pareto set with more than 3000 solutions. Since the implementation is highly recursive, the recursion stack and console user limits must be appropriately resized
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaal_HTML.png)
9 Fast Ant TSP Test Program
Code 12.9 test_tsp_FANT.pyProgram to test a method inspired by artificial ant colonies
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaam_HTML.png)
10 Taboo Search TSP Test Program
Code 12.10 test_tsp_TS.pyA taboo search test program for a TSP with a randomly generated symmetric distance matrix
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaan_HTML.png)
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaao_HTML.png)
11 Memetic TSP Test Program
Code 12.11 test_tsp_GA.pyA memetic algorithm test program for a TSP with a randomly generated symmetric distance matrix
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaap_HTML.png)
12 GRASP with Path Relinking TSP Test Program
Code 12.12 test_tsp_GRASP_PR.pyA GRASP with path relinking test program. This method uses GRASP which calls for a local search based on ejection chains, as well as other utility functions
![](http://media.springernature.com/lw566/springer-static/image/chp%3A10.1007%2F978-3-031-13714-3_12/MediaObjects/522503_1_En_12_Figaaq_HTML.png)
References
L’Ecuyer P.: Combined multiple recursive random number generators. Operations Research 44(5), 816–822 (1996). http://www.jstor.org/stable/171570
Author information
Authors and Affiliations
Rights and permissions
Open Access This chapter is licensed under the terms of the Creative Commons Attribution 4.0 International License (http://creativecommons.org/licenses/by/4.0/), which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons license and indicate if changes were made.
The images or other third party material in this chapter are included in the chapter's Creative Commons license, unless indicated otherwise in a credit line to the material. If material is not included in the chapter's Creative Commons license and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder.
Copyright information
© 2023 The Author(s)
About this chapter
Cite this chapter
Taillard, É.D. (2023). Codes. In: Design of Heuristic Algorithms for Hard Optimization. Graduate Texts in Operations Research. Springer, Cham. https://doi.org/10.1007/978-3-031-13714-3_12
Download citation
DOI: https://doi.org/10.1007/978-3-031-13714-3_12
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-031-13713-6
Online ISBN: 978-3-031-13714-3
eBook Packages: Business and ManagementBusiness and Management (R0)