Introduction to AMPL

lectures 8-10
Author

Harun Pirim

Published

January 28, 2024

Syntax

param parameter1; var variable1 >=0; set S:= 1..s;

minimize objective f: 100*x1 + 75*x2;

subject to
cons1: x1 <= 10;

var x{1..5};

x[1], x[5]

param l:= 5;

param a:= 1 10 2 5; here a is a vector of length 2 and it needs to be declared before assigning values.

param p:= 1 2 3:=

1 10 20 30

2 12 14 16;

this is 2x3 matrix data.

Two Crude Example

var x1 >=0; # non-negativity is expressed here var x2 >=0

minimize z: 100*x1 + 75*x2; #notice the name z and colon after

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; display x1,x2;

Generalized Model

param m; param n;

set R = 1..m; set A = 1..n;

param cost{A};

param requireRHS{R};

param require{R,A};

param availRHS{A};

var x{A} >= 0;

minimize z: sum{j in A}cost[j]*x[j]; #look like

\(\sum_{j=1}^ncost_jx_j\)

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\)

c2{j in A}: x[j] <= availRHS[j]; #look like

\(x_j\le availRHS_j \ \forall j\)

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;

option solver cplex

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\)

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].

display supply > multi.out [page 251]

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

  1. R. Rardin, Optimization in Operations Research↩︎