In this video, I'll give you a quick start guide about AMPL, and introduce how to use
AMPL to model and solve simple LP problems.
We use this problem as an example.
This is a maximization problem with two decision variables, x1 and x2.
We can solve it graphically by hand.
This is the constraint M1, and this is the constraint M2.
This shaded area is the feasible region.
This point A (2.67, 2.67) is the optimal point.
The objective function z value at this point is 1333.33.
Now, let's see how we can model this problem and solve it in AMPL.
In order to solve a problem, we need to create three different types of AMPL files, including
a model file ".mod", a data file ".dat", and a command file ".run".
The model file contains the problem formulation, the data file contains the values of the parameters,
and the command file contains the information about which solver to use to solve the problem
and how to display the solutions.
The data file is optional and sometimes the parameters can be integrated directly into
the model file.
AMPL reads the model from the .mod file, data from the .dat file, and puts them together
into a format that the solver understands.
Then, it hands over this problem instance to the solver, which in turn, solves the instance,
and hands back the solution to AMPL for display.
First of all, you need to start AMPL IDE.
If you don't know how to do that, you need to watch a previous video titled AMPL - Download
and Installation.
The AMPL IDE can be divided into three sections.
The left side shows the Current Directory.
I usually put my program files in my Downloads folder.
You may choose any folder that works for you.
In the middle, we have a console or command window.
AMPL is a "command line" system with a prompt "ampl:".
You can type the code directly in this command window for AMPL to execute.
The right side is a text editor.
You can write you code and save it on you computer for later use.
In addition, we have a few menus and a few toolbar buttons.
I have already created the files needed for this example and you can download them using
the links below this video.
After downloading, you can put these files in the Downloads folder.
You can choose a different folder if you want.
In AMPL, make sure your current directory is the folder you choose to put these files.
If your files do not show up in the folder, you can click this refresh button.
Now, double click to open the model file example1.mod.
AMPL syntax is very close to the mathematical expressions of the problem formulation.
The # indicates the start of a comment.
Everything after that symbol will not be executed.
I use comments to clearly indicate different parts of the problem, the variables, the objective
function, and constraints.
Variables must be declared using the var keyword.
All lines of code must end with a semi-colon.
The objective function starts with maximize or minimize, followed by a name and a colon
(:). After that, the objective function appears.
Each constraint starts with "s.t.", which is short for "subject to", followed by a constraint
name and a colon.
After that, the equation or inequality appears.
Names must be unique.
Keywords (such as var, maximize, and s.t.) must be in lower case.
AMPL is case sensitive.
That means, a upper case letter A is not the same as the lower case letter a.
We don't need to create a separate data file for this example because all the parameters
(such as the coefficients of the objective function, the coefficients of the constraints,
and the right hand side of the constraints) are already integrated in this model file.
Now, double click to open the command file example1.run.
First, we reset the AMPL environment using the reset command.
Then we load the model file example1.mod.
We can also choose a different solver if we want.
Right now, we are solving the LP problem using the CPLEX solver.
Then, we solve the problem using the command solve, and display the results for optimal
decision variables x1, x2, and the objective function z.
You can use the model file and the command file I showed here as a template, and modify
them for new problems.
After we are done with creating or modifying these files, we need to save it by clicking
the menu File-Save or Save as.
Now, in the middle command window, we type in the command "include example1.run;" and
then hit Enter to run the model and get the results.
The optimal solutions is x1 = 2.67, x2 = 2.67, and z = 1333.33, which is the same as the
graphical solution we found by hand.
Okay, that is how to use AMPL to model and solve simple LP problems.
Thanks for watching.