jump-dev / MiniZinc.jl

A Julia interface to the MiniZinc constraint modeling language
https://www.minizinc.org/
MIT License
18 stars 4 forks source link

MiniZinc example in the docs does not work. #77

Closed mbataillou closed 3 months ago

mbataillou commented 3 months ago

Hello!

Looking at the JuMP docs for MiniZinc, we find a toy problem to be solved:

using JuMP
import MiniZinc
model = Model(() -> MiniZinc.Optimizer{Float64}("highs"))
@variable(model, 1 <= x[1:3] <= 3, Int)
@constraint(model, x in MOI.AllDifferent(3))
@objective(model, Max, sum(i * x[i] for i in 1:3))
optimize!(model)
@show value.(x)

However this returns no solution:

ERROR: Result index of attribute MathOptInterface.VariablePrimal(1) out of bounds. There are currently 0 solution(s) in the model.

Also not sure if it helps but the previous example (not using JuMP) works:

julia> import MiniZinc

julia> import MathOptInterface as MOI

julia> function main()
           model = MOI.Utilities.CachingOptimizer(
               MiniZinc.Model{Int}(),
               MiniZinc.Optimizer{Int}("chuffed"),
           )
           # xᵢ ∈ {1, 2, 3} ∀i=1,2,3
           x = MOI.add_variables(model, 3)
           MOI.add_constraint.(model, x, MOI.Interval(1, 3))
           MOI.add_constraint.(model, x, MOI.Integer())
           # zⱼ ∈ {0, 1}    ∀j=1,2
           z = MOI.add_variables(model, 2)
           MOI.add_constraint.(model, z, MOI.ZeroOne())
           # z₁ <-> x₁ != x₂
           MOI.add_constraint(
               model,
               MOI.VectorOfVariables([z[1], x[1], x[2]]),
               MOI.Reified(MOI.AllDifferent(2)),
           )
           # z₂ <-> x₂ != x₃
           MOI.add_constraint(
               model,
               MOI.VectorOfVariables([z[2], x[2], x[3]]),
               MOI.Reified(MOI.AllDifferent(2)),
           )
           # z₁ + z₂ = 1
           MOI.add_constraint(model, 1 * z[1] + x[2], MOI.EqualTo(1))
           MOI.optimize!(model)
           x_star = MOI.get(model, MOI.VariablePrimal(), x)
           z_star = MOI.get(model, MOI.VariablePrimal(), z)
           return x_star, z_star
       end
main (generic function with 1 method)

julia> main()
odow commented 3 months ago

Thanks for finding this. I've transferred this issue to MiniZinc.jl.

I can reproduce, with more information:

julia> solution_summary(model)
* Solver : MiniZinc

* Status
  Result count       : 0
  Termination status : OTHER_ERROR
  Message from the solver:
  "=====ERROR=====
Error: type error: no function or predicate with this signature found: `alldifferent(array[int] of var float)'
Cannot use the following functions or predicates with the same identifier:
predicate alldifferent(array [$X] of var set of int: x);
    (argument 1 expects type array[$_] of var set of int, but type array[int] of var float given)
predicate alldifferent(array [$X] of var int: x);
    (argument 1 expects type array[$_] of var int, but type array[int] of var float given)
predicate alldifferent(array [$X] of var opt int: x);
    (argument 1 expects type array[$_] of var opt int, but type array[int] of var float given)

/private/var/folders/bg/dzq_hhvx1dxgy6gb5510pxj80000gn/T/jl_fPnuhi/model.mzn:4.12-43
"

* Candidate solution (result #1)
  Primal status      : NO_SOLUTION
  Dual status        : NO_SOLUTION

* Work counters
  Solve time (sec)   : 1.20307e+00