Knitro#

Knitro is a software library used to find solutions of continuous and discrete optimization models. It is designed for general, nonlinear optimization problems and can be used for a wide range of cases such as systems of non-linear equations, least square problems, convex and non-convex quadratic problems etc. Please refer to the user manual for a complete set of instructions on how to use Knitro.

Knitro is not a standalone program. It provides a programmable API to be used within other software. This guide provides a high-level overview of how to use Knitro with some of the more common applications on the system.

You can check the versions of Knitro installed on Quest by running the command below:

$ module spider knitro

------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 knitro:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Versions:
    knitro/8
    knitro/9.1.orig
    knitro/9.1
    knitro/10.0
    knitro/10.3
    knitro/12.4
    knitro/13.2.0

If users need a newer version of Knitro then is available on Quest currently, please fill out the Software Installation Request form.

MATLAB#

Knitro is often used as part of a MATLAB process. Knitro is loaded by each version of MATLAB. Additional information about the MATLAB versions available can be found on the MATLAB documentation page. The path to the Knitro installation is automatically added when the application starts. If users want to have a different version of Knitro loaded they must modify the search path within their MATLAB code. Instructions on how to do this are included at the end of this section.

knitromatlab is the interface used to call Knitro from the MATLAB environment. To test that the installation exists, and to find out the version of Knitro loaded, a user can run a test function on MATLAB.

Here is an example of running a test (find the minimum value of cos(x)) from the MATLAB command line, but a similar command could be included in a MATLAB script.

>> [x fval] = knitromatlab(@(x)cos(x),1)

=======================================
           Academic License
       (NOT FOR COMMERCIAL USE)
         Artelys Knitro 10.0.0
=======================================

Knitro presolve eliminated 0 variables and 0 constraints.

algorithm:            1
gradopt:              4
hessopt:              2
honorbnds:            1
maxit:                10000
outlev:               1
par_concurrent_evals: 0
The problem is identified as unconstrained.
Knitro changing bar_initpt from AUTO to 3.
Knitro changing bar_murule from AUTO to 4.
Knitro changing bar_penaltycons from AUTO to 1.
Knitro changing bar_penaltyrule from AUTO to 2.
Knitro changing bar_switchrule from AUTO to 1.
Knitro changing linsolver from AUTO to 2.

Problem Characteristics                    ( Presolved)
-----------------------
Objective goal:  Minimize
Number of variables:                     1 (         1)
    bounded below:                       0 (         0)
    bounded above:                       0 (         0)
    bounded below and above:             0 (         0)
    fixed:                               0 (         0)
    free:                                1 (         1)
Number of constraints:                   0 (         0)
    linear equalities:                   0 (         0)
    nonlinear equalities:                0 (         0)
    linear inequalities:                 0 (         0)
    nonlinear inequalities:              0 (         0)
    range:                               0 (         0)
Number of nonzeros in Jacobian:          0 (         0)
Number of nonzeros in Hessian:           1 (         1)

EXIT: Locally optimal solution found.

Final Statistics
----------------
Final objective value               =  -1.00000000000000e+00
Final feasibility error (abs / rel) =   0.00e+00 / 0.00e+00
Final optimality error  (abs / rel) =   2.37e-09 / 2.37e-09
# of iterations                     =          7
# of CG iterations                  =          0
# of function evaluations           =         22
# of gradient evaluations           =          0
Total program time (secs)           =       0.46135 (     0.092 CPU time)
Time spent in evaluations (secs)    =       0.04807

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


x =

    3.1416
fval =

   -1.0000

More information on using the MATLAB/Knitro interface can be found in the Knitro Documentation .

Changing the knitromatlab Version#

Users can access different versions of kntiromatlab by updating their path to the relevant location. Please query the Knitro module to find the installation directory. For example, MATLAB 2020b loads by default knitro/10.0. You can verify this by running the commands below:

$ module load matlab/r2020b
$ module list

In the output, you can see that the knitro/10.0 module is listed:

Currently Loaded Modules:
  1) knitro/10.0   2) java/jdk1.8.0_191   3) matlab/r2020b

To use version 12.4 type in the command line:

$ module load matlab/r2020b
$ module load knitro/12.4 

Querying the 12.4 knitro module shows:

$ module show knitro/12.4
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 /software/Modules/3.2.9/modulefiles/knitro/12.4:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
whatis("Artelys Knitro is a commercial software package for solving large scale nonlinear mathematical optimization problems. ")
whatis("KNITRO -- (the original solver name) short for 'Nonlinear Interior point Trust Region Optimization' (the 'K' is silent) -- was co-created by Richard Waltz, Jorge Nocedal, Todd Plantenga and Richard Byrd. ")
whatis("It was first introduced in 2001, as a derivative of academic research at Northwestern University (through Ziena Optimization LLC), and has since been continually improved by developers at Artelys. ")
prepend_path("KNITRODIR","/software/knitro/12.4")
prepend_path("PATH","/software/knitro/12.4/knitroampl:/software/knitro/12.4/knitromatlab")
prepend_path("LD_LIBRARY_PATH","/software/knitro/12.4/lib")
setenv("ARTELYS_LICENSE_NETWORK_ADDR","129.105.119.173:8349")
help([[Optimization problems must be presented to Knitro in mathematical form, and should provide a way of computing function derivatives using sparse matrices (Knitro can compute derivatives approximation but in most cases providing the exact derivatives is beneficial).
An often easier approach is to develop the optimization problem in an algebraic modeling language.
]]) 

Therefore the path to the knitromatlab API is available on Quest in: /software/knitro/12.4/knitromatlab. You can now start MATLAB. At the MATLAB command prompt you can type:

>> addpath('/hpc/software/knitro/12.4/knitromatlab')
>> savepath

The Knitro interface for the user is now updated to version 12.4.

R#

The Knitro API for R can be installed by any user for any version of R running the following commands:

$ module load knitro/12.4
$ module load R/4.1.1
$ rm -rf ~/R_knitro
$ mkdir -p ~/R_knitro
$ cp -r /software/knitro/12.4/examples/R ~/R_knitro/
$ cd ~/R_knitro/R
$ R CMD INSTALL --build KnitroR 

The KnitroR package is now installed for the version of R you loaded above. You can load it from within R with the normal library("KnitroR") command.

To verify that R is communicating with knitro correctly run a test calculation. Here is an example of the minimization of the Rosenbrock function: f(x,y) = (1-x\^2)+100\*(y-x\^2)\^2.

At the R prompt, run the following commands that create a function, evaluate the function and its derivatives, and then call the KnitroR interface to calculate the location and value of the global minimum based on an initial estimate.

> eval_f <- function(x) {
    return( 100 * (x[2] - x[1] * x[1])^2 + (1 - x[1])^2 )}

> eval_grad_f <- function(x) {
    grad_f <-rep(0, length(x))
    grad_f[1] <- 2*x[1]-2+400*x[1]^3-400*x[1]*x[2]
    grad_f[2] <- 200*(x[2]-x[1]^2)
    return( grad_f )}

> x0 <- c( -1.2, 1 )

> sol <- knitro(x0 = x0, objective = eval_f)

Knitro then returns:

=======================================
     Academic License
   (NOT FOR COMMERCIAL USE)
    Artelys Knitro 12.4.0
=======================================

Knitro performing finite-difference gradient computation with 1 thread.
Knitro presolve eliminated 0 variables and 0 constraints.
Knitro performing finite-difference gradient computation with 1 thread.
Knitro presolve eliminated 0 variables and 0 constraints.

gradopt:                 2
hessopt:                 2
outlev:                  1
par_concurrent_evals:    0
The problem is identified as unconstrained.
Knitro changing algorithm from AUTO to 1.
Knitro changing bar_initpt from AUTO to 3.
Knitro changing bar_murule from AUTO to 4.
Knitro changing bar_penaltycons from AUTO to 1.
Knitro changing bar_penaltyrule from AUTO to 2.
Knitro changing bar_switchrule from AUTO to 1.
Knitro changing linesearch from AUTO to 1.
Knitro changing linsolver from AUTO to 2.

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

EXIT: Locally optimal solution found.

Final Statistics
----------------
Final objective value               =   2.00694602943133e-11
Final feasibility error (abs / rel) =   0.00e+00 / 0.00e+00
Final optimality error  (abs / rel) =   1.54e-08 / 1.54e-08
# of iterations                     =         18
# of CG iterations                  =          2
# of function evaluations           =         71
# of gradient evaluations           =          0
Total program time (secs)           =       0.00355 (     0.004 CPU time)
Time spent in evaluations (secs)    =       0.00062

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

> sol$x
[1] 0.9999955 0.9999910

The theoretical location of the minimum is at (1,1) and the global minimum value of 0. Knitro returns:

Final objective value               =   2.00694602943133e-11

Further examples are provided on Quest in /software/knitro/12.4/examples/R. Further information is available in the Knitro documentation .

Python#

Knitro provides a Python interface for the Knitro callable library functions. Knitro’s Python API is thread-safe; Python code can launch multiple instances of the knitro solver in different threads, each solving a different problems.

Examples are provided on Quest in /software/knitro/12.4/examples/Python that show the structure of a Knitro-enabled Python program.

To use Knitro with Python on Quest, first create an anaconda virtual environment with the Python distribution of your choice and then load the knitro module. In the example below, we create a virtual environment with the name knitro-py38 and install Python 3.8 into it.

$ module load knitro/12.4
$ module load python-miniconda3
$ conda create --name knitro-py38 python=3.8 --yes
$ source activate knitro-py38
$ mkdir ~/python_knitro
$ cp -r /software/knitro/12.4/examples/Python ~/python_knitro/
$ cd ~/python_knitro/Python
$ python setup.py install

After the initial step of creating the virtual environment and installing Knitro into it, each subsequent time you would like to use Knitro and/or the virtual environment, you need only add the following to your Slurm submission script or run the following on the command line

$ module load knitro/12.4
$ module load python-miniconda3
$ source activate knitro-py38

If this is done on the command line, you can test that importing Knitro works by launching the Python command line and importing knitro.

(knitro-py38) $ python
Python 3.8.13 (default, Mar 28 2022, 11:38:47)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import knitro
>>> 

For more information please refer to the Knitro documentation .

AMPL#

AMPL is a modeling language to solve optimization problems. AMPL is installed on Quest and can be loaded as:

$ module load ampl

You can check the version of AMPL you are running with the command:

$ module load ampl
$ ampl --version

The output will be similar to:

AMPL Version 20220730 (Linux x86_64 (gcc 7.5.0))

There are only a few licenses available on Quest. Users can check if any there are any licenses available by running the following command:

$ ampl_lic status

Loading the AMPL module also loads knitro/10.3 which sets the path to the knitroampl executable found on Quest in /software/knitro/10.3/knitroampl.

Invoke knitro within AMPL with the command:

option solver knitroampl;

For more information and examples please see the Knitro documentation .