Vous êtes sur la page 1sur 5

IEMS 313 Lab 1 - Getting started with AMPL

Fall 2017

1 The Model
You will discuss how to formulate linear programs in class. However, in this lab we will focus on implementing a
simple linear program using AMPL IDE and not on how to formulate it. The model we will consider is:

min 2x1 + 3x2


s.t.
1
x1 + x2 1
2
4x1 + x2 0
x1 , x2 0

2 Using AMPL in the IEMS lab


Skip this section if you are using your own laptop. AMPL has already been installed on all lab desktops. You can
choose to use the lab desktop to finish lab assignments and homework instead of your personal computer.

1. Log in with your NetID and password.


2. Open a file explorer and go to the following path: C:\Program Files\amplcml\amplide.
3. Open AMPL IDE (amplide.exe).

Figure 1: AMPL IDE file path.

4. For consistency, all lab documents are written according to the hardware/software configurations of the
machines in C135 (the lab room).

1
3 First LP in AMPL
1. The first step every time you open AMPL is to setup your working directory. Your working directory is
where you will save the files you are working on and where the output of your models will be written.
2. Create a new folder at C:\Users\<your netID> and name it IEMS313 Lab (notice the underscore, dont
use space). The example henceforth uses my netID: ddv4817.

Figure 2: Creating a working directory folder.

Figure 3: Creating a working directory folder.

3. Click on the folder you just created. To write a model in AMPL, we need to create a .mod file. Right click
on the file explored and create a File.

2
Figure 4: Creating a file

4. Name your file as simpleLP. There is no need of adding the .mod extension in the file name. Notice that
the file extension is set to .mod by default.

Figure 5: Creating a model file (.mod)

5. In the new file, type the model as shown in Figure 6. AMPL is an algebraic language, so we can type a model
in AMPL almost as we write it on paper. Nonetheless, we still need to use few AMPL reserved words:
var: denotes a new decision variable.
s.t.: denotes a constraint (or set of constraints).
minimize: denotes the model sense and relates de objective function.
It is important to finish every line with a semicolon (;). Also, we need to name every expression by typing a
name followed by a colon (:). In this small LP, the objective function is named as obj, and the constraint
are named as c1 and c2, respectively.

3
Figure 6: Writing the model.

6. To execute the model, type the following commands on the AMPL shell (see Figure 7). Remember to finish
every command with a semicolon.

option solver gurobi;


model simpleLP.mod;
solve;
display x1>simpleLP.ans;
display x2>simpleLP.ans;

The first command establishes the solver we will use (AMPL is just the language, we still need an optimization
solver). In this case, we are using gurobi, but more options are available; feel free to try cplex or knitro. The
second command loads the model we want to solve. The third command solves it. Finally, the last two
commands output the solution to a text file.

Figure 7: Executing the model.

7. The last two commands printout the solution of the model in a new file. Refresh the file explorer and open
the file simpleLP.ans.

Figure 8: Displaying the solution.

If you are done, start with the homework. Dont forget to email to yourself the files you would like to keep.

4
4 Troubleshooting
If you have problems logging in, please contact your course instructor or the IEMS System Administrator,
Johnathan Gaetz, in C145 or by email at arioch@iems.northwestern.edu.
Files saved on the lab machines are cleared periodically. If you want to create a working directory that lasts
for the duration of the course, please do so in the S: drive (the shared drive). Folders on the S: drive can
only be accessed by the accounts from which the folders were created, i.e., you cannot access other peoples
folders.
To avoid compilation problems, dont use spaces in folder or file names.
If you see ampl? after typing a command, it is probably that you forgot the semicolon (;) at the end of the
line. Just type it and hit enter.

The command reset; is useful to start over. For example, when you re-load a model, you are likely to see an
error saying x is already defined, where x is a variable in your model. In this case, you need to reset
AMPL.