Knitro / R reference
What is KnitroR
?
KnitroR
is the interface used to call Knitro from the R environment (requires R 3.0 or later).
KnitroR
offers two routines to call Knitro from R:
knitro
, which is an interface for the standard Knitro nonlinear and mixedinteger nonlinear optimizerknitrolsq
, which solves bound constrained nonlinear leastsquares problems in vectorial format, that isThis type of fitting problems is ubiquitous in statistics and data analytics. This function is based on the internal Knitro implementation of nonlinear leastsquares.
An important point when passing sparse matrices (parameters containing *Index*
) to Knitro is that indices of the sparse matrix vectors must start from 0.
Syntax
The most elaborate form for general nonlinear programs is
res < knitro(nvar=..., ncon=..., x0=...,
objective=..., objGoal=..., gradient=..., gradientIndexVars=...,
constraints=..., constraintsIndexCons,
jacobian=..., jacIndexCons=..., jacIndexVars=...,
hessianLag=..., hessIndexVars1=..., hessIndexVars2=...,
objLinearStruct=..., objLinearStructIndexVars=...,
conLinearStruct=..., conLinearStructIndexCons=..., conLinearStructIndexVars=...,
objQuadraticStruct=..., objQuadraticStructIndexVars1=..., objQuadraticStructIndexVars2=...,
conQuadraticStruct=..., conQuadraticStructIndexCons=...,
conQuadraticStructIndexVars1=..., conQuadraticStructIndexVars2=...,
xL=..., xU=..., cL=..., cU=...,
xTypes=..., xTypesIndexVars=...,
xStrategies=..., xStrategiesIndexVars=...,
xPriorities=..., xPrioritiesIndexVars=...,
xHonorBnds=..., xHonorBndsIndexVars=...,
ccTypes=..., ccIdxList1=..., ccIdxList2=...,
objScaleFactor=..., xScaleFactors=..., xScaleCenters=...,
cScaleFactors=..., ccScaleFactors=...,
newptCallback=...,
options=..., optionsFile=...,)
but the simplest function calls reduce to:
res < knitro(objective=..., x0=...)
res < knitro(objective=..., xL=...)
res < knitro(objective=..., xU=...)
res < knitro(nvar=..., objective=...)
You must provide the number of variables or one of the vector which
KnitroR
can use to infer it.You must provide the number of constraints or one of the vector which
KnitroR
can use to infer it. Otherwise,KnitroR
considers that the problem has no constraint.You must provide an objective function for all terms which have not been added through
objLinearStruct
orobjQuadraticStruct
. If the objective is fully linear and quadratic and defined through these parameters, then it is not required to provide an objective function.Similarly, if all constraints are linear and quadratic and have been defined through parameters
conLinearStruct
andconQuadraticStruct
, then it is not required to provide a constraint function.All other parameters are optional.
When a parameter among
constraints
,gradient
,jacobian
,hessianLag
,xTypes
,xStrategies
,xPriorities
is provided but the corresponding sparsity vectors (gradientIndexVars
,jacIndexCons
,jacIndexVars
,hessIndexVars1
,hessIndexVars2
, …) are not,KnitroR
consider them dense.When a parameter among
objLinearStruct
,conLinearStruct
,objQuadraticStruct
,conQuadraticStruct
is provided, it is required to provide the corresponding sparsity vectors.KnitroR
automatically switches to a mixedinteger nonlinear optimizer if the parameterxTypes
is provided and defines at least one integer (1) or binary (2) variable.An API for MPECs is also available via parameters
ccTypes
,ccIdxList1
andccIdxList2
.
The API for bound constrained nonlinear leastsquares problems has the following form :
res < knitrolsq(dimp=..., par0=...,
dataFrameX=..., dataFrameY=...,
residual=...,
jacobian=..., jacIndexRsds=..., jacIndexVars=...,
rsdLinearStruct=..., rsdLinearStructIndexRsds=..., rsdLinearStructIndexVars=...,
parL=..., parU=...,
xScaleFactors=..., xScaleCenters=...,
objScaleFactor=....,
options=..., optionsFile=...)
Examples
rosenbrock.R
: an NLP with no constraints. One example where it is only defined with theobjective
function. One example where it is defined withobjective
andgradient
functions.HS71.R
: an NLP defined withobjective
,constraints
, sparsejacobian
and sparsehessianLag
functions.HS40.R
: an NLP containing linear, quadratic and other nonlinear structures defined withobjective
,constraints
,gradient
, sparsejacobian
and sparsehessianLag
functions, and parametersobjLinearStruct
,conLinearStruct
,objQuadraticStruct
and conQuadraticStruct.lp1.R
: an LP defined with parametersobjLinearStruct
andconLinearStruct
. Noobjective
orconstraints
function are provided.QCQP1.R
: an QCQP defined with theobjective
function and parametersobjLinearStruct
,conLinearStruct
.objQuadraticStruct
andconQuadraticStruct
. Theobjective
function is provided but not theconstraints
function.multipleKnapsack.R
: an ILP defined with parametersobjLinearStruct
andconLinearStruct
. Variables are set integer with parameterxTypes
. Noobjective
orconstraints
function are provided.minlp_case.R
: a MINLP defined withobjective
,constraints
,gradient
, sparsejacobian
and sparsehessianLag
functions,minlp_case_misqp.R
: a MINLP solved with the MISQP algorithm,minlp1.R
: a MINLP using parameterconstraintsIndexCons
.mpec1.R
: an MPEC problem using parameterccTypes
.lsq.R
andlsfit.R
: examples ofknitrolsq
usage.mle.R
: an example of maximum likelihood estimation with R and Knitro routines using different approaches.
Input Arguments of knitro
Below is a description of the input arguments of the knitro
function of KnitroR
.
Input Argument 
Description 


Number of variables. 

Number of constraints. 

Objective function to be minimized or maximized. Prototype: Output: the objective value at point 

Objective goal.


Gradient of the objective function. Prototype: Output: a vector containing the objective gradient values at point 

Vector containing the indices of the nonzero elements of the 

Nonlinear constraints function. Prototype: Output: a vector containing the constraints values at point 

Vector containing the indices of the nonzero elements of the 

Jacobian of nonlinear constraints function. Prototype: Output: a vector containing the jacobian values at point 

Vector containing the constraint indices of the nonzero elements of the 

Vector containing the variable indices of the nonzero elements of the 

Hessian function of Lagrangian. Prototype:
Output: a vector containing the hessian values at point 

Vector containing the first variable indices of the nonzero elements of the


Vector containing the second variable indices of the nonzero elements of the


Vector containing the coefficients of the linear terms of the objective. 

Vector containing the variable indices of the linear terms of the objective. 

Vector containing the coefficients of the linear terms of the constraints. 

Vector containing the constraint indices of the linear terms of the constraints. 

Vector containing the variable indices of the linear terms of the constraints. 

Vector containing the coefficients of the quadratic terms of the objective. 

Vector containing the first variable indices of the quadratic terms of the objective. 

Vector containing the second variable indices of the quadratic terms of the objective. 

Vector containing the coefficients of the quadratic terms of the constraints. 

Vector containing the constraint indices of the quadratic terms of the constraints. 

Vector containing the first variable indices of the quadratic terms of the constraints. 

Vector containing the second variable indices of the quadratic terms of the constraints. 

Vector containing the initial guess. 

Vector containing the lower bounds of the variables. 

Vector containing the upper bounds of the variables. 

Vector containing the lower bounds of the constraints. 

Vector containing the upper bounds of the constraints. 

Vector containing the types of the variables.


Vector containing the indices of the nonzero elements of parameters 

Vector containing the strategies for dealing with individual integer variables.


Vector containing the indices of the nonzero elements of parameter 

Vector containing the branching priorities for integer variables. Values for continuous variables are ignored. 

Vector containing the indices of the nonzero elements of parameter 

Vector containing the 

Vector containing the indices of the nonzero elements of parameter 

Vector containing the types of the complementarity constraints.


Vector containing the first indices of the complementarity constraints. 

Vector containing the second indices of the complementarity constraints. 

Vector containing the scaling factors of the variables. 

Vector containing the scaling centers of the variables. 

Vector containing scaling factors of the nonlinear constraints. 

Vector containing scaling factors of the complementarity constraints. 

Scaling factor of the objective function. 

Options stored as an R list. Example: See Knitro user options for the list of available options. 

Options stored in a file. 
Input Arguments for knitrolsq
Below is a descriptions of the API of the R function knitrolsq
in KnitroR. The function knitrolsq
is based on
Knitro’s internal implementation of boundconstrained nonlinear leastsquares.
Input Argument 
Description 


Dimension of fitting parameter. 

Initial guess on fitting parameter. 

Data frame containing samples x_i. 

Data frame containing samples y_i. 

Nonlinear leastsquares residual function. Prototype:
Ouput: vector containing the residual values at point 

Jacobian of nonlinear leastsquares residuals. Prototype: Output: a vector containing the jacobian values at point 

Vector containing the residual indices of the nonzero elements of the residual


Vector containing the variable indices of the nonzero elements of the residual


Vector containing the coefficients of the linear terms of the residuals. 

Vector containing the residual indices of the linear terms of the residuals. 

Vector containing the variable indices of the linear terms of the residuals. 

Vector of lower bounds on fitting parameter (R vector). 

Vector of upper bounds on fitting parameter (R vector). 

Vector of scaling centers on fitting parameter. (R vector). 

Vector of scaling factors on fitting parameter. (R vector). 

Scaling factor on nonlienar leastsquares objective function. 

Options stored as an R list. Example: See Knitro user options for the list of available options. 

Options stored in a file. 
Output Arguments of knitro
and knitrolsq
Output Argument 
Description 


Knitro’s status. 

Objective value. 

Vector containing the variable primal values. 

Vector containing the variable dual values. 

Vector containing the objective gradient values. 

Vector containing the indices of the nonzero elements of 

Vector containing the constraint values. 

Vector containing the residual function values (LSQ only). 

Vector containing the Jacobian values. 

Vector containing the constraint indices of the nonzero elements of 

Vector containing the variable indices of the nonzero elements of 

Vector containing the residual Jacobian values (LSQ only). 

Vector containing the constraint indices of the nonzero elements of 

Vector containing the variable indices of the nonzero elements of 

Number of objective evaluations. 

Number of gradient evaluations. 

Number of Hessian evaluations. 

Number of Hessianvector product evaluations. 

Number of iterations (continuous models only). 

Number of conjugate gradient iterations (continuous models only). 