Wikunia / ConstraintSolver.jl

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

Using all_solutions #193

Closed hakank closed 3 years ago

hakank commented 3 years ago

I'm trying to output all the solutions of a model but could not figure how to generate them.

Here's a simple model:

using ConstraintSolver, JuMP
const CS = ConstraintSolver
m = Model(optimizer_with_attributes(CS.Optimizer, "all_solutions"=>true,"logging"=>[]))

n = 4
@variable(m, 0 <= x[1:n] <= n,Int)
@constraint(m, sum(x) == n)

optimize!(m)

status = JuMP.termination_status(m)
println("count:", MOI.get(m, MOI.ResultCount()) )
if status == MOI.OPTIMAL
     t = convert.(Integer,JuMP.value.(x))
     println("t:$t")
end

It outputs correctly that it has 35 solutions and the first solutions:

count:35
t:[0, 0, 0, 4]

But how do I generate all the other 34 solutions?

Wikunia commented 3 years ago

Thanks for trying out the solver and opening the issue. I should put some more work into the documentation.

You can access them with the second parameter of `value' see: https://jump.dev/JuMP.jl/v0.21.5/solutions/#JuMP.value

hakank commented 3 years ago

Thanks! That works nicely!

I realize that I have to read the updated version of the documentation. :-)

[I have a plan to implement quite a few of my "common constraint programming models" (see http://hakank.org/common_cp_models/ ) so I'll probably get back with more questions... ]

Wikunia commented 3 years ago

Sounds great. There is still a lot to work on with this as not that many constraints are implemented atm. Some aren't because JuMP doesn't support them. I would love to see what works and what doesn't.

hakank commented 3 years ago

Yes, I'll have to do a lot of decompositions for the missing global constraints, but that's (mostly) a fun thing. :-)