Skip to content

Commit a7105c3

Browse files
committed
solutions stored in cachde
1 parent e21bb83 commit a7105c3

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

src/MOI_wrapper.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
3333
start::Dict{VI,Float64} # can be partial
3434
moi_separator::Any # ::Union{CutCbSeparator, Nothing}
3535
objective_sense::MOI.OptimizationSense
36+
solution_storage::Vector{Ptr{SCIP_SOL}}
3637

3738
function Optimizer(; kwargs...)
3839
scip = Ref{Ptr{SCIP_}}(C_NULL)
@@ -43,7 +44,7 @@ mutable struct Optimizer <: MOI.AbstractOptimizer
4344

4445
scip_data = SCIPData(scip, Dict(), Dict(), 0, 0, Dict(), Dict(), Dict(), [])
4546

46-
o = new(scip_data, PtrMap(), ConsTypeMap(), Dict(), Dict(), Dict(), nothing, MOI.MIN_SENSE)
47+
o = new(scip_data, PtrMap(), ConsTypeMap(), Dict(), Dict(), Dict(), nothing, MOI.MIN_SENSE, [])
4748
finalizer(free_scip, o)
4849

4950
# Set all parameters given as keyword arguments, replacing the
@@ -203,6 +204,7 @@ function MOI.empty!(o::Optimizer)
203204
for pair in o.params
204205
set_parameter(o.inner, pair.first, pair.second)
205206
end
207+
empty!(o.solution_storage)
206208
return nothing
207209
end
208210

@@ -261,6 +263,7 @@ function MOI.optimize!(o::Optimizer)
261263
MOI.set(o, MOI.ObjectiveFunction{SAF}(), SAF([], 0.0))
262264
end
263265
@SCIP_CALL SCIPsolve(o)
266+
o.solution_storage = unsafe_wrap(Vector{Ptr{SCIP_SOL}}, SCIPgetSols(o), SCIPgetNSols(o))
264267
return nothing
265268
end
266269

src/MOI_wrapper/results.jl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ end
3737

3838
function MOI.get(o::Optimizer, ::MOI.ResultCount)::Int
3939
status = SCIPgetStatus(o)
40-
if status in [SCIP_STATUS_UNBOUNDED, SCIP_STATUS_INFORUNBD]
40+
if status in (SCIP_STATUS_UNBOUNDED, SCIP_STATUS_INFORUNBD)
4141
return 0
4242
end
4343
return SCIPgetNSols(o)
@@ -75,29 +75,25 @@ assert_after_prob(o::Optimizer) = assert_stage(o, SCIP_Stage.(3:10))
7575
function MOI.get(o::Optimizer, attr::MOI.ObjectiveValue)
7676
assert_solved(o)
7777
MOI.check_result_index_bounds(o, attr)
78-
sols = unsafe_wrap(Array{Ptr{SCIP_SOL}}, SCIPgetSols(o), SCIPgetNSols(o))
79-
return SCIPgetSolOrigObj(o, sols[attr.result_index])
78+
return SCIPgetSolOrigObj(o, o.solution_storage[attr.result_index])
8079
end
8180

8281
function MOI.get(o::Optimizer, attr::MOI.VariablePrimal, vi::VI)
8382
assert_solved(o)
8483
MOI.check_result_index_bounds(o, attr)
85-
sols = unsafe_wrap(Array{Ptr{SCIP_SOL}}, SCIPgetSols(o), SCIPgetNSols(o))
86-
return SCIPgetSolVal(o, sols[attr.result_index], var(o, vi))
84+
return SCIPgetSolVal(o, o.solution_storage[attr.result_index], var(o, vi))
8785
end
8886

8987
function MOI.get(o::Optimizer, attr::MOI.ConstraintPrimal, ci::CI{VI,<:BOUNDS})
9088
assert_solved(o)
9189
MOI.check_result_index_bounds(o, attr)
92-
sols = unsafe_wrap(Array{Ptr{SCIP_SOL}}, SCIPgetSols(o), SCIPgetNSols(o))
93-
return SCIPgetSolVal(o, sols[attr.result_index], var(o, VI(ci.value)))
90+
return SCIPgetSolVal(o, o.solution_storage[attr.result_index], var(o, VI(ci.value)))
9491
end
9592

9693
function MOI.get(o::Optimizer, attr::MOI.ConstraintPrimal, ci::CI{<:SAF,<:BOUNDS})
9794
assert_solved(o)
9895
MOI.check_result_index_bounds(o, attr)
99-
sols = unsafe_wrap(Array{Ptr{SCIP_SOL}}, SCIPgetSols(o), SCIPgetNSols(o))
100-
return SCIPgetActivityLinear(o, cons(o, ci), sols[attr.result_index])
96+
return SCIPgetActivityLinear(o, cons(o, ci), o.solution_storage[attr.result_index])
10197
end
10298

10399
function MOI.get(o::Optimizer, ::MOI.ObjectiveBound)

src/conshdlr.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ function lock end
172172
"""
173173
Generic `check` function, matching the signature from SCIP's C API.
174174
"""
175-
function _conscheck(scip::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR},
175+
function _conscheck(::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR},
176176
conss::Ptr{Ptr{SCIP_CONS}}, nconss::Cint,
177177
sol::Ptr{SCIP_SOL}, checkintegrality::SCIP_Bool,
178178
checklprows::SCIP_Bool, printreason::SCIP_Bool,
@@ -220,7 +220,7 @@ end
220220
"""
221221
Generic `enfops` function, matching the signature from SCIP's C API.
222222
"""
223-
function _consenfops(scip::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR},
223+
function _consenfops(::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR},
224224
conss::Ptr{Ptr{SCIP_CONS}}, nconss::Cint,
225225
nusefulconss::Cint, solinfeasible::SCIP_Bool,
226226
objinfeasible::SCIP_Bool, result::Ptr{SCIP_RESULT})
@@ -243,7 +243,7 @@ end
243243
"""
244244
Generic `lock` function, matching the signature from SCIP's C API.
245245
"""
246-
function _conslock(scip::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR},
246+
function _conslock(::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR},
247247
cons::Ptr{SCIP_CONS}, locktype::SCIP_LOCKTYPE,
248248
nlockspos::Cint, nlocksneg::Cint)
249249
# get Julia object out of constraint handler data

test/MOI_conshdlr.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ end
134134

135135
# add three integer variables, in {0, 1, 2}
136136
x, y, z = MOI.add_variables(optimizer, 3)
137-
for v in [x, y, z]
137+
for v in (x, y, z)
138138
MOI.add_constraint(optimizer, v, MOI.Integer())
139139
MOI.add_constraint(optimizer, v, MOI.Interval(0.0, 2.0))
140140
end

test/MOI_nonlinear_exprs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ end
125125
x, y = MOI.add_variables(optimizer, 2)
126126

127127
data1 = MOI.NLPBlockData(
128-
[MOI.NLPBoundsPair(rhs, rhs) for rhs in [1.0, 2.0]],
128+
[MOI.NLPBoundsPair(rhs, rhs) for rhs in (1.0, 2.0)],
129129
ExprEvaluator([:(x[$x] == 1.0), :(x[$y] == 2.0)]),
130130
false
131131
)
@@ -134,7 +134,7 @@ end
134134
MOI.optimize!(optimizer)
135135

136136
data2 = MOI.NLPBlockData(
137-
[MOI.NLPBoundsPair(rhs, rhs) for rhs in [1.0, 2.0, 3.0]],
137+
[MOI.NLPBoundsPair(rhs, rhs) for rhs in 1.0:3.0],
138138
ExprEvaluator([:(x[$x] == 1.0), :(x[$y] == 2.0),
139139
:(x[$x] + x[$y] == 3.0)]),
140140
false

0 commit comments

Comments
 (0)