---+ The AMPL Syntax Guide *%RED%Disclaimer%ENDCOLOR%* This is a _*guide*_ to the AMPL syntax only. For a comprehensive reference to the AMPL language see [[href="https://www.ampl.com/BOOK/index.html][AMPL: A Modeling Language for Mathematical Programming]]. <ol> <li><a href="#comments">Commenting</a> <li><a href="#symbolic">Using {\tt symbolic}</a> <li><a href="Sets in AMPL">Sets</a> <li><a href="Variables in AMPL">Variables</a> <li><a href="Parameters in AMPL">Parameters</a> <li><a href="Expressions in AMPL">Expressions</a> <li><a href="Objective Functions in AMPL">Objective Functions</a> <li><a href="Constraints in AMPL">Constraints</a> <li><a href="Logic in AMPL">Logical Statements</a> <li><a href="Looping in AMPL">Looping Statements</a> <li><a href="Printing in AMPL">Displaying and Printing</a> <li><a href="Miscellaneous AMPL">Miscellaneous AMPL Commands</a> </ol> <a name="comments"><h3>Commenting</h3></a> The {\tt #} is used for commenting, any characters after the {\tt #} are ignored by AMPL. <h4>Examples</h4> <a href="A Blending Problem">The Whiskas Cat Food Problem</a> <p>\begin{verbatim} var x1 >= 0; # Percentage chicken meat used in 100g of cat food (= g) \end{verbatim}<p> <a href="A Price-Production Problem">The Surfboard Production Problem</a> <p>\begin{verbatim} param Resin {SURFBOARDS} >= 0; # Amount of resin (in kilos) needed for a board param TotalResin >= 0; # Total amount of resin available \end{verbatim}<p> <a name="symbolic"><h3>Using {\tt symbolic}</h3></a> The {\tt symbolic} keyword is used to create parameters that represent character strings. Usually they are used to represent members of 1-dimensional sets or part of a <i>tuple</i> for multi-dimensional sets. However, they can also be used to represent file names or to automatically build labels (for entry in a set, for example). <h4>Example. Searching a 1-Dimensional set</h4> <p>\begin{verbatim} param gotcha symbolic within SET; for {s in SET} { if <condition> then let gotcha := s; } # gotcha now contains the (last) element in SET that satisfies the condition \end{verbatim}<p> <h4>Example. Searching a 2-Dimensional Set</h4> <p>\begin{verbatim} set SET1; set SET2; set TWO_D_SET within SET1 cross SET2; param got_one symbolic within SET1; param got_two symbolic within SET2; for {(s, t) in TWO_D_SET} { if <condition> then { let got_one := s; let got_two := t; } } # (got_one, got_two) now contains the (last) element in TWO_D_SET # that satisfies the condition \end{verbatim}<p> To build labels for set elements you need a {\tt symbolic} parameter and the parts of the label. You can then "glue" the parts of the label together by enclosing the label within {\tt (} and {\tt )} and joining the parts with {\tt &}. The parts can be set elements, strings or numbers. Numbers are converted to strings using the <a href="https://www.cppreference.com/stdio/printf.html">{\tt %g} format from {\tt printf}</a>. <h4>Example. Dynamically Building a Set</h4> <p>\begin{verbatim} param name symbolic; let SUPPLY_NODES := {}; for {s in SUPPLY_LOCATIONS} { let name := (s & '-Woodchips'); let SUPPLY_NODES := SUPPLY_NODES union {name}; let Supply[name] := MaterialSupply[s, 'Woodchips']; let {d in DEMAND_NODES} Cost[name, d] := TransportationCost[s, d]; let dummyDemandCost[name] := DisposalCost['Woodchips']; } \end{verbatim}<p> <a href="Sets in AMPL"><font size=+1>Sets</font></a> <a href="Variables in AMPL"><font size=+1>Variables</font></a> <a href="Parameters in AMPL"><font size=+1>Parameters</font></a> <a href="Expressions in AMPL"><font size=+1>Expressions</font></a> <a href="Objective Functions in AMPL"><font size=+1>Objective Function</font></a> <a href="Constraints in AMPL"><font size=+1>Constraints</font></a> <a href="Logic in AMPL"><font size=+1>Logical Statements</font></a> <a href="Looping in AMPL"><font size=+1>Looping Statements</font></a> <a href="Printing in AMPL"><font size=+1>Displaying and Printing</font></a> <a href="Miscellaneous AMPL"><font size=+1>Miscellaneous AMPL Commands</font></a> -- Main.MichaelOSullivan - 27 Feb 2008
This topic: OpsRes
>
WebHome
>
AMPLGuide
>
AMPLSyntax
Topic revision: r1 - 2008-02-27 - MichaelOSullivan
Copyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback