Skip to content

Commit 5631f87

Browse files
authored
Various refactoring annd tidying of HeuristicCallback and UserCutCallback (#317)
1 parent be87ae7 commit 5631f87

File tree

4 files changed

+38
-142
lines changed

4 files changed

+38
-142
lines changed

src/MOI_wrapper.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,6 @@ include(joinpath("MOI_wrapper", "nonlinear_constraints.jl"))
476476
include(joinpath("MOI_wrapper", "objective.jl"))
477477
include(joinpath("MOI_wrapper", "results.jl"))
478478
include(joinpath("MOI_wrapper", "conshdlr.jl"))
479-
include(joinpath("MOI_wrapper", "sepa.jl"))
480-
include(joinpath("MOI_wrapper", "heuristic.jl"))
479+
include(joinpath("MOI_wrapper", "UserCutCallback.jl"))
480+
include(joinpath("MOI_wrapper", "HeuristicCallback.jl"))
481481
include(joinpath("MOI_wrapper", "conflict.jl"))

src/MOI_wrapper/heuristic.jl renamed to src/MOI_wrapper/HeuristicCallback.jl

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,12 @@
33
# Use of this source code is governed by an MIT-style license that can be found
44
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
55

6-
function include_heuristic(
7-
o::Optimizer,
8-
heuristic::HT;
9-
name="",
10-
description="",
11-
dispchar='_',
12-
priority=10000,
13-
frequency=1,
14-
frequency_offset=0,
15-
maximum_depth=-1,
16-
timing_mask=SCIP_HEURTIMING_BEFORENODE,
17-
usessubscip=false,
18-
) where {HT}
6+
function include_heuristic(o::Optimizer, heuristic; kwargs...)
197
return include_heuristic(
208
o.inner.scip[],
219
heuristic,
2210
o.inner.heuristic_storage;
23-
name=name,
24-
description=description,
25-
dispchar=dispchar,
26-
priority=priority,
27-
frequency=frequency,
28-
frequency_offset=frequency_offset,
29-
maximum_depth=maximum_depth,
30-
timing_mask=timing_mask,
31-
usessubscip=usessubscip,
11+
kwargs...,
3212
)
3313
end
3414

@@ -65,9 +45,7 @@ function find_primal_solution(
6545
return SCIP_OKAY, result
6646
end
6747

68-
#
6948
# MOI Interface for heuristic callbacks
70-
#
7149

7250
function MOI.get(
7351
o::Optimizer,
@@ -77,6 +55,8 @@ function MOI.get(
7755
return [SCIPgetSolVal(o, C_NULL, var(o, vi)) for vi in vs]
7856
end
7957

58+
MOI.supports(::Optimizer, ::MOI.HeuristicCallback) = true
59+
8060
function MOI.set(o::Optimizer, ::MOI.HeuristicCallback, cb::Function)
8161
if o.moi_heuristic === nothing
8262
o.moi_heuristic = HeuristicCb(o.inner, cb)
@@ -91,7 +71,8 @@ function MOI.set(o::Optimizer, ::MOI.HeuristicCallback, cb::Function)
9171
end
9272
return nothing
9373
end
94-
MOI.supports(::Optimizer, ::MOI.HeuristicCallback) = true
74+
75+
MOI.supports(::Optimizer, ::MOI.HeuristicSolution{HeuristicCbData}) = true
9576

9677
function MOI.submit(
9778
o::Optimizer,
@@ -102,7 +83,6 @@ function MOI.submit(
10283
callback_data = cb.callback_data
10384
heuristic = callback_data.heur
10485
heur_ = o.inner.heuristic_storage[heuristic]
105-
10686
sol = create_empty_scipsol(o.inner.scip[], heur_)
10787
for idx in eachindex(x)
10888
SCIP.@SCIP_CALL SCIP.SCIPsetSolVal(
@@ -126,5 +106,5 @@ function MOI.submit(
126106
if stored[] == SCIP.TRUE
127107
callback_data.submit_called = true
128108
end
109+
return
129110
end
130-
MOI.supports(::Optimizer, ::MOI.HeuristicSolution{HeuristicCbData}) = true

src/MOI_wrapper/sepa.jl renamed to src/MOI_wrapper/UserCutCallback.jl

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
# Use of this source code is governed by an MIT-style license that can be found
44
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
55

6-
#
7-
# Adding separators to SCIP.Optimizer.
8-
#
9-
106
"""
117
include_sepa(
128
o::Optimizer,
@@ -24,34 +20,11 @@ Include a user defined separator `sepa` to the SCIP optimizer instance `o`.
2420
2521
All parameters have default values, that can be set as keyword arguments.
2622
"""
27-
function include_sepa(
28-
o::Optimizer,
29-
sepa::SEPA;
30-
name="",
31-
description="",
32-
priority=0,
33-
freq=1,
34-
maxbounddist=0.0,
35-
usessubscip=false,
36-
delay=false,
37-
) where {SEPA<:AbstractSeparator}
38-
include_sepa(
39-
o.inner.scip[],
40-
o.inner.sepas,
41-
sepa;
42-
name=name,
43-
description=description,
44-
priority=priority,
45-
freq=freq,
46-
maxbounddist=maxbounddist,
47-
usessubscip=usessubscip,
48-
delay=delay,
49-
)
23+
function include_sepa(o::Optimizer, sepa::AbstractSeparator; kwargs...)
24+
return include_sepa(o.inner.scip[], o.inner.sepas, sepa; kwargs...)
5025
end
5126

52-
#
5327
# Separator for cutcallbacks.
54-
#
5528

5629
mutable struct CutCbSeparator <: AbstractSeparator
5730
scipd::SCIPData
@@ -77,47 +50,44 @@ function exec_lp(sepa::CutCbSeparator)
7750
return cb_data.submit_called ? SCIP_SEPARATED : SCIP_DIDNOTFIND
7851
end
7952

80-
#
8153
# MOI Interface for cutcallbacks
82-
#
8354

8455
function MOI.get(o::Optimizer, ::MOI.CallbackVariablePrimal{CutCbData}, vi)
8556
return SCIPgetSolVal(o, C_NULL, var(o, vi))
8657
end
8758

59+
MOI.supports(::Optimizer, ::MOI.UserCutCallback) = true
60+
8861
function MOI.set(o::Optimizer, ::MOI.UserCutCallback, cb::Function)
8962
if o.moi_separator === nothing
9063
o.moi_separator = CutCbSeparator(o.inner, cb)
9164
include_sepa(o, o.moi_separator)
9265
else
9366
o.moi_separator.cutcallback = cb
9467
end
68+
return
9569
end
96-
MOI.supports(::Optimizer, ::MOI.UserCutCallback) = true
70+
71+
MOI.supports(::Optimizer, ::MOI.UserCut{CutCbData}) = true
9772

9873
function MOI.submit(
9974
o::Optimizer,
10075
cb_data::MOI.UserCut{CutCbData},
10176
func::MOI.ScalarAffineFunction{Float64},
102-
set::S,
103-
) where {S<:BOUNDS}
104-
varrefs = [VarRef(t.variable.value) for t in func.terms]
105-
coefs = [t.coefficient for t in func.terms]
106-
77+
set::BOUNDS,
78+
)
10779
lhs, rhs = bounds(set)
108-
lhs = lhs === nothing ? -SCIPinfinity(o) : lhs
109-
rhs = rhs === nothing ? SCIPinfinity(o) : rhs
110-
80+
inf = SCIPinfinity(o)
11181
add_cut_sepa(
11282
o.inner.scip[],
11383
o.inner.vars,
11484
o.inner.sepas,
11585
cb_data.callback_data.sepa,
116-
varrefs,
117-
coefs,
118-
lhs,
119-
rhs,
86+
[VarRef(t.variable.value) for t in func.terms],
87+
[t.coefficient for t in func.terms],
88+
something(lhs, -inf),
89+
something(rhs, inf),
12090
)
12191
cb_data.callback_data.submit_called = true
92+
return
12293
end
123-
MOI.supports(::Optimizer, ::MOI.UserCut{CutCbData}) = true

src/MOI_wrapper/conshdlr.jl

Lines changed: 14 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
# Use of this source code is governed by an MIT-style license that can be found
44
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
55

6-
#
7-
# Adding constraint handlers, constraints, and cut selectors to SCIP.Optimizer.
8-
#
9-
106
"""
117
include_conshdlr(
128
o::Optimizer,
@@ -31,25 +27,10 @@ In particular, note the boolean `needs_constraints`:
3127
"""
3228
function include_conshdlr(
3329
o::Optimizer,
34-
ch::CH;
35-
name="",
36-
description="",
37-
enforce_priority=-15,
38-
check_priority=-7000000,
39-
eager_frequency=100,
40-
needs_constraints=true,
41-
) where {CH<:AbstractConstraintHandler}
42-
include_conshdlr(
43-
o.inner.scip[],
44-
o.inner.conshdlrs,
45-
ch;
46-
name=name,
47-
description=description,
48-
enforce_priority=enforce_priority,
49-
check_priority=check_priority,
50-
eager_frequency=eager_frequency,
51-
needs_constraints=needs_constraints,
52-
)
30+
ch::AbstractConstraintHandler;
31+
kwargs...,
32+
)
33+
return include_conshdlr(o.inner.scip[], o.inner.conshdlrs, ch; kwargs...)
5334
end
5435

5536
"""
@@ -78,65 +59,30 @@ All keyword arguments are passed to the `SCIPcreateCons` call.
7859
function add_constraint(
7960
o::Optimizer,
8061
ch::CH,
81-
c::C;
82-
initial=true,
83-
separate=true,
84-
enforce=true,
85-
check=true,
86-
propagate=true,
87-
_local=false,
88-
modifiable=false,
89-
dynamic=false,
90-
removable=false,
91-
stickingatnode=false,
92-
) where {CH<:AbstractConstraintHandler,C<:AbstractConstraint{CH}}
93-
return add_constraint(
94-
o.inner,
95-
ch,
96-
c;
97-
initial=initial,
98-
separate=separate,
99-
enforce=enforce,
100-
check=check,
101-
propagate=propagate,
102-
_local=_local,
103-
modifiable=modifiable,
104-
dynamic=dynamic,
105-
removable=removable,
106-
stickingatnode=stickingatnode,
107-
)
62+
c::AbstractConstraint{CH};
63+
kwargs...,
64+
) where {CH<:AbstractConstraintHandler}
65+
return add_constraint(o.inner, ch, c; kwargs...)
10866
end
10967

110-
function include_cutsel(
111-
o::Optimizer,
112-
cutsel::CS;
113-
name="",
114-
description="",
115-
priority=10000,
116-
) where {CS<:AbstractCutSelector}
68+
function include_cutsel(o::Optimizer, cutsel::AbstractCutSelector; kwargs...)
11769
return include_cutsel(
11870
o.inner.scip[],
11971
cutsel,
12072
o.inner.cutsel_storage;
121-
name=name,
122-
description=description,
123-
priority=priority,
73+
kwargs...,
12474
)
12575
end
12676

12777
function include_branchrule(
12878
o::Optimizer,
129-
branchrule::BR;
130-
name="",
131-
description="",
132-
priority=10000,
133-
) where {BR<:AbstractBranchingRule}
79+
branchrule::AbstractBranchingRule;
80+
kwargs...,
81+
)
13482
return include_branchrule(
13583
o.inner.scip[],
13684
branchrule,
13785
o.inner.branchrule_storage;
138-
name=name,
139-
description=description,
140-
priority=priority,
86+
kwargs...,
14187
)
14288
end

0 commit comments

Comments
 (0)