Skip to content

Approximation error when querying solution #177

@S-Inako

Description

@S-Inako

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions