Example Problems Solved by AMPL: Feng-Tien Yu April 27, 2001
Example Problems Solved by AMPL: Feng-Tien Yu April 27, 2001
Feng-Tien Yu
April 27, 2001
Contents
1 Using AMPL at CAEN 2
1.1 Setting Up AMPL Environment at CAEN . . . . . . . . . . . . . 2
1.2 Starting and Quitting AMPL . . . . . . . . . . . . . . . . . . . . 2
5 A Multi-Period LP Problem 14
5.1 AMPL Model File: multi.mod . . . . . . . . . . . . . . . . . . . . 15
5.2 AMPL Data File: multi.dat . . . . . . . . . . . . . . . . . . . . . 16
5.3 AMPL Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1
1 Using AMPL at CAEN
1.1 Setting Up AMPL Environment at CAEN
Before you can start using AMPL, you need to create a symbolic link to AMPL
interactive solver by typing the following command line on any Sun workstation:
ln -s /afs/engin.umich.edu/group/engin/priv/ioe/
ampl/ampl_interactive ampl
Now you can run AMPL by simply typing ampl on any Sun work station.
Note that AMPL only serves as an interface between your programs and various
solvers. The default solver is CPLEX.
In most cases, you won’t need other solvers. However if for some reasons you
would like to use other solvers, you must create a symbolic link for the desired
solver before you can use it. Currently three additional solvers, OSL, MINOS
and ALPO, are available. These command lines would create symbolic links to
various solvers.
ln -s /afs/engin.umich.edu/group/engin/priv/
ioe/ampl/solvers_SunOS/osl osl
ln -s /afs/engin.umich.edu/group/engin/priv/
ioe/ampl/solvers_SunOS/minos minos
ln -s /afs/engin.umich.edu/group/engin/priv/
ioe/ampl/solvers_SunOS/alpo alpo
2
% of Nutrient
in Grain
1 2 3 4
Starch 30 20 40 25
Fiber 40 65 35 40
Protein 20 15 5 30
Gluten 10 0 20 5
Cost (cents/kg.) 70 40 60 80
2.1 LP Formulation
The decision variables are:
s. to x1 + x2 + x3 + x4 = 100
x2 ≤ 20, x3 ≥ 30
10 ≤ x1 ≤ 25
0.20x1 + 0.15x2 + 0.05x3 + 0.30x4 ≥ 18
8 ≤ 0.10x1 + 0.20x3 + 0.05x4 ≤ 13
0.40x1 + 0.65x2 + 0.35x3 + 0.40x4 ≤ 50
xj ≥ 0 for all j = 1 to 4
param n_percent{GRAIN,NUTRIENT}>=0;
# percentage of nutrients in grains
param cost{GRAIN}>=0;
# cost of grains
param u_grain{GRAIN}>=0;
3
# upper bound for the percentage
# of each grain in flour
param l_grain{GRAIN}>=0;
# lower bound for the percentage
# of each grain in flour
param u_nutrient{NUTRIENT} >=0;
# upper bound for the percentage
# of each nutrient in flour
param l_nutrient{NUTRIENT} >=0;
# lower bound for the percentage
# of each nutrient in flour
var G_percent{GRAIN}>=0;
# percentage of each grain in flour
minimize Total_cost:
sum{i in GRAIN} cost[i]*G_percent[i];
subject to Flour:
sum{i in GRAIN} G_percent[i] = 100;
# total percentage should be 100%
subject to Grain_u{i in GRAIN}:
G_percent[i] <= u_grain[i];
# grain percentage <= upper bound
subject to Grain_l{i in GRAIN}:
G_percent[i] >= l_grain[i];
# grain percentage >= lower bound
subject to Nutri_u{j in NUTRIENT}:
sum {i in GRAIN} G_percent[i] *
n_percent[i,j] / 100 <= u_nutrient[j];
# nutrient percentage <= upper bound
subject to Nutri_l{j in NUTRIENT}:
sum {i in GRAIN} G_percent[i] *
n_percent[i,j] /100 >= l_nutrient[j];
# nutrient percentage >= lower bound
4
param cost :=
G1 70 G2 40 G3 60 G4 80 ;
param u_grain :=
G1 25 G2 20 G3 100 G4 100 ;
param l_grain :=
G1 10 G2 0 G3 30 G4 0 ;
param u_nutrient :=
Starch 100 Fiber 50
Protein 100 Gluten 13 ;
param l_nutrient :=
Starch 0 Fiber 0
Protein 18 Gluten 8 ;
5
Flour = 50
6
Protein 18 18 Infinity
Starch 0 29.25 Infinity
;
ampl: quit;
draw%
OBJECTIVES:
Total_cost = 6450
VARIABLES:
G_percent [*] :=
G1 15
G2 20
G3 30
7
G4 35
;
Grain_l [*] :=
G1 0
G2 0
G3 5
G4 0
;
Grain_u [*] :=
G1 0
G2 -25
G3 0
G4 0
;
Nutri_l [*] :=
Fiber 0
Gluten 0
Protein 100
Starch 0
;
Nutri_u [*] :=
Fiber 0
Gluten 0
Protein 0
Starch 0
;
model grain.mod;
data grain.dat;
solve;
8
print ’OBJECTIVES:’ > ampl.out;
display Total_cost >> ampl.out;
print ’VARIABLES:’ >> ampl.out;
display G_percent >> ampl.out;
print ’CONSTRAINTS (Dual Values):’ >> ampl.out;
display Flour >> ampl.out;
display Grain_l >> ampl.out;
display Grain_u >> ampl.out;
display Nutri_l >> ampl.out;
display Nutri_u >> ampl.out;
close ampl.out;
Type include grain.run; in AMPL to run this batch file. The outputs would
be redirected to the file ampl.out.
putt% ampl
ampl: include grain.run;
CPLEX 6.0: optimal solution; objective 6450
3 iterations (0 in phase I)
ampl: quit;
putt%
OBJECTIVES:
Total_cost = 6450
VARIABLES:
G_percent [*] :=
G1 15
G2 20
G3 30
G4 35
;
Grain_l [*] :=
G1 0
G2 0
G3 5
G4 0
;
9
Grain_u [*] :=
G1 0
G2 -25
G3 0
G4 0
;
Nutri_l [*] :=
Fiber 0
Gluten 0
Protein 100
Starch 0
;
Nutri_u [*] :=
Fiber 0
Gluten 0
Protein 0
Starch 0
;
10
param price {ALLOY} >= 0;
var Prod {ALLOY} >= 0;
maximize revenue: sum {j in ALLOY} Prod[j] * price[j];
subject to metal_avail {i in METAL}:
sum {j in ALLOY} portion[i,j] * Prod[j] <= avail[i];
draw% ampl
ampl: model Nonferrous.model;
ampl: data Nonferrous.data;
ampl: solve;
CPLEX 6.0: optimal solution; objective 97777.77778
1 iterations (0 in phase I)
ampl: quit;
draw%
11
4 A Purchase Planning Problem
An American textiles marketing firm buys shirts from manufacturers and sells
them to clothing retailers. For the next season they are considering four styles
with the total orders to be filled as given in the following table (the unit K = kilo,
i.e., one thousand shirts). They are considering 3 manufacturers, M1 , M2 , M3
who can make these styles in quantities and prices ($/shirt) as given below.
Style Orders M1 M2 M3
Capacity Price Capacity Price Capacity Price
1 200K 100K $8 80K $6.75 120K $9
2 150K 80K 10 60K 10.25 100K 10.50
3 90K 75K 11 50K 11 75K 10.75
4 70K 60K 13 40K 14 50K 12.75
Capacity for all 275K 150K 220K
styles together
set MANUFACTURER;
set STYLE;
set FOREIGN within MANUFACTURER;
param capacity {STYLE,MANUFACTURER} >= 0;
param capacity_total {MANUFACTURER} >= 0;
param price {STYLE,MANUFACTURER} >= 0;
param order {STYLE} >= 0;
param quota >= 0;
var Buy {STYLE, MANUFACTURER} >= 0;
minimize cost: sum {i in STYLE, j in MANUFACTURER}
Buy[i,j] * price[i,j];
subject to
max_order {j in MANUFACTURER}:
sum {i in STYLE} Buy[i,j] <= capacity_total[j];
12
subject to
max_import :
sum {i in STYLE, j in FOREIGN} Buy[i,j] <= quota;
subject to
max_order_style {i in STYLE, j in MANUFACTURER}:
Buy[i,j] <= capacity[i,j];
subject to demand {i in STYLE}:
sum {j in MANUFACTURER} Buy[i,j] >= order[i];
13
S2 M2 55
S2 M3 15
S3 M1 0
S3 M2 15
S3 M3 75
S4 M1 20
S4 M2 0
S4 M3 50
;
ampl: quit;
draw%
5 A Multi-Period LP Problem
Important applications in production planning for planning production, storage,
marketing of product over a planning horizon.
14
Production capacity, demand, selling price, production cost, may all vary
from period to period.
AIM: To determine how much to produce, store, sell in each period; to max.
net profit over entire planning horizon.
YOU NEED Variables that represent how much material is in storage at end
of each period, and a material balance constraint for each period.
Planning Horizon = 6 periods.
Storage warehouse capacity: 3000 tons.
Storage cost: $2/ton from one period to next.
Initial stock: 500 tons. Desired final stock: 500 tons.
Demand of each period must be fulfilled in that same period.
Period Prod. Prod. Demand Sell
cost capacity (tons) price
1 20 $/ton 1500 1100 tons 180 $/ton
2 25 2000 1500 180
3 30 2200 1800 250
4 40 3000 1600 270
5 50 2700 2300 300
6 60 2500 2500 320
param sales_period;
param production_cost {PERIOD};
param production_capacity {PERIOD};
param demand {PERIOD};
param sale_price {PERIOD};
param storage_capacity;
param holding_cost;
param initial_stock;
param final_stock;
maximize Profit:
sum {i in PERIOD} demand[i] * sale_price[i]
- sum {i in PERIOD} Production[i] * production_cost[i]
- sum {i in PERIOD: ord(i) < sales_period} Storage[i] * holding_cost;
15
subject to Production_Capacity{i in PERIOD}:
Production[i] <= production_capacity[i];
subject to Initial_Balance:
Production[first(PERIOD)]+ initial_stock-demand[first(PERIOD)]
- Storage[first(PERIOD)] = 0;
subject to Final_Balance:
Storage[last(PERIOD)] = final_stock;
param sales_period:= 6;
param : production_cost production_capacity demand sale_price :=
P1 20 1500 1100 180
P2 25 2000 1500 180
P3 30 2200 1800 250
P4 40 3000 1600 270
P5 50 2700 2300 300
P6 60 2500 2500 320;
param holding_cost := 2;
draw% ampl
ampl: model multi.mod;
ampl: data multi.dat;
ampl: solve;
CPLEX 6.0: optimal solution; objective 2446800
16
7 iterations (3 in phase I)
ampl: display Production;
Production [*] :=
P1 1500
P2 2000
P3 2200
P4 2800
P5 2300
P6 0
;
17
P4 3000 3000
P5 3000 3000
P6 500 3000
;
ampl: quit;
draw%
Note that a solver reports only one optimal dual price or reduced cost to
AMPL, however, which may be the rate in either direction. Therefore, a dual
price or reduced cost can give you only one slope on the piecewise-linear curve
of objective values. Hence these quantities should be used as only an initial
quide to the objective’s sensitivity to certain variable or constraint bounds. If
the sensitivity is very important to your application, you can make a series of
runs with different bound settings.
18