MiniZinc.jl
MiniZinc.jl is a wrapper for the MiniZinc constraint modeling language.
It provides a way to write MathOptInterface models to .mzn
files, and a way to interact with libminizinc
.
Affiliation
This wrapper is maintained by the JuMP community and is not part of the MiniZinc project.
License
MiniZinc.jl
is licensed under the MIT License.
The underlying project, MiniZinc/libminizinc, is licensed under the MPL 2.0 license.
Install
Install MiniZinc.jl using the Julia package manager:
import Pkg
Pkg.add("MiniZinc")
Windows
On Linux and macOS, this package automatically installs libminizinc
. However, we're still working out problems with the install on Windows. To use MiniZinc.jl, you'll need to manually install a copy of libminizinc
from minizinc.org or compile one yourself from MiniZinc/libminizinc.
To teach MiniZinc.jl where to look for libminizinc
, set the JULIA_LIBMINIZINC_DIR
environment variable. For example:
ENV["JULIA_LIBMINIZINC_DIR"] = "C:\\Program Files\\MiniZinc"
Use with MathOptInterface
MiniZinc.jl supports the constraint programming sets defined in MathOptInterface, as well as (in)equality constraints.
The following example solves the following constraint program:
xᵢ ∈ {1, 2, 3} ∀i=1,2,3
zⱼ ∈ {0, 1} ∀j=1,2
z₁ <-> x₁ != x₂
z₂ <-> x₂ != x₃
z₁ + z₂ = 1
julia> import MiniZinc
julia> const MOI = MiniZinc.MOI
MathOptInterface
julia> function main()
model = MOI.Utilities.CachingOptimizer(
MiniZinc.Model{Int}(),
MiniZinc.Optimizer{Int}(MiniZinc.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()
([1, 1, 3], [0, 1])
Use with JuMP
You can also call MiniZinc from JuMP, using any solver that libminizinc
supports. For example, if you have Gurobi installed:
using JuMP
import MiniZinc
model = Model(() -> MiniZinc.Optimizer{Float64}("gurobi"))
@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)
MathOptInterface API
The MiniZinc optimizer supports the following constraints and attributes.
List of supported objective functions:
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{Float64}}
MOI.ObjectiveFunction{MOI.VariableIndex}
List of supported variable types:
List of supported constraint types:
MOI.ScalarAffineFunction{Float64}
inMOI.EqualTo{Float64}
MOI.ScalarAffineFunction{Float64}
inMOI.GreaterThan{Float64}
MOI.ScalarAffineFunction{Float64}
inMOI.Integer
MOI.ScalarAffineFunction{Float64}
inMOI.Interval{Float64}
MOI.ScalarAffineFunction{Float64}
inMOI.LessThan{Float64}
MOI.ScalarAffineFunction{Float64}
inMOI.ZeroOne
MOI.VariableIndex
inMOI.EqualTo{Float64}
MOI.VariableIndex
inMOI.GreaterThan{Float64}
MOI.VariableIndex
inMOI.Integer
MOI.VariableIndex
inMOI.Interval{Float64}
MOI.VariableIndex
inMOI.LessThan{Float64}
MOI.VariableIndex
inMOI.Parameter{Float64}
MOI.VariableIndex
inMOI.Semicontinuous{Float64}
MOI.VariableIndex
inMOI.Semiinteger{Float64}
MOI.VariableIndex
inMOI.ZeroOne
MOI.VectorOfVariables
inMOI.AllDifferent
MOI.VectorOfVariables
inMOI.BinPacking{Float64}
MOI.VectorOfVariables
inMOI.Circuit
MOI.VectorOfVariables
inMOI.CountAtLeast
MOI.VectorOfVariables
inMOI.CountBelongs
MOI.VectorOfVariables
inMOI.CountDistinct
MOI.VectorOfVariables
inMOI.CountGreaterThan
MOI.VectorOfVariables
inMOI.Cumulative
MOI.VectorOfVariables
inMOI.Path
MOI.VectorOfVariables
inMOI.Table{Float64}
List of supported model attributes:
Options
Set options using MOI.RawOptimizerAttribute
in MOI or set_attribute
in JuMP.
MiniZinc.jl
supports the following options:
model_filename::String = ""
: the location at which to write out the.mzn
file during optimization. This option can be helpful during debugging. If left empty, a temporary file will be used instead.