mirror of
https://github.com/ANL-CEEESA/MIPLearn.jl.git
synced 2025-12-06 00:18:51 -06:00
Make python classes available in Julia
This commit is contained in:
@@ -4,6 +4,7 @@ authors = ["Alinson S. Xavier <git@axavier.org>"]
|
||||
version = "0.1.0"
|
||||
|
||||
[deps]
|
||||
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
|
||||
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
|
||||
HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b"
|
||||
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
|
||||
|
||||
BIN
test/fixtures/bell5.h5
vendored
BIN
test/fixtures/bell5.h5
vendored
Binary file not shown.
@@ -4,21 +4,26 @@ using Test
|
||||
using Logging
|
||||
using JuliaFormatter
|
||||
using HiGHS
|
||||
using Glob
|
||||
|
||||
BASEDIR = dirname(@__FILE__)
|
||||
FIXTURES = "$BASEDIR/../fixtures"
|
||||
|
||||
include("fixtures.jl")
|
||||
|
||||
include("Cuts/BlackBox/test_cplex.jl")
|
||||
include("problems/test_setcover.jl")
|
||||
include("test_h5.jl")
|
||||
include("test_io.jl")
|
||||
include("solvers/test_jump.jl")
|
||||
include("test_usage.jl")
|
||||
|
||||
function runtests()
|
||||
@testset "MIPLearn" begin
|
||||
test_cuts_blackbox_cplex()
|
||||
test_h5()
|
||||
test_io()
|
||||
test_problems_setcover()
|
||||
test_solvers_jump()
|
||||
test_usage()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
14
test/src/fixtures.jl
Normal file
14
test/src/fixtures.jl
Normal file
@@ -0,0 +1,14 @@
|
||||
function fixture_setcover_data()
|
||||
return SetCoverData(
|
||||
costs = [5, 10, 12, 6, 8],
|
||||
incidence_matrix = [
|
||||
1 0 0 1 0
|
||||
1 1 0 0 0
|
||||
0 0 1 1 1
|
||||
],
|
||||
)
|
||||
end
|
||||
|
||||
function fixture_setcover_model()
|
||||
return build_setcover_model(fixture_setcover_data())
|
||||
end
|
||||
@@ -1,18 +1,6 @@
|
||||
using JuMP
|
||||
import MIPLearn: from_str_array, to_str_array
|
||||
|
||||
function build_model()
|
||||
data = SetCoverData(
|
||||
costs = [5, 10, 12, 6, 8],
|
||||
incidence_matrix = [
|
||||
1 0 0 1 0
|
||||
1 1 0 0 0
|
||||
0 0 1 1 1
|
||||
],
|
||||
)
|
||||
return build_setcover_model(data)
|
||||
end
|
||||
|
||||
function test_solvers_jump()
|
||||
test_solvers_jump_extract()
|
||||
test_solvers_jump_add_constrs()
|
||||
@@ -51,7 +39,7 @@ function test_solvers_jump_extract()
|
||||
@test all(actual .≈ expected)
|
||||
end
|
||||
|
||||
model = build_model()
|
||||
model = fixture_setcover_model()
|
||||
model.extract_after_load(h5)
|
||||
test_sparse(
|
||||
"static_constr_lhs",
|
||||
@@ -106,7 +94,7 @@ end
|
||||
|
||||
function test_solvers_jump_add_constrs()
|
||||
h5 = H5File(tempname(), "w")
|
||||
model = build_model()
|
||||
model = fixture_setcover_model()
|
||||
model.extract_after_load(h5)
|
||||
model.add_constrs(
|
||||
to_str_array(["x[2]", "x[3]"]),
|
||||
@@ -124,12 +112,9 @@ end
|
||||
|
||||
function test_solvers_jump_fix_vars()
|
||||
h5 = H5File(tempname(), "w")
|
||||
model = build_model()
|
||||
model = fixture_setcover_model()
|
||||
model.extract_after_load(h5)
|
||||
model.fix_variables(
|
||||
to_str_array(["x[2]", "x[3]"]),
|
||||
[0, 0],
|
||||
)
|
||||
model.fix_variables(to_str_array(["x[2]", "x[3]"]), [0, 0])
|
||||
model.optimize()
|
||||
model.extract_after_mip(h5)
|
||||
@test all(h5.get_array("mip_var_values") .≈ [1, 0, 0, 0, 1])
|
||||
@@ -138,7 +123,7 @@ end
|
||||
function test_solvers_jump_warm_starts()
|
||||
# TODO: Check presence of warm start on log file
|
||||
h5 = H5File(tempname(), "w")
|
||||
model = build_model()
|
||||
model = fixture_setcover_model()
|
||||
model.extract_after_load(h5)
|
||||
model.set_warm_starts(
|
||||
to_str_array(["x[0]", "x[1]", "x[2]", "x[3]", "x[4]"]),
|
||||
@@ -149,8 +134,8 @@ end
|
||||
|
||||
function test_solvers_jump_write()
|
||||
mps_filename = "$(tempname()).mps"
|
||||
model = build_model()
|
||||
model = fixture_setcover_model()
|
||||
model.write(mps_filename)
|
||||
@test isfile(mps_filename)
|
||||
rm(mps_filename)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,18 @@
|
||||
using MIPLearn
|
||||
|
||||
function test_io()
|
||||
test_pkl_gz()
|
||||
test_h5()
|
||||
end
|
||||
|
||||
function test_pkl_gz()
|
||||
original = Dict("K1" => 1, "K2" => [0, 1, 2], "K3" => "Hello")
|
||||
dirname = tempdir()
|
||||
MIPLearn.write_pkl_gz([original], dirname)
|
||||
recovered = MIPLearn.read_pkl_gz("$dirname/00000.pkl.gz")
|
||||
@test recovered == original
|
||||
end
|
||||
|
||||
function test_h5()
|
||||
h5 = H5File(tempname(), "w")
|
||||
_test_roundtrip_scalar(h5, "A")
|
||||
40
test/src/test_usage.jl
Normal file
40
test/src/test_usage.jl
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
function test_usage()
|
||||
LogisticRegression = pyimport("sklearn.linear_model").LogisticRegression
|
||||
|
||||
@debug "Generating data files..."
|
||||
dirname = tempdir()
|
||||
data = [fixture_setcover_data()]
|
||||
data_filenames = write_pkl_gz(data, dirname)
|
||||
h5_filenames = ["$(f).h5" for f in data_filenames]
|
||||
|
||||
@debug "Setting up LearningSolver..."
|
||||
solver = LearningSolver(
|
||||
components = [
|
||||
IndependentVarsPrimalComponent(
|
||||
base_clf = SingleClassFix(
|
||||
MinProbabilityClassifier(
|
||||
base_clf = LogisticRegression(),
|
||||
thresholds = [0.95, 0.95],
|
||||
),
|
||||
),
|
||||
extractor = AlvLouWeh2017Extractor(),
|
||||
action = SetWarmStart(),
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
@debug "Collecting training data..."
|
||||
bc = BasicCollector()
|
||||
bc.collect(data_filenames, build_setcover_model)
|
||||
|
||||
@debug "Training models..."
|
||||
solver.fit(data_filenames)
|
||||
|
||||
@debug "Solving model..."
|
||||
solver.optimize(data_filenames[1], build_setcover_model)
|
||||
|
||||
@debug "Checking solution..."
|
||||
h5 = H5File(h5_filenames[1])
|
||||
@test h5.get_scalar("mip_obj_value") == 11.0
|
||||
end
|
||||
Reference in New Issue
Block a user