Professional Documents
Culture Documents
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
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)
s.t.
Trans
jDEST
ij Supplyi...i ORIG
Trans
iORIG
ij Demandj...j DEST
Trans ij 0
10
Model File transp.mod
set ORIG; # origins
set DEST; # destinations
minimize Total_Cost:
sum {i in ORIG, j in DEST} cost[i,j] * Trans[i,j];
11
Data File: transp.dat
data;
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
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
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
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;
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