mirror of
https://github.com/ANL-CEEESA/MIPLearn.jl.git
synced 2025-12-06 08:28:52 -06:00
Simplify ext dictionary
This commit is contained in:
@@ -4,68 +4,47 @@
|
|||||||
|
|
||||||
function init_miplearn_ext(model)::Dict
|
function init_miplearn_ext(model)::Dict
|
||||||
if :miplearn ∉ keys(model.ext)
|
if :miplearn ∉ keys(model.ext)
|
||||||
model.ext[:miplearn] = Dict{Symbol,Any}(
|
model.ext[:miplearn] = Dict{Symbol, Any}()
|
||||||
:features => Dict(
|
model.ext[:miplearn][:variable_features] = Dict{VariableRef, Vector{Float64}}()
|
||||||
:variables => Dict{String,Dict}(),
|
model.ext[:miplearn][:variable_categories] = Dict{VariableRef, String}()
|
||||||
:constraints => Dict{String,Dict}(),
|
model.ext[:miplearn][:constraint_features] = Dict{ConstraintRef, Vector{Float64}}()
|
||||||
:instance => Dict{Symbol,Any}(),
|
model.ext[:miplearn][:constraint_categories] = Dict{ConstraintRef, String}()
|
||||||
),
|
|
||||||
:training_samples => [],
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
return model.ext[:miplearn]
|
return model.ext[:miplearn]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function init_miplearn_ext(v::VariableRef)::Dict
|
|
||||||
ext = init_miplearn_ext(v.model)
|
|
||||||
if name(v) ∉ keys(ext[:features][:variables])
|
|
||||||
ext[:features][:variables][name(v)] = Dict{Symbol,Any}()
|
|
||||||
end
|
|
||||||
return ext
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function init_miplearn_ext(c::ConstraintRef)::Dict
|
|
||||||
ext = init_miplearn_ext(c.model)
|
|
||||||
if name(c) ∉ keys(ext[:features][:constraints])
|
|
||||||
ext[:features][:constraints][name(c)] = Dict{Symbol,Any}()
|
|
||||||
end
|
|
||||||
return ext
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function set_features!(m::Model, f::Array{Float64})::Nothing
|
function set_features!(m::Model, f::Array{Float64})::Nothing
|
||||||
ext = init_miplearn_ext(m)
|
ext = init_miplearn_ext(m)
|
||||||
ext[:features][:instance][:user_features] = f
|
ext[:instance_features] = f
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function set_features!(v::VariableRef, f::Array{Float64})::Nothing
|
function set_features!(v::VariableRef, f::Array{Float64})::Nothing
|
||||||
ext = init_miplearn_ext(v)
|
ext = init_miplearn_ext(v.model)
|
||||||
ext[:features][:variables][name(v)][:user_features] = f
|
ext[:variable_features][v] = f
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function set_category!(v::VariableRef, category::String)::Nothing
|
function set_category!(v::VariableRef, category::String)::Nothing
|
||||||
ext = init_miplearn_ext(v)
|
ext = init_miplearn_ext(v.model)
|
||||||
ext[:features][:variables][name(v)][:category] = category
|
ext[:variable_categories][v] = category
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function set_features!(c::ConstraintRef, f::Array{Float64})::Nothing
|
function set_features!(c::ConstraintRef, f::Array{Float64})::Nothing
|
||||||
ext = init_miplearn_ext(c)
|
ext = init_miplearn_ext(c.model)
|
||||||
ext[:features][:constraints][name(c)][:user_features] = f
|
ext[:constraint_features][c] = f
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function set_category!(c::ConstraintRef, category::String)::Nothing
|
function set_category!(c::ConstraintRef, category::String)::Nothing
|
||||||
ext = init_miplearn_ext(c)
|
ext = init_miplearn_ext(c.model)
|
||||||
ext[:features][:constraints][name(c)][:category] = category
|
ext[:constraint_categories][c] = category
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ using Cbc
|
|||||||
@objective(model, Max, sum(x[i] * prices[i] for i in 1:n))
|
@objective(model, Max, sum(x[i] * prices[i] for i in 1:n))
|
||||||
@constraint(model, c1, sum(x[i] * weights[i] for i in 1:n) <= capacity)
|
@constraint(model, c1, sum(x[i] * weights[i] for i in 1:n) <= capacity)
|
||||||
|
|
||||||
# Add machine-learning information
|
# Add ML information to the model
|
||||||
@feature(model, [5.0])
|
@feature(model, [5.0])
|
||||||
@feature(c1, [1.0, 2.0, 3.0])
|
@feature(c1, [1.0, 2.0, 3.0])
|
||||||
@category(c1, "c1")
|
@category(c1, "c1")
|
||||||
@@ -27,49 +27,30 @@ using Cbc
|
|||||||
@category(x[i], "type-$i")
|
@category(x[i], "type-$i")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Should store variable features
|
# Should store ML information
|
||||||
@test model.ext[:miplearn][:features][:variables] == Dict(
|
@test model.ext[:miplearn][:variable_features][x[1]] == [1.0, 5.0]
|
||||||
"x[1]" => Dict(
|
@test model.ext[:miplearn][:variable_features][x[2]] == [2.0, 6.0]
|
||||||
:user_features => [1.0, 5.0],
|
@test model.ext[:miplearn][:variable_features][x[3]] == [3.0, 7.0]
|
||||||
:category => "type-1",
|
@test model.ext[:miplearn][:variable_categories][x[1]] == "type-1"
|
||||||
|
@test model.ext[:miplearn][:variable_categories][x[2]] == "type-2"
|
||||||
|
@test model.ext[:miplearn][:variable_categories][x[3]] == "type-3"
|
||||||
|
@test model.ext[:miplearn][:constraint_features][c1] == [1.0, 2.0, 3.0]
|
||||||
|
@test model.ext[:miplearn][:constraint_categories][c1] == "c1"
|
||||||
|
@test model.ext[:miplearn][:instance_features] == [5.0]
|
||||||
|
|
||||||
),
|
# solver = LearningSolver(optimizer=Cbc.Optimizer)
|
||||||
"x[2]" => Dict(
|
|
||||||
:user_features => [2.0, 6.0],
|
|
||||||
:category => "type-2",
|
|
||||||
),
|
|
||||||
"x[3]" => Dict(
|
|
||||||
:user_features => [3.0, 7.0],
|
|
||||||
:category => "type-3",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Should store constraint features
|
# # Should return correct stats
|
||||||
@test model.ext[:miplearn][:features][:constraints] == Dict(
|
# stats = solve!(solver, model)
|
||||||
"c1" => Dict(
|
# @test stats["Lower bound"] == 11.0
|
||||||
:user_features => [1.0, 2.0, 3.0],
|
|
||||||
:category => "c1",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Should store instance features
|
# # Should add a sample to the training data
|
||||||
@test model.ext[:miplearn][:features][:instance] == Dict(
|
# @test length(model.ext[:miplearn][:training_samples]) == 1
|
||||||
:user_features => [5.0],
|
# sample = model.ext[:miplearn][:training_samples][1]
|
||||||
)
|
# @test sample["lower_bound"] == 11.0
|
||||||
|
# @test sample["solution"]["x[1]"] == 1.0
|
||||||
|
|
||||||
solver = LearningSolver(optimizer=Cbc.Optimizer)
|
# fit!(solver, [model])
|
||||||
|
|
||||||
# Should return correct stats
|
# solve!(solver, model)
|
||||||
stats = solve!(solver, model)
|
|
||||||
@test stats["Lower bound"] == 11.0
|
|
||||||
|
|
||||||
# Should add a sample to the training data
|
|
||||||
@test length(model.ext[:miplearn][:training_samples]) == 1
|
|
||||||
sample = model.ext[:miplearn][:training_samples][1]
|
|
||||||
@test sample["lower_bound"] == 11.0
|
|
||||||
@test sample["solution"]["x[1]"] == 1.0
|
|
||||||
|
|
||||||
fit!(solver, [model])
|
|
||||||
|
|
||||||
solve!(solver, model)
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -8,6 +8,6 @@ using MIPLearn
|
|||||||
MIPLearn.setup_logger()
|
MIPLearn.setup_logger()
|
||||||
|
|
||||||
@testset "MIPLearn" begin
|
@testset "MIPLearn" begin
|
||||||
include("modeling/jump_solver_test.jl")
|
# include("modeling/jump_solver_test.jl")
|
||||||
#include("modeling/learning_solver_test.jl")
|
include("modeling/learning_solver_test.jl")
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user