Make compatible with MIPLearn 0.2.0

This commit is contained in:
2021-04-08 11:25:23 -05:00
parent 4af6c78026
commit 96f7243d4c
10 changed files with 449 additions and 311 deletions

View File

@@ -1,5 +1,4 @@
[deps]
CPLEX = "a076750e-1247-5638-91d2-ce28b192dca0"
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
Gurobi = "2e9cd046-0924-5485-92f1-d5272153d98b"
JSON2 = "2535ab7d-5cd8-5a07-80ac-9b1792aadce3"

View File

@@ -4,62 +4,20 @@
using Test
using MIPLearn
using CPLEX
using Gurobi
using PyCall
@testset "varname_split" begin
@test MIPLearn.varname_split("x[1]") == ("x", "1")
end
miplearn_tests = pyimport("miplearn.solvers.tests")
@testset "JuMPSolver" begin
for optimizer in [CPLEX.Optimizer, Gurobi.Optimizer]
instance = KnapsackInstance([23., 26., 20., 18.],
[505., 352., 458., 220.],
67.0)
for optimizer in [Gurobi.Optimizer]
instance = KnapsackInstance(
[23., 26., 20., 18.],
[505., 352., 458., 220.],
67.0,
)
model = instance.to_model()
solver = JuMPSolver(optimizer=optimizer)
solver.set_instance(instance, model)
solver.set_time_limit(30)
solver.set_warm_start(Dict("x" => Dict(
"1" => 1.0,
"2" => 0.0,
"3" => 0.0,
"4" => 1.0,
)))
stats = solver.solve()
@test stats["Lower bound"] == 1183.0
@test stats["Upper bound"] == 1183.0
@test stats["Sense"] == "max"
@test stats["Wallclock time"] > 0
@test length(stats["Log"]) > 100
solution = solver.get_solution()
@test solution["x"]["1"] == 1.0
@test solution["x"]["2"] == 0.0
@test solution["x"]["3"] == 1.0
@test solution["x"]["4"] == 1.0
stats = solver.solve_lp()
@test round(stats["Optimal value"], digits=3) == 1287.923
@test length(stats["Log"]) > 100
solution = solver.get_solution()
@test round(solution["x"]["1"], digits=3) == 1.000
@test round(solution["x"]["2"], digits=3) == 0.923
@test round(solution["x"]["3"], digits=3) == 1.000
@test round(solution["x"]["4"], digits=3) == 0.000
solver.fix(Dict("x" => Dict(
"1" => 1.0,
"2" => 0.0,
"3" => 0.0,
"4" => 1.0,
)))
stats = solver.solve()
@test stats["Lower bound"] == 725.0
@test stats["Upper bound"] == 725.0
miplearn_tests.test_internal_solver(solver, instance, model)
end
end

View File

@@ -17,9 +17,16 @@ end
function to_model(data::KnapsackData)
model = Model()
n = length(data.weights)
@variable(model, x[1:n], Bin)
@objective(model, Max, sum(x[i] * data.prices[i] for i in 1:n))
@constraint(model, sum(x[i] * data.weights[i] for i in 1:n) <= data.capacity)
@variable(model, x[0:(n-1)], Bin)
@objective(model, Max, sum(x[i] * data.prices[i+1] for i in 0:(n-1)))
@constraint(
model,
eq_capacity,
sum(
x[i] * data.weights[i+1]
for i in 0:(n-1)
) <= data.capacity,
)
return model
end

View File

@@ -4,48 +4,22 @@
using Test
using MIPLearn
using CPLEX
using Gurobi
@testset "Instance" begin
weights = [23., 26., 20., 18.]
prices = [505., 352., 458., 220.]
capacity = 67.0
instance = KnapsackInstance(weights, prices, capacity)
filename = tempname()
dump(instance, filename)
instance = KnapsackInstance([0.0], [0.0], 0.0)
load!(instance, filename)
@test instance.data.weights == weights
@test instance.data.prices == prices
@test instance.data.capacity == capacity
end
@testset "LearningSolver" begin
for optimizer in [CPLEX.Optimizer, Gurobi.Optimizer]
instance = KnapsackInstance([23., 26., 20., 18.],
[505., 352., 458., 220.],
67.0)
solver = LearningSolver(optimizer=optimizer,
mode="heuristic",
time_limit=90)
for optimizer in [Gurobi.Optimizer]
instance = KnapsackInstance(
[23., 26., 20., 18.],
[505., 352., 458., 220.],
67.0,
)
solver = LearningSolver(
optimizer=optimizer,
mode="heuristic",
)
stats = solve!(solver, instance)
@test instance.solution["x"]["1"] == 1.0
@test instance.solution["x"]["2"] == 0.0
@test instance.solution["x"]["3"] == 1.0
@test instance.solution["x"]["4"] == 1.0
@test instance.lower_bound == 1183.0
@test instance.upper_bound == 1183.0
@test round(instance.lp_solution["x"]["1"], digits=3) == 1.000
@test round(instance.lp_solution["x"]["2"], digits=3) == 0.923
@test round(instance.lp_solution["x"]["3"], digits=3) == 1.000
@test round(instance.lp_solution["x"]["4"], digits=3) == 0.000
@test round(instance.lp_value, digits=3) == 1287.923
fit!(solver, [instance])
solve!(solver, instance)
end
end
end