mirror of
https://github.com/ANL-CEEESA/MIPLearn.jl.git
synced 2025-12-06 08:28:52 -06:00
Make lazy constraints compatible with JuMP
This commit is contained in:
@@ -13,6 +13,7 @@ include("extractors.jl")
|
||||
include("io.jl")
|
||||
include("problems/setcover.jl")
|
||||
include("problems/stab.jl")
|
||||
include("problems/tsp.jl")
|
||||
include("solvers/jump.jl")
|
||||
include("solvers/learning.jl")
|
||||
|
||||
@@ -23,6 +24,7 @@ function __init__()
|
||||
__init_io__()
|
||||
__init_problems_setcover__()
|
||||
__init_problems_stab__()
|
||||
__init_problems_tsp__()
|
||||
__init_solvers_jump__()
|
||||
__init_solvers_learning__()
|
||||
end
|
||||
|
||||
71
src/problems/tsp.jl
Normal file
71
src/problems/tsp.jl
Normal file
@@ -0,0 +1,71 @@
|
||||
# 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
|
||||
|
||||
global TravelingSalesmanData = PyNULL()
|
||||
global TravelingSalesmanGenerator = PyNULL()
|
||||
|
||||
function __init_problems_tsp__()
|
||||
copy!(TravelingSalesmanData, pyimport("miplearn.problems.tsp").TravelingSalesmanData)
|
||||
copy!(TravelingSalesmanGenerator, pyimport("miplearn.problems.tsp").TravelingSalesmanGenerator)
|
||||
end
|
||||
|
||||
function build_tsp_model_jump(data::Any; optimizer=HiGHS.Optimizer)
|
||||
nx = pyimport("networkx")
|
||||
|
||||
if data isa String
|
||||
data = read_pkl_gz(data)
|
||||
end
|
||||
model = Model(optimizer)
|
||||
edges = [(i, j) for i in 1:data.n_cities for j in (i+1):data.n_cities]
|
||||
x = @variable(model, x[edges], Bin)
|
||||
@objective(model, Min, sum(
|
||||
x[(i, j)] * data.distances[i, j] for (i, j) in edges
|
||||
))
|
||||
|
||||
# Eq: Must choose two edges adjacent to each node
|
||||
@constraint(
|
||||
model,
|
||||
eq_degree[i in 1:data.n_cities],
|
||||
sum(x[(min(i, j), max(i, j))] for j in 1:data.n_cities if i != j) == 2
|
||||
)
|
||||
|
||||
function lazy_separate(cb_data)
|
||||
x_val = callback_value.(Ref(cb_data), x)
|
||||
violations = []
|
||||
selected_edges = [e for e in edges if x_val[e] > 0.5]
|
||||
graph = nx.Graph()
|
||||
graph.add_edges_from(selected_edges)
|
||||
for component in nx.connected_components(graph)
|
||||
if length(component) < data.n_cities
|
||||
cut_edges = [
|
||||
[e[1], e[2]]
|
||||
for e in edges
|
||||
if (e[1] ∈ component && e[2] ∉ component)
|
||||
||
|
||||
(e[1] ∉ component && e[2] ∈ component)
|
||||
]
|
||||
push!(violations, cut_edges)
|
||||
end
|
||||
end
|
||||
return violations
|
||||
end
|
||||
|
||||
function lazy_enforce(violations)
|
||||
@info "Adding $(length(violations)) subtour elimination eqs..."
|
||||
for violation in violations
|
||||
constr = @build_constraint(sum(x[(e[1], e[2])] for e in violation) >= 2)
|
||||
submit(model, constr)
|
||||
end
|
||||
end
|
||||
|
||||
return JumpModel(
|
||||
model,
|
||||
lazy_enforce=lazy_enforce,
|
||||
lazy_separate=lazy_separate,
|
||||
)
|
||||
end
|
||||
|
||||
export TravelingSalesmanData, TravelingSalesmanGenerator, build_tsp_model_jump
|
||||
@@ -12,9 +12,12 @@ Base.@kwdef mutable struct _JumpModelExtData
|
||||
aot_cuts = nothing
|
||||
cb_data = nothing
|
||||
cuts = []
|
||||
lazy = []
|
||||
where::Symbol = :WHERE_DEFAULT
|
||||
cuts_enforce::Union{Function,Nothing} = nothing
|
||||
cuts_separate::Union{Function,Nothing} = nothing
|
||||
lazy_enforce::Union{Function,Nothing} = nothing
|
||||
lazy_separate::Union{Function,Nothing} = nothing
|
||||
end
|
||||
|
||||
function JuMP.copy_extension_data(
|
||||
@@ -58,8 +61,10 @@ function submit(model::JuMP.Model, constr)
|
||||
ext = model.ext[:miplearn]
|
||||
if ext.where == :WHERE_CUTS
|
||||
MOI.submit(model, MOI.UserCut(ext.cb_data), constr)
|
||||
elseif ext.where == :WHERE_LAZY
|
||||
MOI.submit(model, MOI.LazyConstraint(ext.cb_data), constr)
|
||||
else
|
||||
error("not implemented")
|
||||
add_constraint(model, constr)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -281,9 +286,10 @@ function _extract_after_mip(model::JuMP.Model, h5)
|
||||
slacks = abs.(lhs * x - rhs)
|
||||
h5.put_array("mip_constr_slacks", slacks)
|
||||
|
||||
# Cuts
|
||||
# Cuts and lazy constraints
|
||||
ext = model.ext[:miplearn]
|
||||
h5.put_scalar("mip_cuts", JSON.json(ext.cuts))
|
||||
h5.put_scalar("mip_lazy", JSON.json(ext.lazy))
|
||||
end
|
||||
|
||||
function _fix_variables(model::JuMP.Model, var_names, var_values, stats)
|
||||
@@ -318,6 +324,23 @@ function _optimize(model::JuMP.Model)
|
||||
set_attribute(model, MOI.UserCutCallback(), cut_callback)
|
||||
end
|
||||
|
||||
# Set up lazy constraint callbacks
|
||||
ext.lazy = []
|
||||
function lazy_callback(cb_data)
|
||||
ext.cb_data = cb_data
|
||||
ext.where = :WHERE_LAZY
|
||||
violations = ext.lazy_separate(cb_data)
|
||||
for v in violations
|
||||
push!(ext.lazy, v)
|
||||
end
|
||||
if !isempty(violations)
|
||||
ext.lazy_enforce(violations)
|
||||
end
|
||||
end
|
||||
if ext.lazy_separate !== nothing
|
||||
set_attribute(model, MOI.LazyConstraintCallback(), lazy_callback)
|
||||
end
|
||||
|
||||
# Optimize
|
||||
ext.where = :WHERE_DEFAULT
|
||||
optimize!(model)
|
||||
@@ -363,12 +386,15 @@ function __init_solvers_jump__()
|
||||
inner;
|
||||
cuts_enforce::Union{Function,Nothing}=nothing,
|
||||
cuts_separate::Union{Function,Nothing}=nothing,
|
||||
lazy_enforce::Union{Function,Nothing}=nothing,
|
||||
lazy_separate::Union{Function,Nothing}=nothing,
|
||||
)
|
||||
AbstractModel.__init__(self)
|
||||
self.inner = inner
|
||||
self.inner.ext[:miplearn] = _JumpModelExtData(
|
||||
cuts_enforce=cuts_enforce,
|
||||
cuts_separate=cuts_separate,
|
||||
lazy_enforce=lazy_enforce,
|
||||
lazy_separate=lazy_separate,
|
||||
)
|
||||
end
|
||||
|
||||
@@ -409,6 +435,10 @@ function __init_solvers_jump__()
|
||||
function set_cuts(self, cuts)
|
||||
self.inner.ext[:miplearn].aot_cuts = cuts
|
||||
end
|
||||
|
||||
function lazy_enforce(self, model, violations)
|
||||
self.inner.ext[:miplearn].lazy_enforce(violations)
|
||||
end
|
||||
end
|
||||
copy!(JumpModel, Class)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user