You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi
I've been playing around with MILP and when trying to solve the knapsack problem using Cbc I encountered suboptimal solutions
It may be an error in my code, a parameter which I should have specified or a real issue (maybe I should report it on the Cbc github page, please bear with me I'm new here).
Here is a MWE
using JuMP, Cbc, LinearAlgebra
ksv(C, ks) = sum(C[ks]) # return cost of a given solution for specified costs
ksw(A, ks) = sum(A[ks]) # return total weight of a given solution for specified weights
function knapsack_milp(C, A, b)
m = Model(Cbc.Optimizer)
n = length(C)
@variable(m, X[1:n], Bin)
@objective(m, Max, dot(C,X))
@constraint(m, dot(A,X) <= b)
optimize!(m)
return sort(findall(x->x==1, value.(X)))
end
C = [5, 29, 60, 37, 66]
A = [36, 87, 69, 0, 28]
b = 80.0
sub_optimal = knapsack_milp(C, A, b)
# Found [1, 5], cost : 71, weight : 64
optimal = [1, 4, 5]
# cost : 108, weight : 64
@assert ksw(A, optimal) <= b
println("Cbc solution value : $(ksv(C, sub_optimal))")
println("Optimal solution value : $(ksv(C, optimal))")
JuMP v0.21.10, Cbc v0.8.1
It should be noted that optimal solutions are found using GLPK v0.14.14
Thanks in advance
The text was updated successfully, but these errors were encountered:
I was running this code in a Pluto notebook so I didn't see the solver output, which indicates an objective value of 108 (optimal).
The problem is due to numerical approximation I guess, as the solution vector before filtering is [1.0, 0.0, 0.0, 0.9999999999999999, 1.0].
Now as I specified the solution vector to be binary I guessed any approximation made during the resolution would have been corrected when querying the solution (by rounding each variable supposed to be integer).
I wonder if this behaviour is to be expected or not, should I close the issue ?
S-Inako
changed the title
Suboptimal solution found in knapsack problem
Approximation error when querying solution
Sep 30, 2021
Hi
I've been playing around with MILP and when trying to solve the knapsack problem using Cbc I encountered suboptimal solutions
It may be an error in my code, a parameter which I should have specified or a real issue (maybe I should report it on the Cbc github page, please bear with me I'm new here).
Here is a MWE
JuMP v0.21.10, Cbc v0.8.1
It should be noted that optimal solutions are found using GLPK v0.14.14
Thanks in advance
The text was updated successfully, but these errors were encountered: