# Example

We begin with an example to motivate the various interfaces. Here is what that problem looks like in Julia with the KNITRO.jlinterface:

using KNITRO
using Base.Test

#    min  9 - 8x1 - 6x2 - 4x3
#         + 2(x1^2) + 2(x2^2) + (x3^2) + 2(x1*x2) + 2(x1*x3)
#    subject to  c:  x1 + x2 + 2x3 <= 3
#                x1 >= 0
#                x2 >= 0
#                x3 >= 0
#    initpt (0.5, 0.5, 0.5)
#
#    Solution is x1=4/3, x2=7/9, x3=4/9, lambda=2/9  (f* = 1/9)
#
#  The problem comes from Hock and Schittkowski, HS35.

function eval_f(x::Vector{Float64})
linear_terms = 9.0 - 8.0*x - 6.0*x - 4.0*x
quad_terms = 2.0*x^2 + 2.0*x^2 + x^2 + 2.0*x*x + 2.0*x*x
return linear_terms + quad_terms
end

function eval_g(x::Vector{Float64}, cons::Vector{Float64})
cons = x + x + 2.0*x
end

grad = -8.0 + 4.0*x + 2.0*x + 2.0*x
grad = -6.0 + 2.0*x + 4.0*x
grad = -4.0 + 2.0*x            + 2.0*x
end

function eval_jac_g(x::Vector{Float64}, jac::Vector{Float64})
jac = 1.0
jac = 1.0
jac = 2.0
end

function eval_h(x::Vector{Float64}, lambda::Vector{Float64},
sigma::Float64, hess::Vector{Float64})
hess = sigma*4.0
hess = sigma*2.0
hess = sigma*2.0
hess = sigma*4.0
hess = sigma*2.0
end

function eval_hv(x::Vector{Float64}, lambda::Vector{Float64},
sigma::Float64, hv::Vector{Float64})
hv = sigma*4.0*hv + sigma*2.0*hv + sigma*2.0*hv
hv = sigma*2.0*hv + sigma*4.0*hv
hv = sigma*2.0*hv                   + sigma*2.0*hv
end

objGoal = KTR_OBJGOAL_MINIMIZE

n = 3
x_L = zeros(n)
x_U = [KTR_INFBOUND,KTR_INFBOUND,KTR_INFBOUND]

m = 1
c_Type = [KTR_CONTYPE_LINEAR]
c_L = [-KTR_INFBOUND]
c_U = [3.0]

jac_con = Int32[0,0,0]
jac_var = Int32[0,1,2]
hess_row = Int32[0,0,0,1,2]
hess_col = Int32[0,1,2,1,2]

x       = [0.5,0.5,0.5]
lambda  = zeros(n+m)
obj     = [0.0]

kp = createProblem()

As you can see, the code mirrors the C interface fairly closely, with some C-specific features abstracted such as replacing the various callback-adding functions with one setCallbacks() method.