ampl cplex tutorial
TRANSCRIPT
AMPL/CPLEX Tutorial
ESI 6316
Applications of OR in Manufacturing
2
AMPL & CPLEX Introduction
AMPLAn Algebraic Modeling Language for Mathematical ProgrammingModeling Language expresses the modeler’s form in a way that a computer system can understand
CPLEXSolver (Optimizer)Algorithms:
• Simplex• Barrier• Mixed Integer (Branch & Bound)
3
AMPL & CPLEX Introduction
AMPL CPLEX
MPS Format
Convert to MPS Read MPS file
Call CPLEX Model
User ApplicationC++,JAVA,VB,Etc…
Callable Libraries, API
4
AMPL- 2 variable LP
2 variable LP (Two Crude Petroleum)
Model file Any text editor e.g. Notepad
Save as “twoCrude.mod”
Extension .mod specifies a model file
vity)(nonnegati 02,1
avail.)n (Venezuela 62
ty)availabili (Saudi 91
reqt.) (lubricant 5.023.012.0
reqt.) fuel(jet 5.122.014.0
reqt.) (gasoline 0.224.013.0 s.t.
cost) (total 1520min 21
xx
x
x
xx
xx
xx
xx
5
AMPL Model File (2 var. LP)
var x1 >= 0;var x2 >= 0;
minimize total_cost:20*x1 + 15*x2;
subject to gasoline_reqt:0.3*x1 + 0.4*x2 >= 2.0;
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;
vity)(nonnegati 02,1
avail.)n (Venezuela 62
ty)availabili (Saudi 91
reqt.) (lubricant 5.023.012.0
reqt.) fuel(jet 5.122.014.0
reqt.) (gasoline 0.224.013.0 s.t.
cost) (total 1520min 21
xx
x
x
xx
xx
xx
xx
6
Running AMPL (2 var. LP)
C:\APML101\ampl.exe
7
Solving bigger problems
Divided into two partsModel file (.mod)
• Consists of the mathematical model– Sets– Parameters– Variables– Objective function– constraints
Data file (.dat)• Actual values for
– Sets and parameters
8
AMPL (Transportation Problem)
1400
2600
2900
900
1200
600
400
1700
1100
1000
Gary
Cleveland
Pittsburg
Steel Mills (Origin) Automobile Factories (Destination)
Farmington
Detroit
Lansing
Windsor
St. Louis
Fremont
Lafayette
Supply in TonsDemand in Tons
Total Production = Total Requirement
9
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
10
AMPL (Transportation Problem)
0
...
...
s.t.
* Minimize
ij
ORIGi
jij
DESTj
iij
ORIGi DESTj
ijij
Trans
DESTjDemandTrans
ORIGiSupplyTrans
TransCost
11
Model File transp.mod
set ORIG; # originsset DEST; # destinations
param supply {ORIG} >= 0; # amounts available at originsparam 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 unitvar 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];
12
Data File: transp.datdata;
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 ;
13
Data FileYou can also define the sets and param seperatelye.g.
data;set ORIG:=GARY CLEV PITT; # defines set ORIGset 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 ;
14
.mps file
MPS file formatDeveloped by IBM’s Mathematical Programming System
Format for linear and integer programming problems
Cannot be used for nonlinear problems
AMPL Syntax to write .mps filewrite mtransp;
15
Understanding .mps
SectionsNAME (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
16
CPLEX
Reading a filereadtranspoptimize
display solution variables -
17
Interpretation of ColumnsORIG DESTGARY FRACLEV DETPITT LAN
WINSTLFRELAF
I in ORIG j in DEST Column # ValueGARY FRA 1 0GARY DET 2 0GARY LAN 3 0GARY WIN 4 0GARY STL 5 0GARY FRE 6 1100GARY LAF 7 300CLEV FRA 8 0CLEV DET 9 1200CLEV LAN 10 600CLEV WIN 11 400CLEV STL 12 0CLEV FRE 13 0CLEV LAF 14 400PITT FRA 15 900PITT DET 16 0PITT LAN 17 0PITT WIN 18 0PITT STL 19 1700PITT FRE 20 0PITT LAF 21 300
6900
Variable Trans was indexed over the set ORIG and DESTDefined the two
sets in the following manner
18
CPLEX
Some other commandschange
• 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
19
Class Assignment
Pi-Hybrids ModelConstruct 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
20
Script File
Script FileAutomatically 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 filecommands myscriptfile.scs;
21
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];}
}
22
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];
}}
}