Constraints.jl: Streamlining Constraint Definition and Integration in Julia
using Constraints
concept(:maximum, [1,1,1,2], val = 2, op = ==) # true
concept(:maximum, [1,2,4,4], val = 2, op = ==) # false
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
using CBLS, JuMP
model = Model(CBLS.Optimizer)
@variable(model, 1≤X[1:5]≤5, Int)
@constraint(model, X in Maximum(; op = ==, val = 5))
optimize!(model)
@info "Maximum" value.(X)
# TODO: How to handle intention in JuMP/MOI
using Constraints
concept(:minimum, [1,1,1,2], val = 1, op = ==) # true
concept(:minimum, [1,2,4,4], val = 2, op = ==) # false
using Constraints
c = x -> Constraints.xcsp_minimum(
list = x,
condition = (==, 1)
)
@info c([1, 1, 4, 1])
@info c([0, 2, 3, 8])
using CBLS, JuMP
model = Model(CBLS.Optimizer)
@variable(model, 1≤X[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.
# TODO: How to handle intention in JuMP/MOI
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])
using Constraints
c = x -> Constraints.xcsp_element(
list = x,
index = 1,
condition = (==, 3)
)
@info c([3, 3, 10])
@info c([1, 1, 4, 3])
using CBLS, JuMP
model = Model(CBLS.Optimizer)
@variable(model, 1≤X[1:5]≤5, Int)
@variable(model, 1≤Y[1:5]≤5, Int)
@variable(model, 0≤Z[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)
# TODO: How to handle intention in JuMP/MOI
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)
using Constraints
c = x -> Constraints.xcsp_channel(
list = x
)
@info c([2, 1, 4, 3])
@info c([2, 3, 1, 4])
using CBLS, JuMP
model = Model(CBLS.Optimizer)
@variable(model, 1≤X[1:4]≤4, Int)
@variable(model, 1≤Y[1:10]≤5, Int)
@variable(model, 0≤Z[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)
# TODO: How to handle intention in JuMP/MOI
Connection Constraints
Constraints.xcsp_maximum Function
xcsp_maximum(; list, condition)
Return true
if the maximum constraint is satisfied, false
otherwise. The maximum constraint is a global constraint specifying 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
: Global constraint ensuring that a certain numerical condition holds for the maximum value inx
.
concept(:maximum, x; op, val)
concept(:maximum)(x; op, val)
Examples
c = concept(:maximum)
c([1, 2, 3, 4, 5]; op = ==, val = 5)
c([1, 2, 3, 4, 5]; op = ==, val = 6)
Constraints.xcsp_minimum Function
xcsp_minimum(; list, condition)
Return true
if the minimum constraint is satisfied, false
otherwise. The minimum constraint is a global constraint specifying 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
: Global constraint ensuring that a certain numerical condition holds for the minimum value inx
.
concept(:minimum, x; op, val)
concept(:minimum)(x; op, val)
Examples
c = concept(:minimum)
c([1, 2, 3, 4, 5]; op = ==, val = 1)
c([1, 2, 3, 4, 5]; op = ==, val = 0)
Constraints.xcsp_element Function
xcsp_element(; list, index, condition)
Return true
if the element constraint is satisfied, false
otherwise. The element constraint is a global constraint specifying that a variable in x
indexed by id
should be equal to a value
.
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
: Global constraint specifying that a variable inx
indexed byid
should be equal to avalue
.
concept(:element, x; id=nothing, op===, val=nothing)
concept(:element)(x; id=nothing, op===, val=nothing)
Examples
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])
Constraints.xcsp_channel Function
xcsp_channel(; list)
Return true
if the channel constraint is satisfied, false
otherwise. The channel constraint ensures that if the i-th element of list
is assigned the value j, then the j-th element of list
must be assigned the value i.
Arguments
list::Union{AbstractVector, Tuple}
: list of values to check.
Variants
:channel
: Ensures that if the i-th element ofx
is assigned the value j, then the j-th element ofx
must be assigned the value i.
concept(:channel, x; dim=1, id=nothing)
concept(:channel)(x; dim=1, id=nothing)
Examples
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)