Skip to content

Commit 790629c

Browse files
committed
WIP: add more interface methods and call them from the C callbacks
1 parent 55e1f34 commit 790629c

File tree

1 file changed

+105
-25
lines changed

1 file changed

+105
-25
lines changed

src/conshdlr.jl

+105-25
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,73 @@ abstract type AbstractConstraintHandler end
2525
# (also: CONSDATA)
2626
abstract type AbstractConstraint{Handler} end
2727

28-
# Example implementation
29-
mutable struct ExampleConstraintHandler <: AbstractConstraintHandler
30-
end
28+
# Parameters:
29+
30+
abstract type AbstractConstraintHandlerParameter end
31+
struct Name <: AbstractConstraintHandlerParameter end
32+
struct Description <: AbstractConstraintHandlerParameter end
33+
struct EnforcePriority <: AbstractConstraintHandlerParameter end
34+
struct CheckPriority <: AbstractConstraintHandlerParameter end
35+
36+
# Interface methods
37+
38+
# Parameters have default values
39+
get(ch::AbstractConstraintHandler, ::Name) = ""
40+
get(ch::AbstractConstraintHandler, ::Description) = ""
41+
# enfoprio: integral has 0, nonlinear: -30 or less
42+
get(ch::AbstractConstraintHandler, ::EnforcePriority) = -15
43+
# checkprio: integral has 0, SCIP's plugins have -6000000 or more
44+
get(ch::AbstractConstraintHandler, ::CheckPriority) = -7000000
45+
46+
# For semantics of callback methods, see SCIP documentation at:
47+
# https://scip.zib.de/doc-6.0.1/html/CONS.php#CONS_FUNDAMENTALCALLBACKS
48+
49+
function check end
50+
51+
# possible return values:
52+
# SCIP_CUTOFF: stating that the current subproblem is infeasible
53+
# SCIP_CONSADDED: adding constraint that resolves the infeasibility
54+
# SCIP_REDUCEDDOM: reducing the domain of a variable
55+
# SCIP_SEPARATED: adding a cutting plane
56+
# SCIP_BRANCHED: performing a branching
57+
function enforce_lp_sol end
58+
59+
# possible return values:
60+
# SCIP_CUTOFF: stating that the current subproblem is infeasible
61+
# SCIP_CONSADDED: adding constraint that resolves the infeasibility
62+
# SCIP_REDUCEDDOM: reducing the domain of a variable
63+
# SCIP_BRANCHED: performing a branching
64+
# SCIP_SOLVELP: force solving of LP
65+
function enforce_pseudo_sol end
66+
67+
function lock end
3168

32-
### ^ above: what it will look like to Julia users
33-
### = here: sketches for the translation
69+
# Methods
3470

35-
### v below: what it will look like to SCIP
71+
# CONSCHECK
72+
# (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss,
73+
# SCIP_SOL* sol, SCIP_Bool checkintegrality, SCIP_Bool checklprows,
74+
# SCIP_Bool printreason, SCIP_Bool completely, SCIP_RESULT* result
75+
function _conscheck(scip::Ptr{SCIP_}, conshdlr::{SCIP_CONSHDLR},
76+
conss::Ptr{Ptr{SCIP_CONS}}, nconss::Cint,
77+
sol::Ptr{SCIP_SOL}, checkintegrality::SCIP_Bool,
78+
checklprows::SCIP_Bool, printreason::SCIP_Bool,
79+
completely::SCIP_Bool, result::Ptr{SCIP_RESULT})
80+
# get Julia object out of constraint handler data
81+
conshdlrdata = SCIPconshdlrGetData(conshdlr)
82+
constraint_handler = unsafe_objref_from_pointer(conshdlrdata)
3683

37-
# Methods
84+
# get Julia array from C pointer
85+
constraint_pointers = unsafe_wrap(Array{Ptr{SCIP_CONS}}, conss, nconss)
86+
87+
# TODO: fetch solution values
88+
# TODO: document meaning of all parameters
89+
90+
# call user method via dispatch
91+
result[] = check(constraint_handler, constraints)
92+
93+
return SCIP_OKAY
94+
end
3895

3996
# CONSENFOLP
4097
# (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss,
@@ -44,6 +101,19 @@ function _consenfolp(scip::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR},
44101
conss::Ptr{Ptr{SCIP_CONS}}, nconss::Cint,
45102
nusefulconss::Cint, solinfeasible::SCIP_Bool,
46103
result::Ptr{SCIP_RESULT})
104+
# get Julia object out of constraint handler data
105+
conshdlrdata = SCIPconshdlrGetData(conshdlr)
106+
constraint_handler = unsafe_objref_from_pointer(conshdlrdata)
107+
108+
# get Julia array from C pointer
109+
constraint_pointers = unsafe_wrap(Array{Ptr{SCIP_CONS}}, conss, nconss)
110+
111+
# TODO: fetch solution values?
112+
# TODO: document meaning of all parameters
113+
114+
# call user method via dispatch
115+
result[] = enforce_lp_sol(constraint_handler, constraints, solinfeasible)
116+
47117
return SCIP_OKAY
48118
end
49119

@@ -55,18 +125,20 @@ function _consenfops(scip::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR},
55125
conss::Ptr{Ptr{SCIP_CONS}}, nconss::Cint,
56126
nusefulconss::Cint, solinfeasible::SCIP_Bool,
57127
objinfeasible::SCIP_Bool, result::Ptr{SCIP_RESULT})
58-
return SCIP_OKAY
59-
end
128+
# get Julia object out of constraint handler data
129+
conshdlrdata = SCIPconshdlrGetData(conshdlr)
130+
constraint_handler = unsafe_objref_from_pointer(conshdlrdata)
131+
132+
# get Julia array from C pointer
133+
constraint_pointers = unsafe_wrap(Array{Ptr{SCIP_CONS}}, conss, nconss)
134+
135+
# TODO: fetch solution values?
136+
# TODO: document meaning of all parameters
137+
138+
# call user method via dispatch
139+
result[] = enforce_pseudo_sol(constraint_handler, constraints,
140+
solinfeasible, objinfeasible)
60141

61-
# CONSCHECK
62-
# (SCIP* scip, SCIP_CONSHDLR* conshdlr, SCIP_CONS** conss, int nconss,
63-
# SCIP_SOL* sol, SCIP_Bool checkintegrality, SCIP_Bool checklprows,
64-
# SCIP_Bool printreason, SCIP_Bool completely, SCIP_RESULT* result
65-
function _conscheck(scip::Ptr{SCIP_}, conshdlr::{SCIP_CONSHDLR},
66-
conss::Ptr{Ptr{SCIP_CONS}}, nconss::Cint,
67-
sol::Ptr{SCIP_SOL}, checkintegrality::SCIP_Bool,
68-
checklprows::SCIP_Bool, printreason::SCIP_Bool,
69-
completely::SCIP_Bool, result::Ptr{SCIP_RESULT})
70142
return SCIP_OKAY
71143
end
72144

@@ -76,6 +148,15 @@ end
76148
function _conslock(scip::Ptr{SCIP_}, conshdlr::Ptr{SCIP_CONSHDLR},
77149
cons::Ptr{SCIP_CONS}, locktype::Ptr{SCIP_LOCKTYPE},
78150
nlockspos::Cint, nlocksneg::Cint)
151+
# get Julia object out of constraint handler data
152+
conshdlrdata = SCIPconshdlrGetData(conshdlr)
153+
constraint_handler = unsafe_objref_from_pointer(conshdlrdata)
154+
155+
# TODO: document meaning of all parameters
156+
157+
# call user method via dispatch
158+
result[] = lock(constraint_handler, cons, locktype, nlockspos, nlocksneg)
159+
79160
return SCIP_OKAY
80161
end
81162

@@ -85,9 +166,8 @@ end
85166
# - set properties (NAME, DESC, ENFOPRIORITY, CHECKPRIORITY).
86167
# - initialize conshdlrdata (== Julia object)
87168
# - could be called from the constructor to constraint handler?
88-
function include_conshdlr(mscip::ManagedSCIP, constraint_handler::CH) where CH <: AbstractConstraintHandler
169+
function include_conshdlr(mscip::ManagedSCIP, ch::CH) where CH <: AbstractConstraintHandler
89170
# get C function pointers from Julia functions
90-
# TODO: need specific methods? ask users to specify as function arguments?
91171
_enfolp = @cfunction(_consenfolp, SCIP_RETCODE, (Ptr{SCIP_}, Ptr{SCIP_CONSHDLR}, Ptr{Ptr{SCIP_CONS}}, Cint, Cint, SCIP_Bool, SCIP_Bool, Ptr{SCIP_RESULT}))
92172
_enfops = @cfunction(_consenfops, SCIP_RETCODE, (Ptr{SCIP_}, Ptr{SCIP_CONSHDLR}, Ptr{Ptr{SCIP_CONS}}, Cint, Cint, SCIP_Bool, SCIP_Bool, SCIP_Bool, Ptr{SCIP_RESULT}))
93173
_check = @cfunction(_conscheck, SCIP_RETCODE, (Ptr{SCIP_}, Ptr{SCIP_CONSHDLR}, Ptr{Ptr{SCIP_CONS}}, Cint, Ptr{SCIP_SOL}, SCIP_Bool, SCIP_Bool, SCIP_Bool, SCIP_Bool, Ptr{SCIP_RESULT}))
@@ -102,16 +182,16 @@ function include_conshdlr(mscip::ManagedSCIP, constraint_handler::CH) where CH <
102182
NEEDSCONS = TRUE
103183

104184
# Get other values from methods:
105-
name = get(constraint_handler, Name())
106-
desc = get(constraint_handler, Description())
107-
enfoprio = get(constraint_handler, EnforcePriority())
108-
checkprio = get(constraint_handler, CheckPriority())
185+
name = get(ch, Name())
186+
desc = get(ch, Description())
187+
enfoprio = get(ch, EnforcePriority())
188+
checkprio = get(ch, CheckPriority())
109189

110190
# Register constraint handler with SCIP instance.
111191
@SC SCIPincludeConshdlrBasic(mscip, conshdlr__, name, desc, enfopriority,
112192
chckpriority, EAGERFREQ, NEEDSCONS,
113193
_enfolp, _enfops, _check, _lock,
114-
constraint_handler)
194+
ch)
115195

116196
# Sanity checks
117197
@assert conshdlr__[] != C_NULL

0 commit comments

Comments
 (0)