Wikunia / ConstraintSolver.jl

ConstraintSolver in Julia: Blog posts ->
https://opensourc.es/blog/constraint-solver-1
MIT License
136 stars 14 forks source link

Strange result for TableConstraint #206

Closed hakank closed 3 years ago

hakank commented 3 years ago

I tested the TableConstraint model from the documentation ( https://wikunia.github.io/ConstraintSolver.jl/stable/how_to/#How-to-define-a-set-of-possibilities-for-more-than-one-variable?-1 ) and added the output of all solutions. However, the 5 solutions are all the same, the last entry in the table ([4 5 5 3 4]).

using ConstraintSolver, JuMP
using Cbc, GLPK
const CS = ConstraintSolver

cbc_optimizer = optimizer_with_attributes(Cbc.Optimizer, "logLevel" => 0)
model = Model(optimizer_with_attributes(
    CS.Optimizer,
    "all_solutions"=>true,
    "lp_optimizer" => cbc_optimizer,
))

# Variables
@variable(model, 1 <= x[1:5] <= 5, Int)

table = [
    1 2 3 1 1;
    1 3 3 2 1;
    1 1 3 2 1;
    1 1 1 2 4;
    4 5 5 3 4;
]

@constraint(model, x in CS.TableSet(table))

@objective(model, Max, sum(x))
optimize!(model)
status = JuMP.termination_status(model)
println("status:$status")
if status == MOI.OPTIMAL

    num_sols = MOI.get(model, MOI.ResultCount())
    println("\nnum_sols:$num_sols\n")

    for sol in 1:num_sols
        println("solution #$sol")
        xx = convert.(Integer,JuMP.value.(x))

        println("x:$xx\n")
    end
end

Here's the result:

julia> @time include("table_constraint_other.jl")
....
num_sols:5

solution #1
x:[4, 5, 5, 3, 4]

solution #2
x:[4, 5, 5, 3, 4]

solution #3
x:[4, 5, 5, 3, 4]

solution #4
x:[4, 5, 5, 3, 4]

solution #5
x:[4, 5, 5, 3, 4]

I tested it with another model (implementing the modulo constraint) and got the same problem: the correct number of solutions are shown, but all solutions are identical.

Wikunia commented 3 years ago

The output code should be:

for sol in 1:num_sols
        println("solution #$sol")
        xx = convert.(Integer,JuMP.value.(x; result=sol))
        println("objective: ", JuMP.objective_value(model; result=sol))
        println("x:$xx\n")
end

you forgot the result = sol part.