From a7105c3ac27daceda3ff89776838b0612c02ad82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Besan=C3=A7on?= Date: Fri, 26 Aug 2022 12:01:50 +0200 Subject: [PATCH] solutions stored in cachde --- src/MOI_wrapper.jl | 5 ++++- src/MOI_wrapper/results.jl | 14 +++++--------- src/conshdlr.jl | 6 +++--- test/MOI_conshdlr.jl | 2 +- test/MOI_nonlinear_exprs.jl | 4 ++-- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index 8585aada..a0bc31dd 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -33,6 +33,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer start::Dict{VI,Float64} # can be partial moi_separator::Any # ::Union{CutCbSeparator, Nothing} objective_sense::MOI.OptimizationSense + solution_storage::Vector{Ptr{SCIP_SOL}} function Optimizer(; kwargs...) scip = Ref{Ptr{SCIP_}}(C_NULL) @@ -43,7 +44,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer scip_data = SCIPData(scip, Dict(), Dict(), 0, 0, Dict(), Dict(), Dict(), []) - o = new(scip_data, PtrMap(), ConsTypeMap(), Dict(), Dict(), Dict(), nothing, MOI.MIN_SENSE) + o = new(scip_data, PtrMap(), ConsTypeMap(), Dict(), Dict(), Dict(), nothing, MOI.MIN_SENSE, []) finalizer(free_scip, o) # Set all parameters given as keyword arguments, replacing the @@ -203,6 +204,7 @@ function MOI.empty!(o::Optimizer) for pair in o.params set_parameter(o.inner, pair.first, pair.second) end + empty!(o.solution_storage) return nothing end @@ -261,6 +263,7 @@ function MOI.optimize!(o::Optimizer) MOI.set(o, MOI.ObjectiveFunction{SAF}(), SAF([], 0.0)) end @SCIP_CALL SCIPsolve(o) + o.solution_storage = unsafe_wrap(Vector{Ptr{SCIP_SOL}}, SCIPgetSols(o), SCIPgetNSols(o)) return nothing end diff --git a/src/MOI_wrapper/results.jl b/src/MOI_wrapper/results.jl index 9f3787e3..afa7f39f 100644 --- a/src/MOI_wrapper/results.jl +++ b/src/MOI_wrapper/results.jl @@ -37,7 +37,7 @@ end function MOI.get(o::Optimizer, ::MOI.ResultCount)::Int status = SCIPgetStatus(o) - if status in [SCIP_STATUS_UNBOUNDED, SCIP_STATUS_INFORUNBD] + if status in (SCIP_STATUS_UNBOUNDED, SCIP_STATUS_INFORUNBD) return 0 end return SCIPgetNSols(o) @@ -75,29 +75,25 @@ assert_after_prob(o::Optimizer) = assert_stage(o, SCIP_Stage.(3:10)) function MOI.get(o::Optimizer, attr::MOI.ObjectiveValue) assert_solved(o) MOI.check_result_index_bounds(o, attr) - sols = unsafe_wrap(Array{Ptr{SCIP_SOL}}, SCIPgetSols(o), SCIPgetNSols(o)) - return SCIPgetSolOrigObj(o, sols[attr.result_index]) + return SCIPgetSolOrigObj(o, o.solution_storage[attr.result_index]) end function MOI.get(o::Optimizer, attr::MOI.VariablePrimal, vi::VI) assert_solved(o) MOI.check_result_index_bounds(o, attr) - sols = unsafe_wrap(Array{Ptr{SCIP_SOL}}, SCIPgetSols(o), SCIPgetNSols(o)) - return SCIPgetSolVal(o, sols[attr.result_index], var(o, vi)) + return SCIPgetSolVal(o, o.solution_storage[attr.result_index], var(o, vi)) end function MOI.get(o::Optimizer, attr::MOI.ConstraintPrimal, ci::CI{VI,<:BOUNDS}) assert_solved(o) MOI.check_result_index_bounds(o, attr) - sols = unsafe_wrap(Array{Ptr{SCIP_SOL}}, SCIPgetSols(o), SCIPgetNSols(o)) - return SCIPgetSolVal(o, sols[attr.result_index], var(o, VI(ci.value))) + return SCIPgetSolVal(o, o.solution_storage[attr.result_index], var(o, VI(ci.value))) end function MOI.get(o::Optimizer, attr::MOI.ConstraintPrimal, ci::CI{<:SAF,<:BOUNDS}) assert_solved(o) MOI.check_result_index_bounds(o, attr) - sols = unsafe_wrap(Array{Ptr{SCIP_SOL}}, SCIPgetSols(o), SCIPgetNSols(o)) - return SCIPgetActivityLinear(o, cons(o, ci), sols[attr.result_index]) + return SCIPgetActivityLinear(o, cons(o, ci), o.solution_storage[attr.result_index]) end function MOI.get(o::Optimizer, ::MOI.ObjectiveBound) diff --git a/src/conshdlr.jl b/src/conshdlr.jl index da4ffc78..d555439d 100644 --- a/src/conshdlr.jl +++ b/src/conshdlr.jl @@ -172,7 +172,7 @@ function lock end """ Generic `check` function, matching the signature from SCIP's C API. """ -function _conscheck(scip::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR}, +function _conscheck(::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR}, conss::Ptr{Ptr{SCIP_CONS}}, nconss::Cint, sol::Ptr{SCIP_SOL}, checkintegrality::SCIP_Bool, checklprows::SCIP_Bool, printreason::SCIP_Bool, @@ -220,7 +220,7 @@ end """ Generic `enfops` function, matching the signature from SCIP's C API. """ -function _consenfops(scip::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR}, +function _consenfops(::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR}, conss::Ptr{Ptr{SCIP_CONS}}, nconss::Cint, nusefulconss::Cint, solinfeasible::SCIP_Bool, objinfeasible::SCIP_Bool, result::Ptr{SCIP_RESULT}) @@ -243,7 +243,7 @@ end """ Generic `lock` function, matching the signature from SCIP's C API. """ -function _conslock(scip::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR}, +function _conslock(::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR}, cons::Ptr{SCIP_CONS}, locktype::SCIP_LOCKTYPE, nlockspos::Cint, nlocksneg::Cint) # get Julia object out of constraint handler data diff --git a/test/MOI_conshdlr.jl b/test/MOI_conshdlr.jl index 1cc93a32..f1dd2d86 100644 --- a/test/MOI_conshdlr.jl +++ b/test/MOI_conshdlr.jl @@ -134,7 +134,7 @@ end # add three integer variables, in {0, 1, 2} x, y, z = MOI.add_variables(optimizer, 3) - for v in [x, y, z] + for v in (x, y, z) MOI.add_constraint(optimizer, v, MOI.Integer()) MOI.add_constraint(optimizer, v, MOI.Interval(0.0, 2.0)) end diff --git a/test/MOI_nonlinear_exprs.jl b/test/MOI_nonlinear_exprs.jl index cb4413ea..39465165 100644 --- a/test/MOI_nonlinear_exprs.jl +++ b/test/MOI_nonlinear_exprs.jl @@ -125,7 +125,7 @@ end x, y = MOI.add_variables(optimizer, 2) data1 = MOI.NLPBlockData( - [MOI.NLPBoundsPair(rhs, rhs) for rhs in [1.0, 2.0]], + [MOI.NLPBoundsPair(rhs, rhs) for rhs in (1.0, 2.0)], ExprEvaluator([:(x[$x] == 1.0), :(x[$y] == 2.0)]), false ) @@ -134,7 +134,7 @@ end MOI.optimize!(optimizer) data2 = MOI.NLPBlockData( - [MOI.NLPBoundsPair(rhs, rhs) for rhs in [1.0, 2.0, 3.0]], + [MOI.NLPBoundsPair(rhs, rhs) for rhs in 1.0:3.0], ExprEvaluator([:(x[$x] == 1.0), :(x[$y] == 2.0), :(x[$x] + x[$y] == 3.0)]), false