Introduction to AMPL
Syntax
- declare parameters, sets, and variables using keywords param, set and var. Notice that each statement ends with semicolon.
param parameter1; var variable1 >=0; set S:= 1..s;
- express objective function using keyword minimize or maximize; constraints after using the keyword subject to. Both objective function and constraints must have names such as objectivef, cons1. Notice colons after names.
minimize objective f: 100*x1 + 75*x2;
subject to
cons1: x1 <= 10;
AMPL statements always end with a semicolon. Any line or part of a line after “#” is only a comment. Blank lines have no impact1.
sum{i in 1..5}does summation over index i. \(\sum_{i=1}^5\)
In all cases where objects are defined over index ranges, the range is shown within “{ }” brackets1. Example:
var x{1..5};
- Subscripts are called out within square “[]” brackets1. Example:
x[1], x[5]
- The symbol “:=” is used to indicate assignment of a constant’s value1. Example:
param l:= 5;
- No commas or other delimiters separate data values1.
param a:= 1 10 2 5; here a is a vector of length 2 and it needs to be declared before assigning values.
Single-subscript parameters are assigned in a list of subscript-value pairs1.
Multiple-subscript parameters are assigned in a table with columns corresponding to the last of the subscripts and rows for each combination of the other indices1. Example:
param p:= 1 2 3:=
1 10 20 30
2 12 14 16;
this is 2x3 matrix data.
You can use console of AMPL software to code line by line or better use a txt (other extensions exist too) file to write and modify your code and read this txt file into AMPL. model keyword sources the txt (or other extension) file.
Once a model is coded, data can be sparely entered after the keyword data.
The keyword solve is used to call a solver to solve the model.
- display displays the solution in terms of variables or objective function (whichever is written after).
- warning!: use reset keyword to delete the existing model in AMPL.
Two Crude Example
- define variables x1, x2
var x1 >=0; # non-negativity is expressed here var x2 >=0
- express the objective function explicitly
minimize z: 100*x1 + 75*x2; #notice the name z and colon after
- express main constraints
subject to
c1: 0.3*x1 + 0.4*x2 >= 2;
c2: 0.4*x1 + 0.2*x2 >= 1.5;
c3: 0.2*x1 + 0.3*x2 >= 0.5;
c4: x1 <= 9; c5: x2 <= 6;
- solve and display results
solve; display x1,x2;
Generalized Model
- define parameters to be used in sets
param m; param n;
- define sets to be used for indexing
set R = 1..m; set A = 1..n;
- define cost coefficients to be used in objective function
param cost{A};
- define right hand side of requirement constraints
param requireRHS{R};
- define coefficients for unit contribution to requirement satisfaction
param require{R,A};
- define availability right hand sides
param availRHS{A};
- define variables
var x{A} >= 0;
- express objective function
minimize z: sum{j in A}cost[j]*x[j]; #look like
\(\sum_{j=1}^ncost_jx_j\)
- express requirement constraints
subject to
c1{i in R}: sum{j in A}require[i,j]*x[j] >= requireRHS[i]; #look like
\(\sum_{j=1}^nrequire_{i,j}x_i\ge requireRHS_i \ \forall i\)
- express availability constraints
c2{j in A}: x[j] <= availRHS[j]; #look like
\(x_j\le availRHS_j \ \forall j\)
- enter data
data;
param m:= 3; param n:= 2;
param cost:= 1 100 2 75;
param requireRHS:= 1 2 2 1.5 3 0.5;
param require: 1 2:=
1 0.3 0.4
2 0.4 0.2
3 0.2 0.3;
param availRHS:= 1 9 2 6;
- link to cplex solver
option solver cplex
- solve and display results
solve; display x;
Practice Example
\(maximize\ 3x_1 + 4x_2 + 2x_3\)
\(subject\ to\)
\(x_1 + 0.5x_2 + 5x_3 \le 2\)
\(2x_1 -x_2 +3x_3 \le 3\)
\(x1,x2,x3 \ge 0\)
define parameter n which will indicate number of variables–in this example the value of n is three.
param n;
define a parameter m which will indicate number of main constraints–in this example the value of m is two.
param m;
define a set which will have elements from 1 to n;
set V:= 1..n;
define a set which will have elements from 1 to m;
set R:= 1..m;
define a profit vector to account for the objective function coefficients
param profit{V};
define constraint coefficient matrix;
param coeff{R,V};
define RHS of constraints
param RHS{R};
define variable vector
var x{V} >=0;
express objective function
maximize z: sum{i in V}profit[i]*x[i];
express contraints
subject to
const{j in R}: sum{i in V}coeff[j,i]*x[i] <= RHS[j];
enter data
data;
param n:= 3; param m:=2;
param profit:= 1 3 2 4 3 -2;
param coeff: 1 2 3:=
1 1 0.5 5
2 2 -1 3;
param RHS:= 1 2 2 3;set solver and solve, display results
option solver cplex; solve; display x;
Display Options
show lists the components of the model
Also try: show vars, obj, constr;
xref displays dependencies on a specific model component: xref variable_1;
expand displays the model component explicitly: expand z;
option omit_zero_rows 1 suppresses 0 values.
option show_stats 1 shows model summary.
print, printf allows flexibility in displaying modified results: printf “Total revenue is $%6.2f.\n”, sum {p in PROD, t in 1..T} revenue[p,t]*Sell[p,t];
Total revenue is $787810.00 [this example is from the AMPL book page 239].
- ‘>’ or ‘>>’ writes results into a file.
display supply > multi.out [page 251]
- read{range} x[.] < foo.txt reads from an external file.
read{i in I}RHS[i] < RHS.txt
Please see the AMPL book and colab notebooks in Black Board to practice more on AMPL. Here is the link to the AMPL book: AMPL book
Footnotes
R. Rardin, Optimization in Operations Research↩︎