mirror of
https://github.com/ANL-CEEESA/MIPLearn.jl.git
synced 2025-12-06 08:28:52 -06:00
Implement FileInstance
This commit is contained in:
@@ -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
|
@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
|
|
||||||
|
|||||||
@@ -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,6 +63,8 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function save(filename::AbstractString, instance::JuMPInstance)::Nothing
|
function save(filename::AbstractString, instance::JuMPInstance)::Nothing
|
||||||
|
@info "Writing: $filename"
|
||||||
|
time = @elapsed begin
|
||||||
# Convert JuMP model to MPS
|
# Convert JuMP model to MPS
|
||||||
mps_filename = "$(tempname()).mps.gz"
|
mps_filename = "$(tempname()).mps.gz"
|
||||||
write_to_file(instance.model, mps_filename)
|
write_to_file(instance.model, mps_filename)
|
||||||
@@ -71,7 +73,7 @@ function save(filename::AbstractString, instance::JuMPInstance)::Nothing
|
|||||||
# 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
|
||||||
@@ -93,11 +95,16 @@ function save(filename::AbstractString, instance::JuMPInstance)::Nothing
|
|||||||
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
|
||||||
|
@info "Reading: $filename"
|
||||||
|
instance = nothing
|
||||||
|
time = @elapsed begin
|
||||||
jldopen(filename, "r") do file
|
jldopen(filename, "r") do file
|
||||||
file["miplearn_version"] == 0.2 || error(
|
file["miplearn_version"] == 0.2 || error(
|
||||||
"MIPLearn version 0.2 cannot read instance files generated by " *
|
"MIPLearn version 0.2 cannot read instance files generated by " *
|
||||||
@@ -112,7 +119,7 @@ function load_jump_instance(filename::AbstractString)::JuMPInstance
|
|||||||
# 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(
|
||||||
@@ -134,9 +141,10 @@ function load_jump_instance(filename::AbstractString)::JuMPInstance
|
|||||||
|
|
||||||
instance = JuMPInstance(model)
|
instance = JuMPInstance(model)
|
||||||
instance.py.samples = py_samples
|
instance.py.samples = py_samples
|
||||||
|
|
||||||
return instance
|
|
||||||
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!
|
||||||
|
|||||||
26
test/instance/file_test.jl
Normal file
26
test/instance/file_test.jl
Normal 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
|
||||||
35
test/instance/jump_test.jl
Normal file
35
test/instance/jump_test.jl
Normal 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
|
||||||
@@ -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
|
||||||
Reference in New Issue
Block a user