mirror of
https://github.com/ANL-CEEESA/MIPLearn.jl.git
synced 2025-12-06 00:18:51 -06:00
Make cuts component compatible with JuMP
This commit is contained in:
@@ -15,6 +15,7 @@ Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
|
||||
MIPLearn = "2b1277c3-b477-4c49-a15e-7ba350325c68"
|
||||
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
|
||||
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
|
||||
SCIP = "82193955-e24f-5292-bf16-6f2c5261a85f"
|
||||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
|
||||
|
||||
[compat]
|
||||
|
||||
BIN
test/fixtures/bell5.h5
vendored
BIN
test/fixtures/bell5.h5
vendored
Binary file not shown.
BIN
test/fixtures/stab-n50-00000.h5
vendored
Normal file
BIN
test/fixtures/stab-n50-00000.h5
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/stab-n50-00000.pkl.gz
vendored
Normal file
BIN
test/fixtures/stab-n50-00000.pkl.gz
vendored
Normal file
Binary file not shown.
@@ -16,10 +16,12 @@ FIXTURES = "$BASEDIR/../fixtures"
|
||||
include("fixtures.jl")
|
||||
|
||||
include("BB/test_bb.jl")
|
||||
include("components/test_cuts.jl")
|
||||
include("Cuts/BlackBox/test_cplex.jl")
|
||||
include("problems/test_setcover.jl")
|
||||
include("test_io.jl")
|
||||
include("problems/test_stab.jl")
|
||||
include("solvers/test_jump.jl")
|
||||
include("test_io.jl")
|
||||
include("test_usage.jl")
|
||||
|
||||
function runtests()
|
||||
@@ -27,17 +29,18 @@ function runtests()
|
||||
@testset "BB" begin
|
||||
test_bb()
|
||||
end
|
||||
# test_cuts_blackbox_cplex()
|
||||
test_io()
|
||||
test_problems_setcover()
|
||||
test_problems_stab()
|
||||
test_solvers_jump()
|
||||
test_usage()
|
||||
test_cuts()
|
||||
end
|
||||
end
|
||||
|
||||
function format()
|
||||
JuliaFormatter.format(BASEDIR, verbose = true)
|
||||
JuliaFormatter.format("$BASEDIR/../../src", verbose = true)
|
||||
JuliaFormatter.format(BASEDIR, verbose=true)
|
||||
JuliaFormatter.format("$BASEDIR/../../src", verbose=true)
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
45
test/src/components/test_cuts.jl
Normal file
45
test/src/components/test_cuts.jl
Normal file
@@ -0,0 +1,45 @@
|
||||
# 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 SCIP
|
||||
|
||||
function gen_stab()
|
||||
np = pyimport("numpy")
|
||||
uniform = pyimport("scipy.stats").uniform
|
||||
randint = pyimport("scipy.stats").randint
|
||||
np.random.seed(42)
|
||||
gen = MaxWeightStableSetGenerator(
|
||||
w=uniform(10.0, scale=1.0),
|
||||
n=randint(low=50, high=51),
|
||||
p=uniform(loc=0.5, scale=0.0),
|
||||
fix_graph=true,
|
||||
)
|
||||
data = gen.generate(1)
|
||||
data_filenames = write_pkl_gz(data, "$BASEDIR/../fixtures", prefix="stab-n50-")
|
||||
collector = BasicCollector(write_mps=false)
|
||||
collector.collect(
|
||||
data_filenames,
|
||||
data -> build_stab_model_jump(data, optimizer=SCIP.Optimizer),
|
||||
progress=true,
|
||||
verbose=true,
|
||||
)
|
||||
end
|
||||
|
||||
function test_cuts()
|
||||
data_filenames = ["$BASEDIR/../fixtures/stab-n50-0000$i.pkl.gz" for i in 0:0]
|
||||
clf = pyimport("sklearn.neighbors").KNeighborsClassifier(n_neighbors=1)
|
||||
extractor = H5FieldsExtractor(
|
||||
instance_fields=["static_var_obj_coeffs"],
|
||||
)
|
||||
comp = MemorizingCutsComponent(clf=clf, extractor=extractor)
|
||||
solver = LearningSolver(components=[comp])
|
||||
solver.fit(data_filenames)
|
||||
@show comp.n_features_
|
||||
@show comp.n_targets_
|
||||
stats = solver.optimize(
|
||||
data_filenames[1],
|
||||
data -> build_stab_model_jump(data, optimizer=SCIP.Optimizer),
|
||||
)
|
||||
@test stats["Cuts: AOT"] > 0
|
||||
end
|
||||
@@ -14,5 +14,5 @@ function fixture_setcover_data()
|
||||
end
|
||||
|
||||
function fixture_setcover_model()
|
||||
return build_setcover_model(fixture_setcover_data())
|
||||
return build_setcover_model_jump(fixture_setcover_data())
|
||||
end
|
||||
|
||||
@@ -51,7 +51,7 @@ function test_problems_setcover_model()
|
||||
)
|
||||
|
||||
h5 = H5File(tempname(), "w")
|
||||
model = build_setcover_model(data)
|
||||
model = build_setcover_model_jump(data)
|
||||
model.extract_after_load(h5)
|
||||
model.optimize()
|
||||
model.extract_after_mip(h5)
|
||||
|
||||
27
test/src/problems/test_stab.jl
Normal file
27
test/src/problems/test_stab.jl
Normal file
@@ -0,0 +1,27 @@
|
||||
# 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 PyCall
|
||||
using SCIP
|
||||
|
||||
function test_problems_stab()
|
||||
test_problems_stab_1()
|
||||
test_problems_stab_2()
|
||||
end
|
||||
|
||||
function test_problems_stab_1()
|
||||
nx = pyimport("networkx")
|
||||
data = MaxWeightStableSetData(
|
||||
graph=nx.gnp_random_graph(25, 0.5, seed=42),
|
||||
weights=repeat([1.0], 25),
|
||||
)
|
||||
h5 = H5File(tempname(), "w")
|
||||
model = build_stab_model_jump(data, optimizer=SCIP.Optimizer)
|
||||
model.extract_after_load(h5)
|
||||
model.optimize()
|
||||
model.extract_after_mip(h5)
|
||||
@test h5.get_scalar("mip_obj_value") == -6
|
||||
@test h5.get_scalar("mip_cuts")[1:20] == "[[0,8,11,13],[0,8,13"
|
||||
h5.close()
|
||||
end
|
||||
@@ -29,13 +29,13 @@ function test_usage()
|
||||
|
||||
@debug "Collecting training data..."
|
||||
bc = BasicCollector()
|
||||
bc.collect(data_filenames, build_setcover_model)
|
||||
bc.collect(data_filenames, build_setcover_model_jump)
|
||||
|
||||
@debug "Training models..."
|
||||
solver.fit(data_filenames)
|
||||
|
||||
@debug "Solving model..."
|
||||
solver.optimize(data_filenames[1], build_setcover_model)
|
||||
solver.optimize(data_filenames[1], build_setcover_model_jump)
|
||||
|
||||
@debug "Checking solution..."
|
||||
h5 = H5File(h5_filenames[1])
|
||||
|
||||
Reference in New Issue
Block a user