AMPL Handout

advertisement
AMPL Handout
In this section you will learn the absolute basics of AMPL. You do not need to take notes since all the
important points that we will stress are in this handout. First we go together through Step 1. Then you are
asked to go through the rest on your own. As you read, do all the AMPL steps. If you do not understand
something, or have problems with doing it, let us know. We will come and help. We are not expecting from
you that you complete all the work by the end of the section. You should finish the work by the due date.
This is very important. Practice is the only way to become comfortable using AMPL.
In this section we do not assume you have read any part of the AMPL book. In the future please read the
parts of the AMPL book that we will assign for reading; this way you will have a much easier time during
the upcoming AMPL sections. You will need practice to feel comfortable using AMPL, so do not freak out
if you are having problems.
1. AMPL is a tool that helps you to model (hopefully) real-life problems as linear, integer and non-linear
programs and solve them. It makes a clear distinction between modeling and actually solving these
kinds of problems. The model and data are two separate entities. In order to illustrate this and main
features of AMPL, let us look at the following simple production mix problem: A car manufacturer has
to decide how many cars to produce. There are different models, each of which brings a net profit per
car. A car from a particular model needs a certain amount of machining time to be completed. The
floor shop of this manufacturer can only provide a fixed amount of machine service time. How would
you model this as an LP?
AMPL model is as follows.
set MODELS; # models the manufacturer can produce
# the total machine service time available
param machine service >= 0;
param profit {i in MODELS} >= 0;
# profit per car
param machine time { i in MODELS} >= 0; # machine time per car
var x {i in MODELS} >= 0;
maximize Profit: sum {i in MODELS} profit[i] * x[i];
subject to Machine limit: sum {i in MODELS} machine time[i] * x[i] <=
machine service; # machine time resource constraint
Now let’s go through each statement of this model.
(a) The part of a line after the # symbol is a comment and AMPL will not read it. Each AMPL line
must end with a ; symbol. It is not important where you break a line. AMPL treats any sentence
that ends with a ; as a line. This means that an AMPL line can be many lines of text in the file.
(b) set MODELS;, declares that there is a set called MODELS, the set of models the manufacturer
can produce.
(c) param machine service >= 0;, tells us that there is a parameter called machine service.
This is the maximum amount of machining time that the manufacturer can provide.
The expression >= 0 indicates that machine service is not allowed to be negative. Although
this is not needed, it is a good practice to tell AMPL in what range a parameter can have its
values. AMPL will complain if you give (in your data file) a machine service time that is negative.
(d) param profit {i in MODELS} >= 0;
param machine time { i in MODELS} >= 0;
state that there will be two parameters for each model, its profit and its machine time. The
expression {i in MODELS} is the way that AMPL indicates that we have such a parameter for
each model i. We will refer to this as indexing: we are giving an index to these two parameters.
Since we are not using the index i later in these lines we could have written
param profit {MODELS} >= 0;, instead.
If you want to refer to the profit or machine time of a model i, you should use profit[i] or
machine time[i]. For example, see the objective function and the constraint in the model.
(e) var x {i in MODELS} >= 0; defines a non-negative variable for each model. As in the case of
the parameters, if you want to refer to the variable of a particular model i, you should use x[i].
(f) maximize Profit: sum {i in MODELS} profit[i] * x[i];
This line is the objective function. In AMPL the objective function must have a name. It is
Profit (with a capital P) in our case. Notice that we needed a : after the name.
The expression sum {i in MODELS} profit[i] * x[i]; says that the product profit[i] * x[i]
should be summed up over all models i. This statement corresponds to the following mathematical
formulation:
X
prof iti xi
i
where the sum is taken over all models.
Note that in AMPL you have to write out the * sign explicitly for multiplication.
(g) subject to Machine limit:sum {i in MODELS} machine time[i]*x[i] <= machine service;
This last line is a constraint. As the objective function, it must have a name. In our particular
model we have only one constraint, but in general there will be many. Every model entry (parameters, variables, constraints etc.) must have a different name. AMPL is case sensitive, hence
the parameter profit and the name of the objective function Profit are two different objects.
2. Now you will start using the computer. If your machine is off, turn it on. The computer will load the
Windows environment and will ask for a username and password. Ignore this by clicking on Cancel
on the little authorization window. Now you can start the Windows version of AMPL, the so-called
AMPLPlus. You can activate it by clicking Start button and then going to Programs submenu and
clicking on AMPLPlus. AMPLPlus will be loaded in a few seconds. When it is done, go ahead and
check out what is there in the pulldown menus at the top left corner.
3. To use AMPLPlus for the car manufacturer’s problem you will need a model file, a file containing the
lines describing our model given above. You will need to use an editor to create this file. AMPLPlus
uses by default Notepad as editor. Actually any editor that saves plain text files will do. The easiest
way to create new files or edit old ones is to use File pulldown menu on the AMPLPlus menu bar.
The common way of naming the model files is with an extension .mod. For example the model file of
our problem could be prod.mod. Using File menu open a new file and type in the model. Remember
that AMPL is very sensitive to ; which ends lines and to : which comes after names. It is also case
sensitive. Once you are done with typing, save it (say as prod.mod in the top directory of the disk
that you brought with you, in drive a:).
4. If you created and saved the model file you are ready to load it into AMPL. On the menu bar, go
into Run pulldown menu and choose Build Model. This will make AMPL read your model file. If
there are multiple model files, through a window AMPL will ask you which file you like to be loaded.
If there are no syntax errors, typos etc. in your model, AMPLPlus will read it succesfully and report
this in a window. If there are errors, you need to click on the Commands window (which must be
floating around somewhere at the bottom of your screen). Read the AMPL error messages (if there
are any), go back to your model file, fix the errors and save the new file and feed it again into AMPL.
You might need to go through this process a couple of times until there are no more errors.
5. So far you have entered the algebraic model. The algebraic model captures all problem instances of the
same problem. But if you want to solve a particular instance of this simple production mix problem
you need numbers for machine time, profit etc. In particular, AMPL needs these numbers too. The
way to tell AMPL what these numbers are is via a data file. So create the following file which we name
prod.dat (The convention is that the data files are named with an extension .dat).
set MODELS := FourDoor TwoDoor Hatchback Minivan Truck;
param machine service := 40000;
param profit :=
FourDoor 1500
2
TwoDoor 1600
Hatchback 1650
Minivan 2000
Truck 2300
;
param machine time :=
FourDoor 35
TwoDoor 32
Hatchback 32
Minivan 38
Truck 40
;
Once you have a data file, use the Run menu to Build Data. This is the way to make AMPL read
your data file. Once again AMPL might complain about typos; fix them and let AMPL read the new
data file.
6. When you have entered both model and data files succesfully, using the Run menu you can solve the
problem. AMPL provides many options on which LP solver to use. You can alter your choice of solver
using the pulldown menu Solver. Use XLSOL as the solver. Another popular solver is CPLEX. Go
to Run menu and solve the problem using Solve Problem. AMPL should obtain a solution with
objective value 2300000 and report it in its Solver Status window. Most of the time we will be
interested in the optimal solution itself. In order to see the optimal solution for this problem, go to the
Commands window and type display x; at the AMPL prompt. Another way of doing this is using
Build Results option inside Run menu. The information you need (and you don‘t need) will be sent
into a results file and it will be displayed on your screen. Try this as well.
7. This is it. We went through the absolute basics of AMPL. From this point on, you can read more
about AMPL from the AMPL book and actually use it to improve your skills and learn what else one
can do with AMPL. The model we discussed is very simple, it has a single constraint, and uses one set
only. In general the models will be more complex. Next we are going to add to the model and change
it to introduce you to some more features of AMPL, and improve your AMPL skills. We will make a
number of different changes to the above model and data file, so save these files before you continue.
You will need to come back to the current form of these files later.
A word on using the Commands window: every command that is described in the AMPL book can be
fed to AMPL through this window. At the AMPL prompt you can type any valid command followed
by a ;.
8. Now assume that the manufacturer can produce up to a certain number of each model. Declare a
new parameter for each model that indicates the number of cars of each model the manufacturer may
produce. Declare the new parameter by the line param car limit {i in MODELS};.
Change the variable declaration to allow it to have values up to this bound. This is done by the line:
var x {i in MODELS} >= 0, <= car limit[i];.
This line will work only if the declaration for the parameter car limit precedes the variable declaration in the file.
9. Make up data for the new parameter, add it to the data file, and solve the new problem.
10. Go back to the original version of the problem. Now assume that the manufacturer can produce the
cars in different colors. Declare a set COLORS, which is the set of colors the models can be painted
with. Assume that the manufacturer can provide machine service amount of time for each color
separately.
3
(a) Assume that the manufacturer can provide different machine service for different color types. So
change the definition of machine service to accomodate this change.
(b) How do your variables look now? Make sure that you understand that it is not enough to have a
variable for each model only, and that we need to know in which color a car is produced. Have a
variable var x {i in MODELS, j in COLORS} >= 0;, indicating how many cars of model i
with color j are produced.
In math notation our new variables would be called xij , AMPL will refer to them as x[i,j]. Similar
notation is used to declare and refer to parameters indexed by two sets, or even variables and
parameters indexed by 3 or more sets.
(c) What are the constraints? We need a constraint for each color expressing that the total machining
time does not exceed the corresponding machine service. You can do this by saying that there
is such a Machine limit inequality for each color.
subject to Machine limit {j in COLORS}: sum {i in MODELS} machine time[i]*x[i,j]
<= machine service[j];
(d) Modify the data accordingly.
(e) Run AMPL to test your new model. Unless you changed the objective function declaration, you
will get an error message indicating that the variable x need two subscripts. Modify the objective
function line and solve the model. Display the solution (by typing display x; at the Commands
window).
11. Now go back again to the AMPL model and data files where we did not have the colors of the cars.
Next we will add a new constraint. Suppose that there is not only a limit on the number of machining
hours the manufacturer can provide, but also a limit on the number of man-hours. So, that means we
need an additional parameter manhour time for each model; this will be the man-hour time required
for a model. You also need to add a parameter manhour limit, and a new constraint, say named
Manhour limit. Once you have added these, make AMPL read your model as above to discover
errors, and fix them. Then add to your data file the missing data (you make it up), and solve the new
problem.
12. So far we had a Machine limit and a Manhour limit constraint. Instead of making a third constraint
for, say, total overtime hours the manufacturer can provide, and a fourth constraint for some other
time limitation etc., we will change the model so that it can have any number of constraints of this
type. This feature of AMPL is one of the things that makes AMPL a nice modeling tool. You will
need to have a new set in your model file for the set of constraints. Let us name this set LIMITS.
In order to do that you need to add the line: set LIMITS; to your model file. Then your data file
should contain the line
set LIMITS := machine manhour;
to get back the model we considered in the previous problem. What else do you need to change in the
model file? Think about this a bit, and then continue reading.
(a) In the previous model we had actual time limit parameters machine service and manhour limit.
Now we will declare one parameter for each kind of limit in the set LIMITS by typing
param time limit {k in LIMITS};
which says that there is a parameter time limit for each different kind of time resource limitation in the set of LIMITS. We will refer to these limits by saying time limit[machine] and
time limit[manhour].
(b) Next we need to declare parameters for the machine and manhours needed for each model. Think
about how you could do this. It is done by the line
param model time {i in MODELS, k in LIMITS} >= 0;
These parameters are indexed on two sets: MODELS and LIMITS, and they express what the
model’s contribution is in each constraint. For example, model time[FourDoor, machine] is
35 etc..
4
(c) Now you should be able to write only one line that expresses all the constraints. It should look
like
subject to Time limits {k in LIMITS}: sum {i in MODELS} ... ;
(d) At this point you should modify your data file for this new model. How you can enter data for
parameters indexed over two sets is explained in your AMPL book (p.16, fig. 1-6b). By default
the rows of the table correspond to the first index and the columns to the second index. So the
data for model time should look like this in your data file.
param model time: machine manhour :=
FourDoor
35
18
TwoDoor
32
17
Hatchback
32
18
Minivan
38
21
Truck
40
24
;
(e) Solve the new model.
13. Finally, make up a model file for the problem that incorporates all the versions of the problem we
discussed (set of colors, set of time limit constraints, upper bounds on the number of cars, etc.). You
will probably need to enter a set of constraints for all colors j and for all time limits k. Your constraint
should look like:
subject to Time limits {j in COLORS, k in LIMITS}: sum {i in MODELS} ...;
Make up your own data file to test your model. This last problem will test all your AMPL skills so far,
and also some of your modeling skills. After you do this, you should have no problem solving AMPL
problems in future homeworks.
5
Download