Tags:
view all tags
<!-- Ready to Review --> ---+<a name="top"></a>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=. <img src="%ATTACHURLPATH%/ampl.jpg" alt="ampl.jpg" width='669' height='338' /> You will then be able to enter AMPL commands at the AMPL prompt. <img src="%ATTACHURLPATH%/ampl_examples.jpg" alt="ampl_examples.jpg" width='669' height='122' /> [[#top][Return to top]] ---++<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> <pre> display {i in INGREDIENTS} Percentage[i]; </pre> If you forget the =;=, then the =ampl:= prompt will be replaced by the =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 =;=. AMPL will then interpret your AMPL command and display any problems. [[#top][Return to top]] ---++<a name="model"></a>The Model Environment AMPL encourages a clean modeling style by explicitly separating the model formulation from the particular problem data. The model formulation, as written in the AMPL syntax, can be saved as a =.mod= file. This file contains all set, variable, and parameter definitions, as well as all constraints and the objective function. *It should not contain any problem specific data or values relating to the actual real world problem you are solving.* The AMPL model is input after switching to the (default) model environment, by typing =model;= and then using the =include= command to import the model file, or else typing in the commands for the AMPL model directly at the =ampl:= prompt. <img src="%ATTACHURLPATH%/model_include.jpg" alt="model_include.jpg" width='669' height='98' /> Alternatively, you can type =model <filename>.mod;=. <img src="%ATTACHURLPATH%/model_file.jpg" alt="model_file.jpg" width='669' height='86' /> [[#top][Return to top]] ---++<a name="data"></a>The Data Environment All data and values specific to the problem instance you are solving are separated from the model formulation when using AMPL. This is enforced by the method used to input the problem details into AMPL - the model formulation is entered in the model environment, the problem specific data is entered in the data environment. To switch from the model environment to the data environment one types =data;= at the AMPL prompt =ampl:= (and switches back to the model environment by typing =model;=). <img src="%ATTACHURLPATH%/model_data.jpg" alt="model_data.jpg" width='669' height='86' /> As with entering the model formulation, it is common to store the data for a specific problem in a data file (=<filename>.dat=). This data is then imported while in data mode by using the =include= command, followed by the filename and a semicolon (or by typing the AMPL commands in directly). <img src="%ATTACHURLPATH%/data_include.jpg" alt="data_include.jpg" width='669' height='86' /> Again, one can use the alternative method of typing =data <filename>.dat= (which does not require a switch from the model environment). <img src="%ATTACHURLPATH%/data_file.jpg" alt="data_file.jpg" width='669' height='86' /> [[#top][Return to top]] ---++<a name="script"></a>Scripting in AMPL 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 =.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: <pre> reset; model <modelname>.mod; data <dataname>.dat; . . . </pre> and you run this file using the =include= command. <img src="%ATTACHURLPATH%/script_file.jpg" alt="script_file.jpg" width='669' height='206' /> ---+++Setting Data Dynamically AMPL allows you to set data "on the fly" using the =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, <pre> reset; model; param a; param b; param maxab := max(a, b); data; param a := 1; param b := 2; # Sets maxab = 2 display a, b, maxab; # Shifts AMPL back to the model environment let a := 3; # Sets a and maxab = 3 display a, b, maxab; let maxab := 4; # Syntax error, parameter defined by an expression display a, b, maxab; </pre> <img src="%ATTACHURLPATH%/dynamic_error.jpg" alt="dynamic_error.jpg" width='669' height='230' /> The previous example also showed how to use both the [[#model][model]] and [[#data"][data]] environments in the same script file. Furthermore, rather than using =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 =display= command). [[#top][Return to top]] ---++<a name="files"></a>Notes on AMPL Files ---+++ Editing AMPL files in Windows The easiest way to edit AMPL files is using a text editor such as _Wordpad_. To open _Wordpad_ go to the _start_ menu, select _Programs_, then _Accessories_ and finally _Wordpad_ <img src="%ATTACHURLPATH%/run_wordpad.jpg" alt="run_wordpad.jpg" width='721' height='690' /> When you have finished editing, save the file in the appropriate directory. Be sure to save your file as a basic text file *%RED%NOT%ENDCOLOR%* in Rich Text Format. <img src="%ATTACHURLPATH%/save_ampl.jpg" alt="save_ampl.jpg" width='563' height='435' /> ---+++ Running AMPL files Once you have finished editing your AMPL files, you can run them by 1. Either [[CommandLineHowTo][starting a command line application]] or [[InstallingAMPL#batch][double-clicking on =setup_ampl.bat=]] (if you are at home); 2. Navigating to the directory where you saved the files; 3. [[#start][Starting AMPL]] 4. Typing AMPL commands, e.g., =model <modelname>.mod;=, =data <dataname>.dat;=, =include <runname>.run;= [[#top][Return to top]] -- Main.MichaelOSullivan - 25 Feb 2008
Edit
|
Attach
|
Watch
|
P
rint version
|
H
istory
:
r10
<
r9
<
r8
<
r7
<
r6
|
B
acklinks
|
V
iew topic
|
Raw edit
|
More topic actions...
Topic revision: r7 - 2008-03-02
-
MichaelOSullivan
Home
Site map
Forum web
Main web
NDSG web
ORUA web
OpsRes web
Sandbox web
TWiki web
OpsRes Web
Create New Topic
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
P
View
Raw View
Print version
Find backlinks
History
More topic actions
Edit
Raw edit
Attach file or image
Edit topic preference settings
Set new parent
More topic actions
Account
Log In
Edit
Attach
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