Getting started with AMPL

AMPL overview

AMPL is a popular modeling language for optimization that allows users to represent their optimization problems in a user-friendly, readable, intuitive format. This makes the job of formulating and modeling a problem much simpler. For a description of AMPL, visit the AMPL web site at:

We assume in the following that the user has successfully installed AMPL. The Knitro/AMPL executable file knitroampl must be in the current directory where AMPL is started, or in a directory included in the PATH environment variable.

Inside of AMPL, to invoke the Knitro solver type:

ampl: option solver knitroampl;

at the prompt. From then on, every time a solve command will be issued in AMPL, the Knitro solver will be called.

A detailed list of Knitro options and settings available through AMPL is provided in Knitro / AMPL reference.

Example AMPL model

This section provides an example AMPL model and AMPL session that calls Knitro to solve the problem. The AMPL model is provided with Knitro in a file called testproblem.mod, which is shown below.

# Example problem formulated as an AMPL model used
# to demonstate using Knitro with AMPL.
# The problem has two local solutions:
#   the point (0,0,8) with objective 936.0, and
#   the point (7,0,0) with objective 951.0

# Define variables and enforce that they be non-negative.
var x{j in 1..3} >= 0;

# Objective function to be minimized.
minimize obj:
        1000 - x[1]^2 - 2*x[2]^2 - x[3]^2 - x[1]*x[2] - x[1]*x[3];

# Equality constraint.
s.t. c1: 8*x[1] + 14*x[2] + 7*x[3] - 56 = 0;

# Inequality constraint.
s.t. c2: x[1]^2 + x[2]^2 + x[3]^2 -25 >= 0;

data;

# Define initial point.
let x[1] := 2;
let x[2] := 2;
let x[3] := 2;

The above example displays the ease with which an optimization problem can be expressed in the AMPL modeling language.

Running the solver

Below is the AMPL session used to solve this problem with Knitro.

1ampl: reset;
2ampl: option solver knitroampl;
3ampl: option knitro_options "nlp_algorithm=2 bar_maxcrossit=2 outlev=1";
4ampl: model testproblem.mod;
5ampl: solve;

The options passed to Knitro on line 3 above mean “use the Interior/CG algorithm” (nlp_algorithm=2), “refine the solution using the Active Set algorithm” (bar_maxcrossit=2) and “limit the output from Knitro” (outlev=1). The meaning of Knitro options and how to tweak them will be explained later, the point here is only to show how easy it is to control Knitro’s behavior in AMPL by using knitro_options. Upon receiving the solve command, AMPL produces the following output.

 1nlp_algorithm=2
 2bar_maxcrossit=2
 3outlev=1
 4
 5=======================================
 6          Commercial License
 7         Artelys Knitro 15.1.0
 8=======================================
 9
10Knitro presolve eliminated 0 variables and 0 constraints.
11
12bar_maxcrossit           2
13concurrent_evals         0
14datacheck                0
15feastol                  1e-06
16feastol_abs              0.001
17hessian_no_f             1
18hessopt                  1
19nlp_algorithm            2
20opttol                   1e-06
21opttol_abs               0.001
22outlev                   1
23
24Problem Characteristics                     |           Presolved
25-----------------------
26Problem type: QCQP
27Objective: minimize / quadratic
28Number of variables:                      3 |                             3
29  bounds:         lower     upper     range |     lower     upper     range
30                      3         0         0 |         3         0         0
31                             free     fixed |                free     fixed
32                                0         0 |                   0         0
33Number of constraints:                    2 |                             2
34                    eq.     ineq.     range |       eq.     ineq.     range
35  linear:             1         0         0 |         1         0         0
36  quadratic:          0         1         0 |         0         1         0
37Number of nonzeros:
38              objective  Jacobian   Hessian | objective  Jacobian   Hessian
39  linear:             0         3           |         0         3
40  quadratic:          3         3         5 |         3         3         5
41  total:              3         6         5 |         3         6         5
42Coefficient range:
43  linear objective:          [0e+00, 0e+00] |                [0e+00, 0e+00]
44  linear constraints:        [7e+00, 1e+01] |                [7e+00, 1e+01]
45  quadratic objective:       [1e+00, 2e+00] |                [1e+00, 2e+00]
46  quadratic constraints:     [1e+00, 1e+00] |                [1e+00, 1e+00]
47  variable bounds:           [0e+00, 0e+00] |                [0e+00, 0e+00]
48  constraint bounds:         [2e+01, 6e+01] |                [2e+01, 6e+01]
49
50Knitro using the Interior-Point/Barrier Conjugate Gradient algorithm.
51
52EXIT: Locally optimal solution found.
53
54Final Statistics
55----------------
56Final objective value               =   9.36000000000000e+02
57Final feasibility error (abs / rel) =   0.00e+00 / 0.00e+00
58Final optimality error  (abs / rel) =   0.00e+00 / 0.00e+00
59# of iterations                     =          7
60# of CG iterations                  =          8
61# of function evaluations           =          0
62# of gradient evaluations           =          0
63# of Hessian evaluations            =          0
64Total program time (secs)           =       0.00115 (     0.001 CPU time)
65Time spent in evaluations (secs)    =       0.00000
66
67===============================================================================
68
69Knitro 15.1.0: Locally optimal or satisfactory solution.
70objective 936; feasibility error 0
717 iterations; 0 function evaluations
72ampl:

The output from Knitro tells us that the algorithm terminated successfully (“Exit: Locally optimal solution found.” on line 52), that the objective value at the optimum found is about 936.0 (line 56) and that it took Knitro about 1 millisecond to solve the problem (line 64). More information is printed, which you do not need to understand for now; the precise meaning of the Knitro output will be discussed in Obtaining information.

After solving an optimization problem, one is typically interested in information about the solution (other than simply the objective value, which we already found by looking at the Knitro log). For instance, one may be interested in printing the value of the variables x; the AMPL display command does just that:

ampl: display x;
x [*] :=
1  0
2  0
3  8
;

More information about AMPL display commands can be found in the AMPL manual.

Additional examples

More examples of using AMPL for nonlinear programming can be found in Chapter 18 of the AMPL book, see the Bibliography.