---+ The AMPL Process * [[#start][Starting AMPL]] * [[#commands][AMPL Commands]] * [[#model"][The Model Environment]] * [[#data][The Data Environment]] * [[#script][Scripting in AMPL]] * [[#files][Notes on AMPL Files]] ---++<a name="start"></a>Starting AMPL AMPL is a command line environment, i.e., you enter [[#commands][commands]] at the AMPL prompt and AMPL will decipher these commands and perform the appropriate actions. To start AMPL in Windows you simply [[CommandLineHowTo][start a command line application]] and type =ampl=. src="ampl.jpg" You will then be able to enter AMPL commands at the AMPL prompt. src="ampl_examples.jpg" ---++<a name="commands"></a>AMPL Commands AMPL commands consist of statements in the [[AMPLSyntax][AMPL syntax]] terminated with a =;=. Some examples include: <pre> set INGREDIENTS; </pre> <pre> data; </pre> ??? Up to here ??? <p>\begin{verbatim}<br />display {i in INGREDIENTS} Percentage[i];<br />\end{verbatim}<p><br />If you forget the {\tt ;}, then the {\tt ampl:} prompt will be replaced by the {\tt ampl?} prompt, indicating that your AMPL command has not been finished. You can finish your command or, if you are not sure how to finish it, type {\tt ;}. AMPL will then interpret you AMPL command and display any problems.<br /><br /><a name="model"><h3>The Model Environment</h3></a><br />AMPL encourages a clean modeling style by explicitly separating the model formulation<br />from the particular problem data. The model formulation, as written in the AMPL syntax,<br />can be saved as a {\tt .mod} file. This file contains all set, variable, and parameter definitions,<br />as well as all constraints and the objective function. <b> It contains no problem specific data<br />or values relating to the actual real world problem you are solving.</b><br /><br />The AMPL model is input after switching to the (default) model environment, by typing {\tt model;}<br />and then using the {\tt include} command to import the <a href="#files">model file</a>, or else<br />typing in the commands for the AMPL model directly at the {\tt ampl:} prompt.<br /><p><img src="model_include.jpg" /><br /><p>Alternatively, you can type {\tt model <filename>.mod;}. <br /><p><img src="model_file.jpg" /><br /><br /><a name="data"><h3>The Data Environment</h3></a><br />All data and values specific to the problem instance you are solving are separated from the model<br />formulation when using AMPL. This is enforced by the method used to input the problem details into<br />AMPL - the model formulation is entered in the model environment, the problem specific data is entered<br />in the data environment. To switch from the model environment to the data environment one types {\tt data;}<br />at the AMPL prompt {\tt ampl:} (and switches back to the model environment by typing {\tt model;}).<br /><p><img src="model_data.jpg" /><br /><p>As with entering the model formulation, it is common to store the data in a data file ({\tt <filename>.dat}). This data is then imported using the {\tt include} command, followed by the filename and a semicolon<br />(or by typing the AMPL commands in directly).<br /><p><img src="data_include.jpg" /><br /><p>Again, one can use the alternative method of typing {\tt data <filename>.dat}<br />(which does not require a switch from the model environment).<br /><p><img src="data_file.jpg" /><br /><br /><a name="script"><h3>Scripting in AMPL</h3></a><br />A script is a text file used to run a number of AMPL commands. It is useful when you may wish to run the same commands multiple times, possibly with slight editing changes. It is also useful for keeping track of what you have done, or if you may wish to rerun your models at a later date. Script files run in the model environment, and are usually named with a {\tt .run} extension. It is good practice to get into the habit of using script files for your AMPL sessions. A typical script file should begin:<br /><p>\begin{verbatim}<br />reset;<br />model <modelname>.mod;<br />data <dataname>.dat;<br />.<br />.<br />.<br />\end{verbatim}<br /><p>and you run this file using the {\tt include} command.<br /><p><img src="script_file.jpg" /><br /><br /><h4>Setting Data Dynamically</h4><br />AMPL allows you to set data "on the fly" using the {\tt let} keyword. If you have a set, parameter or variable as part of your model and defined it using a data file (i.e., you DID NOT USE AN EXPRESSION to set a value for it), then you can change that structure on the fly with let. For example,<br /><p>\begin{verbatim}<br />reset;<br /><br />model;<br /><br />param a; param b;<br />param maxab := max(a, b);<br /><br />data;<br /><br />param a := 1;<br />param b := 2; # Sets maxab = 2<br /><br />display a, b, maxab; # Shifts AMPL back to the<br /> # model environment<br /><br />let a := 3; # Sets a and maxab = 3<br />display a, b, maxab;<br /><br />let maxab := 4; # Syntax error, parameter defined by an<br /> # expression<br />display a, b, maxab;<br />\end{verbatim}<br /><p><img src="dynamic_error.jpg" /><br /><br />The previous example also showed how to use both the <a href="#model">model</a> and <a href="#data">data</a> environments in the same script file. Furthermore, rather than using {\tt model;} to enter the model environment (and leave the data environment), it jumps from the data environment to the model environment by using a command that is not valid in the data environment (the {\tt display} command).<br /><br /><a name="files"<h3>Notes on AMPL Files</h3></a><br /> <br /><h4>Editing AMPL files</h4><br />The easiest way to edit AMPL files is using a text editor such as <i>Wordpad</i>. To open <i>Wordpad</i> go to the <i>start</i> menu, select <i>Programs</i>, then <i>Accessories</i> and finally <i>Wordpad</i>.<p><br /><p><img src="run_wordpad.jpg" height=680 /><br /><br />When you have finished editing, save the file in the appropriate directory. Be sure to save them as a basic text file <i>NOT</i> in Rich Text Format.<br><br /><p><img src="save_ampl.jpg" /><br /> <br /><h4>Running AMPL files</h4><br />Once you have finished editing your AMPL files, you can run them by<br /><ol><br /><li> Either <a href="Command Line How-To">starting a command line application</a> or <a href="Installing AMPL#batch>double-clicking on {\tt setup_ampl.bat}</a> (if you are at home);<br /><li> Navigating to the directory where you saved the files;<br /><li> <a href="#start">Starting AMPL</a>;<br /><li> Typing AMPL commands, e.g.,<br />\begin{verbatim}<br />model <modelname>.mod;<br />\end{verbatim}<br />\begin{verbatim}<br />data <dataname>.dat;<br />\end{verbatim}<br />\begin{verbatim}<br />include <runname>.run;<br />\end{verbatim}<br /> -- Main.MichaelOSullivan - 25 Feb 2008
This topic: OpsRes
>
WebHome
>
AMPLGuide
>
AMPLProcess
Topic revision: r1 - 2008-02-25 - 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