tsp problem in ampl

33
Using AMPL for teaching the TSP Jon Lee Ibm T.J. Watson Research Center P.O. Box 218, Yorktown Heights, NY 10598 USA [email protected] 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 presen- tations, 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. 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 re- searchers. Nothing humbles a graduate student so quickly as a brutally difficult problem, and the TSP is perhaps foremost in that category. 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 ap- proaches (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 ex- ams, and a convenient package of AMPL models and scripts. We have a special focus on visualization, to provide convenient ways for students to view and re- port their solutions. The TSP lends itself well to graphical output, and students find this immensely helpful for learning and for model and algorithm de- bugging. 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. 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 solu- tion to this dilemma is programming ability. Lecturers whose students have good programming skills have an advantage in teaching the TSP. Unfortunately, un- dergraduate students in business have a widespread lack of programming skills. Only recently has the University of Canterbury added a modest program- ming 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 program- ming skill puts a greater burden on the lecturer to ex- plain algorithms in more detail and to provide canned tools. © INFORMS ISSN: 1532-0545 37 INFORMS Transactions on Education 7:1(37-69) LEE & RAFFENSPERGER Using AMPL for teaching the TSP

Upload: ujjawalbhojawala

Post on 07-Feb-2016

307 views

Category:

Documents


7 download

DESCRIPTION

Traveling Salesman Problem Using AMPL

TRANSCRIPT

Page 1: TSP Problem in AMPL

Using AMPL for teaching the TSPJon Lee

Ibm T.J. Watson Research CenterP.O. Box 218, Yorktown Heights, NY 10598 USA

[email protected]

John F. RaffenspergerDept. of Management, Private Bag 4800

University of Canterbury, Christchurch, New [email protected]

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 presen-tations, and a convenient package of AMPL models and scripts. The AMPL files include different formulationsfor the TSP, its relaxations, scripts for its solution, and - particularly useful in class - scripts for visualization ofthose solutions using SVG. We have a special focus on visualization, to provide convenient ways for studentsto view and report their solutions. We observe that the TSP is such a classical O.R. problem, that it can play acentral role in an undergraduate course about integer programming.

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. Thoughit is very well studied, it continues to tantalize re-searchers. Nothing humbles a graduate student soquickly as a brutally difficult problem, and the TSP isperhaps foremost in that category.

The TSP leads into many other related areas of O.R.,including integer programming and cutting planes,approximation algorithms, Lagrangean relaxation andthe subgradient method, and various heuristic ap-proaches (such as greedy and k-opt). The TSP and itsvariations, such as multiple vehicles, time windows,tour length constraints, have enormous real-worldapplicability. Because of its importance, its ease indescription, its many applications, and its historicalinspiration for countless algorithms, the TSP willcontinue to be an important part of teaching integerprogramming and combinatorial optimization.

This paper's contributions include suggestions forpedagogical devices, homework assignments and ex-ams, and a convenient package of AMPL models andscripts. We have a special focus on visualization, to

provide convenient ways for students to view and re-port their solutions. The TSP lends itself well tographical output, and students find this immenselyhelpful for learning and for model and algorithm de-bugging. Furthermore, the lecturer can require thatstudents turn in a graphical print-out of the tour, thusallowing some determination of solution quality at aglance.

Computationally difficult problems are sometimes asource of pedagogical challenge. A model instancethat is too small does not give the right impression ofcomputational complexity to a student. On the otherhand, student versions of solvers are usually too smallto allow solution of large model instances. One solu-tion to this dilemma is programming ability. Lecturerswhose students have good programming skills havean advantage in teaching the TSP. Unfortunately, un-dergraduate students in business have a widespreadlack of programming skills. Only recently has theUniversity of Canterbury added a modest program-ming requirement for third-year courses in operationsresearch; it is still too early to see whether this newrequirement will pay off. The students' lack of program-ming skill puts a greater burden on the lecturer to ex-plain algorithms in more detail and to provide cannedtools.

© INFORMS ISSN: 1532-054537INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

About this document
This document has been generated from XSL (Extensible Stylesheet Language) source with RenderX XEP Formatter, version 3.8.4 Client Academic. For more information about XSL, visit the official World Wide Web Consortium XSL homepage: http://www.w3.org/Style/XSL For more information about RenderX and XEP, visit the RenderX site: http://www.renderx.com
Page 2: TSP Problem in AMPL

Because it does not require programming and becausemany students already have some familiarity with it,the spreadsheet bears some consideration, especiallyto 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 30-city TSP, starting with the assignment problem, andthen manually add subtour-breaking constraints andbinary integrality. This exercise is extremely effectivein conveying the problem's difficulty as well as hintingat some of the key elements that can be effectivelyemployed to solve large instances.

Unfortunately, the spreadsheet has several drawbacksfor solving the TSP, and for teaching students aboutit. The spreadsheet does not scale to larger probleminstances easily. Creating algorithms (such as addingsubtour-breaking constraints) is unnatural, awkward,and time consuming, so the spreadsheet does not lenditself to exploring advanced techniques of integerprogramming. And Excel has no convenient tools todisplay network graphs (not to be confused withcharts), as each arc must be a new series. We havespent some time coding Visual Basic macros to pro-duce visualizations within Excel, only to find that thesemacros are difficult to write, clumsy, and unsatisfacto-ry. In contrast to spreadsheets, algebraic modelinglanguages provide an easy means for making use ofinteger linear programming methods for combinatorialoptimization problems.

We have chosen to use the proprietary modeling lan-guage AMPL, which has scripting capability. Usefulalternatives to AMPL(2) include FLOPC++ (3) (an opensource algebraic modeling language implemented as

a C++ class library), GAMS(4) , Maple(5), Mathematica(6)

, the optimization tools in Matlab(7), ZIMPL(8) (opensource), and, of course, the standard programminglanguages. We have chosen AMPL primarily for itsscripting ability, but also for its low cost, its easy linksto many solvers (including open-source solvers suchas Cbc(9)), and its popularity.

Perhaps the most interesting AMPL alternative is theopen-source Gnu LP Kit(10), with the useful LP_SolveIDE(11) (see also Introduction to lp_solve 5.5.0.9(12)). Infact, these are not really alternatives, but overlappingprograms, because AMPL can call LP_Solve(13), andbecause the Gnu LP Kit language is a subset of AMPL.Though LP_Solve IDE cannot run AMPL scripts, it hasa surprisingly nice interface and can solve many AMPLmodels. LP_Solve IDE has the remarkable ability toconvert models into different formats (e.g. it can con-vert MPS(14) to LP-FML(15), among others).

For editing AMPL, one alternative is AMPL Studio(16)

, which we have not tested. Some popular text editorshave basic syntax highlighting, and this is the path wehave taken, partly for its low cost. In this paper, wegive directions and help files for using AMPL with theopen-source editor Scite(17) .

While using AMPL feels like programming to students,many AMPL models do not require scripts, and theprogramming overhead associated with AMPL scriptsis less than with Visual Basic or Java. We have foundthat when students are given some reasonable initialhelp in AMPL in a workshop, including some scriptsto get them started, the programming demands aremodest. We have no empirical data regarding the ef-

(1) http://www.lindo.com/(2) 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/

© INFORMS ISSN: 1532-054538INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 3: TSP Problem in AMPL

fectiveness of our materials. However, based on stu-dents' look of relief when they get their first AMPLmodel working, the speed with which they can get thebasic algorithms going, and on the overall quality oftheir work, the workshop approach is clearly positiveand worthwhile. Students who cannot write AMPLscripts can modify them and learn from that, whilelearning about the TSP, and thereby eventually learnto write their own AMPL models and scripts. Ofcourse, AMPL models are the first step, but this natu-rally leads to the harder step of writing AMPL scripts,and this can potentally lead to real programming.

The AMPL code that we provide does not usually in-corporate theoretically-efficient algorithms. Except forthe minimum spanning tree and the 1-tree, the sub-problems are modeled as linear or integer linear pro-grams, and AMPL then accesses a general linear pro-gramming or integer linear programming code.

The outline of this paper is as follows. First, we discussother teaching materials for the TSP. Second, we givea syllabus, with suggested software, lectures and tuto-rials, a homework assignment, exam questions, anddescribe the relevant AMPL scripts and data. Third,we give some further ideas to expand the materialbeyond what we currently teach, along with additionalAMPL models and scripts. We conclude with a listingof internet resources for the TSP.

2. Literature review

Lawler et al (1985) and Reinelt (1994) are, of course,excellent texts on the TSP. These books thoroughlycover branch and bound, cutting planes, subgradientoptimization, a thorough review of different relax-ations, and many important heuristics. Similarly,Nemhauser & Wolsey (1988) has quite rich materialon the TSP, and encapsulates it well. Such textbooksare too advanced for our students in business, who donot have advanced skills in math or computer program-ming.

Several classic textbooks that are currently used at theundergraduate level either do not mention the TSP atall, or contain a shallow introduction to the subject.To mention a few, Dantzig's (1963) classic text on linearprogramming gives three different formulations over

a couple pages. Wagner's (1975) classic text gives abranch and bound algorithm. Hillier & Lieberman(2001) has, surprisingly, nothing on the TSP. Ragsdale(2004) gives one example with the heuristic in FrontlineSystem's Premium Solver for Excel. Lawrence &Pasternack (1998) give a similarly brief example, witha branch and bound algorithm in a supplement on aCD-ROM. Winston's (2004) discussion of the TSP in-cludes a dynamic program (aimed at very small in-stances), a branch and bound example, a brief discus-sion about heuristics, and a loose integer programmingformulation. We currently use Winston's text, but lib-erally add to the material.

We feel that the TSP is such a classical problem, per-haps the classical O.R. integer program, that it can playa central role in an undergraduate course about integerprogramming, even in a school of business. Yet theundergraduate textbooks do not seem to do it justice.We have found just one article written specifically forteaching the TSP to undergraduates. Pataki's (2003)excellent paper covers the relative tightness of differentformulations, using Matlab.

Specifically for AMPL, Fourer, Gay & Kernighan (2003)is excellent and authoritative, but much more thanstudents need for one course; by all means the bookshould be available to students as a reference.

3. TSP Syllabus

Here, we describe the software required and give ourcourse material on the TSP. The course material iscomprised of three lectures, two tutorials, homeworkproblems, and exam problems.

3.1. Software required

The student version of AMPL with CPLEX may bedownloaded from AMPL website(18). In fact, we inten-tionally avoid making a more powerful solver availableto students, as we have found that the homework ismore compelling with the limited version. Studentsare still free to try to find a larger solver on their own,but few ever do so.

(18) http://www.ampl.com/

© INFORMS ISSN: 1532-054539INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 4: TSP Problem in AMPL

If you wish to solve models, but not use scripts, wealso suggest the LP_Solve IDE(19). It has a nice interfaceand can read a subset of AMPL's language. The solveris not limited in the number of variables or constraints,but is much slower than CPLEX.

SVG viewer. To see the graphical output from thescripts, you should install an SVG viewer, and wehighly recommend this approach. You can get a freeviewer from Adobe.com(20). The graphical output isdisplayed in scalable vector graphics (SVG)(21). Fromthe website of the World Wide Web Consortium, "SVGis a language for describing two-dimensional graphicsand graphical applications in XML." Almost all webbrowsers can display SVG natively or with an add-in.The file format is plain text, and easy to create with ascript. For example, here is the minimum spanningtree for 1,000 random points(22) on the Euclidean plane,as intermediate output from the Christofides heuristic.

Scite. For editing AMPL files, we prefer the open-source cross-platform Scite, downloadable from theScintilla website(23). This editor is highly configurablefor various languages via properties files. We discov-ered a rudimentary AMPL properties file for Scite inItalian(24), and have modified and extended it extensive-ly (ampl.api(25), ampl.properties(26), SciTEGlobal.properties(27)).

• Scite shows syntax coloring, block indenting, blockcommenting, and section folding.

• Scite can recognize AMPL keywords for statementcompletion with the tab key.

• Scite displays in-line pop-up help for AMPLfunctions such as "first(x)".

• With our attached amplhelp.html(28) file, Sciteprovides context-sensitive help for AMPL.

• The user can start AMPL with the current run file,simply by pressing the F5 key. AMPL runs in aside panel.

• Scite allows infinite undo, and many other features.

More details on installing the files for Scite are provid-ed in section 4.2.

The amplhelp.html(29) file has roughly eight pages ofbeginner-level and reference material for AMPL. Itincludes a section on getting started with AMPL, basicsyntax, descriptions of some important AMPL wordsand commands, commonly used options and functions,and a list of frequently-asked questions. We recom-mend that the lecturer using these materials modifythis help file for the local requirements as needed.

The included AMPL models and scripts. These areorganized in their own directories, with a separatedirectory for data. This encourages students to thinkabout modeling in an object-oriented way, to keep themodel as generic as possible, and separate from thedata.

The scripts and models have, for the most part, uni-form 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, eventhough some will not produce tours. This uniformityhas a couple of advantages, the primary one being thatwe can use a single script to produce the SVG outputgraph, and a secondary advantage being that studentsbecome accustomed to the notation more quickly.

3.2. Lectures and tutorials

Lecture 1. Description & history of the TSP. Someformulations.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 ISSN: 1532-054540INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 5: TSP Problem in AMPL

In the first lecture, we first describe the problem assimply as possible: drive a car via n cities, minimizingtravel distance. We then follow this with some history,especially the seminal Dantzig, Fulkerson & Johnson(1954) paper and formulation, with its key ideas ofbranch and bound and IP cuts. We discuss the difficul-ty of the TSP, touching on complexity theory. We alsomention Concorde (Applegate, Bixby, Chvatal & Cook1998), so students realize how far the research hascome.

Following this introduction, the remainder of the lec-ture covers different formulations related to the TSP,most with their AMPL formulations and solutions.

(Note: if the files do not open on click, consider settingthe ".mod" and ".run" file extensions to open with yourfavorite text editor.)

• the assignment problem (assignment.mod(31), as-signment.run(32)),

• the Miller-Tucker-Zemlin model (MTZ.mod(33),MTZ.run(34)),

• the symmetric TSP,

• the Svestka flow formulation (Svestka.mod(35),Svestka.run(36) ),

• and the Danztig steps formulation with O(n3)variables (Dantzig-steps.mod(37), Dantzig-steps.run(38)).

The algebraic formulations for these are in the Power-Point presentation, and will not be repeated here.Generally, we show the algebraic form and the AMPLmodel directly in the slides (which are printed andgiven to students), so the AMPL formulation itself isused in the lecture. This gets students accustomed toseeing AMPL formulations, which helps them whenit comes time to write their own. It allows them to askquestions in class about AMPL syntax. Further, if theywish, they can copy the examples straight from the

lecture notes for testing. In some cases, we also showthe expanded model for further clarification.

Throughout, we follow a careful format for models,first giving the indices, the parameters, the decisionvariables, the model, and an explanation of each con-straint set using "NPS format" (Brown 2004), due toits invention and use at the O.R. Department of theNaval Postgraduate School(39). (For a published exam-ple of NPS format, see Baker, Morton, Rosenthal, &Williams 2002.) To give students a mnemonic, we callthis IPDME format (i.e., Indices, Parameters, Decisionvariables, Model, Explanation of constraints). Thisformat has several advantages over a less formal ap-proach. The format requires the author to write outformulations in a methodical and uniform way, mak-ing sure that every term of the model is explained, andencouraging the use of a unique meaning for eachterm. The format gives the reader a convenient direc-tory of all symbols. The format leads naturally to analgebraic modeling language, as indices become sets,and the rest follows analogously. We encourage edu-cators and editors to standardize on this format.

For example, here is the classic Dantzig-Fulkerson-Johnson (DFJ) formulation, in IPDME format.

Indices: i, j city.

Parameters: n = number of cities; cij = cost to travelfrom city i to city j.

Decision variables: xij = 1 if the salesman should travelbetween 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. xij ∈ {0, 1} for all i, j:j>i.

(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 ISSN: 1532-054541INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 6: TSP Problem in AMPL

Explanation:

1. Minimize total distance traveled.

2. Each city must have degree 2, i.e., must be enteredand departed.

3. No subset of cities S consist of a tour, unless thatsubset has all the cities.

4. The salesman cannot split his travel between cities.

We write algebraic models in plain text (rather thanEquation Editor or images from LaTeX) using textfonts, subscripts, and superscripts carefully. This al-lows algebraic models to be copied more easily be-tween programs, animated in PowerPoint, and editedin HTML.

While familiar to the O.R. teacher and researcher, thesummations in model DFJ can be quite a challenge tostudents. We suggest taking a little time to explainhow the summations work, and explain that the verti-cal bars on the set, |S|, mean the cardinality of the set,like a spreadsheet's Count() function.

Part of understanding the model is understanding thesolution. Students sometimes have difficulty makingsense of the model until they understand the form ofthe decision variables. Thus, we use a uniform examplefor all formulations, and show the solution for eachformulation. A tour can be encoded in different ways,and it is easy to forget that the IP edge formulation isnot immediate for a newcomer. Without any guidance,a student may suggest making a solution vector besimply a permutation of the integers 1,...,n. It is instruc-tive to pursue this direction, exploring the difficultiesof trying to write down an IP formulation from thesevariables. The first issue that one encounters is thedifficulty of expressing the "all-different" constraintthat the values of each of the n variables should bedistinct. For example, for n=3, we see that 1,2,3 and3,2,1 are both valid solutions. Their midpoint 2,2,2 ison the convex hull and is therefore feasible in any LPformulation based on these variables. But it is infeasi-ble to the IP. Furthermore, it is not possible to formu-late the objective as a linear function in these variables,

as there are only n variables but O(n2) pairwise dis-tances. So it is a useful exercise in modeling, to workone's way to the inevitably fruitful idea of using binaryvariables to select edges for a tour.

Tutorial 1. Intro to AMPL, and two key relaxationsof the TSP.

The goal of this tutorial is to give students a quickhands-on success with AMPL, and to teach two keyrelaxations of the TSP, in a workshop approach. Pleasenote that we assume that the lecturer is familiar withAMPL. Some instructions for running these files aregiven here and further information is given in ampl-help.html(40) but the authoritative discussion on AMPLis Fourer, Gay & Kernighan (2003).

Before the tutorial starts:

• students should be aware of the requirements forHomework 1, so they are thinking about how tosolve it;

• the lab computers should have Scite, AMPL,CPLEX, and an SVG viewer installed.

The lab should have a chalkboard. At the beginningof the tutorial, the algebraic and the AMPL formula-tions for the assignment (assignment.mod(41), assign-ment.run(42)), and 2-matching problems (2match-ing.mod(43), 2matching.run(44)), are put on the board.Do not give the files on disk, but do give students thecomplete AMPL formulation either on paper or on thechalkboard. Remind students to end commands witha semicolon. By giving students an example straightoff, they will start to catch on to AMPL's syntax. Givethe model to students without integrality, and let stu-dents discover for themselves the need for integrality.

Students should copy the AMPL formulations into atext editor, creating their own files, and running AMPLas they are ready. By requiring students to type in twosimple models, they are forced to engage with AMPL,and make it work, at a time when they can get imme-diate 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 ISSN: 1532-054542INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 7: TSP Problem in AMPL

and data then begins to make sense, especially as theyrealize that changing from the tutorial example to thehomework set involves changing only the data file.This contrasts with the need to rewrite the entiremodel, as would be required with Lindo.

The models can be run either through the Scite inter-face or at the AMPL command prompt.

• To run a script in AMPL through the Scite inter-face, 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 youhave a model file open without a "solve" command,AMPL will read the model, but not solve it becauseit has not received the "solve" command.

• To run a script at the AMPL command prompt,click Start, Run, type ampl in the dialog and pressenter. This opens an AMPL command window.To run the 2matching.run script, at the "ampl:"prompt, type "run 2matching.run;", and press en-ter. In the command window, they will need toenter the reset command to clear AMPL's memory.("Reset" is not needed if they use Scite as the inter-face.) Remind them not to reset before using "dis-play" to see results. Explain that when they arefrequently rerunning a script, they may want toput "reset;" at the end of the script.

After students get the two models running, they aregiven access to the script (makesvg.run(45)) whichproduces the SVG graphical output, and shown howto call the script from their existing run files. This visu-alization gives them a powerful and immediate indi-cation as to whether their model is correct, and howit is related to the TSP. This would be the time to ex-plain the commands filename command, which is usedto give AMPL a list of commands from a file.

SVG can be viewed in a web browser with a free add-in (such as Adobe's SVG Viewer,http://www.adobe.com/svg). We have carefully writtenall models and scripts to have uniform variable andset definitions, so they all will work with makesvg.run.For algorithms such as the 1-tree subgradient optimiza-tion, the graph can be viewed as the algorithm is pro-

gressing, simply by refreshing the browser. Copyingthe image to a word processing program is convenient.The images are valuable for teaching, and are alsoconveniently copied to presentation software such asPowerPoint.

Given sufficient time, students may be given AMPLscripts for the minimum spanning tree (prims.run(46)

or kruskal.run(47), originally developed by Olinick(2006). Alternatively, the minimum spanning tree maybe covered in a later tutorial, after the material is pre-sented during the lecture.

The workshop approach has multiple benefits, as im-mediate help can be given to those students who havedifficulty starting and running AMPL. Points of syntaxcan be addressed immediately, and students get aquick positive result, with a problem that is easycomputationally. Key AMPL commands, such as "re-set", "display", and "expand", are explained and imme-diately put to use. Sometimes, students become suffi-ciently confident and enthused that they want to at-tempt the homework assignment immediately.

Provision of this one-hour workshop has alleviated ahost of problems in using AMPL. Students come upto speed much more quickly compared to simply beinggiven a sheet of instructions.

Lecture 2. Relaxations and solution methods. Power-Point presentation(48)

Following Lecture 1's introduction to TSP formula-tions, Lecture 2 introduces some solution methods.We first present the assignment problem with branchand bound, using an example from Winston (2003, pp.530-534), then give the 1-tree, with Prim's andKruskal's algorithms. This information is easy to ex-plain, and tends to be understood easily, partly be-cause of the highly graphical nature of the information,and as it is given just a few weeks following moregeneral IP cuts and branch and bound.

The final section of this lecture covers the Held & Karp1-tree subgradient optimization algorithm. Many stu-dents get completely thrown at this point, as the lecturehas 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 ISSN: 1532-054543INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 8: TSP Problem in AMPL

gorithm. Considerable patience is required. The sub-gradient optimization material is presented in moredetail in Tutorial 2.

Tutorial 2. Subgradient optimization. PowerPointpresentation(49)

In this tutorial, the subgradient optimization algorithmis presented in detail.

With sufficient time for a computer lab, students maybe given the relevant AMPL scripts, including thosefor Prim's (prims.run(50)), Kruskal's (kruskal.run(51) ,originally developed by Olinick (2006), and subgradi-ent optimization (HeldKarp.run(52)). Students shouldbe challenged to match up the steps of the algorithmfrom the notes with lines of code in AMPL.

These scripts do not use separate model files. Sincethese do not call the IP solver, AMPL's script processorcan solve larger spanning trees than would be solvablethrough calls to the student-version IP solver. Ofcourse, this depends on the commands employed. Ifyou modify the script and find that AMPL complainsabout the number of variables, try using parametersinstead of variables.

Lecture 3. More solution methods for the TSP. Pow-erPoint presentation(53)

The majority of Lecture 3 presents the DFJ subtour cutgeneration algorithm. We use a simple 6-city examplefrom Winston (2003), an example which producessubtours with the 2-matching relaxation. The max-flowalgorithm is then developed as a method to find sub-tours. The presentation shows the output from AMPLrunning, so students see the iterative nature of the al-gorithm.

The presentation does not cover all the various AMPLscripts we have for the DFJ model, but the lecturermay want to discuss them with students. These in-clude, in increasing order of sophistication:

• DFJ_all_subtours.mod(54) and DFJ_all_sub-tours.run(55) (modified from AMPL website(56) ).This script explicitly uses the model with all O(2n)subtour constraints. This is meant only for verysmall instances.

• DFJ-simple.mod(57) and DFJ-simple.run(58) . Thisscript starts with the model consisting of just thesimple bounds and the degree constraints. At eachstage the current integer linear programming for-mulation is solved, and then a single violatedsubtour inequality is appended to the formulation.The script stops when there is no violated subtourconstraint.

• DFJ.mod(59) and DFJ.run(60). This script also startswith the model consisting of just the simple boundsand the degree constraints. The scripts executes asequence of stages, repeating until a substage findsan optimal tour. At each stage we do the following:

1. Repeatedly solve the current linear program;identify a violated subtour constraints bysolving further (max-flow/min-cut) linearprograms, and append it to the current formu-lation; this sub-stage terminates when thereare no further violated subtour constraints; ifthe resulting solution is integer, then we havean optimal tour, and we terminate the script.

2. Solve the current formulation as an integer lin-ear program. Identify a violated subtour con-straint and append it to the current formula-tion. 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 ISSN: 1532-054544INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 9: TSP Problem in AMPL

nate the script; otherwise, we initiate a newstage.

DFJ_restricted_arcs.mod(61) and DFJ_restrict-ed_arcs.run(62). This script executes roughly asDFJ.mod/run, but initially the number of arcs isrestricted to the shorter ones. Then, as describedat the end of the PowerPoint presentation, newarcs are added if they might improve on the solu-tion of the linear programming relaxation. Arcswith sufficiently large reduced cost can be omittedfrom the model. In our experience, the 300-variablelimit in the student version of AMPL/CPLEX hasbeen sufficient to prove optimality for 50-cityproblems. Note that the restricted arcs versionscan be used to solve the homework directly, so thescripts probably should not be given to studentsbefore the homework is due.

The max-flow algorithm is then developed further intoa new formulation. Martin (1991) gave an LP formula-tion for the MST, which is best understood in thiscontext. From this formulation for the MST, we thendevelop 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 solvefor a minimum spanning tree, a 1-tree, or the TSP.

The lecture concludes with hints for the homework,which we describe next.

3.3. Homework.

We assign a 50-city problem, which is too big to solvedirectly in the student version of AMPL with CPLEX(65)

. (We have included a script which will generate adata file with random x,y coordinates, get_random_co-ordinates.run(66). This script calls another script,makesvg_points.run(67), which creates an SVG graphof the points. Alternatively, the lecturer can use aspreadsheet "Random_data_genera-

tor_for_TSP_50_points.xls(68)".) Students are requiredto give valid upper & lower bounds on the tour length,with a description of their procedures for finding thesebounds.

The use of the restricted student version of CPLEXrequires students to omit rows and variables. The lim-itation on the student solver should be used to empha-size that one's computer is never fast enough, and thatone's solver is never powerful enough. Real worldproblems can be extremely demanding, and the re-sourcefulness of the operations researcher is thereforeput to the test. This invites students to use creativity(e.g., learn to use the NEOS server (see 5.1 below) orother software, such as the open source COIN-OR Cbc,GNU LP Kit, or LP_Solve).

As mentioned above, the solution is to use DFJ_restrict-ed_arcs.mod(69) and DFJ_restricted_arcs.run(70), whichshould not be given to students before the homeworkis due.

The most common error is to claim optimality whenthey cannot. Students realize that they must followthe common practice of eliminating unlikely variables(e.g. long edges) from consideration, but then theymake claims about optimality that they should not.

Students may gain nearly full marks without an opti-mal solution, but their approach must be reasonable.Minimal nonzero marks would be given for a tourfound visually for the upper bound and an ordinary1-tree for the lower bound. Good answers should in-clude a good feasible solution, and at least a few itera-tions of the Held-Karp subgradient optimization algo-rithm. The lecturer can choose whether or not to givethe students our included script (HeldKarp.run(71)).

(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 ISSN: 1532-054545INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 10: TSP Problem in AMPL

3.4. Exam problems

3.4.1. A minimum spanning tree.

To write the problem:

1. Create a random problem. A 20-node problem isreasonable. Because new problems are easy togenerate, students cannot memorize a given solu-tion from a previous year's exam paper.

2. For this random problem, print the graph and thematrix of distances on the exam paper.

• Ask students to find the MST by hand, using Prim'sor Kruskal's algorithm.

• Ask the students to draw the solution on the exampaper, and show the order in which the arcs wereadded.

• To mark the question, find the correct solutionwith AMPL, and create the graph. Then simplycompare the student's answer to the correct an-swer.

3.4.2. One step of subgradient optimization.

To write the problem:

1. Create a random problem. A ten-node problem isreasonable.

2. Create the initial 1-tree with AMPL.

3. Print the 1-tree and the matrix of distance on theexam paper.

4. Include a table of the current arcs, labeled withFrom node, To node, Distance, and Penalized distance.

5. Provide a line or two of space for students to writenode penalties, for question a.

6. Fill in all the entries except the Penalized distancecolumn, for question b.

7. Print a graph of the nodes without arcs, for ques-tion c.

Questions could be as follows:

• "a. (1 point) Starting with initial penalties of zeroand a step size of 30, calculate the new nodepenalties. Write them here." (Provide space.)

• "b. (2 points) Write the new penalized distances inthe table above."

• "c. (7 points) With the new penalized distances,draw the new 1-tree below."

3.4.3. Subtour cut generation.

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 studentsto write constraints that break the displayed subtours.

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, andhow the algorithm requires those lines of script. Forexample, the Kruskal.run script early on contains theline, "let Best_Edges := {(i,j) in Unscanned: length[i,j]= min{(u,v) in Unscanned} length[u,v]};" For a shortanswer question, you can ask students to explain intwo or three sentences the purpose of this line. A validanswer would be something like, "This line initializesan iteration for one arc. The line finds the shortestunconnected arc among the set Unscanned, and addsthis arc to the set Best_Edges. The algorithm thenconnects this arc to a component of the still-disconnect-ed tree."

3.4.5. Short answer question.

"Describe two ways to get a good lower bound on thetraveling salesman problem." Answer: Method 1. Solvethe assignment or 2-matching problem, then add sub-tour breaking constraints. Every solution is a validlower bound, and the lower bound improves witheach added constraint. Method 2: Use Held & Karpsubgradient optimization for the 1-tree relaxation.Method 3: Solve any other TSP formulation as an LP.

3.4.6. Short answer question.

"Give two reasons why a lower bound would behelpful in solving the traveling salesman problem tooptimality." Answer: First, if we find a tour that isequal to the lower bound, then we know it is optimal.Second, during branch and bound, we can prune offsubproblems that have a worse lower bound than thecurrent best lower bound. Third, with both an upperbound U and a lower bound L, we can delete variables

© INFORMS ISSN: 1532-054546INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 11: TSP Problem in AMPL

that have a reduced cost (in the LP) that is larger thanU − L. Putting those variables in the solution will in-crease the objective above the upper bound, so theycannot be optimal.

3.5. Further ideas

The TSP allows a wide range of creative possibilities.Here are additional ideas for the classroom andhomework.

3.5.1. A half lecture could cover Christofides'heuristic (Christofides.run(72)).

Teaching heuristics requires that students already havegood programming skills and an appreciation for theTSP's difficulty. In short, we think that the integerprogramming approach should be taught first.

3.5.2. A half lecture could cover the 2-matchingproblem in depth, including comb constraints.

This is an example of an easy problem (i.e., there is anefficient algorithm), but the IP is not naturally integral.Instead of relaxing the degree 2 constraints, we couldrelax the subtour breaking constraints, resulting in a2-matching problem. The 2-matching problem can besolved in polynomial time, but its LP relaxation is notnaturally integer. However, Edmonds (1965) showedthat a complete linear description of the 2-matchingproblem is obtained by the constraints 1 and 2 fromthe DFJ formulation above, with 5 below.

5. ∑ (i,j):i ∈ W, j ∈ W xi,j + ∑ (i,j) ∈ F xi,j ≤ |W| + |_|F|/2_|,

for all node subsets W, and all sets of arcs F, where (i,j)∈ F if i ∈ W xor j ∈ W.

These constraints are called the 2-matching inequalities,not to be confused with the constraint set that requiresthat nodes have degree 2. The 2-matching inequalitiesare part of a large set of constraints called comb con-straints; set W is often called the "handle" and set F iscalled the "teeth ". Unfortunately, the 2-matchingproblem has an exponential number of two matchinginequalities. We provide a script to find and add 2-matching inequalities iteratively, 2match-

ing_with_combs.mod(73) and 2match-ing_with_combs.run(74).

3.5.3. Add Dantzig-Wolfe decomposition withthe 1-tree subproblem (Held & Karp).

Held & Karp (1969) showed how to find v(DFJ) withthe following model (note we use underscore to desig-nate fixed variables, as this is easier to type thanoverscore):

Model HK: min ∑tπt∑i∑j:j>icijxtij, where xt

ij is the set ofarcs in 1-tree t,

∑t πt (∑j:j>i (xtj,i + xt

i,j)) = 2, for all i (dual prices λi),

∑t πt = 1,

πt ≥ 0, for all t.

A solution method for Model HK is as follows:

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 andresolve HK. Update dual prices λi from degree con-straints. Quit when v(HKt) = v(1-treet).

This requires many iterations, with a "tail-off " effect.To improve on this method, Held & Karp (1970)gavetheir now-famous subgradient optimization algorithm.

3.5.4. Assign a Petersen graph, giving only thenumerical data.

(An example, Petersen10.txt is given in the AMPL datafiles.) Students who take the time to create a visualiza-tion will have additional insight. A bit of a trick assign-ment, 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 ISSN: 1532-054547INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 12: TSP Problem in AMPL

3.5.5. Add a capacity constraint, such as for fuel.

This is easy to do, and lends realism to the problem.

3.5.6. Use of AMPL's database access to take TSPdata from a spreadsheet.

This is easy to do, and useful for students.

3.5.7. The lecturer can change innocuous data

(e.g., arcs not in the solution) to test for plagiarism. Aglance at the student's data confirms whether the stu-dent has taken another student's files.

3.5.8. Ask students to code the nearest neighborheuristic in AMPL script, and examine its perfor-mance.

4. A directory of teaching materials: files,models and scripts for the TSP

This section gives a directory of all the files associatedwith this paper. (Here are all supporting files in a sin-gle zipped file, Using AMPL for teaching the TSP,supporting files.zip(75).)

4.1. PowerPoint presentations• Lecture 1. Description & history of the TSP. Some

f o r m u l a t i o n s . 0 1 _ F o r m u l a -tions_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_Tutori-al_TSP-MST_Lagrangean_relaxation.ppt(78).

• Lecture 3. More solution methods for the TSP.04_More_solution_methods_for_the_TSP.ppt(79).

4.2. Scite configuration files for AMPL

The following represents one attempt at setting upScite to work with AMPL. We would welcome addi-tional suggestions and improvements from readers.

1. Download Scite from the Scintilla website(80). Thesingle executable Sc1 is convenient. Place this filein the directory of your choice, perhaps "C:\Pro-gram Files\Scite".

2. Add a shortcut to sc1.exe on your WindowsLaunch tool bar.

3. Copy all the files from this paper's Scite directoryto your Scite directory.

4. In Start, Control Panel, System, Advanced, Envi-ronment variables, System variables, add yourScite directory to the path variable.

5. Then, to start Scite, simply click on the shortcut onyour launch bar. You can also drag files onto theshortcut to open them with Scite.

6. If Scite does not seem to find the AMPL files, re-view the settings in the ampl.properties file.

The file SciTEGlobal.properties(81) imports the amplproperties file. SciTEGlobal.properties allows a widerange of configurations. See the Scite documentationfile(82) for more information.

The file ampl.properties(83) provides support for thefile extensions ".mod", "dat" and "run", lists AMPL keywords for colored syntax, and specifies a variety ofother miscellaneous properties. Settings in ampl.prop-erties override settings in SciTEGlobal.properties.Ampl.properties controls the behavior of function keyF5, which currently calls AMPL with the current ".run"file. You can modify this file to use the GNU LP Kit inplace 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 ISSN: 1532-054548INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 13: TSP Problem in AMPL

The file ampl.api(84)lists many of AMPL's functions,for the tooltips.

The file amplhelp.html(85) provides context-sensitivehelp for AMPL key words in Scite. This help file hastaken some effort, but could probably still be im-proved. The file uses images AMPLcmdline.PNG(86),SciteAMPL.PNG(87) , and SciteAMPL0.PNG(88).

The file amplwizard.hta(89) represents an experimentalapproach to guiding a first-time user to AMPL syntax.This is ".hta" application(90), which runs in InternetExplorer as a trusted executable. Amplwizard.hta iswritten in ordinary HTML and VB script, which iseasy to modify. It uses the "SendKeys" function to passa string to Scite. The string is inserted into Scite at thecurrent cursor location in Scite. This also shows howan external command can be placed into Scite's menuand called.

4.3. TSP and TSP-related formulations

The major source of scripts for this paper was the firstauthor's University of Kentucky web site, which is notcurrently available. We discuss other internet resourceslater in the paper.

Some of the directories also contain sample SVG graphfiles, usually named tspgraph.svg.

• The 2-matching problem, 2matching.mod(91),2matching.run(92).

• The assignment problem, assignment.mod(93), as-signment.run(94).

• Miller-Tucker-Zemlin (MTZ) assignment modelwith loose subtour breaking rows, MTZ.mod(95),MTZ.run(96).

• Svestka flow formulation,Svestka.mod(97) , Svest-ka.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, withvariations for the 1-tree and TSP, mar-tin_tree_lp.mod(104), and martin_tree_lp.run(105).

4.4. Algorithms• Prim's algorithm for the MST, prims.run(106) .

• Kruskal's algorithm for the MST, kruskal.run(107),script originally developed by Olinick (2006).

(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 ISSN: 1532-054549INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 14: TSP Problem in AMPL

• Held-Karp subgradient optimization, Held-Karp.run(108).

• The DFJ cut-generation algorithm, adding onesubtour cut at a time, DFJ-simple.mod(109) and DFJ-simple.run(110).

• The classical DFJ cut-generation algorithm, usinga max flow model as a subroutine, DFJ.mod(111)

and DFJ.run(112).

• The DFJ cut-generation algorithm with restrictedarcs and reduced cost test, DFJ_restrict-ed_arcs.mod(113) and DFJ_restricted_arcs.run(114).

• The 2-matching problem with comb generation,using a max flow model as a subroutine, 2match-ing_with_combs.mod(115) and 2match-ing_with_combs.run(116).

• Christofides heuristic, Christofides.run(117). Inter-mediate and final SVG output from this model iscontained the files 1000points_mst.svg(118),1000points_oddmatching.svg(119), and1000points_christo_tour.svg(120).

• Script to create SVG output, makesvg.run(121). Thescript is assumed to be called from another script.The script assumes N nodes, a parameter or objec-tive function named "Tourlength", set "Nodes" and"Arcs", variables or parameters named "x" definedover the set Arcs, and appropriately defined pa-rameters "length", "xcoord" and "ycoord". Thescript creates an SVG graph with solid lines forx[i,j]=1 and dotted lines for 0 < x[i,j] < 1. It tries to

scale the graphical objects to the data, but you maystill need to adjust stroke-width, circle radius, andfont-size.

4.5. Sample data

Note that non-Euclidean examples have dummy x,ycoordinates for the SVG visualization.

• Script to generate a data file with random x,y coor-dinates, get_random_coordinates.run(122). The out-put is written to a file name "random_points.txt".This script calls makesvg_points.run(123).

• Script to create SVG output of only thenodes,Makesvg_points.run(124). The script is as-sumed to be called from another script, and as-sumes N nodes, xcoord[i], ycoord[i], x[i,j]. It triesto scale the graphical objects to the data. This isuseful when creating problems from random data,when you want to see the graph before solving theproblem.

• 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, Nem-wolp474.txt(126). In this example, v(DFJ) = 9, whilethe optimal integer 2-matching and the optimaltour length have length 10.

• Nemhauser & Wolsey (1988), page 476, Nem-wolp476.txt(127). In this example, the optimal integer

(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 ISSN: 1532-054550INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 15: TSP Problem in AMPL

2-matching has length 15, and the optimal tour haslength 23.

• Boyd & Labonté (2002) 10-node example,Boyd10.txt(128). Boyd & Labonté proved that thisexample has the largest possible gap between thevalue of the optimal tour of 40 and v(DFJ) of 34.Symmetric version, Boyd10sym.txt(129).

• A 10-node Petersen graph, Petersen.txt(130), shownhere. Image from http://en.wikipedia.org/wiki/Pe-tersen_graph(131).

• 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 continentalUnited States, 48uscaps.txt(139).

• 52 Berlin locations, 52berlin.txt(140).

• 105 cities, 105cities.txt(141).

5. Additional web resources

The Internet has rich materials on the TSP, with lotsof free software and interactive visualizations. Googlereports 115,000 hits on the search site:edu "travelingsalesman problem". (Note that with the double-elled'travelling', one gets about 25,000 hits.) Here, we aimto give a few key educational sites that are appropriatefor third or fourth year students. (These were last ac-cessed in June 2006.)

5.1. General web sites• TSPLIB. "This page intends to be a comprehensive

listing of papers, source code, preprints, technicalreports, etc, available on the Internet about theTraveling Salesman Problem (TSP) and some asso-c i a t e d p r o b l e m s . "http://www.ing.unlp.edu.ar/cetad/mos/TSP-BIB_home.html or University of Heidelberg web-site(142).

• Concorde. General site concerning the TSP. High-lights: "The full source code to the Concorde net-work optimization package, as well as executablesfor various platforms, and a Windows graphicaluser interface to Concorde's traveling salesmansolver are available for academic research use."TSP website(143)>.

• COIN-OR(144). A repository for open-source codefor O.R. Notably, Cbc (COIN Branch and Cut) canbe used as a C++ library, as a standalone solver,and as an AMPL solver (with all Cbc optionsavailable from an AMPL script). In addition, theCoin-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 ISSN: 1532-054551INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 16: TSP Problem in AMPL

algorithms available for linear, nonlinear, continu-ous, and discrete optimization.

• NEOS Server for Optimization. NEOS providesopen access to a large number of solvers whichrun on NEOS servers. The Concorde server atNEOS provides for exact solution using the TSPsolver of Applegate, Bixby, Chvátal, and Cook. Inaddition, the Concorde server provides for solutionusing the Lin-Kernighan heuristic. For integerprogramming, the open-source solver Cbc andothers are available. Several of the IP solvers onNEOS provide for user input via AMPL, in twodifferent ways. First, AMPL files can be uploadedto the server and then AMPL and the desiredsolver execute on a NEOS server. Second, a usercan run AMPL locally on their own machine, andonly use the server as a back-end solver. This latterpossibility utilizes the Kestrel client/server. Theadvantage of using Kestrel is that the solutionprovided by the remote solver is returned to therunning AMPL session on your own machine,where, for example, a script can then continue.Also, if one has a complicated script that callsmany different solvers, this is the only way toproceed if the solvers are available on differentserver machines of NEOS(145).

• Chvatal's web site, The Traveling Salesman Prob-lem(146).

• Steven S. Skiena's "The Stony Brook AlgorithmRepository," has a section devoted to the TSP: TheTraveling Salesman Problem(147). In addition,Skiena (1997) has a section on the TSP.

• Website of the 8th DIMACS ImplementationChallenge: The Traveling Salesman Problem. In-

stances. results and comparisons.The TravelingSalesman Problem(148).

• Wikipedia article, The Traveling Salesman Prob-lem(149).

• An introductory article by Karla Hoffman, TheTraveling Salesman Problem(150).

• Fractal Instances of the Traveling Salesman Prob-lem. Resources on generation of instances of theTSP with known optimal solution. Fractal Instancesof the Traveling Salesman Problem(151).

• A few links and experimental resources. TheTraveling Salesman Problem(152).

5.2. Optimal solutions and other scripts• Robert Dakin's page, Fractal Instances of the

Traveling Salesman Problem(153). Includes a pro-gram with visualization that implements two algo-rithms for the TSP.

• Neil Simonetti's Traveling Salesman Problem Page.Includes a Dynamic Programming Code for theTSP.Fractal Instances of the Traveling SalesmanProblem(154).

• Onno Waalewijn's TSP applets, JAVA TravelingSalesman Problem(155) (heuristic version), ExactTraveling Salesman Problem(156) (exhaustive ver-sion).

• Sean Forman's web page, TSP Generator(157), Froma list of USA city names, the program generates amap and applies various heuristics and an exactbranch 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 ISSN: 1532-054552INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 17: TSP Problem in AMPL

• Bruno Guerrieri's(158) Maple worksheet and anima-tion for the TSP(159), available from MapleSoft.

• "Solution to the traveling salesman problem isimplemented in Mathematica as TravelingSales-man[g] in the Mathematica add-on package Dis-creteMath Combinatorica (which can be loadedwith the command "DiscreteMath") The TravelingSalesman Problem(160).

5.3. Heuristics• Frits Beuker's simulated annealing heuristic(161) .

• Konstantin Boukreev(162)'s genetic algorithm javacode (source code only)(163).

• Julian Bunn's genetic algorithm applet(164).

• Java visualization of a neural-network algo-rithm(165).

• Sushil J. Louis and Rilun Tang, paper "Interactivegenetic algorithms for the traveling salespersonproblem,"(166) and applet(167).

• Hans Lohninger's TravSalm: Neural network ap-plet(168).

• David Neto's open source implementation LK(169)

of the Lin-Kernighan heuristic for the TSP and theminimum weight perfect matching problem.

• Stephan Mertens'(170) TSP Algorithms in ActionAnimated Examples of Heuristic Algorithms(171).

• Kreimer Natasha's applets for some TSP heuris-tics(172) .

• Prentice Hall Companion Website, Excursions inModern Mathematics Fifth Edition, by Peter Tan-nenbaum. Chapter 6 is on the TSP and it includes,for example, an applet for a heuristic(173).

• Johannes Sarg's Visualisation of Genetic Algo-rithms for the Traveling Salesman Problem in Ja-va(174) .

• An applet with "simple, but effective heuristics forthe Traveling Salesman Problem with Euclideandistances(175)."

• Svengato's applet for a simulated annealingheuristic(176).

• Gregg M. Townsend's travels.icn(177) program illus-trates several construction algorithms followed byapplication of the "2-opt" improvement algorithm".

• Gunno Törnberg's Java applets for heuristics onrandomly generated problems(178).

ReferencesApplegate,David, Robert Bixby, Vasek Chvatal, and

William Cook (1998), "On the Solution ofTravelling Salesman Problems," DocumentaMath., 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 ISSN: 1532-054553INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 18: TSP Problem in AMPL

http://www.nps.navy.mil/orfacpag/re-sumePages/publications/Top%20Ten%20Se-crets%20to%20Success%20with%20Optimiza-tion.pdf>"Top Ten Secrets to Success with Optimiza-tion," The Phalanx, Vol. 37, No. 4, p. 12ff.

Dantzig, G., Fulkerson, R., and Johnson, S. (1954), So-lution of a Large-Scale Traveling-SalesmanProblem, Operations Research, Vol. 2, p. 393.

Edmonds, J. (1965), "Maximum matching and a poly-hedron with 0,1-vertices," J. Res. Nat. Bur. Stand.B, Vol. 69, pp. 125-130.

Fourer, R., Gay, D.M., & Kernighan, B.W. (2003) AMPL:A Modeling Language for Mathematical Program-ming, Thomson Publishing, Pacific Grove, CA.

Guignard, Monique, and Kim, Siwhan (1987), "La-grangean Decomposition: a Model YieldingStronger Lagrangean Bounds," Math Program-ming, Vol. 39, pp. 215-228.

Held, Michael, and Karp, Richard M. (1970), "TheTravelling-Salesman Problem and MinimumSpanning Trees," Operations Research, Vol. 18,No. 6, Nov-Dec, pp. 1138-1162.

Held, Michael, and Karp, Richard M. (1971), "TheTravelling-Salesman Problem and MinimumSpanning Trees: Part II," Math Programming,Vol. 1, pp. 6-25.

Hillier, F. S. and G. J. Lieberman (2001), Introductionto Operations Research, 7th edition, McGraw-Hill,Boston, MA.

Lawler, E.L., J.K. Lenstra, A.H.G. Rinnooy Kan, andD.B. Shmoys (1985), The Traveling SalesmanProblem: a Guided Tour of Combinatorial Optimiza-tion, John Wiley & Sons, Chichester.

Lawrence, John A., and Pasternack, Barry A. (1998),Applied Management Science, John Wiley andSons, New York, NY.

Martin, R. Kipp (1991), "Using Separation Algorithmsto Generate Mixed Integer Model Reformula-tions," Operations Res. Letters, Vol. 10, pp. 119-128.

Nemhauser, George L, and Wolsey, Lawrence A.(1988), Integer and Combinatorial Optimization,John Wiley & Sons, Inc.

Olinick, Eli (2006), available online at

http://engr.smu.edu/~olinick/emis8373/lec-tures/mst/kruskal.htm(last accessed on 7 March 2006).

Pataki, Gabor (2003), "Teaching Integer ProgrammingFormulations Using the Traveling SalesmanProblem," SIAM Review, Vol. 45, No. 1, pp. 116-123.

Ragsdale, Cliff (2004), Spreadsheet Modeling & DecisionAnalysis, 4th edition, Thomson-Southwestern,Mason, Ohio.

Reinelt, Gerhard (1994), The Traveling Salesman: Com-putational Solutions for TSP Applications,Springer-Verlag, Berlin.

Baker, S.F., Morton, D.P., Rosenthal, R.E., andWilliams, L.M. (2002), "Optimizing StrategicAirlift," Operations Research, Vol. 50, July-Au-gust, pp. 582-602.

Skiena, Steven S. (1997), The Algorithm Design Manual,Springer-Verlag, New York.

Winston, Wayne (2003), Operations Research: Applica-tions and Algorithms, 4th edition, DuxburyPress, Belmont, CA.

© INFORMS ISSN: 1532-054554INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 19: TSP Problem in AMPL

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 ahelp for students.

The authoritative reference on AMPL is AMPL: A Modeling Language for Math Programming(179), by RobertFourer, David M. Gay, and Brian W. Kernighan. The AMPL(180) web site has a wealth of information, such asAMPL: A Mathematical Programming Language(181). From www.ampl.com: "AMPL is a comprehensive andpowerful algebraic modeling language for linear and nonlinear optimization problems, in discrete or continuousvariables. Developed at Bell Laboratories, AMPL lets you use common notation and familiar concepts to formulateoptimization models and examine solutions, while the computer manages communication with an appropriatesolver. AMPL's flexibility and convenience render it ideal for rapid prototyping and model development, whileits 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, thisfile should open to show you some help on that key word. This help file is not complete, but should containthe most common AMPL commands.

Now, see here, if you see something in "quotes;", which you are supposed to type into AMPL, you know, deepdown 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 itssets, parameters, variables, objective function, and constraints. This should be independent of the data. Forexample, 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 dif-ferent 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 yourconvenience, to avoid the need to type the same commands over and over into AMPL. You can also put inother commands to have AMPL display the solution. You can even develop algorithms with AMPL's commandlanguage.

(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 ISSN: 1532-054555INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 20: TSP Problem in AMPL

Using AMPL with the Scite editor, the best way to get AMPL to solve is to create a run file, typically with justa 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 "di-et.run". With this file displayed in Scite, just press the F5 function button on your keyboard, and AMPL willrun in a panel at the right. Scite does this by calling AMPL at the operating system command level, with "AMPLdiet.run". You can see it in the Scite output panel, as shown below

© INFORMS ISSN: 1532-054556INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 21: TSP Problem in AMPL

Alternatively, you can use a run file by starting up AMPL yourself, and get the AMPL command prompt. Thentype "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 avisualization 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 fileswill 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 forall 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 alonger AMPL script, you might write "let N := 16;".

© INFORMS ISSN: 1532-054557INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 22: TSP Problem in AMPL

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 aparticular model in it. For example, "model diet.mod;" (at the AMPL command prompt or in your run file) tellsAMPL 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 differentways.

• 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 makethe 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 rightafter 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 inthe "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 param-eters 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 modelfile, like this: "param N; set Cities := {1..N};". Now you can define arcs between the cities as "set Arcs := {i inCities, j in Cities: i < > j};". The ": i < > j" is translated as "such that i not equal to j". If you have a EuclideanTSP, 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 willnot 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 definea parameter named Y with a value of 5, and that Y must always be bigger than or equal to 1. If you do not usethe "default" key word, you should assign it a value somewhere (e.g. in a data or run file). Otherwise AMPLmay complain that Y has not been assigned a value. Use the > = to make sure that your data is reasonable. Ifyour 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 datasection, or with a "let" command), AMPL will give an error if you try to change the parameter elsewhere inyour 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 justone 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 re-ally 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) inArcs} length[i,j] * x[i,j];"

(182) http://www.gnu.org/software/glpk/glpk.html

© INFORMS ISSN: 1532-054558INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 23: TSP Problem in AMPL

The var keyword is used to define a variable that will be calculated by the solver (e.g. CPlex or MINOS). Avariable 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 definea non-negative decision variable named w. Example: "var x > =-4 < = 5 integer;" tells AMPL to define a decisionvariable 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 objectivefunction. Example: "minimize Pain: z;". AMPL can solve over only one objective at a time, so your mod file willusually have one objective. If you have a complex algorithm, where you want to change the objective duringthe 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 objectivefunction. 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 constraintover a set, and you specify the set as part of the constraint name. For example, you may want to require thateach 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 in-cluded 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 linefor the data, and the solve command. See "run file" above. If you press F5 in Scite on a model, you are likely toget all variables zero, because you haven't given AMPL the solve command first. So press F5 in the run file, notthe 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 AMPLwill display all the values for x. Many of these will be zero, which isn't too interesting. If you first give AMPLthe 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, dualprices and reduced costs are not available in an integer program. The command will work, but the values shouldbe 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 ISSN: 1532-054559INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 24: TSP Problem in AMPL

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];". Atthe AMPL command prompt (or in your run file), you could type "expand Tourlength;". AMPL would thendisplay 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 variablesfrom your previous run are still in memory. Just type "reset;" before running your model to clear all the previousinformation.

Problem: AMPL says, syntax error. Solution: AMPL should point to the line with the problem. Check the statementclosely. 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 becorrected 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 ISSN: 1532-054560INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 25: TSP Problem in AMPL

• Tell AMPL which solver to use: option solver cplex; # This tells AMPL to use the commercial version ofCPlex. 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, modelswith variables defined as integer or binary are naturally solved as IPs. option relax_integrality 0; # Solvethe 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 ratherthan wide, e.g.x x xx x xx x xx x xoption display_transpose 0; # This is the default setting. A wide table with 2 rows and 4 columns will betransposed. 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 travellingsalesman problem as an example, with N nodes. Assume our model file has the following declarations:

param N; # Number of nodesset 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 ISSN: 1532-054561INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 26: TSP Problem in AMPL

Note that both xcoord and ycoord are defined over the list of Nodes, 1..10. If we wish, we can use a more conciseformat, 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 maylook like this:

param N; # Number of nodesset 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 tellAMPL that this value is undefined.

© INFORMS ISSN: 1532-054562INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 27: TSP Problem in AMPL

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), Imple-menting 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;". Youcan 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 ISSN: 1532-054563INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 28: TSP Problem in AMPL

Piping. There is no piping command, but rather you can use the " < " and " > " symbols in a computer trick totemporarily connect a file to be input to a program, or output from a program to become a file. Example: "displayx > 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 havea file called "TSPinput.txt;" with one number, and you want AMPL to read the number from the file and put itinto 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 filecan be used by another program, such as AMPL's "read" command. It's always a good idea to close a file afterwriting to it or reading from it. Trying to read a file that hasn't been closed may give the error "unexpected endof file". You can get away without closing files most of the time, because AMPL will sometimes close the fileautomatically (for example, if you exit from AMPL). But if your script writes to a file, then later reads from thefile, 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 minussign 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 parameterN 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 asset 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 inNodes}: sum {(i,j) in Arcs} x[i,j] + sum {(j,i) in Arcs} x[j,i] = 2;". This tells AMPL that the constraint is definedover 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 ISSN: 1532-054564INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 29: TSP Problem in AMPL

The max and min functions may be used over scalars () or over sets {}. Don't confuse these wee functions withthe 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 assignsto 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 setNodes 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 sat-isfy 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 ISSN: 1532-054565INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 30: TSP Problem in AMPL

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 commandto your run file: option solver cplex; If you are using the commercial (non-student) version of CPlex, you shoulduse: 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 ISSN: 1532-054566INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 31: TSP Problem in AMPL

You can also send output from the display command to a file. Inside your .RUN file, you could type: displayx > 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 didnot work. I opened the DOS prompt and typed in c:\ > p:\msci315\ampl part2.run > output.txt The AMPLcode ran, but the file output.txt contains the error message "can not open part2.run". How can I solve thisproblem?

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 inp:\msci315\, the part2.run file (which the computer thinks is in c:\ > ) and output.txt, which the computer willtry 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 didnot work. I opened the DOS prompt and typed in c:\ > p:\msci315\ampl part2.run > output.txt The AMPLcode 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 whichyou have write access.

When I run the cutting stock problem, AMPL complains that my parameter fails an integrality check. Whatis 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 beginningof 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 %iand \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 ISSN: 1532-054567INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 32: TSP Problem in AMPL

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, wecould 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 toindicate 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 keepsrepeating. What could be wrong?

Check the objective value calculations. You could always solve the original linear program, so you know whatthe 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 determinewhen 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, youwould 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 columnname information is lost. You can get AMPL to at least write out a file with a list of the names:ampl: modelyourmodel.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 ISSN: 1532-054568INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP

Page 33: TSP Problem in AMPL

Experimental wizard

You may like to try the experimental AMPL Wizard(191), intended to work with Scite.

Created by John F. Raffensperger([email protected]), 15 March 2006. AMPL is a registered trademarkof AMPL Optimization LLC.

(191) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/Scite/amplwizard.hta

© INFORMS ISSN: 1532-054569INFORMS Transactions on Education 7:1(37-69)

LEE & RAFFENSPERGERUsing AMPL for teaching the TSP