mirror of
https://github.com/ANL-CEEESA/MIPLearn.jl.git
synced 2025-12-06 08:28:52 -06:00
Remove MPS from HDF5 file
This commit is contained in:
@@ -66,6 +66,7 @@ export DynamicLazyConstraintsComponent,
|
||||
ObjectiveValueComponent,
|
||||
PrimalSolutionComponent,
|
||||
StaticLazyConstraintsComponent,
|
||||
MinPrecisionThreshold
|
||||
MinPrecisionThreshold,
|
||||
Hdf5Sample
|
||||
|
||||
end # module
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved.
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
|
||||
using JLD2
|
||||
import Base: flush
|
||||
|
||||
mutable struct FileInstance <: Instance
|
||||
@@ -9,13 +10,13 @@ mutable struct FileInstance <: Instance
|
||||
loaded::Union{Nothing,JuMPInstance}
|
||||
filename::AbstractString
|
||||
h5::PyCall.PyObject
|
||||
lazycb::Union{Nothing,Tuple{Function,Function}}
|
||||
build_model::Function
|
||||
|
||||
function FileInstance(
|
||||
filename::AbstractString;
|
||||
lazycb::Union{Nothing,Tuple{Function,Function}} = nothing,
|
||||
filename::AbstractString,
|
||||
build_model::Function,
|
||||
)::FileInstance
|
||||
instance = new(nothing, nothing, filename, nothing, lazycb)
|
||||
instance = new(nothing, nothing, filename, nothing, build_model)
|
||||
instance.py = PyFileInstance(instance)
|
||||
instance.h5 = Hdf5Sample(filename)
|
||||
instance.filename = filename
|
||||
@@ -55,7 +56,8 @@ end
|
||||
|
||||
function load(instance::FileInstance)
|
||||
if instance.loaded === nothing
|
||||
instance.loaded = load_instance(instance.filename, lazycb = instance.lazycb)
|
||||
data = load_data(instance.filename)
|
||||
instance.loaded = JuMPInstance(instance.build_model(data))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -65,6 +67,16 @@ function free(instance::FileInstance)
|
||||
GC.gc()
|
||||
end
|
||||
|
||||
function save_data(filename::AbstractString, data)::Nothing
|
||||
jldsave(filename, data = data)
|
||||
end
|
||||
|
||||
function load_data(filename::AbstractString)
|
||||
jldopen(filename, "r") do file
|
||||
return file["data"]
|
||||
end
|
||||
end
|
||||
|
||||
function flush(instance::FileInstance) end
|
||||
|
||||
function __init_PyFileInstance__()
|
||||
|
||||
@@ -121,49 +121,4 @@ function __init_PyJuMPInstance__()
|
||||
copy!(PyJuMPInstance, Class)
|
||||
end
|
||||
|
||||
function save(filename::AbstractString, instance::JuMPInstance)::Nothing
|
||||
# Convert JuMP model to MPS
|
||||
mps_filename = "$(tempname()).mps.gz"
|
||||
model = instance.py.to_model()
|
||||
write_to_file(model, mps_filename)
|
||||
mps = read(mps_filename)
|
||||
|
||||
# Generate HDF5
|
||||
h5 = Hdf5Sample(filename, mode = "w")
|
||||
h5.put_scalar("miplearn_version", "0002")
|
||||
h5.put_bytes("mps", mps)
|
||||
|
||||
ext = copy(model.ext[:miplearn])
|
||||
delete!(ext, "lazy_find_cb")
|
||||
delete!(ext, "lazy_enforce_cb")
|
||||
h5.put_scalar("jump_ext", JSON.json(ext))
|
||||
return
|
||||
end
|
||||
|
||||
function _check_miplearn_version(h5)
|
||||
v = h5.get_scalar("miplearn_version")
|
||||
v == "0002" || error(
|
||||
"The file you are trying to load has been generated by " *
|
||||
"MIPLearn $(v) and you are currently running MIPLearn 0002 " *
|
||||
"Reading files generated by different versions of MIPLearn is " *
|
||||
"not currently supported.",
|
||||
)
|
||||
end
|
||||
|
||||
function load_instance(
|
||||
filename::AbstractString;
|
||||
lazycb::Union{Nothing,Tuple{Function,Function}} = nothing,
|
||||
)::JuMPInstance
|
||||
h5 = Hdf5Sample(filename)
|
||||
_check_miplearn_version(h5)
|
||||
mps = h5.get_bytes("mps")
|
||||
ext = JSON.parse(h5.get_scalar("jump_ext"))
|
||||
if lazycb !== nothing
|
||||
ext["lazy_find_cb"] = lazycb[1]
|
||||
ext["lazy_enforce_cb"] = lazycb[2]
|
||||
end
|
||||
instance = JuMPInstance(Vector{UInt8}(mps), ext)
|
||||
return instance
|
||||
end
|
||||
|
||||
export JuMPInstance, save, load_instance
|
||||
|
||||
Reference in New Issue
Block a user