From a2745bda9b6970fcfe58308d1e13d19f8d6dddfa Mon Sep 17 00:00:00 2001 From: yeesian Date: Sun, 19 Oct 2014 13:29:10 -0400 Subject: [PATCH] added `kp.eval_status` for reverse-comms mode to disambiguate from `kp.status`, which is used as the return code from `solveProblem` --- examples/hs015_tuner.jl | 2 -- examples/qcqp_reversecomm.jl | 30 +++++++++++++++++------------- src/KNITRO.jl | 27 ++++++++++++--------------- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/examples/hs015_tuner.jl b/examples/hs015_tuner.jl index d493a8a..12a4529 100644 --- a/examples/hs015_tuner.jl +++ b/examples/hs015_tuner.jl @@ -86,5 +86,3 @@ solveProblem(kp) @test_approx_eq_eps kp.x[1] 0.5 1e-4 @test_approx_eq_eps kp.x[2] 2.0 1e-4 @test_approx_eq_eps kp.obj_val[1] 306.5 0.02 - -freeProblem(kp) \ No newline at end of file diff --git a/examples/qcqp_reversecomm.jl b/examples/qcqp_reversecomm.jl index ef97ca4..a3850de 100644 --- a/examples/qcqp_reversecomm.jl +++ b/examples/qcqp_reversecomm.jl @@ -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 --- diff --git a/src/KNITRO.jl b/src/KNITRO.jl index 91e25b5..1539b4d 100644 --- a/src/KNITRO.jl +++ b/src/KNITRO.jl @@ -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 @@ -56,8 +57,7 @@ module KNITRO function KnitroProblem() prob = new(newcontext()) - # finalizer segfaults upon termination of the running script - # finalizer(prob, freeProblem) + finalizer(prob, freeProblem) prob end end @@ -65,23 +65,16 @@ module KNITRO createProblem() = KnitroProblem() function freeProblem(kp::KnitroProblem) - println("KNITRO: calling freecontext on $(kp.env)") - println("C_NULL (for reference): $(C_NULL)") - return_code = @ktr_ccall(free, Int32, (Ptr{Void},), pointer_from_objref(kp.env)) + return_code = @ktr_ccall(free, Int32, (Ptr{Void},), [kp.env]) if return_code != 0 error("KNITRO: Error freeing memory") end - println("KNITRO: freecontext successful") kp.env = C_NULL - println("KNITRO.jl: set env to 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 @@ -102,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