Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solutions stored in cache #241

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
14 changes: 5 additions & 9 deletions src/MOI_wrapper/results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/conshdlr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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})
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/MOI_conshdlr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/MOI_nonlinear_exprs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
Expand Down