Skip to content

Commit

Permalink
Merge pull request #8 from yeesian/fix-env
Browse files Browse the repository at this point in the history
[WIP] finalize using freeProblem. Closes #7
  • Loading branch information
yeesian committed Oct 19, 2014
2 parents 56ed170 + a2745bd commit 4194985
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
30 changes: 17 additions & 13 deletions examples/qcqp_reversecomm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,35 @@ hessVector = Array(Float64, n)
#---- RETURNS WHENEVER IT NEEDS MORE PROBLEM INFORMATION. THE CALLING
#---- PROGRAM MUST INTERPRET KNITRO'S RETURN STATUS AND CONTINUE
#---- SUPPLYING PROBLEM INFORMATION UNTIL KNITRO IS COMPLETE.
# nEvalStatus = int32(0)
nKnStatus = int32(1)
while nKnStatus > 0
nKnStatus = solveProblem(kp, cons, objGrad, jac, hess, hessVector)
if nKnStatus == KTR_RC_EVALFC
while kp.status > 0
solveProblem(kp, cons, objGrad, jac, hess, hessVector)
if kp.status == KTR_RC_EVALFC
#---- KNITRO WANTS obj AND c EVALUATED AT THE POINT x.
kp.obj_val[1] = eval_f(kp.x)
eval_g(kp.x,cons)
elseif nKnStatus == KTR_RC_EVALGA
elseif kp.status == KTR_RC_EVALGA
#---- KNITRO WANTS objGrad AND jac EVALUATED AT THE POINT x.
eval_grad_f(kp.x, objGrad)
eval_jac_g(kp.x, jac)
elseif nKnStatus == KTR_RC_EVALH
elseif kp.status == KTR_RC_EVALH
#---- KNITRO WANTS hess EVALUATED AT THE POINT x.
eval_h(kp.x, kp.lambda, 1.0, hess)
elseif nKnStatus == KTR_RC_EVALH_NO_F
elseif kp.status == KTR_RC_EVALH_NO_F
#---- KNITRO WANTS hess EVALUATED AT THE POINT x
#---- WITHOUT OBJECTIVE COMPONENT.
eval_h(kp.x, kp.lambda, 0.0, hess)
elseif kp.status == KTR_RC_EVALHV
#---- KNITRO WANTS hessVector EVALUATED AT THE POINT x.
eval_hv(kp.x, kp.lambda, 1.0, hessVector)
elseif kp.status == KTR_RC_EVALHV_NO_F
#---- KNITRO WANTS hessVector EVALUATED AT THE POINT x
#---- WITHOUT OBJECTIVE COMPONENT.
eval_hv(kp.x, kp.lambda, 0.0, hessVector)
end
#---- ASSUME THAT PROBLEM EVALUATION IS ALWAYS SUCCESSFUL.
#---- IF A FUNCTION OR ITS DERIVATIVE COULD NOT BE EVALUATED
#---- AT THE GIVEN (x, lambda), THEN SET kp.status = 1 BEFORE
#---- CALLING solve AGAIN.
# kp.status = int32(0)
#/*---- ASSUME THAT PROBLEM EVALUATION IS ALWAYS SUCCESSFUL.
#*---- IF A FUNCTION OR ITS DERIVATIVE COULD NOT BE EVALUATED
#*---- AT THE GIVEN (x, lambda), THEN SET evalStatus = 1 BEFORE
#*---- CALLING KTR_solve AGAIN. */
end

# --- test optimal solutions ---
Expand Down
30 changes: 17 additions & 13 deletions src/KNITRO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module KNITRO

export
KnitroProblem,
createProblem, #freeProblem,
createProblem, freeProblem,
initializeProblem,
solveProblem,
restartProblem,
Expand Down Expand Up @@ -43,6 +43,7 @@ module KNITRO
lambda::Vector{Float64}
g::Vector{Float64} # Final constraint values
obj_val::Vector{Float64} # (length 1) Final objective
eval_status::Int32 # scalar input used only for reverse comms
status::Int32 # Final status

# Callbacks
Expand All @@ -56,25 +57,24 @@ module KNITRO

function KnitroProblem()
prob = new(newcontext())
# finalizer segfaults upon termination of the running script
# finalizer(prob, freeProblem)
finalizer(prob, freeProblem)
prob
end
end

createProblem() = KnitroProblem()

function freeProblem(kp::KnitroProblem)
freecontext(kp.env)
#kp.env = C_NULL
return_code = @ktr_ccall(free, Int32, (Ptr{Void},), [kp.env])
if return_code != 0
error("KNITRO: Error freeing memory")
end
kp.env = C_NULL
end

function initializeProblem(kp, objGoal, objType, x_l, x_u, c_Type, g_lb,
g_ub, jac_var, jac_con, hess_row, hess_col,
x0 = C_NULL, lambda0 = C_NULL)
# TODO: check return code?
init_problem(kp, objGoal, objType, x_l, x_u, c_Type, g_lb, g_ub,
jac_var, jac_con, hess_row, hess_col, x0, lambda0)
if objGoal == KTR_OBJGOAL_MINIMIZE
kp.sense = :Min
else
Expand All @@ -95,20 +95,24 @@ module KNITRO
kp.lambda = zeros(Float64, kp.n + kp.m)
end

kp.g = Array(Float64, kp.m)
kp.obj_val = Array(Float64, 1)
kp.status = int32(0)
kp.g = zeros(Float64, kp.m)
kp.obj_val = zeros(Float64, 1)
kp.status = int32(1)
kp.eval_status = int32(0)

init_problem(kp, objGoal, objType, x_l, x_u, c_Type, g_lb, g_ub,
jac_var, jac_con, hess_row, hess_col, kp.x, kp.lambda)
end

solveProblem(kp::KnitroProblem) = (kp.status = solve_problem(kp, kp.x, kp.lambda,
kp.status, kp.obj_val))
kp.eval_status, kp.obj_val))
function solveProblem(kp::KnitroProblem,
cons::Vector{Float64},
objGrad::Vector{Float64},
jac::Vector{Float64},
hess::Vector{Float64},
hessVector::Vector{Float64})
kp.status = solve_problem(kp, kp.x, kp.lambda, kp.status, kp.obj_val, cons,
kp.status = solve_problem(kp, kp.x, kp.lambda, kp.eval_status, kp.obj_val, cons,
objGrad, jac, hess, hessVector)
end

Expand Down
5 changes: 4 additions & 1 deletion src/ktr_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ end
@doc "Free all memory and release any KNITRO license acquired with [kp_env]" ->
function freecontext(kp_env::Ptr{Void})
if kp_env != C_NULL
println("KNITRO: calling freecontext on $(kp_env)")
println("C_NULL (for reference): $(C_NULL)")
return_code = @ktr_ccall(free, Int32, (Ptr{Void},), kp_env)
if return_code != 0
error("KNITRO: Error freeing memory")
error("KNITRO: Error freeing memory")
end
println("KNITRO: freecontext successful")
end
end

Expand Down

0 comments on commit 4194985

Please sign in to comment.