mirror of
https://github.com/ANL-CEEESA/MIPLearn.jl.git
synced 2025-12-06 00:18:51 -06:00
Reformat source code
This commit is contained in:
@@ -9,7 +9,13 @@ using SparseArrays
|
|||||||
using Statistics
|
using Statistics
|
||||||
using TimerOutputs
|
using TimerOutputs
|
||||||
|
|
||||||
function collect_gmi(mps_filename; optimizer, max_rounds=10, max_cuts_per_round=100, atol=1e-4)
|
function collect_gmi(
|
||||||
|
mps_filename;
|
||||||
|
optimizer,
|
||||||
|
max_rounds = 10,
|
||||||
|
max_cuts_per_round = 100,
|
||||||
|
atol = 1e-4,
|
||||||
|
)
|
||||||
@info mps_filename
|
@info mps_filename
|
||||||
reset_timer!()
|
reset_timer!()
|
||||||
|
|
||||||
@@ -98,12 +104,12 @@ function collect_gmi(mps_filename; optimizer, max_rounds=10, max_cuts_per_round=
|
|||||||
sol_frac = get_x(model_s)
|
sol_frac = get_x(model_s)
|
||||||
stats_time_select += @elapsed begin
|
stats_time_select += @elapsed begin
|
||||||
selected_rows =
|
selected_rows =
|
||||||
select_gmi_rows(data_s, basis, sol_frac, max_rows=max_cuts_per_round)
|
select_gmi_rows(data_s, basis, sol_frac, max_rows = max_cuts_per_round)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Compute selected tableau rows
|
# Compute selected tableau rows
|
||||||
stats_time_tableau += @elapsed begin
|
stats_time_tableau += @elapsed begin
|
||||||
tableau = compute_tableau(data_s, basis, sol_frac, rows=selected_rows)
|
tableau = compute_tableau(data_s, basis, sol_frac, rows = selected_rows)
|
||||||
|
|
||||||
# Assert tableau rows have been computed correctly
|
# Assert tableau rows have been computed correctly
|
||||||
assert_eq(tableau.lhs * sol_frac, tableau.rhs)
|
assert_eq(tableau.lhs * sol_frac, tableau.rhs)
|
||||||
@@ -180,10 +186,9 @@ function collect_gmi(mps_filename; optimizer, max_rounds=10, max_cuts_per_round=
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
function select_gmi_rows(data, basis, x; max_rows=10, atol=1e-4)
|
function select_gmi_rows(data, basis, x; max_rows = 10, atol = 1e-4)
|
||||||
candidate_rows = [
|
candidate_rows = [
|
||||||
r for
|
r for r = 1:length(basis.var_basic) if (
|
||||||
r in 1:length(basis.var_basic) if (
|
|
||||||
(data.var_types[basis.var_basic[r]] != 'C') &&
|
(data.var_types[basis.var_basic[r]] != 'C') &&
|
||||||
(frac(x[basis.var_basic[r]]) > atol) &&
|
(frac(x[basis.var_basic[r]]) > atol) &&
|
||||||
(frac2(x[basis.var_basic[r]]) > atol)
|
(frac2(x[basis.var_basic[r]]) > atol)
|
||||||
@@ -204,7 +209,7 @@ function compute_gmi(data::ProblemData, tableau::Tableau)::ConstraintSet
|
|||||||
lhs_J = Int[]
|
lhs_J = Int[]
|
||||||
lhs_V = Float64[]
|
lhs_V = Float64[]
|
||||||
@timeit "Compute coefficients" begin
|
@timeit "Compute coefficients" begin
|
||||||
for k in 1:nnz(tableau.lhs)
|
for k = 1:nnz(tableau.lhs)
|
||||||
i::Int = tableau_I[k]
|
i::Int = tableau_I[k]
|
||||||
j::Int = tableau_J[k]
|
j::Int = tableau_J[k]
|
||||||
v::Float64 = 0.0
|
v::Float64 = 0.0
|
||||||
@@ -235,4 +240,5 @@ function compute_gmi(data::ProblemData, tableau::Tableau)::ConstraintSet
|
|||||||
return ConstraintSet(; lhs, ub, lb)
|
return ConstraintSet(; lhs, ub, lb)
|
||||||
end
|
end
|
||||||
|
|
||||||
export compute_gmi, frac, select_gmi_rows, assert_cuts_off, assert_does_not_cut_off, collect_gmi
|
export compute_gmi,
|
||||||
|
frac, select_gmi_rows, assert_cuts_off, assert_does_not_cut_off, collect_gmi
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
@inline frac2(x::Float64) = ceil(x) - x
|
@inline frac2(x::Float64) = ceil(x) - x
|
||||||
|
|
||||||
function assert_leq(a, b; atol=0.01)
|
function assert_leq(a, b; atol = 0.01)
|
||||||
if !all(a .<= b .+ atol)
|
if !all(a .<= b .+ atol)
|
||||||
delta = a .- b
|
delta = a .- b
|
||||||
for i in eachindex(delta)
|
for i in eachindex(delta)
|
||||||
@@ -14,7 +14,7 @@ function assert_leq(a, b; atol=0.01)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function assert_eq(a, b; atol=1e-4)
|
function assert_eq(a, b; atol = 1e-4)
|
||||||
if !all(abs.(a .- b) .<= atol)
|
if !all(abs.(a .- b) .<= atol)
|
||||||
delta = abs.(a .- b)
|
delta = abs.(a .- b)
|
||||||
for i in eachindex(delta)
|
for i in eachindex(delta)
|
||||||
@@ -26,7 +26,7 @@ function assert_eq(a, b; atol=1e-4)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function assert_cuts_off(cuts::ConstraintSet, x::Vector{Float64}, tol=1e-6)
|
function assert_cuts_off(cuts::ConstraintSet, x::Vector{Float64}, tol = 1e-6)
|
||||||
for i = 1:length(cuts.lb)
|
for i = 1:length(cuts.lb)
|
||||||
val = cuts.lhs[i, :]' * x
|
val = cuts.lhs[i, :]' * x
|
||||||
if (val <= cuts.ub[i] - tol) && (val >= cuts.lb[i] + tol)
|
if (val <= cuts.ub[i] - tol) && (val >= cuts.lb[i] + tol)
|
||||||
@@ -35,7 +35,7 @@ function assert_cuts_off(cuts::ConstraintSet, x::Vector{Float64}, tol=1e-6)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function assert_does_not_cut_off(cuts::ConstraintSet, x::Vector{Float64}; tol=1e-6)
|
function assert_does_not_cut_off(cuts::ConstraintSet, x::Vector{Float64}; tol = 1e-6)
|
||||||
for i = 1:length(cuts.lb)
|
for i = 1:length(cuts.lb)
|
||||||
val = cuts.lhs[i, :]' * x
|
val = cuts.lhs[i, :]' * x
|
||||||
ub = cuts.ub[i]
|
ub = cuts.ub[i]
|
||||||
|
|||||||
@@ -53,8 +53,14 @@ function __init_components__()
|
|||||||
)
|
)
|
||||||
copy!(SelectTopSolutions, pyimport("miplearn.components.primal.mem").SelectTopSolutions)
|
copy!(SelectTopSolutions, pyimport("miplearn.components.primal.mem").SelectTopSolutions)
|
||||||
copy!(MergeTopSolutions, pyimport("miplearn.components.primal.mem").MergeTopSolutions)
|
copy!(MergeTopSolutions, pyimport("miplearn.components.primal.mem").MergeTopSolutions)
|
||||||
copy!(MemorizingCutsComponent, pyimport("miplearn.components.cuts.mem").MemorizingCutsComponent)
|
copy!(
|
||||||
copy!(MemorizingLazyComponent, pyimport("miplearn.components.lazy.mem").MemorizingLazyComponent)
|
MemorizingCutsComponent,
|
||||||
|
pyimport("miplearn.components.cuts.mem").MemorizingCutsComponent,
|
||||||
|
)
|
||||||
|
copy!(
|
||||||
|
MemorizingLazyComponent,
|
||||||
|
pyimport("miplearn.components.lazy.mem").MemorizingLazyComponent,
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
export MinProbabilityClassifier,
|
export MinProbabilityClassifier,
|
||||||
|
|||||||
@@ -39,14 +39,14 @@ end
|
|||||||
function PyObject(m::SparseMatrixCSC)
|
function PyObject(m::SparseMatrixCSC)
|
||||||
pyimport("scipy.sparse").csc_matrix(
|
pyimport("scipy.sparse").csc_matrix(
|
||||||
(m.nzval, m.rowval .- 1, m.colptr .- 1),
|
(m.nzval, m.rowval .- 1, m.colptr .- 1),
|
||||||
shape=size(m),
|
shape = size(m),
|
||||||
).tocoo()
|
).tocoo()
|
||||||
end
|
end
|
||||||
|
|
||||||
function write_jld2(
|
function write_jld2(
|
||||||
objs::Vector,
|
objs::Vector,
|
||||||
dirname::AbstractString;
|
dirname::AbstractString;
|
||||||
prefix::AbstractString=""
|
prefix::AbstractString = "",
|
||||||
)::Vector{String}
|
)::Vector{String}
|
||||||
mkpath(dirname)
|
mkpath(dirname)
|
||||||
filenames = [@sprintf("%s/%s%05d.jld2", dirname, prefix, i) for i = 1:length(objs)]
|
filenames = [@sprintf("%s/%s%05d.jld2", dirname, prefix, i) for i = 1:length(objs)]
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ function __init_problems_setcover__()
|
|||||||
copy!(SetCoverGenerator, pyimport("miplearn.problems.setcover").SetCoverGenerator)
|
copy!(SetCoverGenerator, pyimport("miplearn.problems.setcover").SetCoverGenerator)
|
||||||
end
|
end
|
||||||
|
|
||||||
function build_setcover_model_jump(data::Any; optimizer=HiGHS.Optimizer)
|
function build_setcover_model_jump(data::Any; optimizer = HiGHS.Optimizer)
|
||||||
if data isa String
|
if data isa String
|
||||||
data = read_pkl_gz(data)
|
data = read_pkl_gz(data)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -10,10 +10,13 @@ global MaxWeightStableSetGenerator = PyNULL()
|
|||||||
|
|
||||||
function __init_problems_stab__()
|
function __init_problems_stab__()
|
||||||
copy!(MaxWeightStableSetData, pyimport("miplearn.problems.stab").MaxWeightStableSetData)
|
copy!(MaxWeightStableSetData, pyimport("miplearn.problems.stab").MaxWeightStableSetData)
|
||||||
copy!(MaxWeightStableSetGenerator, pyimport("miplearn.problems.stab").MaxWeightStableSetGenerator)
|
copy!(
|
||||||
|
MaxWeightStableSetGenerator,
|
||||||
|
pyimport("miplearn.problems.stab").MaxWeightStableSetGenerator,
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
function build_stab_model_jump(data::Any; optimizer=HiGHS.Optimizer)
|
function build_stab_model_jump(data::Any; optimizer = HiGHS.Optimizer)
|
||||||
nx = pyimport("networkx")
|
nx = pyimport("networkx")
|
||||||
|
|
||||||
if data isa String
|
if data isa String
|
||||||
@@ -50,11 +53,7 @@ function build_stab_model_jump(data::Any; optimizer=HiGHS.Optimizer)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return JumpModel(
|
return JumpModel(model, cuts_separate = cuts_separate, cuts_enforce = cuts_enforce)
|
||||||
model,
|
|
||||||
cuts_separate=cuts_separate,
|
|
||||||
cuts_enforce=cuts_enforce,
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
export MaxWeightStableSetData, MaxWeightStableSetGenerator, build_stab_model_jump
|
export MaxWeightStableSetData, MaxWeightStableSetGenerator, build_stab_model_jump
|
||||||
|
|||||||
@@ -9,7 +9,10 @@ global TravelingSalesmanGenerator = PyNULL()
|
|||||||
|
|
||||||
function __init_problems_tsp__()
|
function __init_problems_tsp__()
|
||||||
copy!(TravelingSalesmanData, pyimport("miplearn.problems.tsp").TravelingSalesmanData)
|
copy!(TravelingSalesmanData, pyimport("miplearn.problems.tsp").TravelingSalesmanData)
|
||||||
copy!(TravelingSalesmanGenerator, pyimport("miplearn.problems.tsp").TravelingSalesmanGenerator)
|
copy!(
|
||||||
|
TravelingSalesmanGenerator,
|
||||||
|
pyimport("miplearn.problems.tsp").TravelingSalesmanGenerator,
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
function build_tsp_model_jump(data::Any; optimizer)
|
function build_tsp_model_jump(data::Any; optimizer)
|
||||||
@@ -19,17 +22,15 @@ function build_tsp_model_jump(data::Any; optimizer)
|
|||||||
data = read_pkl_gz(data)
|
data = read_pkl_gz(data)
|
||||||
end
|
end
|
||||||
model = Model(optimizer)
|
model = Model(optimizer)
|
||||||
edges = [(i, j) for i in 1:data.n_cities for j in (i+1):data.n_cities]
|
edges = [(i, j) for i = 1:data.n_cities for j = (i+1):data.n_cities]
|
||||||
x = @variable(model, x[edges], Bin)
|
x = @variable(model, x[edges], Bin)
|
||||||
@objective(model, Min, sum(
|
@objective(model, Min, sum(x[(i, j)] * data.distances[i, j] for (i, j) in edges))
|
||||||
x[(i, j)] * data.distances[i, j] for (i, j) in edges
|
|
||||||
))
|
|
||||||
|
|
||||||
# Eq: Must choose two edges adjacent to each node
|
# Eq: Must choose two edges adjacent to each node
|
||||||
@constraint(
|
@constraint(
|
||||||
model,
|
model,
|
||||||
eq_degree[i in 1:data.n_cities],
|
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
|
sum(x[(min(i, j), max(i, j))] for j = 1:data.n_cities if i != j) == 2
|
||||||
)
|
)
|
||||||
|
|
||||||
function lazy_separate(cb_data)
|
function lazy_separate(cb_data)
|
||||||
@@ -41,10 +42,8 @@ function build_tsp_model_jump(data::Any; optimizer)
|
|||||||
for component in nx.connected_components(graph)
|
for component in nx.connected_components(graph)
|
||||||
if length(component) < data.n_cities
|
if length(component) < data.n_cities
|
||||||
cut_edges = [
|
cut_edges = [
|
||||||
[e[1], e[2]]
|
[e[1], e[2]] for
|
||||||
for e in edges
|
e in edges if (e[1] ∈ component && e[2] ∉ component) ||
|
||||||
if (e[1] ∈ component && e[2] ∉ component)
|
|
||||||
||
|
|
||||||
(e[1] ∉ component && e[2] ∈ component)
|
(e[1] ∉ component && e[2] ∈ component)
|
||||||
]
|
]
|
||||||
push!(violations, cut_edges)
|
push!(violations, cut_edges)
|
||||||
@@ -63,9 +62,9 @@ function build_tsp_model_jump(data::Any; optimizer)
|
|||||||
|
|
||||||
return JumpModel(
|
return JumpModel(
|
||||||
model,
|
model,
|
||||||
lazy_enforce=lazy_enforce,
|
lazy_enforce = lazy_enforce,
|
||||||
lazy_separate=lazy_separate,
|
lazy_separate = lazy_separate,
|
||||||
lp_optimizer=optimizer,
|
lp_optimizer = optimizer,
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ Base.@kwdef mutable struct _JumpModelExtData
|
|||||||
cuts_separate::Union{Function,Nothing} = nothing
|
cuts_separate::Union{Function,Nothing} = nothing
|
||||||
lazy_enforce::Union{Function,Nothing} = nothing
|
lazy_enforce::Union{Function,Nothing} = nothing
|
||||||
lazy_separate::Union{Function,Nothing} = nothing
|
lazy_separate::Union{Function,Nothing} = nothing
|
||||||
lp_optimizer
|
lp_optimizer::Any
|
||||||
end
|
end
|
||||||
|
|
||||||
function JuMP.copy_extension_data(
|
function JuMP.copy_extension_data(
|
||||||
@@ -26,9 +26,7 @@ function JuMP.copy_extension_data(
|
|||||||
new_model::AbstractModel,
|
new_model::AbstractModel,
|
||||||
::AbstractModel,
|
::AbstractModel,
|
||||||
)
|
)
|
||||||
new_model.ext[:miplearn] = _JumpModelExtData(
|
new_model.ext[:miplearn] = _JumpModelExtData(lp_optimizer = old_ext.lp_optimizer)
|
||||||
lp_optimizer=old_ext.lp_optimizer
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
@@ -297,7 +295,7 @@ end
|
|||||||
function _fix_variables(model::JuMP.Model, var_names, var_values, stats)
|
function _fix_variables(model::JuMP.Model, var_names, var_values, stats)
|
||||||
vars = [variable_by_name(model, v) for v in var_names]
|
vars = [variable_by_name(model, v) for v in var_names]
|
||||||
for (i, var) in enumerate(vars)
|
for (i, var) in enumerate(vars)
|
||||||
fix(var, var_values[i], force=true)
|
fix(var, var_values[i], force = true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -392,19 +390,19 @@ function __init_solvers_jump__()
|
|||||||
function __init__(
|
function __init__(
|
||||||
self,
|
self,
|
||||||
inner;
|
inner;
|
||||||
cuts_enforce::Union{Function,Nothing}=nothing,
|
cuts_enforce::Union{Function,Nothing} = nothing,
|
||||||
cuts_separate::Union{Function,Nothing}=nothing,
|
cuts_separate::Union{Function,Nothing} = nothing,
|
||||||
lazy_enforce::Union{Function,Nothing}=nothing,
|
lazy_enforce::Union{Function,Nothing} = nothing,
|
||||||
lazy_separate::Union{Function,Nothing}=nothing,
|
lazy_separate::Union{Function,Nothing} = nothing,
|
||||||
lp_optimizer=HiGHS.Optimizer,
|
lp_optimizer = HiGHS.Optimizer,
|
||||||
)
|
)
|
||||||
self.inner = inner
|
self.inner = inner
|
||||||
self.inner.ext[:miplearn] = _JumpModelExtData(
|
self.inner.ext[:miplearn] = _JumpModelExtData(
|
||||||
cuts_enforce=cuts_enforce,
|
cuts_enforce = cuts_enforce,
|
||||||
cuts_separate=cuts_separate,
|
cuts_separate = cuts_separate,
|
||||||
lazy_enforce=lazy_enforce,
|
lazy_enforce = lazy_enforce,
|
||||||
lazy_separate=lazy_separate,
|
lazy_separate = lazy_separate,
|
||||||
lp_optimizer=lp_optimizer,
|
lp_optimizer = lp_optimizer,
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -414,7 +412,7 @@ function __init_solvers_jump__()
|
|||||||
constrs_lhs,
|
constrs_lhs,
|
||||||
constrs_sense,
|
constrs_sense,
|
||||||
constrs_rhs,
|
constrs_rhs,
|
||||||
stats=nothing,
|
stats = nothing,
|
||||||
) = _add_constrs(
|
) = _add_constrs(
|
||||||
self.inner,
|
self.inner,
|
||||||
from_str_array(var_names),
|
from_str_array(var_names),
|
||||||
@@ -430,14 +428,14 @@ function __init_solvers_jump__()
|
|||||||
|
|
||||||
extract_after_mip(self, h5) = _extract_after_mip(self.inner, h5)
|
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)
|
_fix_variables(self.inner, from_str_array(var_names), var_values, stats)
|
||||||
|
|
||||||
optimize(self) = _optimize(self.inner)
|
optimize(self) = _optimize(self.inner)
|
||||||
|
|
||||||
relax(self) = Class(_relax(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)
|
_set_warm_starts(self.inner, from_str_array(var_names), var_values, stats)
|
||||||
|
|
||||||
write(self, filename) = _write(self.inner, filename)
|
write(self, filename) = _write(self.inner, filename)
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ function runtests()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function format()
|
function format()
|
||||||
JuliaFormatter.format(BASEDIR, verbose=true)
|
JuliaFormatter.format(BASEDIR, verbose = true)
|
||||||
JuliaFormatter.format("$BASEDIR/../../src", verbose=true)
|
JuliaFormatter.format("$BASEDIR/../../src", verbose = true)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -10,34 +10,32 @@ function gen_stab()
|
|||||||
randint = pyimport("scipy.stats").randint
|
randint = pyimport("scipy.stats").randint
|
||||||
np.random.seed(42)
|
np.random.seed(42)
|
||||||
gen = MaxWeightStableSetGenerator(
|
gen = MaxWeightStableSetGenerator(
|
||||||
w=uniform(10.0, scale=1.0),
|
w = uniform(10.0, scale = 1.0),
|
||||||
n=randint(low=50, high=51),
|
n = randint(low = 50, high = 51),
|
||||||
p=uniform(loc=0.5, scale=0.0),
|
p = uniform(loc = 0.5, scale = 0.0),
|
||||||
fix_graph=true,
|
fix_graph = true,
|
||||||
)
|
)
|
||||||
data = gen.generate(1)
|
data = gen.generate(1)
|
||||||
data_filenames = write_pkl_gz(data, "$BASEDIR/../fixtures", prefix="stab-n50-")
|
data_filenames = write_pkl_gz(data, "$BASEDIR/../fixtures", prefix = "stab-n50-")
|
||||||
collector = BasicCollector()
|
collector = BasicCollector()
|
||||||
collector.collect(
|
collector.collect(
|
||||||
data_filenames,
|
data_filenames,
|
||||||
data -> build_stab_model_jump(data, optimizer=SCIP.Optimizer),
|
data -> build_stab_model_jump(data, optimizer = SCIP.Optimizer),
|
||||||
progress=true,
|
progress = true,
|
||||||
verbose=true,
|
verbose = true,
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
function test_cuts()
|
function test_cuts()
|
||||||
data_filenames = ["$BASEDIR/../fixtures/stab-n50-00000.pkl.gz"]
|
data_filenames = ["$BASEDIR/../fixtures/stab-n50-00000.pkl.gz"]
|
||||||
clf = pyimport("sklearn.dummy").DummyClassifier()
|
clf = pyimport("sklearn.dummy").DummyClassifier()
|
||||||
extractor = H5FieldsExtractor(
|
extractor = H5FieldsExtractor(instance_fields = ["static_var_obj_coeffs"])
|
||||||
instance_fields=["static_var_obj_coeffs"],
|
comp = MemorizingCutsComponent(clf = clf, extractor = extractor)
|
||||||
)
|
solver = LearningSolver(components = [comp])
|
||||||
comp = MemorizingCutsComponent(clf=clf, extractor=extractor)
|
|
||||||
solver = LearningSolver(components=[comp])
|
|
||||||
solver.fit(data_filenames)
|
solver.fit(data_filenames)
|
||||||
stats = solver.optimize(
|
stats = solver.optimize(
|
||||||
data_filenames[1],
|
data_filenames[1],
|
||||||
data -> build_stab_model_jump(data, optimizer=SCIP.Optimizer),
|
data -> build_stab_model_jump(data, optimizer = SCIP.Optimizer),
|
||||||
)
|
)
|
||||||
@test stats["Cuts: AOT"] > 0
|
@test stats["Cuts: AOT"] > 0
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -11,36 +11,34 @@ function gen_tsp()
|
|||||||
np.random.seed(42)
|
np.random.seed(42)
|
||||||
|
|
||||||
gen = TravelingSalesmanGenerator(
|
gen = TravelingSalesmanGenerator(
|
||||||
x=uniform(loc=0.0, scale=1000.0),
|
x = uniform(loc = 0.0, scale = 1000.0),
|
||||||
y=uniform(loc=0.0, scale=1000.0),
|
y = uniform(loc = 0.0, scale = 1000.0),
|
||||||
n=randint(low=20, high=21),
|
n = randint(low = 20, high = 21),
|
||||||
gamma=uniform(loc=1.0, scale=0.25),
|
gamma = uniform(loc = 1.0, scale = 0.25),
|
||||||
fix_cities=true,
|
fix_cities = true,
|
||||||
round=true,
|
round = true,
|
||||||
)
|
)
|
||||||
data = gen.generate(1)
|
data = gen.generate(1)
|
||||||
data_filenames = write_pkl_gz(data, "$BASEDIR/../fixtures", prefix="tsp-n20-")
|
data_filenames = write_pkl_gz(data, "$BASEDIR/../fixtures", prefix = "tsp-n20-")
|
||||||
collector = BasicCollector()
|
collector = BasicCollector()
|
||||||
collector.collect(
|
collector.collect(
|
||||||
data_filenames,
|
data_filenames,
|
||||||
data -> build_tsp_model_jump(data, optimizer=GLPK.Optimizer),
|
data -> build_tsp_model_jump(data, optimizer = GLPK.Optimizer),
|
||||||
progress=true,
|
progress = true,
|
||||||
verbose=true,
|
verbose = true,
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
function test_lazy()
|
function test_lazy()
|
||||||
data_filenames = ["$BASEDIR/../fixtures/tsp-n20-00000.pkl.gz"]
|
data_filenames = ["$BASEDIR/../fixtures/tsp-n20-00000.pkl.gz"]
|
||||||
clf = pyimport("sklearn.dummy").DummyClassifier()
|
clf = pyimport("sklearn.dummy").DummyClassifier()
|
||||||
extractor = H5FieldsExtractor(
|
extractor = H5FieldsExtractor(instance_fields = ["static_var_obj_coeffs"])
|
||||||
instance_fields=["static_var_obj_coeffs"],
|
comp = MemorizingLazyComponent(clf = clf, extractor = extractor)
|
||||||
)
|
solver = LearningSolver(components = [comp])
|
||||||
comp = MemorizingLazyComponent(clf=clf, extractor=extractor)
|
|
||||||
solver = LearningSolver(components=[comp])
|
|
||||||
solver.fit(data_filenames)
|
solver.fit(data_filenames)
|
||||||
stats = solver.optimize(
|
stats = solver.optimize(
|
||||||
data_filenames[1],
|
data_filenames[1],
|
||||||
data -> build_tsp_model_jump(data, optimizer=GLPK.Optimizer),
|
data -> build_tsp_model_jump(data, optimizer = GLPK.Optimizer),
|
||||||
)
|
)
|
||||||
@test stats["Lazy Constraints: AOT"] > 0
|
@test stats["Lazy Constraints: AOT"] > 0
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ using SCIP
|
|||||||
function test_problems_stab()
|
function test_problems_stab()
|
||||||
nx = pyimport("networkx")
|
nx = pyimport("networkx")
|
||||||
data = MaxWeightStableSetData(
|
data = MaxWeightStableSetData(
|
||||||
graph=nx.gnp_random_graph(25, 0.5, seed=42),
|
graph = nx.gnp_random_graph(25, 0.5, seed = 42),
|
||||||
weights=repeat([1.0], 25),
|
weights = repeat([1.0], 25),
|
||||||
)
|
)
|
||||||
h5 = H5File(tempname(), "w")
|
h5 = H5File(tempname(), "w")
|
||||||
model = build_stab_model_jump(data, optimizer=SCIP.Optimizer)
|
model = build_stab_model_jump(data, optimizer = SCIP.Optimizer)
|
||||||
model.extract_after_load(h5)
|
model.extract_after_load(h5)
|
||||||
model.optimize()
|
model.optimize()
|
||||||
model.extract_after_mip(h5)
|
model.extract_after_mip(h5)
|
||||||
|
|||||||
@@ -10,17 +10,12 @@ function test_problems_tsp()
|
|||||||
squareform = pyimport("scipy.spatial.distance").squareform
|
squareform = pyimport("scipy.spatial.distance").squareform
|
||||||
|
|
||||||
data = TravelingSalesmanData(
|
data = TravelingSalesmanData(
|
||||||
n_cities=6,
|
n_cities = 6,
|
||||||
distances=squareform(pdist([
|
distances = squareform(
|
||||||
[0.0, 0.0],
|
pdist([[0.0, 0.0], [1.0, 0.0], [2.0, 0.0], [3.0, 0.0], [0.0, 1.0], [3.0, 1.0]]),
|
||||||
[1.0, 0.0],
|
),
|
||||||
[2.0, 0.0],
|
|
||||||
[3.0, 0.0],
|
|
||||||
[0.0, 1.0],
|
|
||||||
[3.0, 1.0],
|
|
||||||
])),
|
|
||||||
)
|
)
|
||||||
model = build_tsp_model_jump(data, optimizer=GLPK.Optimizer)
|
model = build_tsp_model_jump(data, optimizer = GLPK.Optimizer)
|
||||||
model.optimize()
|
model.optimize()
|
||||||
@test objective_value(model.inner) == 8.0
|
@test objective_value(model.inner) == 8.0
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ function test_jld2()
|
|||||||
_TestStruct(2, [1.0, 2.0, 3.0]),
|
_TestStruct(2, [1.0, 2.0, 3.0]),
|
||||||
_TestStruct(3, [3.0, 3.0, 3.0]),
|
_TestStruct(3, [3.0, 3.0, 3.0]),
|
||||||
]
|
]
|
||||||
filenames = write_jld2(data, dirname, prefix="obj")
|
filenames = write_jld2(data, dirname, prefix = "obj")
|
||||||
@test all(
|
@test all(
|
||||||
filenames .==
|
filenames .==
|
||||||
["$dirname/obj00001.jld2", "$dirname/obj00002.jld2", "$dirname/obj00003.jld2"],
|
["$dirname/obj00001.jld2", "$dirname/obj00002.jld2", "$dirname/obj00003.jld2"],
|
||||||
|
|||||||
@@ -13,16 +13,16 @@ function test_usage()
|
|||||||
|
|
||||||
@debug "Setting up LearningSolver..."
|
@debug "Setting up LearningSolver..."
|
||||||
solver = LearningSolver(
|
solver = LearningSolver(
|
||||||
components=[
|
components = [
|
||||||
IndependentVarsPrimalComponent(
|
IndependentVarsPrimalComponent(
|
||||||
base_clf=SingleClassFix(
|
base_clf = SingleClassFix(
|
||||||
MinProbabilityClassifier(
|
MinProbabilityClassifier(
|
||||||
base_clf=LogisticRegression(),
|
base_clf = LogisticRegression(),
|
||||||
thresholds=[0.95, 0.95],
|
thresholds = [0.95, 0.95],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
extractor=AlvLouWeh2017Extractor(),
|
extractor = AlvLouWeh2017Extractor(),
|
||||||
action=SetWarmStart(),
|
action = SetWarmStart(),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user