Constraints.jl: Streamlining Constraint Definition and Integration in Julia
julia
using Constraints
concept(:all_different, [1,1,1,2]) # false
concept(:all_different, [1,9,3,2]) # true
julia
using Constraints
c = x -> Constraints.xcsp_all_different(
list = x
)
@info c([1, 2, 3, 3]) # false
@info c([1, 2, 3, 4]) # true
julia
using CBLS, JuMP
model = Model(CBLS.Optimizer)
@variable(model, 1≤X[1:4]≤4, Int)
@variable(model, 0≤Y[1:4]≤2, Int)
@constraint(model, X in AllDifferent())
@constraint(model, Y in AllDifferent(; vals = [0]))
JuMP.optimize!(model)
@info "All Different" value.(X) value.(Y)
# Note that this example gives a solution for the all_different constraint.
julia
# TODO: How to handle intention in JuMP/MOI
julia
using Constraints
concept(:all_equal, [1,1,1,2]) #false
concept(:all_equal, [1,1,1,1]) #true
julia
using Constraints
c = x -> Constraints.xcsp_all_equal(
list = x
)
@info c([1, 1, 1, 1]) # false
@info c([1, 2, 3, 4]) # true
julia
using JuMP, CBLS
model = Model(CBLS.Optimizer)
@variable(model, 0≤X[1:4]≤4, Int)
@constraint(model, X in AllEqual())
JuMP.optimize!(model)
@info "All Equal" value.(X)
# Note that this example gives a solution for the all_equal constraint.
julia
# TODO: How to handle intention in JuMP/MOI
julia
using Constraints
@info concept(:ordered, [1, 2, 3, 4, 4]; op=≤)
@info concept(:ordered, [1, 2, 3, 3, 5]; op=<)
@info concept(:increasing, [1,2,2,3])
julia
using Constraints
c = x -> Constraints.xcsp_ordered(
list = x,
operator = ≥
)
@info c([1, 1, 1, 1])
@info c([9, 3, 6, 8])
julia
using CBLS, JuMP
model = Model(CBLS.Optimizer)
@variable(model, 1≤X[1:5]≤5, Int)
@variable(model, 1≤Y[1:5]≤5, Int)
@constraint(model, X in Ordered())
@constraint(model, Y in Ordered(; op = <))
JuMP.optimize!(model)
@info "Ordered" value.(X) value.(Y)
julia
# TODO: How to handle intention in JuMP/MOI
Comparison-based Constraints
Constraints.xcsp_all_different Function
julia
xcsp_all_different(list::Vector{Int})
Return true
if all the values of list
are different, false
otherwise.
Arguments
list::Vector{Int}
: list of values to check.
Variants
:all_different
: Global constraint ensuring that the values inx
are all different.
julia
concept(:all_different, x; vals)
concept(:all_different)(x; vals)
Examples
julia
c = concept(:all_different)
c([1, 2, 3, 4])
c([1, 2, 3, 1])
c([1, 0, 0, 4]; vals=[0])
c([1, 0, 0, 1]; vals=[0])
Constraints.xcsp_all_equal Function
julia
xcsp_all_equal(list::Vector{Int}, val::Int)
Return true
if all the values of list
are equal to val
, false
otherwise.
Arguments
list::Vector{Int}
: list of values to check.val::Int
: value to compare to.
Variants
:all_equal
: Global constraint ensuring that the values inx
are all equal.
julia
concept(:all_equal, x; val=nothing, pair_vars=zeros(x), op=+)
concept(:all_equal)(x; val=nothing, pair_vars=zeros(x), op=+)
Examples
julia
c = concept(:all_equal)
c([0, 0, 0, 0])
c([1, 2, 3, 4])
c([3, 2, 1, 0]; pair_vars=[0, 1, 2, 3])
c([0, 1, 2, 3]; pair_vars=[0, 1, 2, 3])
c([1, 2, 3, 4]; op=/, val=1, pair_vars=[1, 2, 3, 4])
c([1, 2, 3, 4]; op=*, val=1, pair_vars=[1, 2, 3, 4])
Constraints.xcsp_ordered Function
julia
xcsp_ordered(list::Vector{Int}, operator, lengths)
Return true
if all the values of list
are in an increasing order, false
otherwise.
Arguments
list::Vector{Int}
: list of values to check.operator
: comparison operator to use.lengths
: list of lengths to use. Defaults tonothing
.
Variants
:ordered
: Global constraint ensuring that all the values ofx
are in a total order defined by a comparison operator.
julia
concept(:ordered, x; op=≤, pair_vars=nothing)
concept(:ordered)(x; op=≤, pair_vars=nothing)
:increasing
: Global constraint ensuring that all the values ofx
are in an increasing order.
julia
concept(:increasing, x; op=≤, pair_vars=nothing)
concept(:increasing)(x; op=≤, pair_vars=nothing)
:decreasing
: Global constraint ensuring that all the values ofx
are in a decreasing order.
julia
concept(:decreasing, x; op=≥, pair_vars=nothing)
concept(:decreasing)(x; op=≥, pair_vars=nothing)
:strictly_increasing
: Global constraint ensuring that all the values ofx
are in a strictly increasing order.
julia
concept(:strictly_increasing, x; op=<, pair_vars=nothing)
concept(:strictly_increasing)(x; op=<, pair_vars=nothing)
:strictly_decreasing
: Global constraint ensuring that all the values ofx
are in a strictly decreasing order.
julia
concept(:strictly_decreasing, x; op=>, pair_vars=nothing)
concept(:strictly_decreasing)(x; op=>, pair_vars=nothing)
Examples
julia
c = concept(:ordered)
c([1, 2, 3, 4, 4]; op=≤)
c([1, 2, 3, 4, 5]; op=<)
!c([1, 2, 3, 4, 3]; op=≤)
!c([1, 2, 3, 4, 3]; op=<)