Skip to content

Constraints.jl: Streamlining Constraint Definition and Integration in Julia

julia
using Constraints

concept(:maximum, [1,1,1,2], val = 2, op = ==) # true
concept(:maximum, [1,2,4,4], val = 2, op = ==) # false
julia
using Constraints

c = x -> Constraints.xcsp_maximum(
    list = x,
		condition = (==, 4)
)

@info c([1, 1, 4, 1]) # true
@info c([1, 2, 3, 8]) # false
julia
using CBLS, JuMP

model = Model(CBLS.Optimizer)
@variable(model, 1X[1:5]5, Int)
@constraint(model, X in Maximum(; op = ==, val = 5))
optimize!(model)
@info "Maximum" value.(X)
julia
# TODO: How to handle intention in JuMP/MOI
julia
using Constraints

concept(:minimum, [1,1,1,2], val = 1, op = ==) # true
concept(:minimum, [1,2,4,4], val = 2, op = ==) # false
julia
using Constraints

c = x -> Constraints.xcsp_minimum(
    list = x,
    condition = (==, 1)
)

@info c([1, 1, 4, 1])
@info c([0, 2, 3, 8])
julia
using CBLS, JuMP

model = Model(CBLS.Optimizer)
@variable(model, 1X[1:5]5, Int)
@constraint(model, X in Minimum(; op = ==, val = 3))
JuMP.optimize!(model)
@info "Minimum" value.(X)

# Note that this example gives a solution for the minimum constraint.
julia
# TODO: How to handle intention in JuMP/MOI
julia
using Constraints

@info concept(:element, [1, 2, 3, 4, 5]; id=1, val=1)
@info concept(:element, [1, 2, 3, 4, 5]; id=1, val=2)
@info concept(:element, [1, 2, 3, 4, 2])
@info concept(:element, [1, 2, 3, 4, 1])
julia
using Constraints

c = x -> Constraints.xcsp_element(
	list = x,
	index = 1,
	condition = (==, 3)
)

@info c([3, 3, 10])
@info c([1, 1, 4, 3])
julia
using CBLS, JuMP

model = Model(CBLS.Optimizer)
@variable(model, 1X[1:5]5, Int)
@variable(model, 1Y[1:5]5, Int)
@variable(model, 0Z[1:5]5, Int)
@constraint(model, X in Element())
@constraint(model, Y in Element(; id = 1, val = 1))
@constraint(model, Z in Element(; id = 2, val = 2))
JuMP.optimize!(model)
@info "Element" value.(X) value.(Y) value.(Z)
julia
# TODO: How to handle intention in JuMP/MOI
julia
using Constraints

@info concept(:channel, [2, 1, 4, 3])
@info concept(:channel, [1, 2, 3, 4])
@info concept(:channel, [2, 3, 1, 4])
@info concept(:channel, [2, 1, 5, 3, 4, 2, 1, 4, 5, 3]; dim=2)
@info concept(:channel, [2, 1, 4, 3, 5, 2, 1, 4, 5, 3]; dim=2)
@info concept(:channel, [false, false, true, false]; id=3)
@info concept(:channel, [false, false, true, false]; id=1)
julia
using Constraints

c = x -> Constraints.xcsp_channel(
	list = x
)

@info c([2, 1, 4, 3])
@info c([2, 3, 1, 4])
julia
using CBLS, JuMP

model = Model(CBLS.Optimizer)
@variable(model, 1X[1:4]4, Int)
@variable(model, 1Y[1:10]5, Int)
@variable(model, 0Z[1:4]1, Int)
@constraint(model, X in CBLS.Channel())
@constraint(model, Y in CBLS.Channel(; dim = 2))
@constraint(model, Z in CBLS.Channel(; id = 3))
JuMP.optimize!(model)
@info "Channel" value.(X) value.(Y) value.(Z)
julia
# TODO: How to handle intention in JuMP/MOI

Connection Constraints

# Constraints.xcsp_maximumFunction.
julia
xcsp_maximum(; list, condition)

Return true if the maximum constraint is satisfied, false otherwise. The maximum constraint is a global constraint used in constraint programming that specifies that a certain condition should hold for the maximum value in a list of variables.

Arguments

  • list::Union{AbstractVector, Tuple}: list of values to check.

  • condition::Tuple: condition to check.

Variants

  • :maximum: The maximum constraint is a global constraint used in constraint programming that specifies that a certain condition should hold for the maximum value in a list of variables.
julia
concept(:maximum, x; op, val)
concept(:maximum)(x; op, val)

Examples

julia
c = concept(:maximum)

c([1, 2, 3, 4, 5]; op = ==, val = 5)
c([1, 2, 3, 4, 5]; op = ==, val = 6)

source


# Constraints.xcsp_minimumFunction.
julia
xcsp_minimum(; list, condition)

Return true if the minimum constraint is satisfied, false otherwise. The minimum constraint is a global constraint used in constraint programming that specifies that a certain condition should hold for the minimum value in a list of variables.

Arguments

  • list::Union{AbstractVector, Tuple}: list of values to check.

  • condition::Tuple: condition to check.

Variants

  • :minimum: The minimum constraint is a global constraint used in constraint programming that specifies that a certain condition should hold for the minimum value in a list of variables.
julia
concept(:minimum, x; op, val)
concept(:minimum)(x; op, val)

Examples

julia
c = concept(:minimum)

c([1, 2, 3, 4, 5]; op = ==, val = 1)
c([1, 2, 3, 4, 5]; op = ==, val = 0)

source


# Constraints.xcsp_elementFunction.
julia
xcsp_element(; list, index, condition)

Return true if the element constraint is satisfied, false otherwise. The element constraint is a global constraint used in constraint programming that specifies that the value of a variable should be equal to the value of another variable indexed by a third variable.

Arguments

  • list::Union{AbstractVector, Tuple}: list of values to check.

  • index::Int: index of the value to check.

  • condition::Tuple: condition to check.

Variants

  • :element: The element constraint is a global constraint used in constraint programming that specifies that the value of a variable should be equal to the value of another variable indexed by a third variable.
julia
concept(:element, x; id=nothing, op===, val=nothing)
concept(:element)(x; id=nothing, op===, val=nothing)

Examples

julia
c = concept(:element)

c([1, 2, 3, 4, 5]; id=1, val=1)
c([1, 2, 3, 4, 5]; id=1, val=2)
c([1, 2, 3, 4, 2])
c([1, 2, 3, 4, 1])

source


# Constraints.xcsp_channelFunction.
julia
xcsp_channel(; list)

Return true if the channel constraint is satisfied, false otherwise. The channel constraint establishes a bijective correspondence between two sets of variables. This means that each value in the first set of variables corresponds to a unique value in the second set, and vice versa.

Arguments

  • list::Union{AbstractVector, Tuple}: list of values to check.

Variants

  • :channel: The channel constraint establishes a bijective correspondence between two sets of variables. This means that each value in the first set of variables corresponds to a unique value in the second set, and vice versa.
julia
concept(:channel, x; dim=1, id=nothing)
concept(:channel)(x; dim=1, id=nothing)

Examples

julia
c = concept(:channel)

c([2, 1, 4, 3])
c([1, 2, 3, 4])
c([2, 3, 1, 4])
c([2, 1, 5, 3, 4, 2, 1, 4, 5, 3]; dim=2)
c([2, 1, 4, 3, 5, 2, 1, 4, 5, 3]; dim=2)
c([false, false, true, false]; id=3)
c([false, false, true, false]; id=1)

source