MiniZinc.jl
MiniZinc.jl copied to clipboard
A Julia interface to the MiniZinc constraint modeling language
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> 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()
([1, 1, 3], [0, 1])
Use with JuMP
You can also call MiniZinc from JuMP, using any solver that libminizinc
supports. By default, MiniZinc.jl is compiled with the
HiGHS MILP solver,
which can be selected by passing the "highs" parameter to MiniZinc.Optimizer:
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)
MathOptInterface API
The MiniZinc Optimizer{T} supports the following constraints and attributes.
List of supported objective functions:
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{T}}MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{T}}MOI.ObjectiveFunction{MOI.VariableIndex}
List of supported variable types:
MOI.Reals
List of supported constraint types:
MOI.ScalarAffineFunction{T}inMOI.EqualTo{T}MOI.ScalarAffineFunction{T}inMOI.GreaterThan{T}MOI.ScalarAffineFunction{T}inMOI.IntegerMOI.ScalarAffineFunction{T}inMOI.Interval{T}MOI.ScalarAffineFunction{T}inMOI.LessThan{T}MOI.ScalarAffineFunction{T}inMOI.ZeroOneMOI.VariableIndexinMOI.EqualTo{T}MOI.VariableIndexinMOI.GreaterThan{T}MOI.VariableIndexinMOI.IntegerMOI.VariableIndexinMOI.Interval{T}MOI.VariableIndexinMOI.LessThan{T}MOI.VariableIndexinMOI.Parameter{T}MOI.VariableIndexinMOI.Semicontinuous{T}MOI.VariableIndexinMOI.Semiinteger{T}MOI.VariableIndexinMOI.ZeroOneMOI.VectorOfVariablesinMOI.AllDifferentMOI.VectorOfVariablesinMOI.BinPacking{T}MOI.VectorOfVariablesinMOI.CircuitMOI.VectorOfVariablesinMOI.CountAtLeastMOI.VectorOfVariablesinMOI.CountBelongsMOI.VectorOfVariablesinMOI.CountDistinctMOI.VectorOfVariablesinMOI.CountGreaterThanMOI.VectorOfVariablesinMOI.CumulativeMOI.VectorOfVariablesinMOI.PathMOI.VectorOfVariablesinMOI.Table{T}
List of supported model attributes:
MOI.NLPBlock()MOI.Name()MOI.ObjectiveSense()
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.mznfile during optimization. This option can be helpful during debugging. If left empty, a temporary file will be used instead.