Multiple solutions from MiniZinc?

Hi, I am trying to see if it is possible to return multiple solutions from MiniZinc.jl. I am not sure how to set the flag to do so for the MiniZinc solver. Following the example at this stackoverflow question (Multiple output under minizinc - Stack Overflow) the following code returns 1 of 4 possible solutions. Thanks for any help anyone can provide!

using JuMP
import MiniZinc

model = MOI.Utilities.CachingOptimizer(
    MiniZinc.Model{Int}(),
    MiniZinc.Optimizer{Int}("chuffed"),
)
x = MOI.add_variables(model, 2)
MOI.add_constraint.(model, x, MOI.Interval(1, 9))
MOI.add_constraint.(model, x, MOI.Integer())

MOI.add_constraint(model, MOI.VectorOfVariables(x), MOI.AllDifferent(2))
MOI.add_constraint(model, 1 * x[1] + x[2], MOI.EqualTo(3))
MOI.optimize!(model)

MOI.get(model, MOI.VariablePrimal(), x)
MOI.get(model, MOI.ResultCount())
1 Like

Set MOI.SolutionLimit:

julia> using JuMP

julia> import MiniZinc

julia> model = GenericModel{Int}(() -> MiniZinc.Optimizer{Int}("chuffed"));

julia> set_attribute(model, MOI.SolutionLimit(), 10)  # Or some large value

julia> @variable(model, 1 <= x[1:2] <= 9, Int);

julia> @constraint(model, x in MOI.AllDifferent(2));

julia> @constraint(model, sum(x) == 3);

julia> optimize!(model)

julia> [value.(x; result = i) for i in 1:result_count(model)]
2-element Vector{Vector{Int64}}:
 [1, 2]
 [2, 1]

I guess this isn’t documented :smile:

1 Like

See Document MOI.SolutionLimit by odow · Pull Request #70 · jump-dev/MiniZinc.jl · GitHub

Thank you @odow! Ah, the JuMP syntax is much nicer than working at the “MOI level”. Can you explain what GenericModel{Int} is, and why we need to set the optimizer as an anonymous function?

GenericModel{Int}

The default model = Model() constructor assumes coefficients are Float64. This is a feature to use Int coefficients (and variable values). See Arbitrary precision arithmetic · JuMP

why we need to set the optimizer as an anonymous function

Because you are passing an argument to MiniZinc.Optimizer. See Models · JuMP

1 Like