Implement FileInstance

master
Alinson S. Xavier 4 years ago
parent 6784b2153d
commit e72831039c

@ -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

@ -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
loaded::Union{Nothing,JuMPInstance}
end
@pydef mutable struct PyFileInstance <: miplearn.Instance
function __init__(self, filename)
self.filename = filename
self.loaded = nothing
self.samples = nothing
end
function FileInstance(filename::AbstractString)::FileInstance function to_model(self)
return FileInstance( return self.loaded.py.to_model()
filename, end
nothing,
) function get_instance_features(self)
end 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 load!(instance::FileInstance) function get_constraint_category(self, cname)
instance.loaded = load_jump_instance(instance.filename) 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 free!(instance::FileInstance) struct FileInstance <: Instance
instance.loaded = nothing py::PyCall.PyObject
end end
function flush!(instance::FileInstance) function FileInstance(filename)::FileInstance
save(instance.filename, instance.loaded) filename isa AbstractString || error("filename should be a string. Found $(typeof(filename)) instead.")
return FileInstance(PyFileInstance(filename))
end end
export FileInstance

@ -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)
# Pickle instance.py.samples. Ideally, we would use dumps and loads, but this
# causes some issues with PyCall, probably due to automatic type conversions.
py_samples_filename = tempname()
miplearn.write_pickle_gz(instance.py.samples, py_samples_filename)
py_samples = read(py_samples_filename)
# Replace variable/constraint refs by names
_to_names(d) = Dict(name(var) => value for (var, value) in d)
ext_original = instance.model.ext[:miplearn]
ext_names = Dict(
:variable_features => _to_names(ext_original[:variable_features]),
:variable_categories => _to_names(ext_original[:variable_categories]),
:constraint_features => _to_names(ext_original[:constraint_features]),
:constraint_categories => _to_names(ext_original[:constraint_categories]),
:instance_features => ext_original[:instance_features],
)
# Generate JLD2 file
jldsave(
filename;
miplearn_version=0.2,
mps=mps,
ext=ext_names,
py_samples=py_samples,
)
return
end
function load_jump_instance(filename::AbstractString)::JuMPInstance
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
mps_filename = "$(tempname()).mps.gz" mps_filename = "$(tempname()).mps.gz"
write(mps_filename, file["mps"]) write_to_file(instance.model, mps_filename)
model = read_from_file(mps_filename) mps = read(mps_filename)
# Unpickle instance.py.samples # Pickle instance.py.samples. Ideally, we would use dumps and loads, but this
# causes some issues with PyCall, probably due to automatic type conversions.
py_samples_filename = tempname() py_samples_filename = tempname()
write(py_samples_filename, file["py_samples"]) miplearn.write_pickle_gz(instance.py.samples, py_samples_filename, quiet=true)
py_samples = miplearn.read_pickle_gz(py_samples_filename) py_samples = read(py_samples_filename)
# Replace variable/constraint names by refs # Replace variable/constraint refs by names
_to_var(model, d) = Dict( _to_names(d) = Dict(name(var) => value for (var, value) in d)
variable_by_name(model, varname) => value ext_original = instance.model.ext[:miplearn]
for (varname, value) in d ext_names = Dict(
:variable_features => _to_names(ext_original[:variable_features]),
:variable_categories => _to_names(ext_original[:variable_categories]),
:constraint_features => _to_names(ext_original[:constraint_features]),
:constraint_categories => _to_names(ext_original[:constraint_categories]),
:instance_features => ext_original[:instance_features],
) )
_to_constr(model, d) = Dict(
constraint_by_name(model, cname) => value # Generate JLD2 file
for (cname, value) in d jldsave(
) filename;
ext = file["ext"] miplearn_version=0.2,
model.ext[:miplearn] = Dict( mps=mps,
:variable_features => _to_var(model, ext[:variable_features]), ext=ext_names,
:variable_categories => _to_var(model, ext[:variable_categories]), py_samples=py_samples,
:constraint_features => _to_constr(model, ext[:constraint_features]),
:constraint_categories => _to_constr(model, ext[:constraint_categories]),
:instance_features => ext[:instance_features],
) )
end
@info @sprintf("File written in %.2f seconds", time)
return
end
instance = JuMPInstance(model)
instance.py.samples = py_samples
return instance function load_jump_instance(filename::AbstractString)::JuMPInstance
@info "Reading: $filename"
instance = nothing
time = @elapsed begin
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
mps_filename = "$(tempname()).mps.gz"
write(mps_filename, file["mps"])
model = read_from_file(mps_filename)
# Unpickle instance.py.samples
py_samples_filename = tempname()
write(py_samples_filename, file["py_samples"])
py_samples = miplearn.read_pickle_gz(py_samples_filename, quiet=true)
# Replace variable/constraint names by refs
_to_var(model, d) = Dict(
variable_by_name(model, varname) => value
for (varname, value) in d
)
_to_constr(model, d) = Dict(
constraint_by_name(model, cname) => value
for (cname, value) in d
)
ext = file["ext"]
model.ext[:miplearn] = Dict(
:variable_features => _to_var(model, ext[:variable_features]),
:variable_categories => _to_var(model, ext[:variable_categories]),
:constraint_features => _to_constr(model, ext[:constraint_features]),
:constraint_categories => _to_constr(model, ext[:constraint_categories]),
:instance_features => ext[:instance_features],
)
instance = JuMPInstance(model)
instance.py.samples = py_samples
end
end end
@info @sprintf("File read in %.2f seconds", time)
return instance
end end

@ -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!

@ -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

@ -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

@ -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

@ -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
Loading…
Cancel
Save