Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

SSIE 553: Operations Research

Professor: Soongeol Kwon Ph.D.

AMPL Review and Examples


By
Teaching Assistant: Ademola Adedoyin (Demmy)

11/28/2021
Useful AMPL console commands:
model file Upload model from the file ‘file’

data file Upload data from the file ‘file’

reset Erase the previous model and data

option solver GUROBI Use GUROBI for solving the problems

option solver CPLEX Use CPLEX for solving the problems

Solve Solve the specified model with the specified data

display Name Display the current value of entity Name

include file Execute commands from file file

expand Output the current model with data


Reserved AMPL words:
AMPL IDE

Working directory console


File window
MODEL and DATA:
Useful AMPL console commands:
Set Members:
{1, 2, 3, 4, 5} or {1..5}
{“a”, “b”, “c”}

Type this into the console


set y = setof {i in 1..5} (i, i^2); (press shift + enter)
display y; (press enter)
Useful AMPL console commands:
Declarations of model entities: var declaration:
set declaration: var name indexingopt attributesopt;
set name aliasopt indexingopt attributesopt; e.g.
e.g. var Make {p in PROD} >= 0, <= market[p];
set ORIG := A B C var Buy {j in FOOD} >= f_min[j], <= f_max[j];
set DEST; var Trans {ORIG,DEST,PROD} >= 0;
set PERIOD = 1..4; Objective declaration
set YEAR = 1990..2010 by 4; maximize (or minimize)
#The members would be 1990, 1994, ..., 2010; obj name: objexpression;
e.g.:
parameter declaration: maximize Total_Profit: sum {p in PROD} profit[p] * Make[p];
param name aliasopt indexingopt attributesopt; minimize Total_Cost: sum {i in ORIG, j in DEST, p in PROD}
e.g. cost[i,j,p] * Trans[i,j,p];
param T;
param avail >=0;
param profit {PROD};
param market {PROD} >=0; constraints declaration:
param capacity {1..T} >0; [subject to]op const name indexing: constexpression;
param profit {ORIG,DEST} >= 0 e.g.
param cost {ORIG,DEST,PROD}; subject to Time: sum {p in PROD} (1/rate[p]) * Make[p] <= avail;
subject to Diet {i in NUTR}:
n_min[i] <= sum {j in FOOD} amt[i,j] * Buy[j] <= n_max[i];
subject to Demand {j in DEST, p in PROD}:
sum {i in ORIG} Trans[i,j,p] = demand[j,p];
Examples
Find the optimal solution of the given problem
Maximize z = 3x1 + 2x2
subject to
2x1 + 5x2 ≤ 9
4x1 + 2x2 ≤ 9 model file (ex1.mod)
x1, x2 ≥ 0 and x2 integer

run file (ex1.run)


Examples
A taxi service operates in four areas: A, B, C, D. It has three vehicle classes: regular, premium and elite. The fleet
currently consists of 51 regular, 23 premium and 13 elite vehicles, which need to be assigned to the areas of operation.
There is a minimum number of vehicles (regardless of type) that need to be assigned to each area. Further, since you
can charge different prices, each vehicle assigned to each area will bring a different profit. Formulate an LP to find the
most profitable vehicle assignment. Using AMPL, find the optimal solution.

Area minimum number of vehicles needed Profit Table


Area regular premium elite
A 10
A 5 7 13
B 11
B 9 11 17
C 20 C 15 16 17
D 15
D 1 8 0
Examples
A taxi service operates in four areas: A, B, C, D. It has three vehicle classes: regular, premium and elite. The fleet
currently consists of 51 regular, 23 premium and 13 elite vehicles, which need to be assigned to the areas of operation.
There is a minimum number of vehicles (regardless of type) that need to be assigned to each area. Further, since you
can charge different prices, each vehicle assigned to each area will bring a different profit. Formulate an LP to find the
most profitable vehicle assignment. Using AMPL, find the optimal solution.

Area minimum number of vehicles needed • i: area, i ∈{A, B, C, D}


• j: type of vehicle, j ∈{regular, premium, elite}
A 10 • Dj: minimum number of vehicles needed to be assigned to area i
B 11 • Si: total available vehicles of type i
C 20 • pij : profit from assigning vehicle type j to area i
D 15 Max z:
෍ ෍ 𝑝𝑖𝑗𝑥𝑖𝑗
Profit Table 𝑖 𝑗
Area regular premium elite Subject to:
A 5 7 13 ෍ 𝑥𝑖𝑗 = 𝐷𝑗 ∀𝑖
B 9 11 17 𝑗

C 15 16 17 ෍ 𝑥𝑖𝑗 = 𝑆𝑖 ∀𝑗
D 1 8 0 𝑖
𝑥𝑖𝑗 ≥ 0 ∀𝑖, 𝑗
Examples

data file (ex2.dat) model file (ex2.mod)


Examples

run file (ex2.run)

mod file location


dat file location
run file location

console
Examples
Fall 2020 Class Project
Examples
Fall 2020 Class Project
Examples
Fall 2020 Class Project – AMPL data
Examples
Fall 2020 Class Project – AMPL model
Examples
Fall 2020 Class Project – AMPL run
References

https://ampl.com/BOOK/CHAPTERS/24-refman.pdf

https://ampl.com/BOOK/CHAPTERS/16-script.pdf

AMPL Tutorial
Take the soot survey. Send me a screenshot
of completed survey.
aadedoy1@binghamton.edu, cc:
Attendance skwon@binghamton.edu

Subject Line: SSIE 553 – “Your Names”


Soot Survey.

You might also like