Make cuts component compatible with JuMP

This commit is contained in:
2024-02-01 12:01:55 -06:00
parent 1ea432fb57
commit d69c4bbfa7
16 changed files with 252 additions and 38 deletions

View File

@@ -12,6 +12,7 @@ include("components.jl")
include("extractors.jl")
include("io.jl")
include("problems/setcover.jl")
include("problems/stab.jl")
include("solvers/jump.jl")
include("solvers/learning.jl")
@@ -21,6 +22,7 @@ function __init__()
__init_extractors__()
__init_io__()
__init_problems_setcover__()
__init_problems_stab__()
__init_solvers_jump__()
__init_solvers_learning__()
end

View File

@@ -2,19 +2,21 @@
# Copyright (C) 2020-2023, UChicago Argonne, LLC. All rights reserved.
# Released under the modified BSD license. See COPYING.md for more details.
global MinProbabilityClassifier = PyNULL()
global SingleClassFix = PyNULL()
global PrimalComponentAction = PyNULL()
global SetWarmStart = PyNULL()
global FixVariables = PyNULL()
global EnforceProximity = PyNULL()
global ExpertPrimalComponent = PyNULL()
global FixVariables = PyNULL()
global IndependentVarsPrimalComponent = PyNULL()
global JointVarsPrimalComponent = PyNULL()
global SolutionConstructor = PyNULL()
global MemorizingCutsComponent = PyNULL()
global MemorizingLazyComponent = PyNULL()
global MemorizingPrimalComponent = PyNULL()
global SelectTopSolutions = PyNULL()
global MergeTopSolutions = PyNULL()
global MinProbabilityClassifier = PyNULL()
global PrimalComponentAction = PyNULL()
global SelectTopSolutions = PyNULL()
global SetWarmStart = PyNULL()
global SingleClassFix = PyNULL()
global SolutionConstructor = PyNULL()
function __init_components__()
copy!(
@@ -51,6 +53,8 @@ function __init_components__()
)
copy!(SelectTopSolutions, pyimport("miplearn.components.primal.mem").SelectTopSolutions)
copy!(MergeTopSolutions, pyimport("miplearn.components.primal.mem").MergeTopSolutions)
copy!(MemorizingCutsComponent, pyimport("miplearn.components.cuts.mem").MemorizingCutsComponent)
copy!(MemorizingLazyComponent, pyimport("miplearn.components.lazy.mem").MemorizingLazyComponent)
end
export MinProbabilityClassifier,
@@ -65,4 +69,6 @@ export MinProbabilityClassifier,
SolutionConstructor,
MemorizingPrimalComponent,
SelectTopSolutions,
MergeTopSolutions
MergeTopSolutions,
MemorizingCutsComponent,
MemorizingLazyComponent

View File

@@ -13,12 +13,11 @@ function __init_problems_setcover__()
copy!(SetCoverGenerator, pyimport("miplearn.problems.setcover").SetCoverGenerator)
end
function build_setcover_model(data::Any; optimizer = HiGHS.Optimizer)
function build_setcover_model_jump(data::Any; optimizer = HiGHS.Optimizer)
if data isa String
data = read_pkl_gz(data)
end
model = Model(optimizer)
set_silent(model)
n_elements, n_sets = size(data.incidence_matrix)
E = 0:n_elements-1
S = 0:n_sets-1
@@ -32,4 +31,4 @@ function build_setcover_model(data::Any; optimizer = HiGHS.Optimizer)
return JumpModel(model)
end
export SetCoverData, SetCoverGenerator, build_setcover_model
export SetCoverData, SetCoverGenerator, build_setcover_model_jump

60
src/problems/stab.jl Normal file
View File

@@ -0,0 +1,60 @@
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
# Copyright (C) 2020-2024, UChicago Argonne, LLC. All rights reserved.
# Released under the modified BSD license. See COPYING.md for more details.
using JuMP
using HiGHS
global MaxWeightStableSetData = PyNULL()
global MaxWeightStableSetGenerator = PyNULL()
function __init_problems_stab__()
copy!(MaxWeightStableSetData, pyimport("miplearn.problems.stab").MaxWeightStableSetData)
copy!(MaxWeightStableSetGenerator, pyimport("miplearn.problems.stab").MaxWeightStableSetGenerator)
end
function build_stab_model_jump(data::Any; optimizer=HiGHS.Optimizer)
nx = pyimport("networkx")
if data isa String
data = read_pkl_gz(data)
end
model = Model(optimizer)
# Variables and objective function
nodes = data.graph.nodes
x = @variable(model, x[nodes], Bin)
@objective(model, Min, sum(-data.weights[i+1] * x[i] for i in nodes))
# Edge inequalities
for (i1, i2) in data.graph.edges
@constraint(model, x[i1] + x[i2] <= 1, base_name = "eq_edge[$i1,$i2]")
end
function cuts_separate(cb_data)
x_val = callback_value.(Ref(cb_data), x)
violations = []
for clique in nx.find_cliques(data.graph)
if sum(x_val[i] for i in clique) > 1.0001
push!(violations, sort(clique))
end
end
return violations
end
function cuts_enforce(violations)
@info "Adding $(length(violations)) clique cuts..."
for clique in violations
constr = @build_constraint(sum(x[i] for i in clique) <= 1)
submit(model, constr)
end
end
return JumpModel(
model,
cuts_separate=cuts_separate,
cuts_enforce=cuts_enforce,
)
end
export MaxWeightStableSetData, MaxWeightStableSetGenerator, build_stab_model_jump

View File

@@ -4,9 +4,19 @@
using JuMP
using HiGHS
using JSON
global JumpModel = PyNULL()
Base.@kwdef mutable struct _JumpModelExtData
aot_cuts = nothing
cb_data = nothing
cuts = []
where::Symbol = :WHERE_DEFAULT
cuts_enforce::Union{Function,Nothing} = nothing
cuts_separate::Union{Function,Nothing} = nothing
end
# -----------------------------------------------------------------------------
function _add_constrs(
@@ -35,6 +45,15 @@ function _add_constrs(
end
end
function submit(model::JuMP.Model, constr)
ext = model.ext[:miplearn]
if ext.where == :WHERE_CUTS
MOI.submit(model, MOI.UserCut(ext.cb_data), constr)
else
error("not implemented")
end
end
function _extract_after_load(model::JuMP.Model, h5)
if JuMP.objective_sense(model) == MOI.MIN_SENSE
h5.put_scalar("static_sense", "min")
@@ -109,6 +128,9 @@ function _extract_after_load_constrs(model::JuMP.Model, h5)
end
end
end
if isempty(names)
error("no model constraints found; note that MIPLearn ignores unnamed constraints")
end
lhs = sparse(lhs_rows, lhs_cols, lhs_values, length(rhs), JuMP.num_variables(model))
h5.put_sparse("static_constr_lhs", lhs)
h5.put_array("static_constr_rhs", rhs)
@@ -249,17 +271,50 @@ function _extract_after_mip(model::JuMP.Model, h5)
rhs = h5.get_array("static_constr_rhs")
slacks = abs.(lhs * x - rhs)
h5.put_array("mip_constr_slacks", slacks)
# Cuts
ext = model.ext[:miplearn]
h5.put_scalar("mip_cuts", JSON.json(ext.cuts))
end
function _fix_variables(model::JuMP.Model, var_names, var_values, stats)
vars = [variable_by_name(model, v) for v in var_names]
for (i, var) in enumerate(vars)
fix(var, var_values[i], force = true)
fix(var, var_values[i], force=true)
end
end
function _optimize(model::JuMP.Model)
# Set up cut callbacks
ext = model.ext[:miplearn]
ext.cuts = []
function cut_callback(cb_data)
ext.cb_data = cb_data
ext.where = :WHERE_CUTS
if ext.aot_cuts !== nothing
@info "Enforcing $(length(ext.aot_cuts)) cuts ahead-of-time..."
violations = ext.aot_cuts
ext.aot_cuts = nothing
else
violations = ext.cuts_separate(cb_data)
for v in violations
push!(ext.cuts, v)
end
end
if !isempty(violations)
ext.cuts_enforce(violations)
end
end
if ext.cuts_separate !== nothing
set_attribute(model, MOI.UserCutCallback(), cut_callback)
end
# Optimize
ext.where = :WHERE_DEFAULT
optimize!(model)
# Cleanup
ext.cb_data = nothing
flush(stdout)
Libc.flush_cstdio()
end
@@ -291,10 +346,21 @@ end
# -----------------------------------------------------------------------------
function __init_solvers_jump__()
@pydef mutable struct Class
AbstractModel = pyimport("miplearn.solvers.abstract").AbstractModel
@pydef mutable struct Class <: AbstractModel
function __init__(self, inner)
function __init__(
self,
inner;
cuts_enforce::Union{Function,Nothing}=nothing,
cuts_separate::Union{Function,Nothing}=nothing,
)
AbstractModel.__init__(self)
self.inner = inner
self.inner.ext[:miplearn] = _JumpModelExtData(
cuts_enforce=cuts_enforce,
cuts_separate=cuts_separate,
)
end
add_constrs(
@@ -303,7 +369,7 @@ function __init_solvers_jump__()
constrs_lhs,
constrs_sense,
constrs_rhs,
stats = nothing,
stats=nothing,
) = _add_constrs(
self.inner,
from_str_array(var_names),
@@ -319,17 +385,21 @@ function __init_solvers_jump__()
extract_after_mip(self, h5) = _extract_after_mip(self.inner, h5)
fix_variables(self, var_names, var_values, stats = nothing) =
fix_variables(self, var_names, var_values, stats=nothing) =
_fix_variables(self.inner, from_str_array(var_names), var_values, stats)
optimize(self) = _optimize(self.inner)
relax(self) = Class(_relax(self.inner))
set_warm_starts(self, var_names, var_values, stats = nothing) =
set_warm_starts(self, var_names, var_values, stats=nothing) =
_set_warm_starts(self.inner, from_str_array(var_names), var_values, stats)
write(self, filename) = _write(self.inner, filename)
function set_cuts(self, cuts)
self.inner.ext[:miplearn].aot_cuts = cuts
end
end
copy!(JumpModel, Class)
end