Setting options
Knitro offers a number of user options for modifying behavior of
the solver. Each option takes a value that may be an integer, double precision
number, or character string.
Options are usually identified by a string name
(for example, algorithm
), but programmatic interfaces
also identify options by an integer value associated with a C language macro
defined in the file knitro.h
.
(for example, KN_PARAM_ALG
).
Most user options can be specified with either a numeric value or a
string value.
Note
The naming convention is that user options beginning with
bar_
apply only to the barrier/interior-point algorithms;
options beginning with
act_
apply only to the active-set/SQP algorithms;
options beginning with
mip_
apply only to the mixed integer programming (MIP)
solvers; options beginning with ms_
apply only to the multi-start
procedure; and options specific to the multi-algorithm procedure begin with
ma_
.
Setting Knitro options within AMPL
We have seen how to specify user options, for example:
ampl: option knitro_options "algorithm=2 bar_maxcrossit=2 outlev=1";
A complete list of Knitro options that are available in AMPL can be shown by typing:
knitroampl -=
The output produced by this command, along with a description of each option, is provided in Section Knitro / AMPL reference.
Note
When specifying multiple options, all options must be set
with one knitro_options
command as shown in the example above.
If multiple knitro_options
commands are specified in an AMPL session, only the last one will be read.
When running knitroampl directly with an AMPL file, user options can be set on the command line as follows:
knitroampl testproblem.nl maxit=100 opttol=1.0e-5
Setting Knitro options with MATLAB
There are two ways the Knitro-MATLAB interface functions can read user options: either passing an options structure generated from the knitro_options function, or using the Knitro options file (explained below). If both types of options are used, the Knitro options file options override the options structure settings.
The Knitro option file is a simple text file that contains, on each line, the name of a Knitro option and its value. For instance, the content of the file could be:
algorithm auto
bar_directinterval 10
bar_feasible no
Assuming that the Knitro options file is named knitro.opt
and is stored
in the current directory, and that the options structure is named
optionsStruct, the call to knitro_nlp would be:
[x fval] = ...
knitro_nlp(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,extendedFeatures,optionsStruct, ...
'knitro.opt')
The Knitro options file is a general mechanism to pass options to Knitro. It can also be used with the callable library interface. See Setting Options for more details on setting Knitro options through the MATLAB interface.
Setting Knitro options with the callable library
The functions for setting user options have the form:
int KN_set_int_param (KN_context *kc, int param_id, int value)
for setting integer valued parameters, or:
int KN_set_double_param (KN_context *kc, int param_id, double value)
for setting double precision valued parameters.
For example, to specify the Interior/CG algorithm and a tight optimality stop tolerance:
status = KN_set_int_param (kc, KN_PARAM_ALG, KN_ALG_BAR_CG);
status = KN_set_double_param (kc, KN_PARAM_OPTTOL, 1.0e-8);
Refer to the Callable Library Reference Manual (Changing and reading solver parameters) for a comprehensive list.
Setting KNITRO options with the object-oriented interface
User options are set with a single overloaded KNSolver function that has the form:
void solver.setParam(param_identifier, param_value);
The argument param_identifier
is either a string with the parameter’s name or
an integer with the parameter’s identifier number (enumerated object with all options have
been defined to simplify this step).
The argument param_value
is an integer, double, or string, depending on the type of parameter that is set.
For example, to specify the Interior/CG algorithm and a tight optimality stop tolerance:
solver.setParam(KN_PARAM_ALG, KN_ALG_BAR_CG);
solver.setParam(KN_PARAM_OPTTOL, 1.0e-8);
Refer to the Callable Library Reference Manual (Changing and reading solver parameters) for a comprehensive list of parameters. The object-oriented interface uses the same parameters as the callable library.
The Knitro options file
The Knitro options file allows the user to easily change user options by editing a text file, instead of modifying application code.
Options are set by specifying a keyword and a corresponding value on a line in the options file. Lines that begin with a “#” character are treated as comments and blank lines are ignored. For example, to set the maximum allowable number of iterations to 500, you could create the following options file:
# Knitro Options file
maxit 500
MATLAB users may simply pass the name of the Knitro options file to the appropriate Knitro/MATLAB function as demonstrated in Getting started with MATLAB. When using the callable library, the options file is read into Knitro by calling the following function:
int KN_load_param_file (KN_context *kc, char const *filename)
For example, if the options file is named myoptions.opt
:
status = KN_load_param_file (kc, "myoptions.opt");
The full set of options used by Knitro in a given solve may be written to a text file through the function call:
int KN_save_param_file (KN_context *kc, char const *filename)
For example:
status = KN_save_param_file (kc, "knitro.opt");
A sample options file knitro.opt
is provided for convenience
and can be found in the examples/C
directory.
Note that this file is only read by application drivers that call
KN_load_param_file()
, such as examples/C/exampleNLP1.c
.
In the object oriented interface, the equivalent functions for loading and saving parameter files are the following:
void KNSolver::loadParamFile(std::string filename);
void KNSolver::saveParamFile(std::string filename);