Skip to content

Commit

Permalink
[breaking] rewrite the C wrapper (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Nov 24, 2023
1 parent 4db649c commit 14b5497
Show file tree
Hide file tree
Showing 29 changed files with 1,332 additions and 2,036 deletions.
2 changes: 2 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore:
- 'src/libknitro.jl'
10 changes: 5 additions & 5 deletions examples/advanced/space_shuttle/space_shuttle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,12 @@ objIndex, objCoef = ind_θ[end], 1.0
KNITRO.KN_add_obj_linear_struct(kc, objIndex - 1, objCoef)
KNITRO.KN_set_obj_goal(kc, KNITRO.KN_OBJGOAL_MAXIMIZE)

KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_ALG, KNITRO.KN_ALG_BAR_DIRECT)
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_BAR_MURULE, KNITRO.KN_BAR_MURULE_QUALITY)
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_LINSOLVER, KNITRO.KN_LINSOLVER_MA27)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_ALG, KNITRO.KN_ALG_BAR_DIRECT)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_BAR_MURULE, KNITRO.KN_BAR_MURULE_QUALITY)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_LINSOLVER, KNITRO.KN_LINSOLVER_MA27)

KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_HESSOPT, KNITRO.KN_HESSOPT_LBFGS)
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_LMSIZE, 5) # limited-memory pairs stored
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_HESSOPT, KNITRO.KN_HESSOPT_LBFGS)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_LMSIZE, 5) # limited-memory pairs stored

KNITRO.KN_solve(kc)
nStatus, objSol, x, lambda_ = KNITRO.KN_get_solution(kc)
Expand Down
43 changes: 23 additions & 20 deletions examples/conic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function example_conic(; verbose=true)
####* unbounded below and any unset upper bounds are
####* assumed to be unbounded above. */
n = 4
KNITRO.KN_add_vars(kc, n)
KNITRO.KN_add_vars(kc, n, C_NULL)

xLoBnds = [-KNITRO.KN_INFINITY, 1.0, -KNITRO.KN_INFINITY, 2.0]
xUpBnds = [KNITRO.KN_INFINITY, KNITRO.KN_INFINITY, 1.0, KNITRO.KN_INFINITY]
Expand All @@ -43,31 +43,31 @@ function example_conic(; verbose=true)

#** Add the constraints and set the RHS and coefficients */
m = 3
KNITRO.KN_add_cons(kc, m)
KNITRO.KN_add_cons(kc, m, C_NULL)
KNITRO.KN_set_con_upbnd(kc, 0, 0.0)
KNITRO.KN_set_con_upbnd(kc, 1, 100.0)
KNITRO.KN_set_con_upbnd(kc, 2, 100.0)

#** coefficients for linear terms in constraint c2 */
indexVars1 = Cint[1, 2]
coefs1 = [2.0, 3.0]
KNITRO.KN_add_con_linear_struct(kc, 2, indexVars1, coefs1)
KNITRO.KN_add_con_linear_struct_one(kc, 2, 2, indexVars1, coefs1)

#** coefficient for linear term in constraint c1 */
indexVars2 = Cint[0]
coefs2 = [5.0]
KNITRO.KN_add_con_linear_struct(kc, 1, indexVars2, coefs2)
KNITRO.KN_add_con_linear_struct_one(kc, 1, 1, indexVars2, coefs2)

#** coefficient for linear term in constraint c0 */
indexVars3 = Cint[1]
coefs3 = [-10.0]
KNITRO.KN_add_con_linear_struct(kc, 0, indexVars3, coefs3)
KNITRO.KN_add_con_linear_struct_one(kc, 1, 0, indexVars3, coefs3)

#** coefficient for quadratic term in constraint c1 */
qconIndexVar1 = 3
qconIndexVar2 = 3
qconCoef = 1.0
KNITRO.KN_add_con_quadratic_struct(kc, 1, qconIndexVar1, qconIndexVar2, qconCoef)
qconIndexVar1 = Cint[3]
qconIndexVar2 = Cint[3]
qconCoef = [1.0]
KNITRO.KN_add_con_quadratic_struct_one(kc, 1, 1, qconIndexVar1, qconIndexVar2, qconCoef)

#** Coefficients for L2-norm constraint components in c0.
#* Assume the form ||Ax+b|| (here with b = 0)
Expand All @@ -90,48 +90,51 @@ function example_conic(; verbose=true)
qobjIndexVars1 = Cint[0, 2, 3, 2, 1]
qobjIndexVars2 = Cint[0, 2, 3, 3, 1]
qobjCoefs = [1.0, 1.0, 1.0, 2.0, 1.0]
KNITRO.KN_add_obj_quadratic_struct(kc, qobjIndexVars1, qobjIndexVars2, qobjCoefs)
KNITRO.KN_add_obj_quadratic_struct(kc, 5, qobjIndexVars1, qobjIndexVars2, qobjCoefs)

#** Add linear objective term. */
lobjIndexVar = Cint[2]
lobjCoef = [1.0]
KNITRO.KN_add_obj_linear_struct(kc, lobjIndexVar, lobjCoef)
KNITRO.KN_add_obj_linear_struct(kc, 1, lobjIndexVar, lobjCoef)

#** Interior/Direct algorithm is required for models with
#* L2 norm structure.
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_ALGORITHM, KNITRO.KN_ALG_BAR_DIRECT)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_ALGORITHM, KNITRO.KN_ALG_BAR_DIRECT)
#** Enable the special barrier tools for second order cone(SOC) constraints. */
KNITRO.KN_set_param(
KNITRO.KN_set_int_param(
kc,
KNITRO.KN_PARAM_BAR_CONIC_ENABLE,
KNITRO.KN_BAR_CONIC_ENABLE_SOC,
)
#** Specify maximum output */
outlev = verbose ? KNITRO.KN_OUTLEV_ALL : KNITRO.KN_OUTLEV_NONE
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_OUTLEV, outlev)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_OUTLEV, outlev)
#** Specify special barrier update rule */
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_BAR_MURULE, KNITRO.KN_BAR_MURULE_FULLMPC)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_BAR_MURULE, KNITRO.KN_BAR_MURULE_FULLMPC)

#** Solve the problem.
####*
####* Return status codes are defined in "knitro.h" and described
####* in the Knitro manual. */
nStatus = KNITRO.KN_solve(kc)
nStatus, objSol, x, _ = KNITRO.KN_get_solution(kc)
feasError = KNITRO.KN_get_abs_feas_error(kc)
optError = KNITRO.KN_get_abs_opt_error(kc)
feasError = Ref{Cdouble}()
KNITRO.KN_get_abs_feas_error(kc, feasError)
optError = Ref{Cdouble}()
KNITRO.KN_get_abs_opt_error(kc, optError)

#** An example of obtaining solution information. */
if verbose
println("Knitro converged with final status = ", nStatus)
println(" optimal objective value = ", objSol)
println(" optimal primal values x = ", x)
println(" feasibility violation = ", feasError)
println(" KKT optimality violation = ", optError)
println(" feasibility violation = ", feasError[])
println(" KKT optimality violation = ", optError[])
end

#** Delete the Knitro solver instance. */
return KNITRO.KN_free(kc)
KNITRO.KN_free(kc)
return
end

example_conic(; verbose=isdefined(Main, :KN_VERBOSE) ? KN_VERBOSE : true)
22 changes: 14 additions & 8 deletions examples/fcga.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,21 @@ function example_fcga(; verbose=true)
# Note: any unset lower bounds are assumed to be
# unbounded below and any unset upper bounds are
# assumed to be unbounded above.
vars = KNITRO.KN_add_vars(kc, 4)
vars = zeros(Cint, 4)
KNITRO.KN_add_vars(kc, 4, vars)
for x in vars
KNITRO.KN_set_var_primal_init_value(kc, x, 0.8)
end

# Add the constraints and set the rhs and coefficients
KNITRO.KN_add_cons(kc, 3)
KNITRO.KN_add_cons(kc, 3, C_NULL)
KNITRO.KN_set_con_eqbnds_all(kc, [1.0, 0.0, 0.0])

# Coefficients for 2 linear terms
lconIndexCons = Int32[1, 2]
lconIndexVars = Int32[2, 1]
lconCoefs = [-1.0, -1.0]
KNITRO.KN_add_con_linear_struct(kc, lconIndexCons, lconIndexVars, lconCoefs)
KNITRO.KN_add_con_linear_struct(kc, 2, lconIndexCons, lconIndexVars, lconCoefs)

# Coefficients for 2 quadratic terms

Expand All @@ -132,6 +133,7 @@ function example_fcga(; verbose=true)

KNITRO.KN_add_con_quadratic_struct(
kc,
2,
qconIndexCons,
qconIndexVars1,
qconIndexVars2,
Expand Down Expand Up @@ -185,26 +187,29 @@ function example_fcga(; verbose=true)

# Set option to print output after every iteration.
kn_outlev = verbose ? KNITRO.KN_OUTLEV_ITER : KNITRO.KN_OUTLEV_NONE
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_OUTLEV, kn_outlev)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_OUTLEV, kn_outlev)

# Set option to tell Knitro that the gradients are being provided
# with the functions in one callback.
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_EVAL_FCGA, KNITRO.KN_EVAL_FCGA_YES)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_EVAL_FCGA, KNITRO.KN_EVAL_FCGA_YES)

# Solve the problem.
#
# Return status codes are defined in "knitro.h" and described
# in the Knitro manual.
nStatus = KNITRO.KN_solve(kc)
nStatus, objSol, x, lambda_ = KNITRO.KN_get_solution(kc)

# An example of obtaining solution information.
if verbose
feasError = Ref{Cdouble}()
KNITRO.KN_get_abs_feas_error(kc, feasError)
optError = Ref{Cdouble}()
KNITRO.KN_get_abs_opt_error(kc, optError)
println("Knitro converged with final status = ", nStatus)
println(" optimal objective value = ", objSol)
println(" optimal primal values x = ", x)
println(" feasibility violation = ", KNITRO.KN_get_abs_feas_error(kc))
println(" KKT optimality violation = ", KNITRO.KN_get_abs_opt_error(kc))
println(" feasibility violation = ", feasError[])
println(" KKT optimality violation = ", optError[])
end

# Delete the Knitro solver instance.
Expand All @@ -215,6 +220,7 @@ function example_fcga(; verbose=true)
@test objSol 0.25
@test x [0.793701, 0.707107, 0.529732, 0.840896] atol = 1e-5
end
return
end

example_fcga(; verbose=isdefined(Main, :KN_VERBOSE) ? KN_VERBOSE : true)
10 changes: 5 additions & 5 deletions examples/licensemanager/nlp1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ KNITRO.KN_set_con_lobnds(kc, [1.0, 0.0])
# structure for these constraints.

# First load quadratic structure x0*x1 for the first constraint
KNITRO.KN_add_con_quadratic_struct(kc, 0, 0, 1, 1.0)
KNITRO.KN_add_con_quadratic_struct_one(kc, 1, 0, Cint[0], Cint[1], [1.0])

# Load structure for the second constraint. below we add the linear
# structure and the quadratic structure separately, though it
Expand All @@ -122,10 +122,10 @@ KNITRO.KN_add_con_quadratic_struct(kc, 0, 0, 1, 1.0)
# supports adding linear terms.

# Add linear term x0 in the second constraint
KNITRO.KN_add_con_linear_struct(kc, 1, 0, 1.0)
KNITRO.KN_add_con_linear_struct_one(kc, 1, 1, Cint[0], [1.0])

# Add quadratic term x1^2 in the second constraint
KNITRO.KN_add_con_quadratic_struct(kc, 1, 1, 1, 1.0)
KNITRO.KN_add_con_quadratic_struct_one(kc, 1, 1, Cint[1], Cint[1], [1.0])

# Add a callback function "callbackEvalF" to evaluate the nonlinear
#(non-quadratic) objective. Note that the linear and
Expand Down Expand Up @@ -160,13 +160,13 @@ KNITRO.KN_set_cb_hess(kc, cb, KNITRO.KN_DENSE_ROWMAJOR, callbackEvalH!)
# Specify that the user is able to provide evaluations
# of the hessian matrix without the objective component.
# turned off by default but should be enabled if possible.
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_HESSIAN_NO_F, KNITRO.KN_HESSIAN_NO_F_ALLOW)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_HESSIAN_NO_F, KNITRO.KN_HESSIAN_NO_F_ALLOW)

# Set minimize or maximize(if not set, assumed minimize)
KNITRO.KN_set_obj_goal(kc, KNITRO.KN_OBJGOAL_MINIMIZE)

# Perform a derivative check.
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_DERIVCHECK, KNITRO.KN_DERIVCHECK_ALL)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_DERIVCHECK, KNITRO.KN_DERIVCHECK_ALL)

# Solve the problem.
#
Expand Down
20 changes: 13 additions & 7 deletions examples/lp1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ function example_lp1(; verbose=true)

# Add the variables and set their bounds.
# Note: unset bounds assumed to be infinite.
xIndices = KNITRO.KN_add_vars(kc, 4)
xIndices = zeros(Cint, 4)
KNITRO.KN_add_vars(kc, 4, xIndices)
for x in xIndices
KNITRO.KN_set_var_lobnd(kc, x, 0.0)
end

# Add the constraints and set the rhs and coefficients.
cons = KNITRO.KN_add_cons(kc, 2)
cons = zeros(Cint, 2)
KNITRO.KN_add_cons(kc, 2, cons)
KNITRO.KN_set_con_eqbnds_all(kc, [5.0, 8.0])
# Add Jacobian structure and coefficients.
# First constraint
Expand All @@ -53,8 +55,8 @@ function example_lp1(; verbose=true)
jacIndexCons = [jacIndexCons; Int32[1, 1, 1]]
jacIndexVars = [jacIndexVars; Int32[0, 1, 3]]
jacCoefs = [jacCoefs; [2.0, 0.5, 1.0]]
KNITRO.KN_add_con_linear_struct(kc, 0, Int32[0, 1, 2], [1.0, 1.0, 1.0])
KNITRO.KN_add_con_linear_struct(kc, 1, Int32[0, 1, 3], [2.0, 0.5, 1.0])
KNITRO.KN_add_con_linear_struct_one(kc, 3, 0, Int32[0, 1, 2], [1.0, 1.0, 1.0])
KNITRO.KN_add_con_linear_struct_one(kc, 3, 1, Int32[0, 1, 3], [2.0, 0.5, 1.0])

# Set minimize or maximize (if not set, assumed minimize).
KNITRO.KN_set_obj_goal(kc, KNITRO.KN_OBJGOAL_MINIMIZE)
Expand All @@ -65,7 +67,7 @@ function example_lp1(; verbose=true)
KNITRO.KN_add_obj_linear_struct(kc, 2, objIndices, objCoefs)

kn_outlev = verbose ? KNITRO.KN_OUTLEV_ALL : KNITRO.KN_OUTLEV_NONE
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_OUTLEV, kn_outlev)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_OUTLEV, kn_outlev)

# Solve the problem.
#
Expand All @@ -75,11 +77,15 @@ function example_lp1(; verbose=true)
nStatus, objSol, x, lambda_ = KNITRO.KN_get_solution(kc)

if verbose
feasError = Ref{Cdouble}()
KNITRO.KN_get_abs_feas_error(kc, feasError)
optError = Ref{Cdouble}()
KNITRO.KN_get_abs_opt_error(kc, optError)
println("Knitro converged with final status = ", nStatus)
println(" optimal objective value = ", objSol)
println(" optimal primal values x = ", x)
println(" feasibility violation = ", KNITRO.KN_get_abs_feas_error(kc))
println(" KKT optimality violation = ", KNITRO.KN_get_abs_opt_error(kc))
println(" feasibility violation = ", feasError[])
println(" KKT optimality violation = ", optError[])
end

# Delete the Knitro solver instance.
Expand Down
8 changes: 4 additions & 4 deletions examples/lsq1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ function example_lsq1(; verbose=true)
# unbounded below and any unset upper bounds are
# assumed to be unbounded above.
n = 3 # # of variables/parameters
KNITRO.KN_add_vars(kc, n)
KNITRO.KN_add_vars(kc, n, C_NULL)

# Add the residuals.
m = 5 # # of residuals
KNITRO.KN_add_rsds(kc, m)
KNITRO.KN_add_rsds(kc, m, C_NULL)

# Set the array of constants, y, in the residuals
KNITRO.KN_add_rsd_constants_all(kc, [1.0, 0.5, 0.0, 0.5, 2.0])
Expand Down Expand Up @@ -79,10 +79,10 @@ function example_lsq1(; verbose=true)
coefs = [coefs; [-1.0, -1.0, -1.0]]

# Pass in the linear coefficients
KNITRO.KN_add_rsd_linear_struct(kc, indexRsds, indexVars, coefs)
KNITRO.KN_add_rsd_linear_struct(kc, length(indexRsds), indexRsds, indexVars, coefs)

kn_outlev = verbose ? KNITRO.KN_OUTLEV_ALL : KNITRO.KN_OUTLEV_NONE
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_OUTLEV, kn_outlev)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_OUTLEV, kn_outlev)

# Solve the problem.
#
Expand Down
6 changes: 3 additions & 3 deletions examples/lsq2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function example_lsq2(; verbose=true)
# unbounded below and any unset upper bounds are
# assumed to be unbounded above.
n = 2 # # of variables/parameters
KNITRO.KN_add_vars(kc, n)
KNITRO.KN_add_vars(kc, n, C_NULL)

# In order to prevent the possiblity of numerical
# overflow from very large numbers, we set a
Expand All @@ -110,7 +110,7 @@ function example_lsq2(; verbose=true)

# Add the residuals.
m = 6 # # of residuals
KNITRO.KN_add_rsds(kc, m)
KNITRO.KN_add_rsds(kc, m, C_NULL)

# Set the array of constants in the residuals
KNITRO.KN_add_rsd_constants_all(kc, [-2.138, -3.421, -3.597, -4.34, -4.882, -5.66])
Expand All @@ -132,7 +132,7 @@ function example_lsq2(; verbose=true)
KNITRO.KN_set_cb_rsd_jac(kc, cb, KNITRO.KN_DENSE_ROWMAJOR, callbackEvalRJ)

kn_outlev = verbose ? KNITRO.KN_OUTLEV_ALL : KNITRO.KN_OUTLEV_NONE
KNITRO.KN_set_param(kc, KNITRO.KN_PARAM_OUTLEV, kn_outlev)
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_OUTLEV, kn_outlev)

# Solve the problem.
#
Expand Down
Loading

0 comments on commit 14b5497

Please sign in to comment.