Implement FileInstance

This commit is contained in:
2021-05-25 08:09:40 -05:00
parent 6784b2153d
commit e72831039c
9 changed files with 205 additions and 116 deletions

View File

@@ -16,9 +16,10 @@ miplearn = pyimport("miplearn")
include("utils/log.jl") include("utils/log.jl")
include("utils/exceptions.jl") include("utils/exceptions.jl")
include("instance/jump.jl")
include("solvers/jump.jl") include("solvers/jump.jl")
include("solvers/learning.jl") include("solvers/learning.jl")
include("solvers/macros.jl") include("solvers/macros.jl")
include("instance/jump.jl")
include("instance/file.jl")
end # module end # module

View File

@@ -2,30 +2,66 @@
# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved. # Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved.
# Released under the modified BSD license. See COPYING.md for more details. # Released under the modified BSD license. See COPYING.md for more details.
struct FileInstance
filename::AbstractString @pydef mutable struct PyFileInstance <: miplearn.Instance
loaded::Union{Nothing,JuMPInstance} function __init__(self, filename)
self.filename = filename
self.loaded = nothing
self.samples = nothing
end
function to_model(self)
return self.loaded.py.to_model()
end
function get_instance_features(self)
return self.loaded.py.get_instance_features()
end
function get_variable_features(self, var_name)
return self.loaded.py.get_variable_features(var_name)
end
function get_variable_category(self, var_name)
return self.loaded.py.get_variable_category(var_name)
end
function get_constraint_features(self, cname)
return self.loaded.py.get_constraint_features(cname)
end
function get_constraint_category(self, cname)
return self.loaded.py.get_constraint_category(cname)
end
function load(self)
if self.loaded === nothing
self.loaded = load_jump_instance(self.filename)
self.samples = self.loaded.py.samples
end
end
function free(self)
self.loaded = nothing
self.samples = nothing
end
function flush(self)
self.loaded.py.samples = self.samples
save(self.filename, self.loaded)
end
end end
function FileInstance(filename::AbstractString)::FileInstance struct FileInstance <: Instance
return FileInstance( py::PyCall.PyObject
filename,
nothing,
)
end end
function load!(instance::FileInstance) function FileInstance(filename)::FileInstance
instance.loaded = load_jump_instance(instance.filename) filename isa AbstractString || error("filename should be a string. Found $(typeof(filename)) instead.")
return FileInstance(PyFileInstance(filename))
end end
function free!(instance::FileInstance) export FileInstance
instance.loaded = nothing
end
function flush!(instance::FileInstance)
save(instance.filename, instance.loaded)
end

View File

@@ -47,7 +47,7 @@ using JLD2
end end
struct JuMPInstance struct JuMPInstance <: Instance
py::PyCall.PyObject py::PyCall.PyObject
model::Model model::Model
end end
@@ -63,80 +63,88 @@ end
function save(filename::AbstractString, instance::JuMPInstance)::Nothing function save(filename::AbstractString, instance::JuMPInstance)::Nothing
# Convert JuMP model to MPS @info "Writing: $filename"
mps_filename = "$(tempname()).mps.gz" time = @elapsed begin
write_to_file(instance.model, mps_filename) # Convert JuMP model to MPS
mps = read(mps_filename) mps_filename = "$(tempname()).mps.gz"
write_to_file(instance.model, mps_filename)
mps = read(mps_filename)
# Pickle instance.py.samples. Ideally, we would use dumps and loads, but this # Pickle instance.py.samples. Ideally, we would use dumps and loads, but this
# causes some issues with PyCall, probably due to automatic type conversions. # causes some issues with PyCall, probably due to automatic type conversions.
py_samples_filename = tempname() py_samples_filename = tempname()
miplearn.write_pickle_gz(instance.py.samples, py_samples_filename) miplearn.write_pickle_gz(instance.py.samples, py_samples_filename, quiet=true)
py_samples = read(py_samples_filename) py_samples = read(py_samples_filename)
# Replace variable/constraint refs by names # Replace variable/constraint refs by names
_to_names(d) = Dict(name(var) => value for (var, value) in d) _to_names(d) = Dict(name(var) => value for (var, value) in d)
ext_original = instance.model.ext[:miplearn] ext_original = instance.model.ext[:miplearn]
ext_names = Dict( ext_names = Dict(
:variable_features => _to_names(ext_original[:variable_features]), :variable_features => _to_names(ext_original[:variable_features]),
:variable_categories => _to_names(ext_original[:variable_categories]), :variable_categories => _to_names(ext_original[:variable_categories]),
:constraint_features => _to_names(ext_original[:constraint_features]), :constraint_features => _to_names(ext_original[:constraint_features]),
:constraint_categories => _to_names(ext_original[:constraint_categories]), :constraint_categories => _to_names(ext_original[:constraint_categories]),
:instance_features => ext_original[:instance_features], :instance_features => ext_original[:instance_features],
) )
# Generate JLD2 file # Generate JLD2 file
jldsave( jldsave(
filename; filename;
miplearn_version=0.2, miplearn_version=0.2,
mps=mps, mps=mps,
ext=ext_names, ext=ext_names,
py_samples=py_samples, py_samples=py_samples,
) )
end
@info @sprintf("File written in %.2f seconds", time)
return return
end end
function load_jump_instance(filename::AbstractString)::JuMPInstance function load_jump_instance(filename::AbstractString)::JuMPInstance
jldopen(filename, "r") do file @info "Reading: $filename"
file["miplearn_version"] == 0.2 || error( instance = nothing
"MIPLearn version 0.2 cannot read instance files generated by " * time = @elapsed begin
"version $(file["miplearn_version"])." jldopen(filename, "r") do file
) file["miplearn_version"] == 0.2 || error(
"MIPLearn version 0.2 cannot read instance files generated by " *
"version $(file["miplearn_version"])."
)
# Convert MPS to JuMP # Convert MPS to JuMP
mps_filename = "$(tempname()).mps.gz" mps_filename = "$(tempname()).mps.gz"
write(mps_filename, file["mps"]) write(mps_filename, file["mps"])
model = read_from_file(mps_filename) model = read_from_file(mps_filename)
# Unpickle instance.py.samples # Unpickle instance.py.samples
py_samples_filename = tempname() py_samples_filename = tempname()
write(py_samples_filename, file["py_samples"]) write(py_samples_filename, file["py_samples"])
py_samples = miplearn.read_pickle_gz(py_samples_filename) py_samples = miplearn.read_pickle_gz(py_samples_filename, quiet=true)
# Replace variable/constraint names by refs # Replace variable/constraint names by refs
_to_var(model, d) = Dict( _to_var(model, d) = Dict(
variable_by_name(model, varname) => value variable_by_name(model, varname) => value
for (varname, value) in d for (varname, value) in d
) )
_to_constr(model, d) = Dict( _to_constr(model, d) = Dict(
constraint_by_name(model, cname) => value constraint_by_name(model, cname) => value
for (cname, value) in d for (cname, value) in d
) )
ext = file["ext"] ext = file["ext"]
model.ext[:miplearn] = Dict( model.ext[:miplearn] = Dict(
:variable_features => _to_var(model, ext[:variable_features]), :variable_features => _to_var(model, ext[:variable_features]),
:variable_categories => _to_var(model, ext[:variable_categories]), :variable_categories => _to_var(model, ext[:variable_categories]),
:constraint_features => _to_constr(model, ext[:constraint_features]), :constraint_features => _to_constr(model, ext[:constraint_features]),
:constraint_categories => _to_constr(model, ext[:constraint_categories]), :constraint_categories => _to_constr(model, ext[:constraint_categories]),
:instance_features => ext[:instance_features], :instance_features => ext[:instance_features],
) )
instance = JuMPInstance(model) instance = JuMPInstance(model)
instance.py.samples = py_samples instance.py.samples = py_samples
end
return instance
end end
@info @sprintf("File read in %.2f seconds", time)
return instance
end end

View File

@@ -7,20 +7,31 @@ struct LearningSolver
end end
abstract type Instance
end
function LearningSolver(optimizer_factory)::LearningSolver function LearningSolver(optimizer_factory)::LearningSolver
py = miplearn.LearningSolver(solver=JuMPSolver(optimizer_factory)) py = miplearn.LearningSolver(solver=JuMPSolver(optimizer_factory))
return LearningSolver(py) return LearningSolver(py)
end end
function solve!(solver::LearningSolver, instance::JuMPInstance) function solve!(
return @python_call solver.py.solve(instance.py) solver::LearningSolver,
instance::Instance;
tee::Bool = false,
)
return @python_call solver.py.solve(instance.py, tee=tee)
end end
function fit!(solver::LearningSolver, instances::Vector{JuMPInstance}) function fit!(solver::LearningSolver, instances::Vector{<:Instance})
@python_call solver.py.fit([instance.py for instance in instances]) @python_call solver.py.fit([instance.py for instance in instances])
end end
export LearningSolver, solve!, fit! export Instance,
LearningSolver,
solve!,
fit!

View File

@@ -0,0 +1,26 @@
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved.
# Released under the modified BSD license. See COPYING.md for more details.
using JuMP
using MIPLearn
using Gurobi
@testset "FileInstance" begin
@testset "solve" begin
model = Model()
@variable(model, x, Bin)
@variable(model, y, Bin)
@objective(model, Max, x + y)
instance = JuMPInstance(model)
filename = tempname()
save(filename, instance)
file_instance = FileInstance(filename)
solver = LearningSolver(Gurobi.Optimizer)
solve!(solver, file_instance)
loaded = load_jump_instance(filename)
@test length(loaded.py.samples) == 1
end
end

View File

@@ -0,0 +1,35 @@
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved.
# Released under the modified BSD license. See COPYING.md for more details.
@testset "JuMPInstance" begin
@testset "save and load" begin
# Create basic model
model = Model()
@variable(model, x, Bin)
@variable(model, y, Bin)
@objective(model, Max, x + y)
@feature(x, [1.0])
@category(x, "cat1")
@feature(model, [5.0])
# Solve
instance = JuMPInstance(model)
solver = LearningSolver(Gurobi.Optimizer)
stats = solve!(solver, instance)
@test length(instance.py.samples) == 1
# Save model to file
filename = tempname()
save(filename, instance)
@test isfile(filename)
# Read model from file
loaded = load_jump_instance(filename)
x = variable_by_name(loaded.model, "x")
@test loaded.model.ext[:miplearn][:variable_features][x] == [1.0]
@test loaded.model.ext[:miplearn][:variable_categories][x] == "cat1"
@test loaded.model.ext[:miplearn][:instance_features] == [5.0]
@test length(loaded.py.samples) == 1
end
end

View File

@@ -8,6 +8,7 @@ using MIPLearn
MIPLearn.setup_logger() MIPLearn.setup_logger()
@testset "MIPLearn" begin @testset "MIPLearn" begin
include("solvers/jump.jl") include("solvers/jump_test.jl")
include("solvers/learning.jl") include("solvers/learning_test.jl")
include("instance/file_test.jl")
end end

View File

@@ -58,33 +58,4 @@ using Gurobi
stats = solve!(solver, instance) stats = solve!(solver, instance)
end end
@testset "file model" begin
# Create basic model
model = Model()
@variable(model, x, Bin)
@variable(model, y, Bin)
@objective(model, Max, x + y)
@feature(x, [1.0])
@category(x, "cat1")
@feature(model, [5.0])
# Solve
instance = JuMPInstance(model)
solver = LearningSolver(Gurobi.Optimizer)
stats = solve!(solver, instance)
@test length(instance.py.samples) == 1
# Save model to file
filename = tempname()
save(filename, instance)
@test isfile(filename)
# Read model from file
loaded = load_jump_instance(filename)
x = variable_by_name(loaded.model, "x")
@test loaded.model.ext[:miplearn][:variable_features][x] == [1.0]
@test loaded.model.ext[:miplearn][:variable_categories][x] == "cat1"
@test loaded.model.ext[:miplearn][:instance_features] == [5.0]
@test length(loaded.py.samples) == 1
end
end end