Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Approximation error when querying solution #177

Closed
S-Inako opened this issue Sep 30, 2021 · 2 comments
Closed

Approximation error when querying solution #177

S-Inako opened this issue Sep 30, 2021 · 2 comments

Comments

@S-Inako
Copy link

S-Inako commented 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

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

@S-Inako
Copy link
Author

S-Inako commented Sep 30, 2021

So this may be a short issue

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 S-Inako changed the title Suboptimal solution found in knapsack problem Approximation error when querying solution Sep 30, 2021
@odow
Copy link
Member

odow commented Sep 30, 2021

Closing because this is expected behavior. All solvers (not just Cbc) only find solutions to a tolerance, typically ~1e-6.

Gurobi has a good set of articles if this is new to you:
https://www.gurobi.com/documentation/9.0/refman/num_tolerances_and_user_sc.html

@odow odow closed this as completed Sep 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants