TSP Problem in AMPL
TSP Problem in AMPL
John F. Raffensperger
Dept. of Management, Private Bag 4800
University of Canterbury, Christchurch, New Zealand
[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 presentations, and a convenient package of AMPL models and scripts. The AMPL files include different formulations
for the TSP, its relaxations, scripts for its solution, and - particularly useful in class - scripts for visualization of
those solutions using SVG. We have a special focus on visualization, to provide convenient ways for students
to view and report their solutions. We observe that the TSP is such a classical O.R. problem, that it can play a
central role in an undergraduate course about integer programming.
provide convenient ways for students to view and report their solutions. The TSP lends itself well to
graphical output, and students find this immensely
helpful for learning and for model and algorithm debugging. Furthermore, the lecturer can require that
students turn in a graphical print-out of the tour, thus
allowing some determination of solution quality at a
glance.
37
We have chosen to use the proprietary modeling language AMPL, which has scripting capability. Useful
alternatives to AMPL(2) include FLOPC++ (3) (an open
source algebraic modeling language implemented as
(1)
http://www.lindo.com/
http://www.ampl.com/
(3) http://www.coin-or.org/
(4) http://www.gams.com/
(5) http://www.maplesoft.com/
(6) http://www.wolfram.com/
(7) http://www.mathworks.com/
(8) http://www.zib.de/koch/zimpl/
(9) http://www.coin-or.org/
(10) http://www.gnu.org/software/glpk/glpk.html
(11) http://www.progdigy.com/
(12) http://lpsolve.sourceforge.net/5.5/Intro.htm
(13) http://lpsolve.sourceforge.net/
(14) http://en.wikipedia.org/wiki/MPS_(format)
(15) http://gsbkip.chicagogsb.edu/fml/fml.html
(16) http://www.optirisk-systems.com/Default.aspx
(17) http://www.scintilla.org/
(2)
38
fectiveness of our materials. However, based on students' look of relief when they get their first AMPL
model working, the speed with which they can get the
basic algorithms going, and on the overall quality of
their work, the workshop approach is clearly positive
and worthwhile. Students who cannot write AMPL
scripts can modify them and learn from that, while
learning about the TSP, and thereby eventually learn
to write their own AMPL models and scripts. Of
course, AMPL models are the first step, but this naturally leads to the harder step of writing AMPL scripts,
and this can potentally lead to real programming.
The AMPL code that we provide does not usually incorporate theoretically-efficient algorithms. Except for
the minimum spanning tree and the 1-tree, the subproblems are modeled as linear or integer linear programs, and AMPL then accesses a general linear programming or integer linear programming code.
We feel that the TSP is such a classical problem, perhaps the classical O.R. integer program, that it can play
a central role in an undergraduate course about integer
programming, even in a school of business. Yet the
undergraduate textbooks do not seem to do it justice.
We have found just one article written specifically for
teaching the TSP to undergraduates. Pataki's (2003)
excellent paper covers the relative tightness of different
formulations, using Matlab.
3. TSP Syllabus
2. Literature review
(18) http://www.ampl.com/
39
Scite. For editing AMPL files, we prefer the opensource cross-platform Scite, downloadable from the
Scintilla website(23). This editor is highly configurable
for various languages via properties files. We discovered a rudimentary AMPL properties file for Scite in
Italian(24), and have modified and extended it extensively (ampl.api(25), ampl.properties(26), SciTEGlobal.
properties(27)).
Scite shows syntax coloring, block indenting, block
commenting, and section folding.
The user can start AMPL with the current run file,
simply by pressing the F5 key. AMPL runs in a
side panel.
More details on installing the files for Scite are provided in section 4.2.
The scripts and models have, for the most part, uniform data formats and uniform definitions for sets,
variables, and parameters. For example, "param N;"
is used in all models to define the number of nodes,
and "Tourlength" is the name of every objective, even
though some will not produce tours. This uniformity
has a couple of advantages, the primary one being that
we can use a single script to produce the SVG output
graph, and a secondary advantage being that students
become accustomed to the notation more quickly.
(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
40
Following this introduction, the remainder of the lecture covers different formulations related to the TSP,
most with their AMPL formulations and solutions.
(Note: if the files do not open on click, consider setting
the ".mod" and ".run" file extensions to open with your
favorite text editor.)
For example, here is the classic Dantzig-FulkersonJohnson (DFJ) formulation, in IPDME format.
(35)
Indices: i, j city.
Parameters: n = number of cities; cij = cost to travel
from city i to city j.
The algebraic formulations for these are in the PowerPoint presentation, and will not be repeated here.
Generally, we show the algebraic form and the AMPL
model directly in the slides (which are printed and
given to students), so the AMPL formulation itself is
used in the lecture. This gets students accustomed to
seeing AMPL formulations, which helps them when
it comes time to write their own. It allows them to ask
questions in class about AMPL syntax. Further, if they
wish, they can copy the examples straight from the
(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/
41
as there are only n variables but O(n2) pairwise distances. So it is a useful exercise in modeling, to work
one's way to the inevitably fruitful idea of using binary
variables to select edges for a tour.
Explanation:
1. Minimize total distance traveled.
2. Each city must have degree 2, i.e., must be entered
and departed.
(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
42
The models can be run either through the Scite interface or at the AMPL command prompt.
To run a script in AMPL through the Scite interface, the user simply opens the script (usually a
".run" file) in Scite, and presses the F5 function key.
AMPL then runs in a side panel. Note that if you
have a model file open without a "solve" command,
AMPL will read the model, but not solve it because
it has not received the "solve" command.
The workshop approach has multiple benefits, as immediate help can be given to those students who have
difficulty starting and running AMPL. Points of syntax
can be addressed immediately, and students get a
quick positive result, with a problem that is easy
computationally. Key AMPL commands, such as "reset", "display", and "expand", are explained and immediately put to use. Sometimes, students become sufficiently confident and enthused that they want to attempt the homework assignment immediately.
Following Lecture 1's introduction to TSP formulations, Lecture 2 introduces some solution methods.
We first present the assignment problem with branch
and bound, using an example from Winston (2003, pp.
530-534), then give the 1-tree, with Prim's and
Kruskal's algorithms. This information is easy to explain, and tends to be understood easily, partly because of the highly graphical nature of the information,
and as it is given just a few weeks following more
general IP cuts and branch and bound.
The final section of this lecture covers the Held & Karp
1-tree subgradient optimization algorithm. Many students get completely thrown at this point, as the lecture
has moved from tree, to 1-tree, to an entirely new al-
(45) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/makesvg.run
(46) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Prims/prims.run
(47) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/kruskal/kruskal.run
(48) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/PowerPoint/02_Solution_methods_for_the_TSP.ppt
43
gorithm. Considerable patience is required. The subgradient optimization material is presented in more
detail in Tutorial 2.
DFJ_all_subtours.mod(54)
and
DFJ_all_subtours.run(55) (modified from AMPL website(56) ).
This script explicitly uses the model with all O(2n)
subtour constraints. This is meant only for very
small instances.
2. Solve the current formulation as an integer linear program. Identify a violated subtour constraint and append it to the current formulation. If there is no violated subtour constraint,
then we have an optimal tour, and we termi-
(49) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/PowerPoint/03_Tutorial_TSP-MST_Lagrangean_relaxation.ppt
(50) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/Prims/prims.run
(51) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/kruskal/kruskal.run
(52) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/HeldKarp/HeldKarp.run
(53) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/PowerPoint/04_More_solution_methods_for_the_TSP.ppt
(54) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/All_subtours/DFJ_all_subtours.mod
(55) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/All_subtours/DFJ_all_subtours.run
(56) http://www.ampl.com/FAQ/tsp.mod
(57) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/1_subtour_at_a_time/DFJ-simple.mod
(58) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/1_subtour_at_a_time/DFJ-simple.run
(59) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/subtours_by_maxflow/DFJ.mod
(60) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/subtours_by_maxflow/DFJ.run
44
Students may gain nearly full marks without an optimal solution, but their approach must be reasonable.
Minimal nonzero marks would be given for a tour
found visually for the upper bound and an ordinary
1-tree for the lower bound. Good answers should include a good feasible solution, and at least a few iterations of the Held-Karp subgradient optimization algorithm. The lecturer can choose whether or not to give
the students our included script (HeldKarp.run(71)).
3.3. Homework.
We assign a 50-city problem, which is too big to solve
directly in the student version of AMPL with CPLEX(65)
. (We have included a script which will generate a
data file with random x,y coordinates, get_random_coordinates.run(66). This script calls another script,
makesvg_points.run(67), which creates an SVG graph
of the points. Alternatively, the lecturer can use a
spreadsheet
"Random_data_genera-
(61) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/Restricted_arcs/DFJ_restricted_arcs.mod
(62) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/Restricted_arcs/DFJ_restricted_arcs.run
(63) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/MartinLP/martin_tree_lp.mod
(64) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/MartinLP/martin_tree_lp.run
(65) http://www.cplex.com/
(66) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/get_random_coordinates.run
(67) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/makesvg_points.run
(68) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/data/Random_data_generator_for_TSP_50_points.xls
(69) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/Restricted_arcs/DFJ_restricted_arcs.mod
(70) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/DFJ/Restricted_arcs/DFJ_restricted_arcs.run
(71) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/AMPL_files/HeldKarp/HeldKarp.run
45
46
ing_with_combs.mod(73)
ing_with_combs.run(74).
that have a reduced cost (in the LP) that is larger than
U L. Putting those variables in the solution will increase the objective above the upper bound, so they
cannot be optimal.
and
2match-
t 0, for all t.
arcs in 1-tree t,
t t = 1,
for all node subsets W, and all sets of arcs F, where (i,j)
F if i W xor j W.
(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
47
4. In Start, Control Panel, System, Advanced, Environment variables, System variables, add your
Scite directory to the path variable.
5. Then, to start Scite, simply click on the shortcut on
your launch bar. You can also drag files onto the
shortcut to open them with Scite.
6. If Scite does not seem to find the AMPL files, review the settings in the ampl.properties file.
(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
48
4.4. Algorithms
The major source of scripts for this paper was the first
author's University of Kentucky web site, which is not
currently available. We discuss other internet resources
later in the paper.
49
Held-Karp subgradient
Karp.run(108).
optimization,
Held-
Christofides heuristic, Christofides.run(117). Intermediate and final SVG output from this model is
contained the files 1000points_mst.svg(118),
1000points_oddmatching.svg(119),
and
1000points_christo_tour.svg(120).
Script to generate a data file with random x,y coordinates, get_random_coordinates.run(122). The output is written to a file name "random_points.txt".
This script calls makesvg_points.run(123).
Nemhauser & Wolsey (1988), page 474, Nemwolp474.txt(126). In this example, v(DFJ) = 9, while
the optimal integer 2-matching and the optimal
tour length have length 10.
Nemhauser & Wolsey (1988), page 476, Nemwolp476.txt(127). In this example, the optimal integer
(121)
(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
50
Concorde. General site concerning the TSP. Highlights: "The full source code to the Concorde network optimization package, as well as executables
for various platforms, and a Windows graphical
user interface to Concorde's traveling salesman
solver are available for academic research use."
TSP website(143)>.
(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/
51
Fractal Instances of the Traveling Salesman Problem. Resources on generation of instances of the
TSP with known optimal solution. Fractal Instances
of the Traveling Salesman Problem(151).
(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
52
Bruno Guerrieri's(158) Maple worksheet and animation for the TSP(159), available from MapleSoft.
Johannes Sarg's Visualisation of Genetic Algorithms for the Traveling Salesman Problem in Java(174) .
5.3. Heuristics
(169)
References
Applegate,David, Robert Bixby, Vasek Chvatal, and
William Cook (1998), "On the Solution of
Travelling Salesman Problems," Documenta
Math., Vol. 3, pp. 645-656.
Brown, Gerald G. (2004),
(158) http://www.maplesoft.com/applications/app_center_advanced_search.aspx?ABA=265
(159) http://www.maplesoft.com/applications/app_center_view.aspx?AID=966
(160) http://mathworld.wolfram.com/TravelingSalesmanProblem.html
(161) http://www.math.uu.nl/people/beukers/anneal/anneal.html
(162) http://www.codeproject.com/script/profile/whos_who.asp?id=128
(163) http://www.codeproject.com/cpp/tspapp.asp
(164) http://pcbunn.cithep.caltech.edu/Java/Genetic.html
(165) http://www.patol.com/java/TSP/
(166) http://www.cse.unr.edu/~sushil/papers/newpapers/99/gecco99/iga/GECCO/gecco/gecco.html
(167) http://gaspac.cse.unr.edu:8323/~sushil/tsp/data/
(168) http://www.vias.org/simulations/simusoft_travsalm.html
(169) http://www.cs.toronto.edu/~neto/research/lk/
(170) http://www-e.uni-magdeburg.de/mertens/index.php
(171) http://www-e.uni-magdeburg.de/mertens/TSP/index.html
(172) http://valis.cs.uiuc.edu/~sariel/research/CG/applets/tsp/TspAlg.html
(173) http://wps.prenhall.com/esm_tannenbaum_excursions_5/0,8136,944272-,00.html
(174) http://www.ads.tuwien.ac.at/~guenther/tspga/TSPGA.html
(175) http://riot.ieor.berkeley.edu/~cander/cs270/
(176) http://www.svengato.com/salesman.html
(177) http://www.cs.arizona.edu/icon/oddsends/travels/travels.htm
(178) http://web.telia.com/~u85905224/tsp/TSP.htm
53
http://www.nps.navy.mil/orfacpag/resumePages/publications/Top%20Ten%20Secrets%20to%20Success%20with%20Optimization.pdf
>"Top Ten Secrets to Success with Optimization," The Phalanx, Vol. 37, No. 4, p. 12ff.
http://engr.smu.edu/~olinick/emis8373/lectures/mst/kruskal.htm
(last accessed on 7 March 2006).
Pataki, Gabor (2003), "Teaching Integer Programming
Formulations Using the Traveling Salesman
Problem," SIAM Review, Vol. 45, No. 1, pp. 116123.
Dantzig, G., Fulkerson, R., and Johnson, S. (1954), Solution of a Large-Scale Traveling-Salesman
Problem, Operations Research, Vol. 2, p. 393.
Edmonds, J. (1965), "Maximum matching and a polyhedron with 0,1-vertices," J. Res. Nat. Bur. Stand.
B, Vol. 69, pp. 125-130.
Reinelt, Gerhard (1994), The Traveling Salesman: Computational Solutions for TSP Applications,
Springer-Verlag, Berlin.
Guignard, Monique, and Kim, Siwhan (1987), "Lagrangean Decomposition: a Model Yielding
Stronger Lagrangean Bounds," Math Programming, Vol. 39, pp. 215-228.
Winston, Wayne (2003), Operations Research: Applications and Algorithms, 4th edition, Duxbury
Press, Belmont, CA.
54
Appendix
Help with AMPL and Scite
This file gives help for AMPL, and is designed to work with the Scite editor. This web page is intended to be a
help for students.
The authoritative reference on AMPL is AMPL: A Modeling Language for Math Programming(179), by Robert
Fourer, David M. Gay, and Brian W. Kernighan. The AMPL(180) web site has a wealth of information, such as
AMPL: A Mathematical Programming Language(181). From www.ampl.com: "AMPL is a comprehensive and
powerful algebraic modeling language for linear and nonlinear optimization problems, in discrete or continuous
variables. Developed at Bell Laboratories, AMPL lets you use common notation and familiar concepts to formulate
optimization models and examine solutions, while the computer manages communication with an appropriate
solver. AMPL's flexibility and convenience render it ideal for rapid prototyping and model development, while
its speed and control options make it an especially efficient choice for repeated production runs."
In Scite, if you put the cursor on an AMPL key word, such as "option", "display", or "asinh", then press F1, this
file should open to show you some help on that key word. This help file is not complete, but should contain
the most common AMPL commands.
Now, see here, if you see something in "quotes;", which you are supposed to type into AMPL, you know, deep
down inside, that you shouldn't type the quotes into AMPL. But don't forget the semicolon.
Getting started with AMPL
Generally you should have three files, a model file, a data file, and a run file.
A model file contains the AMPL formulation for the model (and is often given the extension ".mod"), with its
sets, parameters, variables, objective function, and constraints. This should be independent of the data. For
example, if you want to solve a diet model with 15 foods, the model file would store a named parameter like
"param N;" for the number of foods. The actual number "param N := 16;" is typically stored in the data file.
A data file contains the data for one instance of the model (and is sometimes given the extension ".dat" or ".txt").
For example, you may want to solve 5 different instances of the travelling salesman problem, each with a different number of cities. You simply put each instance in a different data file.
A run file contains a list of commands for AMPL to follow in solving the model (and is often given the extension
".run"). The run file tells AMPL which model file and which data file to use. The run files are used for your
convenience, to avoid the need to type the same commands over and over into AMPL. You can also put in
other commands to have AMPL display the solution. You can even develop algorithms with AMPL's command
language.
(179) http://www.amazon.com/gp/product/0534378951/qid=1142410713/sr=8-3/ref=sr_8_xs_ap_i3_xgl14/104-4898613-
0507106?n=507846&s=books&v=glance
(180) http://www.ampl.com/
(181) http://www.ampl.com/REFS/amplmod.pdf
55
Using AMPL with the Scite editor, the best way to get AMPL to solve is to create a run file, typically with just
a few lines, a line for the model, a line for the data, and the solve command. Example:
model diet.mod;
data diet.dat;
solve;
display Buy;
This script tells AMPL to use the model described in diet.mod with the data in diet.dat, to solve the problem,
and then to show the values for the variable Buy. Now suppose you put these three lines in a file named "diet.run". With this file displayed in Scite, just press the F5 function button on your keyboard, and AMPL will
run in a panel at the right. Scite does this by calling AMPL at the operating system command level, with "AMPL
diet.run". You can see it in the Scite output panel, as shown below
56
Alternatively, you can use a run file by starting up AMPL yourself, and get the AMPL command prompt. Then
type "commands diet.run;", and AMPL will attempt to run the commands that are in that run file.
Yet another way to use the commands statement is within a script, to call another file of AMPL commands,
similar to a subroutine. Example: "commands makesvg.run;" which might be part of a TSP script to output a
visualization of the solution.
AMPL doesn't care about the file names or extensions. In fact, you could put everything into one file - model,
data, and the "solve;" statement at the bottom. But putting the model, data, and commands into separate files
will make understanding and communcating easier. Also, the Scite editor is configured to recognize the ".mod"
and ".run" file extensions.
Basic syntax
AMPL is case sensitive! That means that "param y;" and "param Y;" are two different parameters.
AMPL ignores white space, so "param y;" is the same as "paramy;"
End statements with semicolons, ";".
Watch your parentheses. Scite will highlight matching parentheses and brackets.
You can comment out to the end of a line with a # comment. Scite should show it green.
You can comment out a block with /* comment. Scite should show it green.*/
AMPL treats "=" and ":=" very differently, and it's important to understand when to use each of these.
Use "=" with "subject to" to define an equality constraint. Example: "subject to Budget: x + y = 100;"
Use "=" when specifing parts of sets. Example: "subject to target_level{(c,v) in ROADS: c = 'Do_nothing'}
sum{m in MAINT} Area[c,v,m] < = initial[c,v];". This tells AMPL to define the constraint target_level for
all pairs (c,v), where c='Do_nothing'.
Use ":=" when specifying data. Example: "param N := 16;". This assigns the value 16 to the parameter N.
Use ":=" when specifying parameters or variables in a script with the "let" key word. Example: as part of a
longer AMPL script, you might write "let N := 16;".
57
First, you can put "data;" at the end of your model file, and follow it with the actual data you want to use,
in the same file. This puts your model and data in the same file, which isn't so clever, but which does make
the file compatible with the open-source Gnu LP Kit(182)
Second, you can type "data diet.dat;" (if you have a diet model) at the AMPL command line, usually right
after the "model" command. This will tell AMPL that your diet data is in the file diet.dat.
Third, you can type "data diet.dat;" in your run file, again usually right after the "model" command, as in
the "run file" discussion above. Again, this will tell AMPL that your diet data is in the file diet.dat.
The set keyword is used to tell AMPL that you have a bunch of related objects, such as cities, nodes, arcs, etc.
You use sets most often in a model file. Once you have defined some sets, you can use the sets to define parameters and variables. See also the set-oriented functions below.
Example: to define a set of cities for a TSP problem in your model file, you might write "set Cities := {1..20};".
But it's better to put the "20" in a data file, so you can change the number of cities without changing the model
file, like this: "param N; set Cities := {1..N};". Now you can define arcs between the cities as "set Arcs := {i in
Cities, j in Cities: i < > j};". The ": i < > j" is translated as "such that i not equal to j". If you have a Euclidean
TSP, you could write ": i < j", because the cost from i to j is the same as the cost from j to i.
The param keyword is used to define a parameter that is used for data. It is not a decision variable, and will
not be calculated by the solver (e.g. CPlex or MINOS). Rather, a parameter is data that you give the model.
Example: "param N;" defines a parameter named N. Example: "param Y > = 1 default 5;" tells AMPL to define
a parameter named Y with a value of 5, and that Y must always be bigger than or equal to 1. If you do not use
the "default" key word, you should assign it a value somewhere (e.g. in a data or run file). Otherwise AMPL
may complain that Y has not been assigned a value. Use the > = to make sure that your data is reasonable. If
your script or data assigns it a data that violates this, AMPL will let you know.
If you assign the value to the parameter at the same time that you define the parameter (rather than in a data
section, or with a "let" command), AMPL will give an error if you try to change the parameter elsewhere in
your model or script. So specifying a parameter's value when the parameter is defined makes it permanent.
To avoid your own confusion, consider using 2 or more characters in every parameter, and consider using just
one character in set indices. Example: you define "param i default 0;" and later write "let y := sum {i in Foods}
cost[i]*Buy[i];". This is bad, because AMPL will use the parameter i which is probably fixed to 0, when you really want i to be an ordinary counter. You should make your parameter names a bit more descriptive, anyway!
You do not have to define a parameter for an index, such as the i or j in "minimize Tourlength: sum {(i,j) in
Arcs} length[i,j] * x[i,j];"
(182) http://www.gnu.org/software/glpk/glpk.html
58
The var keyword is used to define a variable that will be calculated by the solver (e.g. CPlex or MINOS). A
variable is not data that you give the model. Rather, it is represents a decision or value that you want to know,
and are expecting AMPL to tell you when it has finished solving. Example: "var w > =0;" tells AMPL to define
a non-negative decision variable named w. Example: "var x > =-4 < = 5 integer;" tells AMPL to define a decision
variable named x, with a lower bound of -4, an upper bound of 5, and that x must be integer. In place of "integer",
you can use the key word "binary" to restrict the variable to 0 or 1.
The minimize key word is used to declare a minimizing objective function. You must name your objective
function. Example: "minimize Pain: z;". AMPL can solve over only one objective at a time, so your mod file will
usually have one objective. If you have a complex algorithm, where you want to change the objective during
the algorithm, you may wish to use the problem keyword, to have a named model.
The maximize key word is used to declare a maximizing objective function. You must name your objective
function. Example: "maximize Happiness: z;".
Don't confuse the "minimize" or "maximize" keywords with the "min()" or "max()" functions.
The subject to or s.t. key phrase is used to declare a constraint. You must name each constraint. Example:
"subject to Limit: x+y < = 5;" or equivalently, "s.t. Limit: x+y < = 5;". Often, you will want to declare a constraint
over a set, and you specify the set as part of the constraint name. For example, you may want to require that
each node have degree 2 in the travelling salesman problem: "subject to Degrees {i in Nodes}: sum {(i,j) in Arcs}
x[i,j] + sum {(j,i) in Arcs} x[j,i] = 2;" In this case, note that the constraint is defined over i in Nodes, which is included in the specification for the sum{(j,i) in Arcs}. Cool, huh?
The solve key word tells AMPL to solve the model, and calculate the optimal values of the decision variables.
The best way to get AMPL to solve is to create a run file, typically with just 3 lines, a line for the model, a line
for the data, and the solve command. See "run file" above. If you press F5 in Scite on a model, you are likely to
get all variables zero, because you haven't given AMPL the solve command first. So press F5 in the run file, not
the mod file.
Commonly used commands
The display command tells AMPL to show the value of a parameter or variable.
Example: "display x;" shows the value for the variable x. If x is a vector (e.g. was defined over a set), then AMPL
will display all the values for x. Many of these will be zero, which isn't too interesting. If you first give AMPL
the command "option omit_zero_cols 1;" AMPL will show only the nonzeroes. See "option omit_zero_cols 1;"
below.
Example: "display x;" shows the values for all components of the variable (vector) x.
Example: "display x[1,2];" shows the value for the variable x[1,2].
You can also use "display" to show the reduced costs and dual prices of a linear program. (Remember, dual
prices and reduced costs are not available in an integer program. The command will work, but the values should
be considered rubbish.)
Example: "display x.rc;" shows the reduced costs for the variable x.
Example: "display Budget.dual;" shows the dual price for a constraint named Budget.
INFORMS Transactions on Education 7:1(37-69)
59
The expand command tells AMPL to show an objective or constraint in long form.
Example: suppose your model has the objective "minimize Tourlength: sum {(i,j) in Arcs} length[i,j] * x[i,j];". At
the AMPL command prompt (or in your run file), you could type "expand Tourlength;". AMPL would then
display something like this:
minimize Tourlength: 210.117*x[1,2] + 1663.01*x[1,3] + 3845.68*x[1,4]
+ 2809.79*x[1,5] + 3455.81*x[1,6] + 1142.42*x[1,7] + 4307.28*x[1,8] + 2503.7*x[1,9]
+ ...
+ 857.03*x[16,19] + 611.945*x[16,20] + 3185.03*x[17,18] + 3379.88*x[17,19]
+ 4054.48*x[17,20] + 393.102*x[18,19] + 935.548*x[18,20] + 675.246*x[19,20];
Only it woudn't have the three dots, but would show the whole thing.
Example: suppose your model has the objective "subject to Degrees {i in Nodes}: sum {(i,j) in Arcs} x[i,j] + sum
{(j,i) in Arcs} x[j,i] = 2;". At the AMPL command prompt (or in your run file), you could type "expand Degrees[3];".
AMPL would then display something like this:
subject to Degrees[3]: x[1,3] + x[2,3] + x[3,4] + x[3,5] + x[3,6] + x[3,7]
+ x[3,8] + x[3,9] + x[3,10] + x[3,11] + x[3,12] + x[3,13] + x[3,14]
+ x[3,15] + x[3,16] + x[3,17] + x[3,18] + x[3,19] + x[3,20] = 2;
If you typed "expand Degrees;", you would get all of the Degrees constraints.
Common problems with running AMPL
Problem: AMPL says Error executing "commands" command: is already defined. Solution: all the sets and variables
from your previous run are still in memory. Just type "reset;" before running your model to clear all the previous
information.
Problem: AMPL says, syntax error. Solution: AMPL should point to the line with the problem. Check the statement
closely. Many times, a semicolon is missing, a parenthesis or bracket is missing, or you have misspelled a term.
Remember that AMPL is case sensitive.
Problem: when you run AMPL in the program panel for Scite, AMPL says, error processing set ...: no value for ...
or error processing param ...: no value for .... Solution: AMPL can't find your data, as it sees only the model file.
Run AMPL from within a run file, not a model or data file.
Problem: AMPL says that a parameter or variable is undefined. Solution: you are using a parameter, variable,
or subscript that AMPL doesn't understand. Example: "minimize Tourlength: sum {(i,k) in Arcs} length[i,j] *
x[i,j];" results in "j is undefined", because the sum{} statement has subscripts i and k, but no j. This could be
corrected either as "minimize Tourlength: sum {(i,j) in Arcs} length[i,j] * x[i,j];" or "minimize Tourlength: sum
{(i,k) in Arcs} length[i,k] * x[i,k];"
Commonly used options
The "option" command is used at the AMPL command prompt, in a run file, or at the beginning of a model file,
to give any of a variety of instructions to AMPL. These include which solver to use, how output is displayed,
and precision of calculation:
60
Tell AMPL which solver to use: option solver cplex; # This tells AMPL to use the commercial version of
CPlex. option solver cplexamp; # This tells AMPL to use the student version of CPlex. option solver minos;
# This tells AMPL to use the non-linear solver MINOS. option solver your_ampl_compatible_solver_here;
/* Perhaps you have another AMPL-compatible solver available, such as LP_Solve(183) or IPOPT(184).
Show or omit the variables that are zero: option omit_zero_cols 1; # The default is 0, which shows everything.
option omit_zero_rows 1;
Solve an IP as its continuous LP relaxation. Zero is the default, so if you don't specify anything, models
with variables defined as integer or binary are naturally solved as IPs. option relax_integrality 0; # Solve
the integer program. option relax_integrality 1; # Solve the LP relaxation.
Show output as a wide table rather than a table table. By default, "display" tries to make a table tall rather
than wide, e.g.
xxx
xxx
xxx
xxx
option display_transpose 0; # This is the default setting. A wide table with 2 rows and 4 columns will be
transposed. option display_transpose 1000; # This makes tables come out tall, and transposes if needed.
option display_transpose -1000; # This makes tables come out wide, and transposes if needed. In general,
if (number of rows) - (number of columns) < the display_transpose value, the table will be transposed.
option show_stats 1; # Tells AMPL to show the number of variables and constraints in the model.
option presolve_eps 1e-9; # Tells AMPL to use a precision of 10-9 when running its presolve.
(183) http://groups.yahoo.com/group/lp_solve/
(184) http://www.coin-or.org/Ipopt/documentation/node21.html
61
Note that both xcoord and ycoord are defined over the list of Nodes, 1..10. If we wish, we can use a more concise
format, as below. Note the extra colon after the word "param".
If we don't care about x,y coordinates, but instead want to give a list of lengths, then the model and data may
look like this:
param N; # Number of nodes
set Nodes := {1..N};
set Arcs := {i in Nodes, j in Nodes: i < j}; # Euclidean TSP. (i,j) same as (j,i), so don't use both.
param length {(i,j) in Arcs};
Then we need to declare N and length in a data file. Watch your colons! The dots are placeholders, that tell
AMPL that this value is undefined.
62
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:
(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
63
Piping. There is no piping command, but rather you can use the " < " and " > " symbols in a computer trick to
temporarily connect a file to be input to a program, or output from a program to become a file. Example: "display
x > myoutput.txt;" sends the current value of x to the file myoutput.txt. See also the "close", "read" and
"printf"commands.
The read command can be used to read data from a file. Example: "read N < TSPinput.txt;", where you have
a file called "TSPinput.txt;" with one number, and you want AMPL to read the number from the file and put it
into a parameter named N.
Example: "read N < TSPinput.txt; close TSPinput.txt;" Note the "close" command &em;. See also piping below.
The close command tells AMPL to close the file. This releases the operating system lock on the file, so the file
can be used by another program, such as AMPL's "read" command. It's always a good idea to close a file after
writing to it or reading from it. Trying to read a file that hasn't been closed may give the error "unexpected end
of file". You can get away without closing files most of the time, because AMPL will sometimes close the file
automatically (for example, if you exit from AMPL). But if your script writes to a file, then later reads from the
file, you will have to close it immediately after opening it.
You can use the "read" command to make AMPL slightly interactive, so it asks you for a value. Use a minus
sign for the file, and AMPL will read from the keyboard, waiting for you to type. Example:
printf "How many nodes are in this TSP problem?\n";
read N < - ;
Some commonly used AMPL functions
For help with strings, see Character Strings on AMPL.com(189).
Set oriented functions
card(set) Returns the number of elements in the set. Example: "let N := card(Nodes);", which sets the parameter
N to be the number elements in the set Nodes.
diff Set subtraction. Example: "let NotInTree := Nodes diff {v, u};" assigns the set NotInTree to be the same as
set Nodes, but leaving out elements v and u.
first(set) Returns the first element in the set.
in This operator specifies that the left hand side is contained in the right. Example: "subject to Degrees {i in
Nodes}: sum {(i,j) in Arcs} x[i,j] + sum {(j,i) in Arcs} x[j,i] = 2;". This tells AMPL that the constraint is defined
over all elements i that are contained in the set Nodes.
A inter B returns the intersection of A and B, which is the set of elements that are in both A and B.
last(set) Returns the last element in the set.
member(j, set) Returns the element in position j of the set.
(189) http://www.ampl.com/NEW/strings.html
64
The max and min functions may be used over scalars () or over sets {}. Don't confuse these wee functions with
the minimize and maximize key words, which are used to define objective functions.
max(x,y[,z,...]) Returns the largest of the arguments. Example: "let biggest := max(x[a,b], y[a,b]);" which assigns
to biggest the largest of two values.
min(x,y[,z,...]) Returns the smallest of the arguments. Example: "smallest_arc := min {(i,j) in Arcs} length[i,j];"
which scans the set of Arcs, and assigns to smallest_arc the length of the smallest arc.
next(t, S, n) Returns the element that is n positions after element t in the set S.
ord(t, S) Returns the position of element t in the set.
ord0(t, S) Returns the position of element t in the set, and returns 0 if t is not in the set S.
prev(t, S, n) Returns the element that is n position before element t in the set S.
union This operator does set addition. Example: "let Nodes := Nodes union {i,j};" adds elements i and j to set
Nodes Note the curly brackets, which tell AMPL that i and j are set objects. If you add sets with the same elements,
the resulting set will not have copies.
within This operator specifies that one set is contained in another. Example: "set Destinations within Cities;"
tells AMPL that each element in the set Destinations is also in the set Cities. If your data or script does not satisfy this condition, then AMPL will let you know.
Numerical functions
abs(x) Returns the absolute value of x. Example: "let y := abs(-3);" will set y to 3.
acos(x)
acosh(x)
asin(x)
asinh(x)
atanh(x)
ceil(x) Rounds x up to the nearest higher integer.
cos(x) Returns the cosine of x.
ctime() Prints the current time as a string. Try using "printf "Start time %s.\n", ctime();"
exp(x) Returns e^x.
floor(x) Rounds x down to the nearest lower integer.
log(x) Natural log of x.
INFORMS Transactions on Education 7:1(37-69)
65
(190) http://www.ampl.com/FAQ/
66
You can also send output from the display command to a file. Inside your .RUN file, you could type: display
x > myoutput.txt;
This will put the x values in the file myoutput.txt.
When I tried to use piping to save the output, I typed in ampl: commands part2.run > output.txt but this did
not work. I opened the DOS prompt and typed in c:\ > p:\msci315\ampl part2.run > output.txt The AMPL
code ran, but the file output.txt contains the error message "can not open part2.run". How can I solve this
problem?
If AMPL cannot open part2.run, then the problem is that the file part2.run is not in the local directory c:\ > ,
displayed when you typed the MS-DOS command. You have three files: the AMPL executable (which is in
p:\msci315\, the part2.run file (which the computer thinks is in c:\ > ) and output.txt, which the computer will
try to write to c:\ > .
Try being precise about where files are read from, and where they should be written to, such as:
p:\msci315\ampl a:\part2.run > a:\output.txt
Instead of the A: drive, you can use any drive and directory for which you have read and write access.
When I tried to use piping to save the output, I typed in ampl: commands part2.run > output.txt but this did
not work. I opened the DOS prompt and typed in c:\ > p:\msci315\ampl part2.run > output.txt The AMPL
code did not run. I got the error message, "Access is denied." What's wrong?
"p:\msci315\ampl part2.run > output.txt" did not work because you do not have write access to p:\msci315\.
Try "p:\msci315\ampl part2.run > a:output.txt" with a disk in the A: drive, or try refer to a directory to which
you have write access.
When I run the cutting stock problem, AMPL complains that my parameter fails an integrality check. What
is wrong?
AMPL is probably using MINOS to solve the problem. MINOS does not solve integer programs.
You need to have AMPL call CPlex instead. To make AMPL call CPlex, add the following line at the beginning
of your .RUN file: option solver cplex;
What does the following line mean? I understand it is doing primal retrieval, but I don't understand the %i
and \n. printf "x[%i] = %g\n", i, sum {k in 1..K} theta[k]*x_fxd[k,i];
The printf statement prints a string to the screen. For example,
printf "hello";
does the obvious.
printf "hello"; printf "hello";
will show
67
hellohello
on the screen. To get it to do a carriage return between "hellohello", we need to use
printf "hello\n"; printf "hello\n";
The "\n" tells AMPL to insert a carriage return.
We would also like to print data in the statement sometimes. If we want to print an integer such as 2005, we
could use %i as a place-holder, to indicate where we want the data to go:
let year := 2005; printf "The year is %i.\n", year;
AMPL should print
The year is 2005.
If we want to print a floating point number, such as the number pi, we can use %f or %g as a place-holder to
indicate where the data should go.
let my_pi := 3.1415;
printf "Today, I feel like my_pi = %g.\n", my_pi;
AMPL should print
Today, I feel like my_pi = 3.1415.
If you try to use %i for a floating point number or vice versa, AMPL is likely to print rubbish.
When solving the decomposition, the master and subproblem don't converge, and so the algorithm keeps
repeating. What could be wrong?
Check the objective value calculations. You could always solve the original linear program, so you know what
the correct value is. That would tell you which one is wrong (subproblem or master).
Use the display command to show the values, so you can see them as the algorithm runs. This will help determine
when an error occurs.
How do I get AMPL to make an MPS file?
Let's say your model is "yourmodel.mod" and your data is "yourdata.dat". Then at the AMPL prompt, you
would type:ampl: model yourmodel.mod;ampl: data yourdata.dat;ampl: write myourmps;
This will write an MPS file called "yourmps.mps" to the local directory. Unfortunately, all the row and column
name information is lost. You can get AMPL to at least write out a file with a list of the names:ampl: model
yourmodel.mod;ampl: data yourdata.dat;ampl: option auxfiles rc;ampl: write myourmps;
Now you get two extra files, "yourmps.col" and "yourmps.row", which list the names of the columns and rows.
You would have to match up the numbers with the names.
INFORMS Transactions on Education 7:1(37-69)
68
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 trademark
of AMPL Optimization LLC.
(191) http://ite.pubs.informs.org/Vol7No1/LeeRaffensperger/Scite/amplwizard.hta
69