|
|
@ -5,46 +5,46 @@
|
|
|
|
using JuMP
|
|
|
|
using JuMP
|
|
|
|
using MIPLearn
|
|
|
|
using MIPLearn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Base.@kwdef struct KnapsackData
|
|
|
|
function build_knapsack_model()
|
|
|
|
|
|
|
|
# Create standard JuMP model
|
|
|
|
|
|
|
|
weights = [1.0, 2.0, 3.0]
|
|
|
|
weights = [1.0, 2.0, 3.0]
|
|
|
|
prices = [5.0, 6.0, 7.0]
|
|
|
|
prices = [5.0, 6.0, 7.0]
|
|
|
|
capacity = 3.0
|
|
|
|
capacity = 3.0
|
|
|
|
model = Model()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
n = length(weights)
|
|
|
|
|
|
|
|
|
|
|
|
function build_knapsack_model(data = KnapsackData())
|
|
|
|
|
|
|
|
model = Model()
|
|
|
|
|
|
|
|
n = length(data.weights)
|
|
|
|
@variable(model, x[1:n], Bin)
|
|
|
|
@variable(model, x[1:n], Bin)
|
|
|
|
@objective(model, Max, sum(x[i] * prices[i] for i = 1:n))
|
|
|
|
@objective(model, Max, sum(x[i] * data.prices[i] for i = 1:n))
|
|
|
|
@constraint(model, c1, sum(x[i] * weights[i] for i = 1:n) <= capacity)
|
|
|
|
@constraint(model, c1, sum(x[i] * data.weights[i] for i = 1:n) <= data.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 = 1:n
|
|
|
|
# for i = 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
|
|
|
|
# # Should store ML information
|
|
|
|
@test model.ext[:miplearn]["variable_features"]["x[1]"] == [1.0, 5.0]
|
|
|
|
# @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[2]"] == [2.0, 6.0]
|
|
|
|
@test model.ext[:miplearn]["variable_features"]["x[3]"] == [3.0, 7.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[1]"] == "type-1"
|
|
|
|
@test model.ext[:miplearn]["variable_categories"]["x[2]"] == "type-2"
|
|
|
|
# @test model.ext[:miplearn]["variable_categories"]["x[2]"] == "type-2"
|
|
|
|
@test model.ext[:miplearn]["variable_categories"]["x[3]"] == "type-3"
|
|
|
|
# @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_features"]["c1"] == [1.0, 2.0, 3.0]
|
|
|
|
@test model.ext[:miplearn]["constraint_categories"]["c1"] == "c1"
|
|
|
|
# @test model.ext[:miplearn]["constraint_categories"]["c1"] == "c1"
|
|
|
|
@test model.ext[:miplearn]["instance_features"] == [5.0]
|
|
|
|
# @test model.ext[:miplearn]["instance_features"] == [5.0]
|
|
|
|
|
|
|
|
|
|
|
|
return model
|
|
|
|
return model
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function build_knapsack_file_instance()
|
|
|
|
function build_knapsack_file_instance()
|
|
|
|
model = build_knapsack_model()
|
|
|
|
data = KnapsackData()
|
|
|
|
instance = JuMPInstance(model)
|
|
|
|
|
|
|
|
filename = tempname()
|
|
|
|
filename = tempname()
|
|
|
|
save(filename, instance)
|
|
|
|
MIPLearn.save_data(filename, data)
|
|
|
|
return FileInstance(filename)
|
|
|
|
return FileInstance(filename, build_knapsack_model)
|
|
|
|
end
|
|
|
|
end
|
|
|
|