You are on page 1of 2

Running AMPL/CPLEX through MATLAB

Alexandre M. Bayen

November 30, 2003

This document explains how to use CPLEX for the MS&E212 projects, and how to record
its running time. This can be done within MATLAB, in order for you to be able to compare
it to the running time of your algorithms if you wish. You can download the corresponding
files at:

http://cherokee.stanford.edu/bayen/mandse212.html

These programs have been designed to work on the leland UNIX system. They might not
run on Windows. Once you have downloaded the files, you can unzip / untar them. Put
all the files in the same directory.

1 Matlab code

The main file is called CPUtimeCPLEX.m. The function of this program is to solve several
times the same problem with CPLEX, and to record the CPU time required. In order to
get a quick start, you can just run it in MATLAB and see what happens. If you want to
time algorithms which you program in MATLAB, you can use the commands tic, toc, and
cputime.
The variable nAircraft represents the number of aircraft for the current scenario, which we
vary from AIRCRAFT MIN to AIRCRAFT MAX. For each value of nAircraft, we run numRuns
experiments, indexed by runNumber. The constants AIRCRAFT MIN, AIRCRAFT MAX and
numRuns are your choice. This programs calls runSchedule.m, which does each of the
runs.
The function runSchedule.m cleans the directory from eventual residual files, and calls
several subroutines which download or compute relevant data for the problem. If you are
just interested in learnig how to use CPLEX and time it, you do not need to know what
is inside some of these files. The only one which is important is CreateAmplData.m, which
directly creates AMPL/CPLEX readable files from MATLAB.

2 Running your AMPL/CPLEX scripts with MATLAB

In order to run an AMPL/CPLEX script from MATLAB, you need three files. In the
files which you downloaded, these three files are included, and are cllaed: schedule.mod,

1
schedule.dat, schedule.run.

The file schedule.mod contains the mathematical model of the program which you
are trying to solve. This file is straightforward to write. You can use the AMPL
book suggested in the background reading to learn how to write this file. It contains
declarations of parameters, variables and sets, and poses the optimization program in
the usual format maximize subject to .

The file schedule.dat contains the numerical values corresponding to your model.
For example, if you declare a parameter nAircraft, and you want it to be equal to
18, your schedule.dat will contain the line param nAircraft := 18;.

The file schedule.run contains the AMPL/CPLEX code, i.e. what you tell it to
do with your model and your data. In particular, once you told CPLEX to solve
the problem, you can use this file to save data into result files, by using commands
such as display deltaT > ArrivalResults.out;. This command will generate a
file ArrivalResults.out containing the variable deltaT.

The function CreateAmplData.m will thus create the file schedule.dat for each of your
runs: you know the model, and you want to try it for several sets of parameters; therefore
you do not want to manually generate a schedule.dat file for each run, this function does
it for you.
Once the AMPL/CPLEX files generated, the program runSchedule.m runs AMPL (from
MATLAB), using the command:
! (time ampl schedule.run) > CPLEXrun.txt
which also times this process and saves the CPU time into the file CPLEXrun.txt. The
UNIX command outputs several measures of CPU time (user, system); not all of them
are useful. The program RecordPerfo.m saves the useful CPU time for your computation,
which is read by the program AnalyzePerfo.m at the end of the runs. This last program
plots the CPU time as a function of the parameter nAircraft.
Thus, if you run the program as it is without modifying it (it might take a little while), you
will see a curve which looks exponential, for nAircraft {3, , 15}.

3 Writing your own scripts

Now that you understand how these scripts work, you can modify them in order to solve
your own problems. Remember that the files schedule.mod, schedule.dat, schedule.run
have to be consistent with each other; otherwise, CPLEX will most likely output errors.

You might also like