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

@@ -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()
@testset "MIPLearn" begin
include("solvers/jump.jl")
include("solvers/learning.jl")
include("solvers/jump_test.jl")
include("solvers/learning_test.jl")
include("instance/file_test.jl")
end

View File

@@ -58,33 +58,4 @@ using Gurobi
stats = solve!(solver, instance)
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