|
|
@ -6,43 +6,55 @@ using JuMP
|
|
|
|
using MIPLearn
|
|
|
|
using MIPLearn
|
|
|
|
using Gurobi
|
|
|
|
using Gurobi
|
|
|
|
|
|
|
|
|
|
|
|
@testset "macros" begin
|
|
|
|
@testset "LearningSolver" begin
|
|
|
|
weights = [1.0, 2.0, 3.0]
|
|
|
|
@testset "model with annotations" begin
|
|
|
|
prices = [5.0, 6.0, 7.0]
|
|
|
|
# Create standard JuMP model
|
|
|
|
capacity = 3.0
|
|
|
|
weights = [1.0, 2.0, 3.0]
|
|
|
|
|
|
|
|
prices = [5.0, 6.0, 7.0]
|
|
|
|
|
|
|
|
capacity = 3.0
|
|
|
|
|
|
|
|
model = Model()
|
|
|
|
|
|
|
|
|
|
|
|
# Create standard JuMP model
|
|
|
|
n = length(weights)
|
|
|
|
model = Model()
|
|
|
|
@variable(model, x[1:n], Bin)
|
|
|
|
n = length(weights)
|
|
|
|
@objective(model, Max, sum(x[i] * prices[i] for i in 1:n))
|
|
|
|
@variable(model, x[1:n], Bin)
|
|
|
|
@constraint(model, c1, sum(x[i] * weights[i] for i in 1:n) <= capacity)
|
|
|
|
@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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Add ML information to the model
|
|
|
|
# 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")
|
|
|
|
for i in 1:n
|
|
|
|
for i in 1:n
|
|
|
|
@feature(x[i], [weights[i]; prices[i]])
|
|
|
|
@feature(x[i], [weights[i]; prices[i]])
|
|
|
|
@category(x[i], "type-$i")
|
|
|
|
@category(x[i], "type-$i")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Should store ML information
|
|
|
|
|
|
|
|
@test model.ext[:miplearn][:variable_features][x[1]] == [1.0, 5.0]
|
|
|
|
|
|
|
|
@test model.ext[:miplearn][:variable_features][x[2]] == [2.0, 6.0]
|
|
|
|
|
|
|
|
@test model.ext[:miplearn][:variable_features][x[3]] == [3.0, 7.0]
|
|
|
|
|
|
|
|
@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]
|
|
|
|
|
|
|
|
|
|
|
|
# Should store ML information
|
|
|
|
solver = LearningSolver(Gurobi.Optimizer)
|
|
|
|
@test model.ext[:miplearn][:variable_features][x[1]] == [1.0, 5.0]
|
|
|
|
instance = JuMPInstance(model)
|
|
|
|
@test model.ext[:miplearn][:variable_features][x[2]] == [2.0, 6.0]
|
|
|
|
stats = solve!(solver, instance)
|
|
|
|
@test model.ext[:miplearn][:variable_features][x[3]] == [3.0, 7.0]
|
|
|
|
@test stats["mip_lower_bound"] == 11.0
|
|
|
|
@test model.ext[:miplearn][:variable_categories][x[1]] == "type-1"
|
|
|
|
@test length(instance.py.samples) == 1
|
|
|
|
@test model.ext[:miplearn][:variable_categories][x[2]] == "type-2"
|
|
|
|
fit!(solver, [instance])
|
|
|
|
@test model.ext[:miplearn][:variable_categories][x[3]] == "type-3"
|
|
|
|
solve!(solver, instance)
|
|
|
|
@test model.ext[:miplearn][:constraint_features][c1] == [1.0, 2.0, 3.0]
|
|
|
|
end
|
|
|
|
@test model.ext[:miplearn][:constraint_categories][c1] == "c1"
|
|
|
|
|
|
|
|
@test model.ext[:miplearn][:instance_features] == [5.0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
solver = LearningSolver(Gurobi.Optimizer)
|
|
|
|
@testset "plain model" begin
|
|
|
|
instance = JuMPInstance(model)
|
|
|
|
model = Model()
|
|
|
|
stats = solve!(solver, instance)
|
|
|
|
@variable(model, x, Bin)
|
|
|
|
@test stats["mip_lower_bound"] == 11.0
|
|
|
|
@variable(model, y, Bin)
|
|
|
|
@test length(instance.py.samples) == 1
|
|
|
|
@objective(model, Max, x + y)
|
|
|
|
fit!(solver, [instance])
|
|
|
|
solver = LearningSolver(Gurobi.Optimizer)
|
|
|
|
solve!(solver, instance)
|
|
|
|
instance = JuMPInstance(model)
|
|
|
|
|
|
|
|
stats = solve!(solver, instance)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|