You are on page 1of 22

AMPL/CPLEX Tutorial

ESI 6316
Applications of OR in Manufacturing
AMPL & CPLEX Introduction
AMPL
An Algebraic Modeling Language for Mathematical
Programming
Modeling Language expresses the modeler’s form in
a way that a computer system can understand
CPLEX
Solver (Optimizer)
Algorithms:
• Simplex
• Barrier
• Mixed Integer (Branch & Bound)

2
AMPL & CPLEX Introduction

Callable
Libraries,
User Application
API
C++,
JAVA,
VB,
Model Call CPLEX Etc…
AMPL CPLEX

Convert to MPS Read MPS file

MPS Format

3
AMPL- 2 variable LP
2 variable LP (Two Crude Petroleum)
min 20 x1  15 x 2 (total cost)
s.t. 0.3x1  0.4 x 2  2.0 (gasoline reqt.)
0.4 x1  0.2 x 2  1.5 (jet fuel reqt.)
0.2 x1  0.3x 2  0.5 (lubricant reqt.)
x1  9 (Saudi availability)
x2  6 (Venezuela n avail.)
Model file x1, x 2  0 (nonnegativity)
Any text editor e.g. Notepad
Save as “twoCrude.mod”
Extension .mod specifies a model file

4
AMPL Model File (2 var. LP)
min 20 x1  15 x 2 (total cost)
var x1 >= 0; s.t. 0.3x1  0.4 x 2  2.0 (gasoline reqt.)
var x2 >= 0;
0.4 x1  0.2 x 2  1.5 (jet fuel reqt.)
minimize total_cost: 0.2 x1  0.3 x 2  0.5 (lubricant reqt.)
20*x1 + 15*x2;
x1  9 (Saudi availability)
subject to gasoline_reqt: x2  6 (Venezuela n avail.)
0.3*x1 + 0.4*x2 >= 2.0;
x1, x 2  0 (nonnegativity)
subject to jetfuel_reqt:
0.4*x1 + 0.2*x2 >= 1.5;

subject to lubricant_reqt:
0.2*x1 + 0.3*x2 >= 0.5;

subject to saudi_avail:
x1 <= 9;

subject to venezuelan_avail:
x1 <= 6;

5
Running AMPL (2 var. LP)

C:\APML101\ampl.exe

6
Solving bigger problems
Divided into two parts
Model file (.mod)
• Consists of the mathematical model
– Sets
– Parameters
– Variables
– Objective function
– constraints
Data file (.dat)
• Actual values for
– Sets and parameters

7
AMPL (Transportation Problem)
Steel Mills (Origin) Automobile Factories (Destination)
Demand in Tons
Supply in Tons 900
Farmington
1400
Gary 1200
Detroit
600
Lansing
2600
Cleveland 400
Windsor
1700
St. Louis
2900
Pittsburg 1100
Fremont
1000
Total Production = Total Lafayette
Requirement
8
AMPL (Transportation Problem)
Shipping Costs Per Ton (Arc Costs)
Mill Locations
Plants GARY CLEV PITT
FRA 39 27 24
DET 14 9 14
LAN 11 12 17
WIN 14 9 13
STL 16 26 28
FRE 82 95 99
LAF 8 17 20

9
AMPL (Transportation Problem)

Minimize   Cost *Trans


iORIG jDEST
ij ij

s.t.
 Trans
jDEST
ij  Supplyi...i  ORIG

 Trans
iORIG
ij  Demandj...j  DEST

Trans ij  0
10
Model File transp.mod
set ORIG; # origins
set DEST; # destinations

param supply {ORIG} >= 0; # amounts available at origins


param demand {DEST} >= 0; # amounts required at destinations

check: sum {i in ORIG} supply[i] = sum {j in DEST} demand[j];

param cost {ORIG,DEST} >= 0; # shipment costs per unit


var Trans {ORIG,DEST} >= 0; # units to be shipped

minimize Total_Cost:
sum {i in ORIG, j in DEST} cost[i,j] * Trans[i,j];

subject to Supply {i in ORIG}:


sum {j in DEST} Trans[i,j] = supply[i];

subject to Demand {j in DEST}:


sum {i in ORIG} Trans[i,j] = demand[j];

11
Data File: transp.dat
data;

param: ORIG: supply := # defines set "ORIG" and param "supply"


GARY 1400
CLEV 2600
PITT 2900 ;

param: DEST: demand := # defines "DEST" and "demand"


FRA 900
DET 1200
LAN 600
WIN 400
STL 1700
FRE 1100
LAF 1000 ;

param cost:
FRA DET LAN WIN STL FRE LAF :=
GARY 39 14 11 14 16 82 8
CLEV 27 9 12 9 26 95 17
PITT 24 14 17 13 28 99 20 ;

12
Data File
You can also define the sets and param seperately
e.g.
data;
set ORIG:=GARY CLEV PITT; # defines set ORIG
set DEST:=FRA DET LAN WIN STL FRE LAF; # defines set DEST

param supply := # defines param "supply"


GARY 1400
CLEV 2600
PITT 2900 ;

param demand := # defines param "demand"


FRA 900
DET 1200
LAN 600
WIN 400
STL 1700
FRE 1100
LAF 1000 ;

13
.mps file
MPS file format
Developed by IBM’s Mathematical
Programming System
Format for linear and integer programming
problems
Cannot be used for nonlinear problems
AMPL Syntax to write .mps file
write mtransp;

14
Understanding .mps
Sections
NAME (title of the problem)
ROWS
• Constraint type E(=), L(<=), G(>=), N (Objective)
• Row name
COLUMNS
• Column name
• Row name
• Coefficient (constraint and objective)
RHS
• RHS value

15
CPLEX
Reading a file
read
transp
optimize

display solution variables -

16
Interpretation of Columns
ORIG DEST
GARY FRA
CLEV
PITT
DET
LAN
Variable Trans was indexed
WIN over the set ORIG and
Defined the two STL
FRE DEST
sets in the following LAF

manner I in ORIG j in DEST Column # Value


GARY FRA 1 0
GARY DET 2 0
GARY LAN 3 0
GARY WIN 4 0
GARY STL 5 0
GARY FRE 6 1100
GARY LAF 7 300
CLEV FRA 8 0
CLEV DET 9 1200
CLEV LAN 10 600
CLEV WIN 11 400
CLEV STL 12 0
CLEV FRE 13 0
CLEV LAF 14 400
PITT FRA 15 900
PITT DET 16 0
PITT LAN 17 0
PITT WIN 18 0
PITT STL 19 1700
PITT FRE 20 0
PITT LAF 21 300
6900

17
CPLEX
Some other commands
change
• Used to change variable or constraint name
• Constraint sense
• Problem type
• RHS value
• Etc
display
• Used to view problem characteristics
• Sensitivity analysis
• Parameter settings
• Etc

18
Class Assignment
Pi-Hybrids Model
Construct a AMPL Model
Make the corresponding data file
Solve using AMPL
Create a .mps file
Use CPLEX to read and optimize the above
problem
Verify both the results

19
Script File
Script File
Automatically load model file and data file
Create random data
Display formatted output
Read from and write to text files. Etc….
.scs extension
Executing a script file
commands myscriptfile.scs;

20
Example of script file for transportation
problem
model transp.mod;
data transp1.dat;

solve;

for {i in ORIG,j in DEST} {


if (Trans[i,j]>0) then {
printf "%s %s %.0f\n",i,j,Trans[i,j];
}
}

21
Example of Script file for Pi-Hybrids problem

model c:\ampl101\pihybrids.mod;
data c:\ampl101\pihybrids.dat;
solve;
printf "\nPRINTING FORMATTED OUTPUT............\n";
printf "TOTAL COST= $%.2f\n",total_cost;
printf "Bags to be produced:\n";
printf "Facility # Hybrid # No. of Bags\n";
for {f in facilities,h in hybrids} {
if (X[f,h]>0) then {
printf "%d %d %d\n",f,h,X[f,h];
}
}
for {f in facilities} {
printf "-------------------\nBags to be shipped from Facility #%d to:\n",f;
for {r in regions}{
printf "Region #%d\n",r;
for{h in hybrids} {
printf " Hybrid #%d %d Bags\n",h,Y[f,h,r];
}
}
}

22

You might also like