Thursday, January 26, 2017


AMPL, short for “A Mathematical Programming Language”, is an algebraic modeling language and the most powerful tool for linear programming problem and operation research. One advantage of AMPL is that its syntax is similar to the mathematical notation of optimization problems. It is the most popular input is NEOS:
market share
AMPL itself is free, but it doesn’t solve problems directly. Instead, different algorithms have been developed to solve application-oriented problems. These algorithms are packaged as so-called solvers. Hence, solvers are equivalent to toolbox of Matlab, library of R, or API of many general-purpose programming languages. Among AMPL solvers, CPLEX by IBM Corporation is most widely used. Solver performance is highly application-dependent.
To get started, AMPL IDE can be downloaded from official website Basically, there are 4 ways to get the free lunch:
  1. demo version, problem-size limited (under 300 variables/constraints, etc)
  2. 30-day trial, full feature, requested individually
  3. courses version, requested by teacher
  4. cloud service by NEOS Server
For purchase, you will see solvers are sold annually, because solvers are the core part. Anyway, demo version is enough for personal use.

AMPL grammer

  • comment by #
  • variables are declared by var
  • parameters are declared by param
  • each line of code ends with a semi-colon; otherwise, you get ==ampl?== in console window because the computer thinks you have not finished.
  • objective format: maximize or minimize, a name, and a colon, then statement
  • constraint format: subject to, a name, and a colon, then statement.
  • keywords are in lowercase.
  • \sum_{i=1}^n is written as sum{i in 1..n}
  • output variables in the console by display. It’s a sharp contrast with Matlab, in which you can directly output anything without keyword without the semicolon. In AMPL, you must display 1+1,sqrt(2),2^3;
A typical input in console is:
model example.mod;
data exmple.dat;
display x;
The separation of model and data is the key to describing complex problems.

key concepts

  • decision variables: whose values are to maximize profits or reduce loss
  • feasible solutions: those satisfy all constraints.

the hardest tasks

  1. formulating a correct model
  2. providing accurate data


A simple 2-variable example

# prod0.mod
var XB;
var XC;
maximize Profit: 25 * XB + 30 * XC;
subject to Time: (1/200) * XB + (1/140) * XC <= 40;
subject to B_limit: 0 <= XB <= 6000;
subject to C_limit: 0 <= XC <= 4000;

display XB,XC,Profit
A shortcut to execute is to save first, then right click in the script window-> AMPL command -> model. As we see in the console window, computer finds an optimal solver for you: MINOS 5.51.

multi-parameter example

# prod.mod
set P;
param a {j in P};  # tons per hour of product j
param b;           # hours available
param c {j in P};  # profit per ton of product j
param u {j in P};  # max tons of product j
var X {j in P};    # tons of product j
maximize Total_Profit: sum {j in P} c[j] * X[j];
subject to Time: sum {j in P} (1/a[j]) * X[j] <= b;
subject to Limit {j in P}: 0 <= X[j] <= u[j];

data /Users/yuchaojiang/Downloads/amplide.macosx64/models/prod.dat;
display X, Total_Profit;
# prod.dat
set P := bands coils;
param:     a     c     u  :=
  bands   200   25   6000
  coils   140   30   4000 ;
param b := 40;
Actually, you can also combine the two files into one. This save the calling by data prod.dat and you have all the data in one page.

some tricks

  • prefer longer, more meaningful names
  • use cd; or display _cd; and cd path; to show directory and change directory