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.

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

The options passed to Knitro on line 3 above mean “use the Interior/CG algorithm” (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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
algorithm=2
bar_maxcrossit=2
outlev=1

=======================================
          Commercial License
         Artelys Knitro 14.2.0
=======================================

Knitro presolve eliminated 0 variables and 0 constraints.

algorithm:               2
bar_maxcrossit:          2
concurrent_evals:        0
datacheck:               0
hessian_no_f:            1
outlev:                  1
The problem is identified as a QCQP.

Problem Characteristics                                 (   Presolved)
-----------------------
Objective goal:  Minimize
Objective type:  quadratic
Number of variables:                                  3 (           3)
    bounded below only:                               3 (           3)
    bounded above only:                               0 (           0)
    bounded below and above:                          0 (           0)
    fixed:                                            0 (           0)
    free:                                             0 (           0)
Number of constraints:                                2 (           2)
    linear equalities:                                1 (           1)
    quadratic equalities:                             0 (           0)
    gen. nonlinear equalities:                        0 (           0)
    linear one-sided inequalities:                    0 (           0)
    quadratic one-sided inequalities:                 1 (           1)
    gen. nonlinear one-sided inequalities:            0 (           0)
    linear two-sided inequalities:                    0 (           0)
    quadratic two-sided inequalities:                 0 (           0)
    gen. nonlinear two-sided inequalities:            0 (           0)
Number of nonzeros in Jacobian:                       6 (           6)
Number of nonzeros in Hessian:                        5 (           5)

Knitro using the Interior-Point/Barrier Conjugate Gradient algorithm.

EXIT: Locally optimal solution found.

Final Statistics
----------------
Final objective value               =   9.36000000000000e+02
Final feasibility error (abs / rel) =   0.00e+00 / 0.00e+00
Final optimality error  (abs / rel) =   0.00e+00 / 0.00e+00
# of iterations                     =          7
# of CG iterations                  =          8
# of function evaluations           =          0
# of gradient evaluations           =          0
# of Hessian evaluations            =          0
Total program time (secs)           =       0.00115 (     0.001 CPU time)
Time spent in evaluations (secs)    =       0.00000

===============================================================================

Knitro 14.2.0: Locally optimal or satisfactory solution.
objective 936; feasibility error 0
7 iterations; 0 function evaluations
ampl:

The output from Knitro tells us that the algorithm terminated successfully (“Exit: Locally optimal solution found.” on line 45), that the objective value at the optimum found is about 936.0 (line 49) and that it took Knitro about 1 millisecond to solve the problem (line 57). 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.