LEE & RAFFENSPERGER Using AMPL for teaching the TSP Using AMPL for teaching the TSP Jon Lee Ibm T.J. Watson Research Center P.O. Box 218, Yorktown Heights, NY 10598 USA jonlee@us.ibm.com John F. Raffensperger Dept. of Management, Private Bag 4800 University of Canterbury, Christchurch, New Zealand john.raffensperger@canterbury.ac.nz Abstract In this paper, we discuss the use of AMPL in teaching students about the traveling salesman problem (TSP). The paper gives suggestions for pedagogical devices, homework assignments and exams, PowerPoint presentations, and a convenient package of AMPL models and scripts. The AMPL files include different formulations for the TSP, its relaxations, scripts for its solution, and - particularly useful in class - scripts for visualization of those solutions using SVG. We have a special focus on visualization, to provide convenient ways for students to view and report their solutions. We observe that the TSP is such a classical O.R. problem, that it can play a central role in an undergraduate course about integer programming. provide convenient ways for students to view and report their solutions. The TSP lends itself well to graphical output, and students find this immensely helpful for learning and for model and algorithm debugging. Furthermore, the lecturer can require that students turn in a graphical print-out of the tour, thus allowing some determination of solution quality at a glance. 1. Why teach the TSP? This paper describes our experience in teaching O.R. students about the traveling salesman problem (TSP), as part of advanced courses in math programming, for third year, fourth year, and masters level courses. The TSP remains a very important problem. Though it is very well studied, it continues to tantalize researchers. Nothing humbles a graduate student so quickly as a brutally difficult problem, and the TSP is perhaps foremost in that category. Computationally difficult problems are sometimes a source of pedagogical challenge. A model instance that is too small does not give the right impression of computational complexity to a student. On the other hand, student versions of solvers are usually too small to allow solution of large model instances. One solution to this dilemma is programming ability. Lecturers whose students have good programming skills have an advantage in teaching the TSP. Unfortunately, undergraduate students in business have a widespread lack of programming skills. Only recently has the University of Canterbury added a modest programming requirement for third-year courses in operations research; it is still too early to see whether this new requirement will pay off. The students' lack of programming skill puts a greater burden on the lecturer to explain algorithms in more detail and to provide canned tools. The TSP leads into many other related areas of O.R., including integer programming and cutting planes, approximation algorithms, Lagrangean relaxation and the subgradient method, and various heuristic approaches (such as greedy and k-opt). The TSP and its variations, such as multiple vehicles, time windows, tour length constraints, have enormous real-world applicability. Because of its importance, its ease in description, its many applications, and its historical inspiration for countless algorithms, the TSP will continue to be an important part of teaching integer programming and combinatorial optimization. This paper's contributions include suggestions for pedagogical devices, homework assignments and exams, and a convenient package of AMPL models and scripts. We have a special focus on visualization, to INFORMS Transactions on Education 7:1(37-69) 37 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP a C++ class library), GAMS(4) , Maple(5), Mathematica(6) , the optimization tools in Matlab(7), ZIMPL(8) (open source), and, of course, the standard programming languages. We have chosen AMPL primarily for its scripting ability, but also for its low cost, its easy links to many solvers (including open-source solvers such as Cbc(9)), and its popularity. Because it does not require programming and because many students already have some familiarity with it, the spreadsheet bears some consideration, especially to solve small problems in first or second year courses, and in MBA programs. We use Excel and What'sBest(1) for such courses; students are required to solve a 30city TSP, starting with the assignment problem, and then manually add subtour-breaking constraints and binary integrality. This exercise is extremely effective in conveying the problem's difficulty as well as hinting at some of the key elements that can be effectively employed to solve large instances. Perhaps the most interesting AMPL alternative is the open-source Gnu LP Kit(10), with the useful LP_Solve IDE(11) (see also Introduction to lp_solve 5.5.0.9(12)). In fact, these are not really alternatives, but overlapping programs, because AMPL can call LP_Solve(13), and because the Gnu LP Kit language is a subset of AMPL. Though LP_Solve IDE cannot run AMPL scripts, it has a surprisingly nice interface and can solve many AMPL models. LP_Solve IDE has the remarkable ability to convert models into different formats (e.g. it can convert MPS(14) to LP-FML(15), among others). Unfortunately, the spreadsheet has several drawbacks for solving the TSP, and for teaching students about it. The spreadsheet does not scale to larger problem instances easily. Creating algorithms (such as adding subtour-breaking constraints) is unnatural, awkward, and time consuming, so the spreadsheet does not lend itself to exploring advanced techniques of integer programming. And Excel has no convenient tools to display network graphs (not to be confused with charts), as each arc must be a new series. We have spent some time coding Visual Basic macros to produce visualizations within Excel, only to find that these macros are difficult to write, clumsy, and unsatisfactory. In contrast to spreadsheets, algebraic modeling languages provide an easy means for making use of integer linear programming methods for combinatorial optimization problems. For editing AMPL, one alternative is AMPL Studio(16) , which we have not tested. Some popular text editors have basic syntax highlighting, and this is the path we have taken, partly for its low cost. In this paper, we give directions and help files for using AMPL with the open-source editor Scite(17) . While using AMPL feels like programming to students, many AMPL models do not require scripts, and the programming overhead associated with AMPL scripts is less than with Visual Basic or Java. We have found that when students are given some reasonable initial help in AMPL in a workshop, including some scripts to get them started, the programming demands are modest. We have no empirical data regarding the ef- We have chosen to use the proprietary modeling language AMPL, which has scripting capability. Useful alternatives to AMPL(2) include FLOPC++ (3) (an open source algebraic modeling language implemented as (1) http://www.lindo.com/ http://www.ampl.com/ (3) http://www.coin-or.org/ (4) http://www.gams.com/ (5) http://www.maplesoft.com/ (6) http://www.wolfram.com/ (7) http://www.mathworks.com/ (8) http://www.zib.de/koch/zimpl/ (9) http://www.coin-or.org/ (10) http://www.gnu.org/software/glpk/glpk.html (11) http://www.progdigy.com/ (12) http://lpsolve.sourceforge.net/5.5/Intro.htm (13) http://lpsolve.sourceforge.net/ (14) http://en.wikipedia.org/wiki/MPS_(format) (15) http://gsbkip.chicagogsb.edu/fml/fml.html (16) http://www.optirisk-systems.com/Default.aspx (17) http://www.scintilla.org/ (2) INFORMS Transactions on Education 7:1(37-69) 38 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP fectiveness of our materials. However, based on students' look of relief when they get their first AMPL model working, the speed with which they can get the basic algorithms going, and on the overall quality of their work, the workshop approach is clearly positive and worthwhile. Students who cannot write AMPL scripts can modify them and learn from that, while learning about the TSP, and thereby eventually learn to write their own AMPL models and scripts. Of course, AMPL models are the first step, but this naturally leads to the harder step of writing AMPL scripts, and this can potentally lead to real programming. a couple pages. Wagner's (1975) classic text gives a branch and bound algorithm. Hillier & Lieberman (2001) has, surprisingly, nothing on the TSP. Ragsdale (2004) gives one example with the heuristic in Frontline System's Premium Solver for Excel. Lawrence & Pasternack (1998) give a similarly brief example, with a branch and bound algorithm in a supplement on a CD-ROM. Winston's (2004) discussion of the TSP includes a dynamic program (aimed at very small instances), a branch and bound example, a brief discussion about heuristics, and a loose integer programming formulation. We currently use Winston's text, but liberally add to the material. The AMPL code that we provide does not usually incorporate theoretically-efficient algorithms. Except for the minimum spanning tree and the 1-tree, the subproblems are modeled as linear or integer linear programs, and AMPL then accesses a general linear programming or integer linear programming code. We feel that the TSP is such a classical problem, perhaps the classical O.R. integer program, that it can play a central role in an undergraduate course about integer programming, even in a school of business. Yet the undergraduate textbooks do not seem to do it justice. We have found just one article written specifically for teaching the TSP to undergraduates. Pataki's (2003) excellent paper covers the relative tightness of different formulations, using Matlab. The outline of this paper is as follows. First, we discuss other teaching materials for the TSP. Second, we give a syllabus, with suggested software, lectures and tutorials, a homework assignment, exam questions, and describe the relevant AMPL scripts and data. Third, we give some further ideas to expand the material beyond what we currently teach, along with additional AMPL models and scripts. We conclude with a listing of internet resources for the TSP. Specifically for AMPL, Fourer, Gay & Kernighan (2003) is excellent and authoritative, but much more than students need for one course; by all means the book should be available to students as a reference. 3. TSP Syllabus 2. Literature review Here, we describe the software required and give our course material on the TSP. The course material is comprised of three lectures, two tutorials, homework problems, and exam problems. Lawler et al (1985) and Reinelt (1994) are, of course, excellent texts on the TSP. These books thoroughly cover branch and bound, cutting planes, subgradient optimization, a thorough review of different relaxations, and many important heuristics. Similarly, Nemhauser & Wolsey (1988) has quite rich material on the TSP, and encapsulates it well. Such textbooks are too advanced for our students in business, who do not have advanced skills in math or computer programming. 3.1. Software required The student version of AMPL with CPLEX may be downloaded from AMPL website(18). In fact, we intentionally avoid making a more powerful solver available to students, as we have found that the homework is more compelling with the limited version. Students are still free to try to find a larger solver on their own, but few ever do so. Several classic textbooks that are currently used at the undergraduate level either do not mention the TSP at all, or contain a shallow introduction to the subject. To mention a few, Dantzig's (1963) classic text on linear programming gives three different formulations over (18) http://www.ampl.com/ INFORMS Transactions on Education 7:1(37-69) 39 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP If you wish to solve models, but not use scripts, we also suggest the LP_Solve IDE(19). It has a nice interface and can read a subset of AMPL's language. The solver is not limited in the number of variables or constraints, but is much slower than CPLEX. • Scite can recognize AMPL keywords for statement completion with the tab key. • Scite displays in-line pop-up help for AMPL functions such as "first(x)". • With our attached amplhelp.html(28) file, Scite provides context-sensitive help for AMPL. • Scite allows infinite undo, and many other features. The amplhelp.html(29) file has roughly eight pages of beginner-level and reference material for AMPL. It includes a section on getting started with AMPL, basic syntax, descriptions of some important AMPL words and commands, commonly used options and functions, and a list of frequently-asked questions. We recommend that the lecturer using these materials modify this help file for the local requirements as needed. The included AMPL models and scripts. These are organized in their own directories, with a separate directory for data. This encourages students to think about modeling in an object-oriented way, to keep the model as generic as possible, and separate from the data. Scite. For editing AMPL files, we prefer the opensource cross-platform Scite, downloadable from the Scintilla website(23). This editor is highly configurable for various languages via properties files. We discovered a rudimentary AMPL properties file for Scite in Italian(24), and have modified and extended it extensively (ampl.api(25), ampl.properties(26), SciTEGlobal. properties(27)). Scite shows syntax coloring, block indenting, block commenting, and section folding. The user can start AMPL with the current run file, simply by pressing the F5 key. AMPL runs in a side panel. More details on installing the files for Scite are provided in section 4.2. SVG viewer. To see the graphical output from the scripts, you should install an SVG viewer, and we highly recommend this approach. You can get a free viewer from Adobe.com(20). The graphical output is displayed in scalable vector graphics (SVG)(21). From the website of the World Wide Web Consortium, "SVG is a language for describing two-dimensional graphics and graphical applications in XML." Almost all web browsers can display SVG natively or with an add-in. The file format is plain text, and easy to create with a script. For example, here is the minimum spanning tree for 1,000 random points(22) on the Euclidean plane, as intermediate output from the Christofides heuristic. • • The scripts and models have, for the most part, uniform data formats and uniform definitions for sets, variables, and parameters. For example, "param N;" is used in all models to define the number of nodes, and "Tourlength" is the name of every objective, even though some will not produce tours. This uniformity has a couple of advantages, the primary one being that we can use a single script to produce the SVG output graph, and a secondary advantage being that students become accustomed to the notation more quickly. 3.2. Lectures and tutorials Lecture 1. Description & history of the TSP. Some formulations.PowerPoint presentation(30) (19) http://www.progdigy.com/ (20) http://www.adobe.com/svg (21) http://www.w3.org/Graphics/SVG/ (22) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Christofides/1000points_mst.svg (23) http://scintilla.sourceforge.net/SciTEDownload.html (24) http://naufraghi.free.fr/informatica/ampl_scintilla.php (25) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/ampl.api (26) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/ampl.properties (27) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/SciTEGlobal.properties (28) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/amplhelp.html (29) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/amplhelp.html (30) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/PowerPoint/01_Formulations_for_the_TSP_with_AMPL.ppt INFORMS Transactions on Education 7:1(37-69) 40 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP In the first lecture, we first describe the problem as simply as possible: drive a car via n cities, minimizing travel distance. We then follow this with some history, especially the seminal Dantzig, Fulkerson & Johnson (1954) paper and formulation, with its key ideas of branch and bound and IP cuts. We discuss the difficulty of the TSP, touching on complexity theory. We also mention Concorde (Applegate, Bixby, Chvatal & Cook 1998), so students realize how far the research has come. lecture notes for testing. In some cases, we also show the expanded model for further clarification. Throughout, we follow a careful format for models, first giving the indices, the parameters, the decision variables, the model, and an explanation of each constraint set using "NPS format" (Brown 2004), due to its invention and use at the O.R. Department of the Naval Postgraduate School(39). (For a published example of NPS format, see Baker, Morton, Rosenthal, & Williams 2002.) To give students a mnemonic, we call this IPDME format (i.e., Indices, Parameters, Decision variables, Model, Explanation of constraints). This format has several advantages over a less formal approach. The format requires the author to write out formulations in a methodical and uniform way, making sure that every term of the model is explained, and encouraging the use of a unique meaning for each term. The format gives the reader a convenient directory of all symbols. The format leads naturally to an algebraic modeling language, as indices become sets, and the rest follows analogously. We encourage educators and editors to standardize on this format. Following this introduction, the remainder of the lecture covers different formulations related to the TSP, most with their AMPL formulations and solutions. (Note: if the files do not open on click, consider setting the ".mod" and ".run" file extensions to open with your favorite text editor.) • the assignment problem (assignment.mod(31), assignment.run(32)), • the Miller-Tucker-Zemlin model (MTZ.mod(33), MTZ.run(34)), • the symmetric TSP, For example, here is the classic Dantzig-FulkersonJohnson (DFJ) formulation, in IPDME format. (35) • the Svestka flow formulation (Svestka.mod , Svestka.run(36) ), • and the Danztig steps formulation with O(n3) variables (Dantzig-steps.mod(37), Dantzig(38) steps.run ). Indices: i, j city. Parameters: n = number of cities; cij = cost to travel from city i to city j. The algebraic formulations for these are in the PowerPoint presentation, and will not be repeated here. Generally, we show the algebraic form and the AMPL model directly in the slides (which are printed and given to students), so the AMPL formulation itself is used in the lecture. This gets students accustomed to seeing AMPL formulations, which helps them when it comes time to write their own. It allows them to ask questions in class about AMPL syntax. Further, if they wish, they can copy the examples straight from the Decision variables: xij = 1 if the salesman should travel between cities i and j, else 0. 1. Model DFJ: min ∑ ni=1 ∑ j:j>icij xij, 2. ∑j:j>i(xij + xji) = 2 for all i=1,...,n. 3. ∑i,j ∈ S xij ≤ |S| - 1, for every subset S. 4. x ∈ {0, 1} for all i, j:j>i. ij (31) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/assignment/assignment.mod (32) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/assignment/assignment.run (33) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/MTZ/MTZ.mod (34) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/MTZ/MTZ.run (35) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Svestka/Svestka.mod (36) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Svestka/Svestka.run (37) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Dantzig-steps/Dantzig-steps.mod (38) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Dantzig-steps/Dantzig-steps.run (39) http://www.nps.navy.mil/or/ INFORMS Transactions on Education 7:1(37-69) 41 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP as there are only n variables but O(n2) pairwise distances. So it is a useful exercise in modeling, to work one's way to the inevitably fruitful idea of using binary variables to select edges for a tour. Explanation: 1. Minimize total distance traveled. 2. Each city must have degree 2, i.e., must be entered and departed. Tutorial 1. Intro to AMPL, and two key relaxations of the TSP. 3. No subset of cities S consist of a tour, unless that subset has all the cities. The goal of this tutorial is to give students a quick hands-on success with AMPL, and to teach two key relaxations of the TSP, in a workshop approach. Please note that we assume that the lecturer is familiar with AMPL. Some instructions for running these files are given here and further information is given in amplhelp.html(40) but the authoritative discussion on AMPL is Fourer, Gay & Kernighan (2003). 4. The salesman cannot split his travel between cities. We write algebraic models in plain text (rather than Equation Editor or images from LaTeX) using text fonts, subscripts, and superscripts carefully. This allows algebraic models to be copied more easily between programs, animated in PowerPoint, and edited in HTML. Before the tutorial starts: While familiar to the O.R. teacher and researcher, the summations in model DFJ can be quite a challenge to students. We suggest taking a little time to explain how the summations work, and explain that the vertical bars on the set, |S|, mean the cardinality of the set, like a spreadsheet's Count() function. Part of understanding the model is understanding the solution. Students sometimes have difficulty making sense of the model until they understand the form of the decision variables. Thus, we use a uniform example for all formulations, and show the solution for each formulation. A tour can be encoded in different ways, and it is easy to forget that the IP edge formulation is not immediate for a newcomer. Without any guidance, a student may suggest making a solution vector be simply a permutation of the integers 1,...,n. It is instructive to pursue this direction, exploring the difficulties of trying to write down an IP formulation from these variables. The first issue that one encounters is the difficulty of expressing the "all-different" constraint that the values of each of the n variables should be distinct. For example, for n=3, we see that 1,2,3 and 3,2,1 are both valid solutions. Their midpoint 2,2,2 is on the convex hull and is therefore feasible in any LP formulation based on these variables. But it is infeasible to the IP. Furthermore, it is not possible to formulate the objective as a linear function in these variables, • students should be aware of the requirements for Homework 1, so they are thinking about how to solve it; • the lab computers should have Scite, AMPL, CPLEX, and an SVG viewer installed. The lab should have a chalkboard. At the beginning of the tutorial, the algebraic and the AMPL formulations for the assignment (assignment.mod(41), assignment.run(42)), and 2-matching problems (2matching.mod(43), 2matching.run(44)), are put on the board. Do not give the files on disk, but do give students the complete AMPL formulation either on paper or on the chalkboard. Remind students to end commands with a semicolon. By giving students an example straight off, they will start to catch on to AMPL's syntax. Give the model to students without integrality, and let students discover for themselves the need for integrality. Students should copy the AMPL formulations into a text editor, creating their own files, and running AMPL as they are ready. By requiring students to type in two simple models, they are forced to engage with AMPL, and make it work, at a time when they can get immediate help. The concept of the AMPL run file is taught, and students put it to use. Separation of model, script, (40) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/amplhelp.html (41) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/assignment/assignment.mod (42) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/assignment/assignment.run (43) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/2matching/2matching.mod (44) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/2matching/2matching.run INFORMS Transactions on Education 7:1(37-69) 42 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP and data then begins to make sense, especially as they realize that changing from the tutorial example to the homework set involves changing only the data file. This contrasts with the need to rewrite the entire model, as would be required with Lindo. gressing, simply by refreshing the browser. Copying the image to a word processing program is convenient. The images are valuable for teaching, and are also conveniently copied to presentation software such as PowerPoint. The models can be run either through the Scite interface or at the AMPL command prompt. Given sufficient time, students may be given AMPL scripts for the minimum spanning tree (prims.run(46) or kruskal.run(47), originally developed by Olinick (2006). Alternatively, the minimum spanning tree may be covered in a later tutorial, after the material is presented during the lecture. • • To run a script in AMPL through the Scite interface, the user simply opens the script (usually a ".run" file) in Scite, and presses the F5 function key. AMPL then runs in a side panel. Note that if you have a model file open without a "solve" command, AMPL will read the model, but not solve it because it has not received the "solve" command. The workshop approach has multiple benefits, as immediate help can be given to those students who have difficulty starting and running AMPL. Points of syntax can be addressed immediately, and students get a quick positive result, with a problem that is easy computationally. Key AMPL commands, such as "reset", "display", and "expand", are explained and immediately put to use. Sometimes, students become sufficiently confident and enthused that they want to attempt the homework assignment immediately. To run a script at the AMPL command prompt, click Start, Run, type ampl in the dialog and press enter. This opens an AMPL command window. To run the 2matching.run script, at the "ampl:" prompt, type "run 2matching.run;", and press enter. In the command window, they will need to enter the reset command to clear AMPL's memory. ("Reset" is not needed if they use Scite as the interface.) Remind them not to reset before using "display" to see results. Explain that when they are frequently rerunning a script, they may want to put "reset;" at the end of the script. Provision of this one-hour workshop has alleviated a host of problems in using AMPL. Students come up to speed much more quickly compared to simply being given a sheet of instructions. Lecture 2. Relaxations and solution methods. PowerPoint presentation(48) After students get the two models running, they are given access to the script (makesvg.run(45)) which produces the SVG graphical output, and shown how to call the script from their existing run files. This visualization gives them a powerful and immediate indication as to whether their model is correct, and how it is related to the TSP. This would be the time to explain the commands filename command, which is used to give AMPL a list of commands from a file. Following Lecture 1's introduction to TSP formulations, Lecture 2 introduces some solution methods. We first present the assignment problem with branch and bound, using an example from Winston (2003, pp. 530-534), then give the 1-tree, with Prim's and Kruskal's algorithms. This information is easy to explain, and tends to be understood easily, partly because of the highly graphical nature of the information, and as it is given just a few weeks following more general IP cuts and branch and bound. SVG can be viewed in a web browser with a free addin (such as Adobe's SVG Vi e we r, http://www.adobe.com/svg). We have carefully written all models and scripts to have uniform variable and set definitions, so they all will work with makesvg.run. For algorithms such as the 1-tree subgradient optimization, the graph can be viewed as the algorithm is pro- The final section of this lecture covers the Held & Karp 1-tree subgradient optimization algorithm. Many students get completely thrown at this point, as the lecture has moved from tree, to 1-tree, to an entirely new al- (45) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/makesvg.run (46) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Prims/prims.run (47) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/kruskal/kruskal.run (48) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/PowerPoint/02_Solution_methods_for_the_TSP.ppt INFORMS Transactions on Education 7:1(37-69) 43 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP gorithm. Considerable patience is required. The subgradient optimization material is presented in more detail in Tutorial 2. The presentation does not cover all the various AMPL scripts we have for the DFJ model, but the lecturer may want to discuss them with students. These include, in increasing order of sophistication: Tutorial 2. Subgradient optimization. PowerPoint presentation(49) • DFJ_all_subtours.mod(54) and DFJ_all_subtours.run(55) (modified from AMPL website(56) ). This script explicitly uses the model with all O(2n) subtour constraints. This is meant only for very small instances. • DFJ-simple.mod(57) and DFJ-simple.run(58) . This script starts with the model consisting of just the simple bounds and the degree constraints. At each stage the current integer linear programming formulation is solved, and then a single violated subtour inequality is appended to the formulation. The script stops when there is no violated subtour constraint. • DFJ.mod(59) and DFJ.run(60). This script also starts with the model consisting of just the simple bounds and the degree constraints. The scripts executes a sequence of stages, repeating until a substage finds an optimal tour. At each stage we do the following: In this tutorial, the subgradient optimization algorithm is presented in detail. With sufficient time for a computer lab, students may be given the relevant AMPL scripts, including those for Prim's (prims.run(50)), Kruskal's (kruskal.run(51) , originally developed by Olinick (2006), and subgradient optimization (HeldKarp.run(52)). Students should be challenged to match up the steps of the algorithm from the notes with lines of code in AMPL. These scripts do not use separate model files. Since these do not call the IP solver, AMPL's script processor can solve larger spanning trees than would be solvable through calls to the student-version IP solver. Of course, this depends on the commands employed. If you modify the script and find that AMPL complains about the number of variables, try using parameters instead of variables. 1. Repeatedly solve the current linear program; identify a violated subtour constraints by solving further (max-flow/min-cut) linear programs, and append it to the current formulation; this sub-stage terminates when there are no further violated subtour constraints; if the resulting solution is integer, then we have an optimal tour, and we terminate the script. Lecture 3. More solution methods for the TSP. PowerPoint presentation(53) The majority of Lecture 3 presents the DFJ subtour cut generation algorithm. We use a simple 6-city example from Winston (2003), an example which produces subtours with the 2-matching relaxation. The max-flow algorithm is then developed as a method to find subtours. The presentation shows the output from AMPL running, so students see the iterative nature of the algorithm. 2. Solve the current formulation as an integer linear program. Identify a violated subtour constraint and append it to the current formulation. If there is no violated subtour constraint, then we have an optimal tour, and we termi- (49) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/PowerPoint/03_Tutorial_TSP-MST_Lagrangean_relaxation.ppt (50) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Prims/prims.run (51) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/kruskal/kruskal.run (52) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/HeldKarp/HeldKarp.run (53) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/PowerPoint/04_More_solution_methods_for_the_TSP.ppt (54) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/All_subtours/DFJ_all_subtours.mod (55) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/All_subtours/DFJ_all_subtours.run (56) http://www.ampl.com/FAQ/tsp.mod (57) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/1_subtour_at_a_time/DFJ-simple.mod (58) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/1_subtour_at_a_time/DFJ-simple.run (59) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/subtours_by_maxflow/DFJ.mod (60) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/subtours_by_maxflow/DFJ.run INFORMS Transactions on Education 7:1(37-69) 44 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP tor_for_TSP_50_points.xls(68)".) Students are required to give valid upper & lower bounds on the tour length, with a description of their procedures for finding these bounds. nate the script; otherwise, we initiate a new stage. DFJ_restricted_arcs.mod(61) and DFJ_restricted_arcs.run(62). This script executes roughly as DFJ.mod/run, but initially the number of arcs is restricted to the shorter ones. Then, as described at the end of the PowerPoint presentation, new arcs are added if they might improve on the solution of the linear programming relaxation. Arcs with sufficiently large reduced cost can be omitted from the model. In our experience, the 300-variable limit in the student version of AMPL/CPLEX has been sufficient to prove optimality for 50-city problems. Note that the restricted arcs versions can be used to solve the homework directly, so the scripts probably should not be given to students before the homework is due. The use of the restricted student version of CPLEX requires students to omit rows and variables. The limitation on the student solver should be used to emphasize that one's computer is never fast enough, and that one's solver is never powerful enough. Real world problems can be extremely demanding, and the resourcefulness of the operations researcher is therefore put to the test. This invites students to use creativity (e.g., learn to use the NEOS server (see 5.1 below) or other software, such as the open source COIN-OR Cbc, GNU LP Kit, or LP_Solve). As mentioned above, the solution is to use DFJ_restricted_arcs.mod(69) and DFJ_restricted_arcs.run(70), which should not be given to students before the homework is due. The max-flow algorithm is then developed further into a new formulation. Martin (1991) gave an LP formulation for the MST, which is best understood in this context. From this formulation for the MST, we then develop a formulation for the TSP with O(n3) variables. The relevant AMPL files are martin_tree_lp.mod(63), and martin_tree_lp.run(64), which can be used to solve for a minimum spanning tree, a 1-tree, or the TSP. The most common error is to claim optimality when they cannot. Students realize that they must follow the common practice of eliminating unlikely variables (e.g. long edges) from consideration, but then they make claims about optimality that they should not. The lecture concludes with hints for the homework, which we describe next. Students may gain nearly full marks without an optimal solution, but their approach must be reasonable. Minimal nonzero marks would be given for a tour found visually for the upper bound and an ordinary 1-tree for the lower bound. Good answers should include a good feasible solution, and at least a few iterations of the Held-Karp subgradient optimization algorithm. The lecturer can choose whether or not to give the students our included script (HeldKarp.run(71)). 3.3. Homework. We assign a 50-city problem, which is too big to solve directly in the student version of AMPL with CPLEX(65) . (We have included a script which will generate a data file with random x,y coordinates, get_random_coordinates.run(66). This script calls another script, makesvg_points.run(67), which creates an SVG graph of the points. Alternatively, the lecturer can use a spreadsheet "Random_data_genera- (61) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/Restricted_arcs/DFJ_restricted_arcs.mod (62) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/Restricted_arcs/DFJ_restricted_arcs.run (63) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/MartinLP/martin_tree_lp.mod (64) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/MartinLP/martin_tree_lp.run (65) http://www.cplex.com/ (66) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/get_random_coordinates.run (67) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/makesvg_points.run (68) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/Random_data_generator_for_TSP_50_points.xls (69) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/Restricted_arcs/DFJ_restricted_arcs.mod (70) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/Restricted_arcs/DFJ_restricted_arcs.run (71) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/HeldKarp/HeldKarp.run INFORMS Transactions on Education 7:1(37-69) 45 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP 3.4. Exam problems • "b. (2 points) Write the new penalized distances in the table above." 3.4.1. A minimum spanning tree. • "c. (7 points) With the new penalized distances, draw the new 1-tree below." To write the problem: 3.4.3. Subtour cut generation. 1. Create a random problem. A 20-node problem is reasonable. Because new problems are easy to generate, students cannot memorize a given solution from a previous year's exam paper. To write the problem, create a random problem. Modify the DFJ AMPL script to stop with subtours. Copy the graph into the exam paper, and ask students to write constraints that break the displayed subtours. 2. For this random problem, print the graph and the matrix of distances on the exam paper. • Ask students to find the MST by hand, using Prim's or Kruskal's algorithm. • Ask the students to draw the solution on the exam paper, and show the order in which the arcs were added. • To mark the question, find the correct solution with AMPL, and create the graph. Then simply compare the student's answer to the correct answer. 3.4.4. Understanding of specific algorithms. Show students an AMPL script that they have studied. Ask them to explain several lines in the script, and how the algorithm requires those lines of script. For example, the Kruskal.run script early on contains the line, "let Best_Edges := {(i,j) in Unscanned: length[i,j] = min{(u,v) in Unscanned} length[u,v]};" For a short answer question, you can ask students to explain in two or three sentences the purpose of this line. A valid answer would be something like, "This line initializes an iteration for one arc. The line finds the shortest unconnected arc among the set Unscanned, and adds this arc to the set Best_Edges. The algorithm then connects this arc to a component of the still-disconnected tree." 3.4.2. One step of subgradient optimization. To write the problem: 1. Create a random problem. A ten-node problem is reasonable. 3.4.5. Short answer question. 2. Create the initial 1-tree with AMPL. "Describe two ways to get a good lower bound on the traveling salesman problem." Answer: Method 1. Solve the assignment or 2-matching problem, then add subtour breaking constraints. Every solution is a valid lower bound, and the lower bound improves with each added constraint. Method 2: Use Held & Karp subgradient optimization for the 1-tree relaxation. Method 3: Solve any other TSP formulation as an LP. 3. Print the 1-tree and the matrix of distance on the exam paper. 4. Include a table of the current arcs, labeled with From node, To node, Distance, and Penalized distance. 5. Provide a line or two of space for students to write node penalties, for question a. 6. Fill in all the entries except the Penalized distance column, for question b. 3.4.6. Short answer question. 7. Print a graph of the nodes without arcs, for question c. "Give two reasons why a lower bound would be helpful in solving the traveling salesman problem to optimality." Answer: First, if we find a tour that is equal to the lower bound, then we know it is optimal. Second, during branch and bound, we can prune off subproblems that have a worse lower bound than the current best lower bound. Third, with both an upper bound U and a lower bound L, we can delete variables Questions could be as follows: • "a. (1 point) Starting with initial penalties of zero and a step size of 30, calculate the new node penalties. Write them here." (Provide space.) INFORMS Transactions on Education 7:1(37-69) 46 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP ing_with_combs.mod(73) ing_with_combs.run(74). that have a reduced cost (in the LP) that is larger than U − L. Putting those variables in the solution will increase the objective above the upper bound, so they cannot be optimal. and 2match- 3.5.3. Add Dantzig-Wolfe decomposition with the 1-tree subproblem (Held & Karp). 3.5. Further ideas Held & Karp (1969) showed how to find v(DFJ) with the following model (note we use underscore to designate fixed variables, as this is easier to type than overscore): The TSP allows a wide range of creative possibilities. Here are additional ideas for the classroom and homework. 3.5.1. A half lecture could cover Christofides' heuristic (Christofides.run(72)). Model HK: min ∑tπt∑i∑j:j>icijxtij, where xtij is the set of Teaching heuristics requires that students already have good programming skills and an appreciation for the TSP's difficulty. In short, we think that the integer programming approach should be taught first. ∑t πt (∑j:j>i (xtj,i + xti,j)) = 2, for all i (dual prices λi), 3.5.2. A half lecture could cover the 2-matching problem in depth, including comb constraints. πt ≥ 0, for all t. arcs in 1-tree t, ∑t πt = 1, A solution method for Model HK is as follows: This is an example of an easy problem (i.e., there is an efficient algorithm), but the IP is not naturally integral. Instead of relaxing the degree 2 constraints, we could relax the subtour breaking constraints, resulting in a 2-matching problem. The 2-matching problem can be solved in polynomial time, but its LP relaxation is not naturally integer. However, Edmonds (1965) showed that a complete linear description of the 2-matching problem is obtained by the constraints 1 and 2 from the DFJ formulation above, with 5 below. Step 0. Pick dual prices λi. Step 1. Solve the 1-tree: min ∑i∑j>i (cij - λi - λj)xij + 2∑i λi, x ∈ T. Step 2. Add one variable πt+1 to Model HK above and resolve HK. Update dual prices λi from degree constraints. Quit when v(HKt) = v(1-treet). 5. ∑ (i,j):i ∈ W, j ∈ W xi,j + ∑ (i,j) ∈ F xi,j ≤ |W| + |_|F|/2_|, This requires many iterations, with a "tail-off " effect. To improve on this method, Held & Karp (1970)gave their now-famous subgradient optimization algorithm. for all node subsets W, and all sets of arcs F, where (i,j) ∈ F if i ∈ W xor j ∈ W. 3.5.4. Assign a Petersen graph, giving only the numerical data. These constraints are called the 2-matching inequalities, not to be confused with the constraint set that requires that nodes have degree 2. The 2-matching inequalities are part of a large set of constraints called comb constraints; set W is often called the "handle" and set F is called the "teeth ". Unfortunately, the 2-matching problem has an exponential number of two matching inequalities. We provide a script to find and add 2matching inequalities iteratively, 2match- (An example, Petersen10.txt is given in the AMPL data files.) Students who take the time to create a visualization will have additional insight. A bit of a trick assignment, perhaps best for a tutorial. (72) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Christofides/Christofides.run (73) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/2matching/Comb_cuts/2matching_with_combs.mod (74) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/2matching/Comb_cuts/2matching_with_combs.run INFORMS Transactions on Education 7:1(37-69) 47 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP 3.5.5. Add a capacity constraint, such as for fuel. 4.2. Scite configuration files for AMPL This is easy to do, and lends realism to the problem. The following represents one attempt at setting up Scite to work with AMPL. We would welcome additional suggestions and improvements from readers. 3.5.6. Use of AMPL's database access to take TSP data from a spreadsheet. 1. Download Scite from the Scintilla website(80). The single executable Sc1 is convenient. Place this file in the directory of your choice, perhaps "C:\Program Files\Scite". This is easy to do, and useful for students. 3.5.7. The lecturer can change innocuous data 2. Add a shortcut to sc1.exe on your Windows Launch tool bar. (e.g., arcs not in the solution) to test for plagiarism. A glance at the student's data confirms whether the student has taken another student's files. 3. Copy all the files from this paper's Scite directory to your Scite directory. 3.5.8. Ask students to code the nearest neighbor heuristic in AMPL script, and examine its performance. 4. In Start, Control Panel, System, Advanced, Environment variables, System variables, add your Scite directory to the path variable. 5. Then, to start Scite, simply click on the shortcut on your launch bar. You can also drag files onto the shortcut to open them with Scite. 4. A directory of teaching materials: files, models and scripts for the TSP 6. If Scite does not seem to find the AMPL files, review the settings in the ampl.properties file. This section gives a directory of all the files associated with this paper. (Here are all supporting files in a single zipped file, Using AMPL for teaching the TSP, supporting files.zip(75).) The file SciTEGlobal.properties(81) imports the ampl properties file. SciTEGlobal.properties allows a wide range of configurations. See the Scite documentation file(82) for more information. 4.1. PowerPoint presentations • Lecture 1. Description & history of the TSP. Some formulations. 01_Formulations_for_the_TSP_with_AMPL.ppt.(76) • Lecture 2. Relaxations and solution methods. 02_Solution_methods_for_the_TSP.ppt.(77) • Tutorial 2. Subgradient optimization. 03_Tutorial_TSP-MST_Lagrangean_relaxation.ppt(78). • Lecture 3. More solution methods for the TSP. 04_More_solution_methods_for_the_TSP.ppt(79). The file ampl.properties(83) provides support for the file extensions ".mod", "dat" and "run", lists AMPL key words for colored syntax, and specifies a variety of other miscellaneous properties. Settings in ampl.properties override settings in SciTEGlobal.properties. Ampl.properties controls the behavior of function key F5, which currently calls AMPL with the current ".run" file. You can modify this file to use the GNU LP Kit in place of AMPL. (75) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/Using%20AMPL%20for%20teaching%20the%20TSP,%20supporting%20files.zip (76) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/PowerPoint/01_Formulations_for_the_TSP_with_AMPL.ppt (77) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/PowerPoint/02_Solution_methods_for_the_TSP.ppt (78) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/PowerPoint/03_Tutorial_TSP-MST_Lagrangean_relaxation.ppt (79) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/PowerPoint/04_More_solution_methods_for_the_TSP.ppt (80) http://scintilla.sourceforge.net/SciTEDownload.html (81) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/SciTEGlobal.properties (82) http://scintilla.sourceforge.net/SciTEDoc.html (83) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/ampl.properties INFORMS Transactions on Education 7:1(37-69) 48 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP The file ampl.api(84)lists many of AMPL's functions, for the tooltips. • The 2-matching problem, 2matching.mod(91), 2matching.run(92). The file amplhelp.html(85) provides context-sensitive help for AMPL key words in Scite. This help file has taken some effort, but could probably still be improved. The file uses images AMPLcmdline.PNG(86), SciteAMPL.PNG(87) , and SciteAMPL0.PNG(88). • The assignment problem, assignment.mod(93), assignment.run(94). • Miller-Tucker-Zemlin (MTZ) assignment model with loose subtour breaking rows, MTZ.mod(95), MTZ.run(96). The file amplwizard.hta(89) represents an experimental approach to guiding a first-time user to AMPL syntax. This is ".hta" application(90), which runs in Internet Explorer as a trusted executable. Amplwizard.hta is written in ordinary HTML and VB script, which is easy to modify. It uses the "SendKeys" function to pass a string to Scite. The string is inserted into Scite at the current cursor location in Scite. This also shows how an external command can be placed into Scite's menu and called. • Svestka flow formulation,Svestka.mod(97) , Svestka.run(98) . • Dantzig's steps formulation, Dantzig-steps.mod(99), Dantzig-steps.run(100). • DFJ with all subtours, dfj_all_subtours.mod(101), dfj_all_subtours.run(102) (from AMPL website(103)). • Martin's (1991) formulation for the MST, with variations for the 1-tree and TSP, martin_tree_lp.mod(104), and martin_tree_lp.run(105). 4.3. TSP and TSP-related formulations 4.4. Algorithms The major source of scripts for this paper was the first author's University of Kentucky web site, which is not currently available. We discuss other internet resources later in the paper. • Prim's algorithm for the MST, prims.run(106) . • Kruskal's algorithm for the MST, kruskal.run(107), script originally developed by Olinick (2006). Some of the directories also contain sample SVG graph files, usually named tspgraph.svg. (84) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/ampl.api (85) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/amplhelp.html (86) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/AMPLcmdline.PNG (87) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/SciteAMPL.PNG (88) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/SciteAMPL0.PNG (89) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/scite/amplwizard.hta (90) http://msdn.microsoft.com/workshop/author/hta/overview/htaoverview.asp (91) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/2matching/2matching.mod (92) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/2matching/2matching.run (93) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/assignment/assignment.mod (94) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/assignment/assignment.run (95) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/MTZ/MTZ.mod (96) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/MTZ/MTZ.run (97) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Svestka/Svestka.mod (98) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Svestka/Svestka.run (99) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Dantzig-steps/Dantzig-steps.mod (100) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Dantzig-steps/Dantzig-steps.run (101) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/all_subtours/dfj_all_subtours.mod (102) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/all_subtours/dfj_all_subtours.run (103) http://www.ampl.com/FAQ/tsp.mod (104) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/MartinLP/martin_tree_lp.mod (105) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/MartinLP/martin_tree_lp.run (106) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Prims/prims.run (107) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/kruskal/kruskal.run INFORMS Transactions on Education 7:1(37-69) 49 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP • Held-Karp subgradient Karp.run(108). optimization, Held- • The DFJ cut-generation algorithm, adding one subtour cut at a time, DFJ-simple.mod(109) and DFJsimple.run(110). • The classical DFJ cut-generation algorithm, using a max flow model as a subroutine, DFJ.mod(111) and DFJ.run(112). • The DFJ cut-generation algorithm with restricted arcs and reduced cost test, DFJ_restricted_arcs.mod(113) and DFJ_restricted_arcs.run(114). • The 2-matching problem with comb generation, using a max flow model as a subroutine, 2matching_with_combs.mod(115) and 2matching_with_combs.run(116). • Christofides heuristic, Christofides.run(117). Intermediate and final SVG output from this model is contained the files 1000points_mst.svg(118), 1000points_oddmatching.svg(119), and 1000points_christo_tour.svg(120). • scale the graphical objects to the data, but you may still need to adjust stroke-width, circle radius, and font-size. 4.5. Sample data Note that non-Euclidean examples have dummy x,y coordinates for the SVG visualization. • Script to generate a data file with random x,y coordinates, get_random_coordinates.run(122). The output is written to a file name "random_points.txt". This script calls makesvg_points.run(123). • Script to create SVG output of only the nodes,Makesvg_points.run(124). The script is assumed to be called from another script, and assumes N nodes, xcoord[i], ycoord[i], x[i,j]. It tries to scale the graphical objects to the data. This is useful when creating problems from random data, when you want to see the graph before solving the problem. • A spreadsheet to generate random x,y coordinates, " R a n d o m _ d a t a _ g e n e r a tor_for_TSP_50_points.xls(125)". • Nemhauser & Wolsey (1988), page 474, Nemwolp474.txt(126). In this example, v(DFJ) = 9, while the optimal integer 2-matching and the optimal tour length have length 10. • Nemhauser & Wolsey (1988), page 476, Nemwolp476.txt(127). In this example, the optimal integer (121) Script to create SVG output, makesvg.run . The script is assumed to be called from another script. The script assumes N nodes, a parameter or objective function named "Tourlength", set "Nodes" and "Arcs", variables or parameters named "x" defined over the set Arcs, and appropriately defined parameters "length", "xcoord" and "ycoord". The script creates an SVG graph with solid lines for x[i,j]=1 and dotted lines for 0 < x[i,j] < 1. It tries to (108) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/HeldKarp/HeldKarp.run (109) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/1_subtour_at_a_time/DFJ-simple.mod (110) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/1_subtour_at_a_time/DFJ-simple.run (111) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/subtours_by_maxflow/DFJ.mod (112) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/subtours_by_maxflow/DFJ.run (113) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/Restricted_arcs/DFJ_restricted_arcs.mod (114) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/Restricted_arcs/DFJ_restricted_arcs.run (115) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/2matching/Comb_cuts/2matching_with_combs.mod (116) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/2matching/Comb_cuts/2matching_with_combs.run (117) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Christofides/Christofides.run (118) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Christofides/1000points_mst.svg (119) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Christofides/1000points_oddmatching.svg (120) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Christofides/1000points_christo_tour.svg (121) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/makesvg.run (122) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/get_random_coordinates.run (123) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/makesvg_points.run (124) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Makesvg_points.run (125) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/Random_data_generator_for_TSP_50_points.xls (126) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/Nemwolp474.txt (127) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/Nemwolp476.txt INFORMS Transactions on Education 7:1(37-69) 50 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP 2-matching has length 15, and the optimal tour has length 23. 5. Additional web resources • Boyd & Labonté (2002) 10-node example, Boyd10.txt(128). Boyd & Labonté proved that this example has the largest possible gap between the value of the optimal tour of 40 and v(DFJ) of 34. Symmetric version, Boyd10sym.txt(129). • A 10-node Petersen graph, Petersen.txt(130), shown here. Image from http://en.wikipedia.org/wiki/Petersen_graph(131). The Internet has rich materials on the TSP, with lots of free software and interactive visualizations. Google reports 115,000 hits on the search site:edu "traveling salesman problem". (Note that with the double-elled 'travelling', one gets about 25,000 hits.) Here, we aim to give a few key educational sites that are appropriate for third or fourth year students. (These were last accessed in June 2006.) • Random problems, 6points.txt(132) 20points.txt(133), 50points.txt (134), 100points.txt(135), 1000points.txt(136), 2000points.txt(137). • Data from TSPLIB(138): • Coordinates of the 48 capitals of the continental United States, 48uscaps.txt(139). • 52 Berlin locations, 52berlin.txt(140). • 105 cities, 105cities.txt(141). 5.1. General web sites • TSPLIB. "This page intends to be a comprehensive listing of papers, source code, preprints, technical reports, etc, available on the Internet about the Traveling Salesman Problem (TSP) and some assoc i a t e d p r o b l e m s . " http://www.ing.unlp.edu.ar/cetad/mos/TSPBIB_home.html or University of Heidelberg website(142). • Concorde. General site concerning the TSP. Highlights: "The full source code to the Concorde network optimization package, as well as executables for various platforms, and a Windows graphical user interface to Concorde's traveling salesman solver are available for academic research use." TSP website(143)>. • COIN-OR(144). A repository for open-source code for O.R. Notably, Cbc (COIN Branch and Cut) can be used as a C++ library, as a standalone solver, and as an AMPL solver (with all Cbc options available from an AMPL script). In addition, the Coin-OR website has a variety of other solvers and (128) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/Boyd10.txt (129) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/Boyd10sym.txt (130) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/Petersen10.txt (131) http://en.wikipedia.org/wiki/Petersen_graph (132) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/6points.txt (133) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/20points.txt (134) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/50points.txt (135) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/100points.txt (136) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/1000points.txt (137) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/2000points.txt (138) http://www.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/ (139) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/48uscaps.txt (140) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/52berlin.txt (141) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/105cities.txt (142) http://www.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/ (143) http://www.tsp.gatech.edu/ (144) http://www.coin-or.org/ INFORMS Transactions on Education 7:1(37-69) 51 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP algorithms available for linear, nonlinear, continuous, and discrete optimization. • stances. results and comparisons.The Traveling Salesman Problem(148). NEOS Server for Optimization. NEOS provides open access to a large number of solvers which run on NEOS servers. The Concorde server at NEOS provides for exact solution using the TSP solver of Applegate, Bixby, Chvátal, and Cook. In addition, the Concorde server provides for solution using the Lin-Kernighan heuristic. For integer programming, the open-source solver Cbc and others are available. Several of the IP solvers on NEOS provide for user input via AMPL, in two different ways. First, AMPL files can be uploaded to the server and then AMPL and the desired solver execute on a NEOS server. Second, a user can run AMPL locally on their own machine, and only use the server as a back-end solver. This latter possibility utilizes the Kestrel client/server. The advantage of using Kestrel is that the solution provided by the remote solver is returned to the running AMPL session on your own machine, where, for example, a script can then continue. Also, if one has a complicated script that calls many different solvers, this is the only way to proceed if the solvers are available on different server machines of NEOS(145). • Chvatal's web site, The Traveling Salesman Problem(146). • Steven S. Skiena's "The Stony Brook Algorithm Repository," has a section devoted to the TSP: The Traveling Salesman Problem(147). In addition, Skiena (1997) has a section on the TSP. • Website of the 8th DIMACS Implementation Challenge: The Traveling Salesman Problem. In- • Wikipedia article, The Traveling Salesman Problem(149). • An introductory article by Karla Hoffman, The Traveling Salesman Problem(150). • Fractal Instances of the Traveling Salesman Problem. Resources on generation of instances of the TSP with known optimal solution. Fractal Instances of the Traveling Salesman Problem(151). • A few links and experimental resources. The Traveling Salesman Problem(152). 5.2. Optimal solutions and other scripts • Robert Dakin's page, Fractal Instances of the Traveling Salesman Problem(153). Includes a program with visualization that implements two algorithms for the TSP. • Neil Simonetti's Traveling Salesman Problem Page. Includes a Dynamic Programming Code for the TSP.Fractal Instances of the Traveling Salesman Problem(154). • Onno Waalewijn's TSP applets, JAVA Traveling Salesman Problem(155) (heuristic version), Exact Traveling Salesman Problem(156) (exhaustive version). • Sean Forman's web page, TSP Generator(157), From a list of USA city names, the program generates a map and applies various heuristics and an exact branch and bound search. (145) http://neos.mcs.anl.gov/neos/ (146) http://www.cs.concordia.ca/~chvatal/tsp/tsp.html (147) http://www.cs.sunysb.edu/~algorith/files/traveling-salesman.shtml (148) http://public.research.att.com/~dsj/chtsp/ (149) http://en.wikipedia.org/wiki/Traveling_salesman_problem (150) http://iris.gmu.edu/~khoffman/papers/trav_salesman.html (151) http://www.ing.unlp.edu.ar/cetad/mos/FRACTAL_TSP_home.html (152) http://delphiforfun.org/Programs/traveling_salesman.htm (153) http://www.pcug.org.au/~dakin/tsp.htm (154) http://www.andrew.cmu.edu/user/neils/tsp/index.html (155) http://home.planet.nl/~onno.waalewijn/tspfast.html (156) http://home.planet.nl/~onno.waalewijn/tspx.html (157) http://www.sju.edu/~sforman/research/usa_tsp.html INFORMS Transactions on Education 7:1(37-69) 52 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP • Bruno Guerrieri's(158) Maple worksheet and animation for the TSP(159), available from MapleSoft. • Kreimer Natasha's applets for some TSP heuristics(172) . • "Solution to the traveling salesman problem is implemented in Mathematica as TravelingSalesman[g] in the Mathematica add-on package DiscreteMath Combinatorica (which can be loaded with the command "DiscreteMath") The Traveling Salesman Problem(160). • Prentice Hall Companion Website, Excursions in Modern Mathematics Fifth Edition, by Peter Tannenbaum. Chapter 6 is on the TSP and it includes, for example, an applet for a heuristic(173). • Johannes Sarg's Visualisation of Genetic Algorithms for the Traveling Salesman Problem in Java(174) . • An applet with "simple, but effective heuristics for the Traveling Salesman Problem with Euclidean distances(175)." • Svengato's applet for a simulated annealing heuristic(176). • Gregg M. Townsend's travels.icn(177) program illustrates several construction algorithms followed by application of the "2-opt" improvement algorithm". • Gunno Törnberg's Java applets for heuristics on randomly generated problems(178). 5.3. Heuristics • Frits Beuker's simulated annealing heuristic(161) . • Konstantin Boukreev(162)'s genetic algorithm java code (source code only)(163). • Julian Bunn's genetic algorithm applet(164). • Java visualization of a neural-network algorithm(165). • Sushil J. Louis and Rilun Tang, paper "Interactive genetic algorithms for the traveling salesperson problem,"(166) and applet(167). • Hans Lohninger's TravSalm: Neural network applet(168). • (169) David Neto's open source implementation LK of the Lin-Kernighan heuristic for the TSP and the minimum weight perfect matching problem. • Stephan Mertens'(170) TSP Algorithms in Action Animated Examples of Heuristic Algorithms(171). References Applegate,David, Robert Bixby, Vasek Chvatal, and William Cook (1998), "On the Solution of Travelling Salesman Problems," Documenta Math., Vol. 3, pp. 645-656. Brown, Gerald G. (2004), (158) http://www.maplesoft.com/applications/app_center_advanced_search.aspx?ABA=265 (159) http://www.maplesoft.com/applications/app_center_view.aspx?AID=966 (160) http://mathworld.wolfram.com/TravelingSalesmanProblem.html (161) http://www.math.uu.nl/people/beukers/anneal/anneal.html (162) http://www.codeproject.com/script/profile/whos_who.asp?id=128 (163) http://www.codeproject.com/cpp/tspapp.asp (164) http://pcbunn.cithep.caltech.edu/Java/Genetic.html (165) http://www.patol.com/java/TSP/ (166) http://www.cse.unr.edu/~sushil/papers/newpapers/99/gecco99/iga/GECCO/gecco/gecco.html (167) http://gaspac.cse.unr.edu:8323/~sushil/tsp/data/ (168) http://www.vias.org/simulations/simusoft_travsalm.html (169) http://www.cs.toronto.edu/~neto/research/lk/ (170) http://www-e.uni-magdeburg.de/mertens/index.php (171) http://www-e.uni-magdeburg.de/mertens/TSP/index.html (172) http://valis.cs.uiuc.edu/~sariel/research/CG/applets/tsp/TspAlg.html (173) http://wps.prenhall.com/esm_tannenbaum_excursions_5/0,8136,944272-,00.html (174) http://www.ads.tuwien.ac.at/~guenther/tspga/TSPGA.html (175) http://riot.ieor.berkeley.edu/~cander/cs270/ (176) http://www.svengato.com/salesman.html (177) http://www.cs.arizona.edu/icon/oddsends/travels/travels.htm (178) http://web.telia.com/~u85905224/tsp/TSP.htm INFORMS Transactions on Education 7:1(37-69) 53 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP http://www.nps.navy.mil/orfacpag/resumePages/publications/Top%20Ten%20Secrets%20to%20Success%20with%20Optimization.pdf >"Top Ten Secrets to Success with Optimization," The Phalanx, Vol. 37, No. 4, p. 12ff. http://engr.smu.edu/~olinick/emis8373/lectures/mst/kruskal.htm (last accessed on 7 March 2006). Pataki, Gabor (2003), "Teaching Integer Programming Formulations Using the Traveling Salesman Problem," SIAM Review, Vol. 45, No. 1, pp. 116123. Dantzig, G., Fulkerson, R., and Johnson, S. (1954), Solution of a Large-Scale Traveling-Salesman Problem, Operations Research, Vol. 2, p. 393. Ragsdale, Cliff (2004), Spreadsheet Modeling & Decision Analysis, 4th edition, Thomson-Southwestern, Mason, Ohio. Edmonds, J. (1965), "Maximum matching and a polyhedron with 0,1-vertices," J. Res. Nat. Bur. Stand. B, Vol. 69, pp. 125-130. Reinelt, Gerhard (1994), The Traveling Salesman: Computational Solutions for TSP Applications, Springer-Verlag, Berlin. Fourer, R., Gay, D.M., & Kernighan, B.W. (2003) AMPL: A Modeling Language for Mathematical Programming, Thomson Publishing, Pacific Grove, CA. Baker, S.F., Morton, D.P., Rosenthal, R.E., and Williams, L.M. (2002), "Optimizing Strategic Airlift," Operations Research, Vol. 50, July-August, pp. 582-602. Guignard, Monique, and Kim, Siwhan (1987), "Lagrangean Decomposition: a Model Yielding Stronger Lagrangean Bounds," Math Programming, Vol. 39, pp. 215-228. Skiena, Steven S. (1997), The Algorithm Design Manual, Springer-Verlag, New York. Held, Michael, and Karp, Richard M. (1970), "The Travelling-Salesman Problem and Minimum Spanning Trees," Operations Research, Vol. 18, No. 6, Nov-Dec, pp. 1138-1162. Winston, Wayne (2003), Operations Research: Applications and Algorithms, 4th edition, Duxbury Press, Belmont, CA. Held, Michael, and Karp, Richard M. (1971), "The Travelling-Salesman Problem and Minimum Spanning Trees: Part II," Math Programming, Vol. 1, pp. 6-25. Hillier, F. S. and G. J. Lieberman (2001), Introduction to Operations Research, 7th edition, McGraw-Hill, Boston, MA. Lawler, E.L., J.K. Lenstra, A.H.G. Rinnooy Kan, and D.B. Shmoys (1985), The Traveling Salesman Problem: a Guided Tour of Combinatorial Optimization, John Wiley & Sons, Chichester. Lawrence, John A., and Pasternack, Barry A. (1998), Applied Management Science, John Wiley and Sons, New York, NY. Martin, R. Kipp (1991), "Using Separation Algorithms to Generate Mixed Integer Model Reformulations," Operations Res. Letters, Vol. 10, pp. 119128. Nemhauser, George L, and Wolsey, Lawrence A. (1988), Integer and Combinatorial Optimization, John Wiley & Sons, Inc. Olinick, Eli (2006), available online at INFORMS Transactions on Education 7:1(37-69) 54 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP Appendix Help with AMPL and Scite This file gives help for AMPL, and is designed to work with the Scite editor. This web page is intended to be a help for students. The authoritative reference on AMPL is AMPL: A Modeling Language for Math Programming(179), by Robert Fourer, David M. Gay, and Brian W. Kernighan. The AMPL(180) web site has a wealth of information, such as AMPL: A Mathematical Programming Language(181). From www.ampl.com: "AMPL is a comprehensive and powerful algebraic modeling language for linear and nonlinear optimization problems, in discrete or continuous variables. Developed at Bell Laboratories, AMPL lets you use common notation and familiar concepts to formulate optimization models and examine solutions, while the computer manages communication with an appropriate solver. AMPL's flexibility and convenience render it ideal for rapid prototyping and model development, while its speed and control options make it an especially efficient choice for repeated production runs." In Scite, if you put the cursor on an AMPL key word, such as "option", "display", or "asinh", then press F1, this file should open to show you some help on that key word. This help file is not complete, but should contain the most common AMPL commands. Now, see here, if you see something in "quotes;", which you are supposed to type into AMPL, you know, deep down inside, that you shouldn't type the quotes into AMPL. But don't forget the semicolon. Getting started with AMPL Generally you should have three files, a model file, a data file, and a run file. A model file contains the AMPL formulation for the model (and is often given the extension ".mod"), with its sets, parameters, variables, objective function, and constraints. This should be independent of the data. For example, if you want to solve a diet model with 15 foods, the model file would store a named parameter like "param N;" for the number of foods. The actual number "param N := 16;" is typically stored in the data file. A data file contains the data for one instance of the model (and is sometimes given the extension ".dat" or ".txt"). For example, you may want to solve 5 different instances of the travelling salesman problem, each with a different number of cities. You simply put each instance in a different data file. A run file contains a list of commands for AMPL to follow in solving the model (and is often given the extension ".run"). The run file tells AMPL which model file and which data file to use. The run files are used for your convenience, to avoid the need to type the same commands over and over into AMPL. You can also put in other commands to have AMPL display the solution. You can even develop algorithms with AMPL's command language. (179) http://www.amazon.com/gp/product/0534378951/qid=1142410713/sr=8-3/ref=sr_8_xs_ap_i3_xgl14/104-4898613- 0507106?n=507846&s=books&v=glance (180) http://www.ampl.com/ (181) http://www.ampl.com/REFS/amplmod.pdf INFORMS Transactions on Education 7:1(37-69) 55 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP Using AMPL with the Scite editor, the best way to get AMPL to solve is to create a run file, typically with just a few lines, a line for the model, a line for the data, and the solve command. Example: model diet.mod; data diet.dat; solve; display Buy; This script tells AMPL to use the model described in diet.mod with the data in diet.dat, to solve the problem, and then to show the values for the variable Buy. Now suppose you put these three lines in a file named "diet.run". With this file displayed in Scite, just press the F5 function button on your keyboard, and AMPL will run in a panel at the right. Scite does this by calling AMPL at the operating system command level, with "AMPL diet.run". You can see it in the Scite output panel, as shown below INFORMS Transactions on Education 7:1(37-69) 56 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP Alternatively, you can use a run file by starting up AMPL yourself, and get the AMPL command prompt. Then type "commands diet.run;", and AMPL will attempt to run the commands that are in that run file. Yet another way to use the commands statement is within a script, to call another file of AMPL commands, similar to a subroutine. Example: "commands makesvg.run;" which might be part of a TSP script to output a visualization of the solution. AMPL doesn't care about the file names or extensions. In fact, you could put everything into one file - model, data, and the "solve;" statement at the bottom. But putting the model, data, and commands into separate files will make understanding and communcating easier. Also, the Scite editor is configured to recognize the ".mod" and ".run" file extensions. Basic syntax AMPL is case sensitive! That means that "param y;" and "param Y;" are two different parameters. AMPL ignores white space, so "param y;" is the same as "paramy;" End statements with semicolons, ";". Watch your parentheses. Scite will highlight matching parentheses and brackets. You can comment out to the end of a line with a # comment. Scite should show it green. You can comment out a block with /* comment. Scite should show it green.*/ AMPL treats "=" and ":=" very differently, and it's important to understand when to use each of these. • Use "=" with "subject to" to define an equality constraint. Example: "subject to Budget: x + y = 100;" • Use "=" when specifing parts of sets. Example: "subject to target_level{(c,v) in ROADS: c = 'Do_nothing'} sum{m in MAINT} Area[c,v,m] < = initial[c,v];". This tells AMPL to define the constraint target_level for all pairs (c,v), where c='Do_nothing'. • Use ":=" when specifying data. Example: "param N := 16;". This assigns the value 16 to the parameter N. • Use ":=" when specifying parameters or variables in a script with the "let" key word. Example: as part of a longer AMPL script, you might write "let N := 16;". INFORMS Transactions on Education 7:1(37-69) 57 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP Some important AMPL key words The model keyword is used, at the AMPL command prompt or in a run file, to tell AMPL to use a file with a particular model in it. For example, "model diet.mod;" (at the AMPL command prompt or in your run file) tells AMPL that you intend to use the model in the file diet.mod. Frequently followed by a "data" command. The data keyword is used to tell AMPL to use some data. The command can be used at least three different ways. • First, you can put "data;" at the end of your model file, and follow it with the actual data you want to use, in the same file. This puts your model and data in the same file, which isn't so clever, but which does make the file compatible with the open-source Gnu LP Kit(182) • Second, you can type "data diet.dat;" (if you have a diet model) at the AMPL command line, usually right after the "model" command. This will tell AMPL that your diet data is in the file diet.dat. • Third, you can type "data diet.dat;" in your run file, again usually right after the "model" command, as in the "run file" discussion above. Again, this will tell AMPL that your diet data is in the file diet.dat. The set keyword is used to tell AMPL that you have a bunch of related objects, such as cities, nodes, arcs, etc. You use sets most often in a model file. Once you have defined some sets, you can use the sets to define parameters and variables. See also the set-oriented functions below. Example: to define a set of cities for a TSP problem in your model file, you might write "set Cities := {1..20};". But it's better to put the "20" in a data file, so you can change the number of cities without changing the model file, like this: "param N; set Cities := {1..N};". Now you can define arcs between the cities as "set Arcs := {i in Cities, j in Cities: i < > j};". The ": i < > j" is translated as "such that i not equal to j". If you have a Euclidean TSP, you could write ": i < j", because the cost from i to j is the same as the cost from j to i. The param keyword is used to define a parameter that is used for data. It is not a decision variable, and will not be calculated by the solver (e.g. CPlex or MINOS). Rather, a parameter is data that you give the model. Example: "param N;" defines a parameter named N. Example: "param Y > = 1 default 5;" tells AMPL to define a parameter named Y with a value of 5, and that Y must always be bigger than or equal to 1. If you do not use the "default" key word, you should assign it a value somewhere (e.g. in a data or run file). Otherwise AMPL may complain that Y has not been assigned a value. Use the > = to make sure that your data is reasonable. If your script or data assigns it a data that violates this, AMPL will let you know. If you assign the value to the parameter at the same time that you define the parameter (rather than in a data section, or with a "let" command), AMPL will give an error if you try to change the parameter elsewhere in your model or script. So specifying a parameter's value when the parameter is defined makes it permanent. To avoid your own confusion, consider using 2 or more characters in every parameter, and consider using just one character in set indices. Example: you define "param i default 0;" and later write "let y := sum {i in Foods} cost[i]*Buy[i];". This is bad, because AMPL will use the parameter i which is probably fixed to 0, when you really want i to be an ordinary counter. You should make your parameter names a bit more descriptive, anyway! You do not have to define a parameter for an index, such as the i or j in "minimize Tourlength: sum {(i,j) in Arcs} length[i,j] * x[i,j];" (182) http://www.gnu.org/software/glpk/glpk.html INFORMS Transactions on Education 7:1(37-69) 58 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP The var keyword is used to define a variable that will be calculated by the solver (e.g. CPlex or MINOS). A variable is not data that you give the model. Rather, it is represents a decision or value that you want to know, and are expecting AMPL to tell you when it has finished solving. Example: "var w > =0;" tells AMPL to define a non-negative decision variable named w. Example: "var x > =-4 < = 5 integer;" tells AMPL to define a decision variable named x, with a lower bound of -4, an upper bound of 5, and that x must be integer. In place of "integer", you can use the key word "binary" to restrict the variable to 0 or 1. The minimize key word is used to declare a minimizing objective function. You must name your objective function. Example: "minimize Pain: z;". AMPL can solve over only one objective at a time, so your mod file will usually have one objective. If you have a complex algorithm, where you want to change the objective during the algorithm, you may wish to use the problem keyword, to have a named model. The maximize key word is used to declare a maximizing objective function. You must name your objective function. Example: "maximize Happiness: z;". Don't confuse the "minimize" or "maximize" keywords with the "min()" or "max()" functions. The subject to or s.t. key phrase is used to declare a constraint. You must name each constraint. Example: "subject to Limit: x+y < = 5;" or equivalently, "s.t. Limit: x+y < = 5;". Often, you will want to declare a constraint over a set, and you specify the set as part of the constraint name. For example, you may want to require that each node have degree 2 in the travelling salesman problem: "subject to Degrees {i in Nodes}: sum {(i,j) in Arcs} x[i,j] + sum {(j,i) in Arcs} x[j,i] = 2;" In this case, note that the constraint is defined over i in Nodes, which is included in the specification for the sum{(j,i) in Arcs}. Cool, huh? The solve key word tells AMPL to solve the model, and calculate the optimal values of the decision variables. The best way to get AMPL to solve is to create a run file, typically with just 3 lines, a line for the model, a line for the data, and the solve command. See "run file" above. If you press F5 in Scite on a model, you are likely to get all variables zero, because you haven't given AMPL the solve command first. So press F5 in the run file, not the mod file. Commonly used commands The display command tells AMPL to show the value of a parameter or variable. Example: "display x;" shows the value for the variable x. If x is a vector (e.g. was defined over a set), then AMPL will display all the values for x. Many of these will be zero, which isn't too interesting. If you first give AMPL the command "option omit_zero_cols 1;" AMPL will show only the nonzeroes. See "option omit_zero_cols 1;" below. Example: "display x;" shows the values for all components of the variable (vector) x. Example: "display x[1,2];" shows the value for the variable x[1,2]. You can also use "display" to show the reduced costs and dual prices of a linear program. (Remember, dual prices and reduced costs are not available in an integer program. The command will work, but the values should be considered rubbish.) Example: "display x.rc;" shows the reduced costs for the variable x. Example: "display Budget.dual;" shows the dual price for a constraint named Budget. INFORMS Transactions on Education 7:1(37-69) 59 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP The expand command tells AMPL to show an objective or constraint in long form. Example: suppose your model has the objective "minimize Tourlength: sum {(i,j) in Arcs} length[i,j] * x[i,j];". At the AMPL command prompt (or in your run file), you could type "expand Tourlength;". AMPL would then display something like this: minimize Tourlength: 210.117*x[1,2] + 1663.01*x[1,3] + 3845.68*x[1,4] + 2809.79*x[1,5] + 3455.81*x[1,6] + 1142.42*x[1,7] + 4307.28*x[1,8] + 2503.7*x[1,9] + ... + 857.03*x[16,19] + 611.945*x[16,20] + 3185.03*x[17,18] + 3379.88*x[17,19] + 4054.48*x[17,20] + 393.102*x[18,19] + 935.548*x[18,20] + 675.246*x[19,20]; Only it woudn't have the three dots, but would show the whole thing. Example: suppose your model has the objective "subject to Degrees {i in Nodes}: sum {(i,j) in Arcs} x[i,j] + sum {(j,i) in Arcs} x[j,i] = 2;". At the AMPL command prompt (or in your run file), you could type "expand Degrees[3];". AMPL would then display something like this: subject to Degrees[3]: x[1,3] + x[2,3] + x[3,4] + x[3,5] + x[3,6] + x[3,7] + x[3,8] + x[3,9] + x[3,10] + x[3,11] + x[3,12] + x[3,13] + x[3,14] + x[3,15] + x[3,16] + x[3,17] + x[3,18] + x[3,19] + x[3,20] = 2; If you typed "expand Degrees;", you would get all of the Degrees constraints. Common problems with running AMPL Problem: AMPL says Error executing "commands" command: is already defined. Solution: all the sets and variables from your previous run are still in memory. Just type "reset;" before running your model to clear all the previous information. Problem: AMPL says, syntax error. Solution: AMPL should point to the line with the problem. Check the statement closely. Many times, a semicolon is missing, a parenthesis or bracket is missing, or you have misspelled a term. Remember that AMPL is case sensitive. Problem: when you run AMPL in the program panel for Scite, AMPL says, error processing set ...: no value for ... or error processing param ...: no value for .... Solution: AMPL can't find your data, as it sees only the model file. Run AMPL from within a run file, not a model or data file. Problem: AMPL says that a parameter or variable is undefined. Solution: you are using a parameter, variable, or subscript that AMPL doesn't understand. Example: "minimize Tourlength: sum {(i,k) in Arcs} length[i,j] * x[i,j];" results in "j is undefined", because the sum{} statement has subscripts i and k, but no j. This could be corrected either as "minimize Tourlength: sum {(i,j) in Arcs} length[i,j] * x[i,j];" or "minimize Tourlength: sum {(i,k) in Arcs} length[i,k] * x[i,k];" Commonly used options The "option" command is used at the AMPL command prompt, in a run file, or at the beginning of a model file, to give any of a variety of instructions to AMPL. These include which solver to use, how output is displayed, and precision of calculation: INFORMS Transactions on Education 7:1(37-69) 60 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP • Tell AMPL which solver to use: option solver cplex; # This tells AMPL to use the commercial version of CPlex. option solver cplexamp; # This tells AMPL to use the student version of CPlex. option solver minos; # This tells AMPL to use the non-linear solver MINOS. option solver your_ampl_compatible_solver_here; /* Perhaps you have another AMPL-compatible solver available, such as LP_Solve(183) or IPOPT(184). • Show or omit the variables that are zero: option omit_zero_cols 1; # The default is 0, which shows everything. option omit_zero_rows 1; • Solve an IP as its continuous LP relaxation. Zero is the default, so if you don't specify anything, models with variables defined as integer or binary are naturally solved as IPs. option relax_integrality 0; # Solve the integer program. option relax_integrality 1; # Solve the LP relaxation. • Show output as a wide table rather than a table table. By default, "display" tries to make a table tall rather than wide, e.g. xxx xxx xxx xxx option display_transpose 0; # This is the default setting. A wide table with 2 rows and 4 columns will be transposed. option display_transpose 1000; # This makes tables come out tall, and transposes if needed. option display_transpose -1000; # This makes tables come out wide, and transposes if needed. In general, if (number of rows) - (number of columns) < the display_transpose value, the table will be transposed. • option show_stats 1; # Tells AMPL to show the number of variables and constraints in the model. • option presolve_eps 1e-9; # Tells AMPL to use a precision of 10-9 when running its presolve. Formatting data and data files Getting data formatted properly to satisfy AMPL can take some patience. In general, do it by following examples. Most often, you will want to define one, two, or three dimensional lists of parameters. We'll use the travelling salesman problem as an example, with N nodes. Assume our model file has the following declarations: param N; # Number of nodes set Nodes := {1..N}; set Arcs := {i in Nodes, j in Nodes: i < j}; # Euclidean TSP. (i,j) same as (j,i), so don't use both. param xcoord {Nodes}; param ycoord {Nodes}; param length {(i,j) in Arcs} := sqrt((xcoord[i] - xcoord[j])^2 + (ycoord[i] - ycoord[j])^2); # Euclidean. Then we need to declare N, xcoord, and ycoord in a data file. Our data file might look like this: (183) http://groups.yahoo.com/group/lp_solve/ (184) http://www.coin-or.org/Ipopt/documentation/node21.html INFORMS Transactions on Education 7:1(37-69) 61 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP Note that both xcoord and ycoord are defined over the list of Nodes, 1..10. If we wish, we can use a more concise format, as below. Note the extra colon after the word "param". If we don't care about x,y coordinates, but instead want to give a list of lengths, then the model and data may look like this: param N; # Number of nodes set Nodes := {1..N}; set Arcs := {i in Nodes, j in Nodes: i < j}; # Euclidean TSP. (i,j) same as (j,i), so don't use both. param length {(i,j) in Arcs}; Then we need to declare N and length in a data file. Watch your colons! The dots are placeholders, that tell AMPL that this value is undefined. INFORMS Transactions on Education 7:1(37-69) 62 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP Now, if you wanted to have a 10,000 city TSP, the length data would be unwieldy in this format. If you wish, you can put data in arc format, listing from-node, to-node, and length, like this: Scripts and algorithms For more information about AMPL's scripting, see: Writing scripts in the AMPL command language(185), Implementing algorithms through AMPL scripts(186), Index to examples, looping and testing 1(187). Index to examples, looping and testing 2 (188). The let key word is most often used in a script to assign a value to a parameter. Example: "let N := 16;". You can also use "let" to assign a value to a variable, but usually we want the solver to assign values to variables. for, repeat, while, break, if then else, printf Named models: problem. File input and output (185) http://www.ampl.com/NEW/loop1.html (186) http://www.ampl.com/NEW/loop2.html (187) http://www.ampl.com/NEW/LOOP1/index.html (188) http://www.ampl.com/NEW/LOOP2/index.html INFORMS Transactions on Education 7:1(37-69) 63 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP Piping. There is no piping command, but rather you can use the " < " and " > " symbols in a computer trick to temporarily connect a file to be input to a program, or output from a program to become a file. Example: "display x > myoutput.txt;" sends the current value of x to the file myoutput.txt. See also the "close", "read" and "printf"commands. The read command can be used to read data from a file. Example: "read N < TSPinput.txt;", where you have a file called "TSPinput.txt;" with one number, and you want AMPL to read the number from the file and put it into a parameter named N. Example: "read N < TSPinput.txt; close TSPinput.txt;" Note the "close" command &em;. See also piping below. The close command tells AMPL to close the file. This releases the operating system lock on the file, so the file can be used by another program, such as AMPL's "read" command. It's always a good idea to close a file after writing to it or reading from it. Trying to read a file that hasn't been closed may give the error "unexpected end of file". You can get away without closing files most of the time, because AMPL will sometimes close the file automatically (for example, if you exit from AMPL). But if your script writes to a file, then later reads from the file, you will have to close it immediately after opening it. You can use the "read" command to make AMPL slightly interactive, so it asks you for a value. Use a minus sign for the file, and AMPL will read from the keyboard, waiting for you to type. Example: printf "How many nodes are in this TSP problem?\n"; read N < - ; Some commonly used AMPL functions For help with strings, see Character Strings on AMPL.com(189). Set oriented functions card(set) Returns the number of elements in the set. Example: "let N := card(Nodes);", which sets the parameter N to be the number elements in the set Nodes. diff Set subtraction. Example: "let NotInTree := Nodes diff {v, u};" assigns the set NotInTree to be the same as set Nodes, but leaving out elements v and u. first(set) Returns the first element in the set. in This operator specifies that the left hand side is contained in the right. Example: "subject to Degrees {i in Nodes}: sum {(i,j) in Arcs} x[i,j] + sum {(j,i) in Arcs} x[j,i] = 2;". This tells AMPL that the constraint is defined over all elements i that are contained in the set Nodes. A inter B returns the intersection of A and B, which is the set of elements that are in both A and B. last(set) Returns the last element in the set. member(j, set) Returns the element in position j of the set. (189) http://www.ampl.com/NEW/strings.html INFORMS Transactions on Education 7:1(37-69) 64 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP The max and min functions may be used over scalars () or over sets {}. Don't confuse these wee functions with the minimize and maximize key words, which are used to define objective functions. max(x,y[,z,...]) Returns the largest of the arguments. Example: "let biggest := max(x[a,b], y[a,b]);" which assigns to biggest the largest of two values. min(x,y[,z,...]) Returns the smallest of the arguments. Example: "smallest_arc := min {(i,j) in Arcs} length[i,j];" which scans the set of Arcs, and assigns to smallest_arc the length of the smallest arc. next(t, S, n) Returns the element that is n positions after element t in the set S. ord(t, S) Returns the position of element t in the set. ord0(t, S) Returns the position of element t in the set, and returns 0 if t is not in the set S. prev(t, S, n) Returns the element that is n position before element t in the set S. union This operator does set addition. Example: "let Nodes := Nodes union {i,j};" adds elements i and j to set Nodes Note the curly brackets, which tell AMPL that i and j are set objects. If you add sets with the same elements, the resulting set will not have copies. within This operator specifies that one set is contained in another. Example: "set Destinations within Cities;" tells AMPL that each element in the set Destinations is also in the set Cities. If your data or script does not satisfy this condition, then AMPL will let you know. Numerical functions abs(x) Returns the absolute value of x. Example: "let y := abs(-3);" will set y to 3. acos(x) acosh(x) asin(x) asinh(x) atanh(x) ceil(x) Rounds x up to the nearest higher integer. cos(x) Returns the cosine of x. ctime() Prints the current time as a string. Try using "printf "Start time %s.\n", ctime();" exp(x) Returns e^x. floor(x) Rounds x down to the nearest lower integer. log(x) Natural log of x. INFORMS Transactions on Education 7:1(37-69) 65 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP log10(x) Log base 10 of x. a mod b Returns the remainder of a/b. Example: "let c := 17 mod 5;" sets c to 2. sin(x) Returns the sine of x. sqrt(x)Returns x^0.5. tan(x) Returns the tangent of x. tanh(x) Returns tanh of x. Uniform01() Returns a pseudo-random number, uniformly distributed between 0 and 1. Example: "let xcoord := 1000*Uniform01();". Often used in a script, to assign random numbers to parameters. Frequently asked questions. See also the AMPL FAQ page(190) The solver runs very slowly, or the numerical result is inaccurate. What could be wrong? You may be using the Minos solver, which is used by default. You can use CPlex instead by adding this command to your run file: option solver cplex; If you are using the commercial (non-student) version of CPlex, you should use: option solver cplexamp; How do I get AMPL to display just the nonzero values of a variable x? At the AMPL prompt, type: ampl: option omit_zero_rows 1, omit_zero_cols 1; ampl: display x; How can I view the reduced cost of a variable in AMPL? To view the reduced cost in AMPL, type "display x.rc;", for all variables named x. To view the reduced cost for a single variable, such as x[5,7], type "display x[5,7].rc;" How do I get AMPL to take a .RUN file, such as CUT.RUN, from the AMPL command prompt? Type "commands cut.run;" at the ampl command prompt: ampl: commands cut.run; Don't forget the semicolon. Is there a function in AMPL to print the output to a file, or some way of viewing all that the program displays? Yes, this is easy. You need to use "piping." At the DOS prompt, you would usually type, D:\STUFF\AMPL\ampl cut.run Instead, add on the piping character " > " and a file name. D:\STUFF\AMPL\ampl cut.run > myoutput.txt When you run it, nothing will be displayed on screen. Instead, all the screen display will be sent to the file. (190) http://www.ampl.com/FAQ/ INFORMS Transactions on Education 7:1(37-69) 66 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP You can also send output from the display command to a file. Inside your .RUN file, you could type: display x > myoutput.txt; This will put the x values in the file myoutput.txt. When I tried to use piping to save the output, I typed in ampl: commands part2.run > output.txt but this did not work. I opened the DOS prompt and typed in c:\ > p:\msci315\ampl part2.run > output.txt The AMPL code ran, but the file output.txt contains the error message "can not open part2.run". How can I solve this problem? If AMPL cannot open part2.run, then the problem is that the file part2.run is not in the local directory c:\ > , displayed when you typed the MS-DOS command. You have three files: the AMPL executable (which is in p:\msci315\, the part2.run file (which the computer thinks is in c:\ > ) and output.txt, which the computer will try to write to c:\ > . Try being precise about where files are read from, and where they should be written to, such as: p:\msci315\ampl a:\part2.run > a:\output.txt Instead of the A: drive, you can use any drive and directory for which you have read and write access. When I tried to use piping to save the output, I typed in ampl: commands part2.run > output.txt but this did not work. I opened the DOS prompt and typed in c:\ > p:\msci315\ampl part2.run > output.txt The AMPL code did not run. I got the error message, "Access is denied." What's wrong? "p:\msci315\ampl part2.run > output.txt" did not work because you do not have write access to p:\msci315\. Try "p:\msci315\ampl part2.run > a:output.txt" with a disk in the A: drive, or try refer to a directory to which you have write access. When I run the cutting stock problem, AMPL complains that my parameter fails an integrality check. What is wrong? AMPL is probably using MINOS to solve the problem. MINOS does not solve integer programs. You need to have AMPL call CPlex instead. To make AMPL call CPlex, add the following line at the beginning of your .RUN file: option solver cplex; What does the following line mean? I understand it is doing primal retrieval, but I don't understand the %i and \n. printf "x[%i] = %g\n", i, sum {k in 1..K} theta[k]*x_fxd[k,i]; The printf statement prints a string to the screen. For example, printf "hello"; does the obvious. printf "hello"; printf "hello"; will show INFORMS Transactions on Education 7:1(37-69) 67 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP hellohello on the screen. To get it to do a carriage return between "hellohello", we need to use printf "hello\n"; printf "hello\n"; The "\n" tells AMPL to insert a carriage return. We would also like to print data in the statement sometimes. If we want to print an integer such as 2005, we could use %i as a place-holder, to indicate where we want the data to go: let year := 2005; printf "The year is %i.\n", year; AMPL should print The year is 2005. If we want to print a floating point number, such as the number pi, we can use %f or %g as a place-holder to indicate where the data should go. let my_pi := 3.1415; printf "Today, I feel like my_pi = %g.\n", my_pi; AMPL should print Today, I feel like my_pi = 3.1415. If you try to use %i for a floating point number or vice versa, AMPL is likely to print rubbish. When solving the decomposition, the master and subproblem don't converge, and so the algorithm keeps repeating. What could be wrong? Check the objective value calculations. You could always solve the original linear program, so you know what the correct value is. That would tell you which one is wrong (subproblem or master). Use the display command to show the values, so you can see them as the algorithm runs. This will help determine when an error occurs. How do I get AMPL to make an MPS file? Let's say your model is "yourmodel.mod" and your data is "yourdata.dat". Then at the AMPL prompt, you would type:ampl: model yourmodel.mod;ampl: data yourdata.dat;ampl: write myourmps; This will write an MPS file called "yourmps.mps" to the local directory. Unfortunately, all the row and column name information is lost. You can get AMPL to at least write out a file with a list of the names:ampl: model yourmodel.mod;ampl: data yourdata.dat;ampl: option auxfiles rc;ampl: write myourmps; Now you get two extra files, "yourmps.col" and "yourmps.row", which list the names of the columns and rows. You would have to match up the numbers with the names. INFORMS Transactions on Education 7:1(37-69) 68 © INFORMS ISSN: 1532-0545 LEE & RAFFENSPERGER Using AMPL for teaching the TSP Experimental wizard You may like to try the experimental AMPL Wizard(191), intended to work with Scite. Created by John F. Raffensperger(john.raffensperger@canterbury.ac.nz), 15 March 2006. AMPL is a registered trademark of AMPL Optimization LLC. (191) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/Scite/amplwizard.hta INFORMS Transactions on Education 7:1(37-69) 69 © INFORMS ISSN: 1532-0545