Make python classes available in Julia

This commit is contained in:
2023-03-22 09:23:20 -05:00
parent 117ed8d4cd
commit dabcfef00f
15 changed files with 209 additions and 34 deletions

View File

@@ -7,15 +7,24 @@ module MIPLearn
using PyCall
using SparseArrays
include("problems/setcover.jl")
include("io.jl")
include("solvers/jump.jl")
include("Cuts/BlackBox/cplex.jl")
include("collectors.jl")
include("components.jl")
include("extractors.jl")
include("io.jl")
include("problems/setcover.jl")
include("solvers/jump.jl")
include("solvers/learning.jl")
function __init__()
__init_problems_setcover__()
__init_collectors__()
__init_components__()
__init_extractors__()
__init_io__()
__init_problems_setcover__()
__init_solvers_jump__()
__init_solvers_learning__()
end
end # module

7
src/collectors.jl Normal file
View File

@@ -0,0 +1,7 @@
global BasicCollector = PyNULL()
function __init_collectors__()
copy!(BasicCollector, pyimport("miplearn.collectors.basic").BasicCollector)
end
export BasicCollector

65
src/components.jl Normal file
View File

@@ -0,0 +1,65 @@
global MinProbabilityClassifier = PyNULL()
global SingleClassFix = PyNULL()
global PrimalComponentAction = PyNULL()
global SetWarmStart = PyNULL()
global FixVariables = PyNULL()
global EnforceProximity = PyNULL()
global ExpertPrimalComponent = PyNULL()
global IndependentVarsPrimalComponent = PyNULL()
global JointVarsPrimalComponent = PyNULL()
global SolutionConstructor = PyNULL()
global MemorizingPrimalComponent = PyNULL()
global SelectTopSolutions = PyNULL()
global MergeTopSolutions = PyNULL()
function __init_components__()
copy!(
MinProbabilityClassifier,
pyimport("miplearn.classifiers.minprob").MinProbabilityClassifier,
)
copy!(SingleClassFix, pyimport("miplearn.classifiers.singleclass").SingleClassFix)
copy!(
PrimalComponentAction,
pyimport("miplearn.components.primal.actions").PrimalComponentAction,
)
copy!(SetWarmStart, pyimport("miplearn.components.primal.actions").SetWarmStart)
copy!(FixVariables, pyimport("miplearn.components.primal.actions").FixVariables)
copy!(EnforceProximity, pyimport("miplearn.components.primal.actions").EnforceProximity)
copy!(
ExpertPrimalComponent,
pyimport("miplearn.components.primal.expert").ExpertPrimalComponent,
)
copy!(
IndependentVarsPrimalComponent,
pyimport("miplearn.components.primal.indep").IndependentVarsPrimalComponent,
)
copy!(
JointVarsPrimalComponent,
pyimport("miplearn.components.primal.joint").JointVarsPrimalComponent,
)
copy!(
SolutionConstructor,
pyimport("miplearn.components.primal.mem").SolutionConstructor,
)
copy!(
MemorizingPrimalComponent,
pyimport("miplearn.components.primal.mem").MemorizingPrimalComponent,
)
copy!(SelectTopSolutions, pyimport("miplearn.components.primal.mem").SelectTopSolutions)
copy!(MergeTopSolutions, pyimport("miplearn.components.primal.mem").MergeTopSolutions)
end
export MinProbabilityClassifier,
SingleClassFix,
PrimalComponentAction,
SetWarmStart,
FixVariables,
EnforceProximity,
ExpertPrimalComponent,
IndependentVarsPrimalComponent,
JointVarsPrimalComponent,
SolutionConstructor,
MemorizingPrimalComponent,
SelectTopSolutions,
MergeTopSolutions

18
src/extractors.jl Normal file
View File

@@ -0,0 +1,18 @@
global FeaturesExtractor = PyNULL()
global AlvLouWeh2017Extractor = PyNULL()
global DummyExtractor = PyNULL()
global H5FieldsExtractor = PyNULL()
function __init_extractors__()
copy!(FeaturesExtractor, pyimport("miplearn.extractors.abstract").FeaturesExtractor)
copy!(
AlvLouWeh2017Extractor,
pyimport("miplearn.extractors.AlvLouWeh2017").AlvLouWeh2017Extractor,
)
copy!(DummyExtractor, pyimport("miplearn.extractors.dummy").DummyExtractor)
copy!(H5FieldsExtractor, pyimport("miplearn.extractors.fields").H5FieldsExtractor)
end
export FeaturesExtractor, AlvLouWeh2017Extractor, DummyExtractor, H5FieldsExtractor

View File

@@ -1,4 +1,6 @@
global H5File = PyNULL()
global write_pkl_gz = PyNULL()
global read_pkl_gz = PyNULL()
to_str_array(values) = py"to_str_array"(values)
@@ -6,6 +8,8 @@ from_str_array(values) = py"from_str_array"(values)
function __init_io__()
copy!(H5File, pyimport("miplearn.h5").H5File)
copy!(write_pkl_gz, pyimport("miplearn.io").write_pkl_gz)
copy!(read_pkl_gz, pyimport("miplearn.io").read_pkl_gz)
py"""
import numpy as np
@@ -32,4 +36,4 @@ function PyObject(m::SparseMatrixCSC)
).tocoo()
end
export H5File
export H5File, write_pkl_gz, read_pkl_gz

View File

@@ -1,15 +1,18 @@
global SetCoverData = PyNULL()
global SetCoverGenerator = PyNULL()
using JuMP
using HiGHS
global SetCoverData = PyNULL()
global SetCoverGenerator = PyNULL()
function __init_problems_setcover__()
copy!(SetCoverData, pyimport("miplearn.problems.setcover").SetCoverData)
copy!(SetCoverGenerator, pyimport("miplearn.problems.setcover").SetCoverGenerator)
end
function build_setcover_model(data; optimizer = HiGHS.Optimizer)
function build_setcover_model(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)

View File

@@ -250,12 +250,14 @@ 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)
optimize!(model)
flush(stdout)
Libc.flush_cstdio()
end
function _relax(model::JuMP.Model)
@@ -325,3 +327,5 @@ function __init_solvers_jump__()
end
copy!(JumpModel, Class)
end
export JumpModel

7
src/solvers/learning.jl Normal file
View File

@@ -0,0 +1,7 @@
global LearningSolver = PyNULL()
function __init_solvers_learning__()
copy!(LearningSolver, pyimport("miplearn.solvers.learning").LearningSolver)
end
export LearningSolver