Constraints.USUAL_CONSTRAINTS
— ConstantUSUAL_CONSTRAINTS::Dict
Dictionary that contains all the usual constraints defined in Constraint.jl. It is based on XCSP3-core specifications available at https://arxiv.org/abs/2009.00514
Adding a new constraint is as simple as defining a new function with the same name as the constraint and using the @usual
macro to define it. The macro will take care of adding the new constraint to the USUAL_CONSTRAINTS
dictionary.
Example
@usual concept_all_different(x; vals=nothing) = xcsp_all_different(list=x, except=vals)
Constraints.USUAL_SYMMETRIES
— ConstantUSUAL_SYMMETRIES
A Dictionary that contains the function to apply for each symmetry to avoid searching a whole space.
Constraints.Constraint
— TypeConstraint
Parametric structure with the following fields.
concept
: a Boolean function that, given an assignmentx
, outputstrue
ifx
satisfies the constraint, andfalse
otherwise.error
: a positive function that works as preferences over invalid assignments. Return0.0
if the constraint is satisfied, and a strictly positive real otherwise.
ConstraintCommons.extract_parameters
— Functionextract_parameters(s::Symbol, constraints_dict=USUAL_CONSTRAINTS; parameters=ConstraintCommons.USUAL_CONSTRAINT_PARAMETERS)
Return the parameters of the constraint s
in constraints_dict
.
Arguments
s::Symbol
: the constraint name.constraints_dict::Dict{Symbol,Constraint}
: dictionary of constraints. Default isUSUAL_CONSTRAINTS
.parameters::Vector{Symbol}
: vector of parameters. Default isConstraintCommons.USUAL_CONSTRAINT_PARAMETERS
.
Example
extract_parameters(:all_different)
Constraints.args
— Methodargs(c::Constraint)
Return the expected length restriction of the arguments in a constraint c
. The value nothing
indicates that any strictly positive number of value is accepted.
Constraints.concept
— Methodconcept(c::Constraint)
Return the concept (function) of constraint c
. concept(c::Constraint, x...; param = nothing) Apply the concept of c
to values x
and optionally param
.
Constraints.concept
— Methodconcept(s::Symbol, args...; kargs...)
Return the concept of the constraint s
applied to args
and kargs
. This is a shortcut for concept(USUAL_CONSTRAINTS[s])(args...; kargs...)
.
Arguments
s::Symbol
: the constraint name.args...
: the arguments to apply the concept to.kargs...
: the keyword arguments to apply the concept to.
Example
concept(:all_different, [1, 2, 3])
Constraints.concept_vs_error
— Methodconcept_vs_error(c, e, args...; kargs...)
Compare the results of a concept function and an error function for the same inputs. It is mainly used for testing purposes.
Arguments
c
: The concept function.e
: The error function.args...
: Positional arguments to be passed to both the concept and error functions.kargs...
: Keyword arguments to be passed to both the concept and error functions.
Returns
- Boolean: Returns true if the result of the concept function is not equal to whether the result of the error function is greater than 0.0. Otherwise, it returns false.
Examples
concept_vs_error(all_different, make_error(:all_different), [1, 2, 3]) # Returns false
Constraints.constraints_descriptions
— Functionconstraints_descriptions(C=USUAL_CONSTRAINTS)
Return a pretty table with the descriptions of the constraints in C
.
Arguments
C::Dict{Symbol,Constraint}
: dictionary of constraints. Default isUSUAL_CONSTRAINTS
.
Example
constraints_descriptions()
Constraints.constraints_parameters
— Functionconstraints_parameters(C=USUAL_CONSTRAINTS)
Return a pretty table with the parameters of the constraints in C
.
Arguments
C::Dict{Symbol,Constraint}
: dictionary of constraints. Default isUSUAL_CONSTRAINTS
.
Example
constraints_parameters()
Constraints.describe
— Functiondescribe(constraints::Dict{Symbol,Constraint}=USUAL_CONSTRAINTS; width=150)
Return a pretty table with the description of the constraints in constraints
.
Arguments
constraints::Dict{Symbol,Constraint}
: dictionary of constraints to describe. Default isUSUAL_CONSTRAINTS
.width::Int
: width of the table.
Example
describe()
Constraints.error_f
— Methoderror_f(c::Constraint)
Return the error function of constraint c
. error_f(c::Constraint, x; param = nothing) Apply the error function of c
to values x
and optionally param
.
Constraints.make_error
— Methodmake_error(symb::Symbol)
Create a function that returns an error based on the predicate of the constraint identified by the symbol provided.
Arguments
symb::Symbol
: The symbol used to determine the error function to be returned. The function first checks if a predicate with the prefix "icn" exists in the Constraints module. If it does, it returns that function. If it doesn't, it checks for a predicate with the prefix "error". If that exists, it returns that function. If neither exists, it returns a function that evaluates the predicate with the prefix "concept_" and returns the negation of its result cast to Float64.
Returns
- Function: A function that takes in a variable
x
and an arbitrary number of parametersparams
. The function returns a Float64.
Examples
e = make_error(:all_different)
e([1, 2, 3]) # Returns 0.0
e([1, 1, 3]) # Returns 1.0
Constraints.params_length
— Methodparams_length(c::Constraint)
Return the expected length restriction of the arguments in a constraint c
. The value nothing
indicates that any strictly positive number of parameters is accepted.
Constraints.shrink_concept
— Methodshrink_concept(s)
Simply delete the concept_
part of symbol or string starting with it. TODO: add a check with a warning if s
starts with something different.
Constraints.symmetries
— Methodsymmetries(c::Constraint)
Return the list of symmetries of c
.
Constraints.xcsp_all_different
— Methodxcsp_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 all the values ofx
are all different.
concept(:all_different, x; vals)
concept(:all_different)(x; vals)
Examples
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
— Methodxcsp_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 all the values ofx
are all equal.
concept(:all_equal, x; val=nothing, pair_vars=zeros(x), op=+)
concept(:all_equal)(x; val=nothing, pair_vars=zeros(x), op=+)
Examples
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_cardinality
— Methodxcsp_cardinality(list, values, occurs, closed)
Return true
if the number of occurrences of the values in values
in list
satisfies the given condition, false
otherwise.
Arguments
list::Vector{Int}
: list of values to check.values::Vector{Int}
: list of values to check.occurs::Vector{Int}
: list of occurrences to check.closed::Bool
: whether the constraint is closed or not.
Variants
:cardinality
: The cardinality constraint, also known as the global cardinality constraint (GCC), is a constraint in constraint programming that restricts the number of times a value can appear in a set of variables.
concept(:cardinality, x; bool=false, vals)
concept(:cardinality)(x; bool=false, vals)
:cardinality_closed
: The closed cardinality constraint, also known as the global cardinality constraint (GCC), is a constraint in constraint programming that restricts the number of times a value can appear in a set of variables. It is closed, meaning that all values in the domain of the variables must be considered.
concept(:cardinality_closed, x; vals)
concept(:cardinality_closed)(x; vals)
:cardinality_open
: The open cardinality constraint, also known as the global cardinality constraint (GCC), is a constraint in constraint programming that restricts the number of times a value can appear in a set of variables. It is open, meaning that only the values in the list of values must be considered.
concept(:cardinality_open, x; vals)
concept(:cardinality_open)(x; vals)
Examples
c = concept(:cardinality)
c([2, 5, 10, 10]; vals=[2 0 1; 5 1 3; 10 2 3])
c([8, 5, 10, 10]; vals=[2 0 1; 5 1 3; 10 2 3], bool=false)
c([8, 5, 10, 10]; vals=[2 0 1; 5 1 3; 10 2 3], bool=true)
c([2, 5, 10, 10]; vals=[2 1; 5 1; 10 2])
c([2, 5, 10, 10]; vals=[2 0 1 42; 5 1 3 7; 10 2 3 -4])
c([2, 5, 5, 10]; vals=[2 0 1; 5 1 3; 10 2 3])
c([2, 5, 10, 8]; vals=[2 1; 5 1; 10 2])
c([5, 5, 5, 10]; vals=[2 0 1 42; 5 1 3 7; 10 2 3 -4])
cc = concept(:cardinality_closed)
cc([8, 5, 10, 10]; vals=[2 0 1; 5 1 3; 10 2 3])
co = concept(:cardinality_open)
co([8, 5, 10, 10]; vals=[2 0 1; 5 1 3; 10 2 3])
Constraints.xcsp_channel
— Methodxcsp_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.
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)
Constraints.xcsp_circuit
— Methodxcsp_circuit(; list, size)
Return true
if the circuit constraint is satisfied, false
otherwise. The circuit constraint is a global constraint used in constraint programming, often in routing problems. It ensures that the values of a list of variables form a circuit, i.e., a sequence where each value is the index of the next value in the sequence, and the sequence eventually loops back to the start.
Arguments
list::AbstractVector
: list of values to check.size::Int
: size of the circuit.
Variants
:circuit
: The circuit constraint is a global constraint used in constraint programming, often in routing problems. It ensures that the values of a list of variables form a circuit, i.e., a sequence where each value is the index of the next value in the sequence, and the sequence eventually loops back to the start.
concept(:circuit, x; op, val)
concept(:circuit)(x; op, val)
Examples
c = concept(:circuit)
c([1, 2, 3, 4])
c([2, 3, 4, 1])
c([2, 3, 1, 4]; op = ==, val = 3)
c([4, 3, 1, 3]; op = >, val = 0)
Constraints.xcsp_count
— Methodxcsp_count(list, values, condition)
Return true
if the number of occurrences of the values in values
in list
satisfies the given condition, false
otherwise.
Arguments
list::Vector{Int}
: list of values to check.values::Vector{Int}
: list of values to check.condition
: condition to satisfy.
Variants
:count
: Constraint ensuring that the number of occurrences of the values invals
inx
satisfies the given condition.
concept(:count, x; vals, op, val)
concept(:count)(x; vals, op, val)
:at_least
: Constraint ensuring that the number of occurrences of the values invals
inx
is at leastval
.
concept(:at_least, x; vals, val)
concept(:at_least)(x; vals, val)
:at_most
: Constraint ensuring that the number of occurrences of the values invals
inx
is at mostval
.
concept(:at_most, x; vals, val)
concept(:at_most)(x; vals, val)
:exactly
: Constraint ensuring that the number of occurrences of the values invals
inx
is exactlyval
.
concept(:exactly, x; vals, val)
concept(:exactly)(x; vals, val)
Examples
c = concept(:count)
c([2, 1, 4, 3]; vals=[1, 2, 3, 4], op=≥, val=2)
c([1, 2, 3, 4]; vals=[1, 2], op==, val=2)
c([2, 1, 4, 3]; vals=[1, 2], op=≤, val=1)
Constraints.xcsp_cumulative
— Methodxcsp_cumulative(; origins, lengths, heights, condition)
Return true
if the cumulative constraint is satisfied, false
otherwise. The cumulative constraint is a global constraint used in constraint programming that is often used in scheduling problems. It ensures that for any point in time, the sum of the "heights" of tasks that are ongoing at that time does not exceed a certain limit.
Arguments
origins::AbstractVector
: list of origins of the tasks.lengths::AbstractVector
: list of lengths of the tasks.heights::AbstractVector
: list of heights of the tasks.condition::Tuple
: condition to check.
Variants
:cumulative
: The cumulative constraint is a global constraint used in constraint programming that is often used in scheduling problems. It ensures that for any point in time, the sum of the "heights" of tasks that are ongoing at that time does not exceed a certain limit.
concept(:cumulative, x; pair_vars, op, val)
concept(:cumulative)(x; pair_vars, op, val)
Examples
c = concept(:cumulative)
c([1, 2, 3, 4, 5]; val = 1)
c([1, 2, 2, 4, 5]; val = 1)
c([1, 2, 3, 4, 5]; pair_vars = [3 2 5 4 2; 1 2 1 1 3], op = ≤, val = 5)
c([1, 2, 3, 4, 5]; pair_vars = [3 2 5 4 2; 1 2 1 1 3], op = <, val = 5)
Constraints.xcsp_element
— Methodxcsp_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.
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_extension
— Methodxcsp_extension(; list, supports=nothing, conflicts=nothing)
Global constraint enforcing that the tuple x
matches a configuration within the supports set pair_vars[1]
or does not match any configuration within the conflicts set pair_vars[2]
. It embodies the logic: x ∈ pair_vars[1] || x ∉ pair_vars[2]
, providing a comprehensive way to define valid (supported) and invalid (conflicted) tuples for constraint satisfaction problems. This constraint is versatile, allowing for the explicit delineation of both acceptable and unacceptable configurations.
Arguments
list::Vector{Int}
: A list of variablessupports::Vector{Vector{Int}}
: A set of supported tuples. Default to nothing.conflicts::Vector{Vector{Int}}
: A set of conflicted tuples. Default to nothing.
Variants
:extension
: Global constraint enforcing that the tuplex
matches a configuration within the supports setpair_vars[1]
or does not match any configuration within the conflicts setpair_vars[2]
. It embodies the logic:x ∈ pair_vars[1] || x ∉ pair_vars[2]
, providing a comprehensive way to define valid (supported) and invalid (conflicted) tuples for constraint satisfaction problems. This constraint is versatile, allowing for the explicit delineation of both acceptable and unacceptable configurations.
concept(:extension, x; pair_vars)
concept(:extension)(x; pair_vars)
:supports
: Global constraint ensuring that the tuplex
matches a configuration listed within the support setpair_vars
. This constraint is derived from the extension model, specifying thatx
must be one of the explicitly defined supported configurations:x ∈ pair_vars
. It is utilized to directly declare the tuples that are valid and should be included in the solution space.
concept(:supports, x; pair_vars)
concept(:supports)(x; pair_vars)
:conflicts
: Global constraint ensuring that the tuplex
does not match any configuration listed within the conflict setpair_vars
. This constraint, originating from the extension model, stipulates thatx
must avoid all configurations defined as conflicts:x ∉ pair_vars
. It is useful for specifying tuples that are explicitly forbidden and should be excluded from the solution space.
concept(:conflicts, x; pair_vars)
concept(:conflicts)(x; pair_vars)
Examples
c = concept(:extension)
c([1, 2, 3, 4, 5]; pair_vars=[[1, 2, 3, 4, 5]])
c([1, 2, 3, 4, 5]; pair_vars=([[1, 2, 3, 4, 5]], [[1, 2, 1, 4, 5], [1, 2, 3, 5, 5]]))
c([1, 2, 3, 4, 5]; pair_vars=[[1, 2, 1, 4, 5], [1, 2, 3, 5, 5]])
c = concept(:supports)
c([1, 2, 3, 4, 5]; pair_vars=[[1, 2, 3, 4, 5]])
c = concept(:conflicts)
c([1, 2, 3, 4, 5]; pair_vars=[[1, 2, 1, 4, 5], [1, 2, 3, 5, 5]])
Constraints.xcsp_instantiation
— Methodxcsp_instantiation(; list, values)
Return true
if the instantiation constraint is satisfied, false
otherwise. The instantiation constraint is a global constraint used in constraint programming that ensures that a list of variables takes on a specific set of values in a specific order.
Arguments
list::AbstractVector
: list of values to check.values::AbstractVector
: list of values to check against.
Variants
:instantiation
: The instantiation constraint is a global constraint used in constraint programming that ensures that a list of variables takes on a specific set of values in a specific order.
concept(:instantiation, x; pair_vars)
concept(:instantiation)(x; pair_vars)
Examples
c = concept(:instantiation)
c([1, 2, 3, 4, 5]; pair_vars=[1, 2, 3, 4, 5])
c([1, 2, 3, 4, 5]; pair_vars=[1, 2, 3, 4, 6])
Constraints.xcsp_intension
— Methodxcsp_intension(list, predicate)
An intensional constraint is usually defined from a predicate
over list
. As such it encompass any generic constraint.
Arguments
list::Vector{Int}
: A list of variablespredicate::Function
: A predicate overlist
Variants
:dist_different
: A constraint ensuring that the distances between marks on the ruler are unique. Specifically, it checks that the distance betweenx[1]
andx[2]
, and the distance betweenx[3]
andx[4]
, are different. This constraint is fundamental in ensuring the validity of a Golomb ruler, where no two pairs of marks should have the same distance between them.
concept(:dist_different, x)
concept(:dist_different)(x)
Examples
2 + 2
2 + 2
using Constraints # hide
c = concept(:dist_different)
c([1, 2, 3, 3]) && !c([1, 2, 3, 4])
using Constraints # hide
c = concept(:dist_different)
c([1, 2, 3, 3]) && !c([1, 2, 3, 4])
Constraints.xcsp_maximum
— Methodxcsp_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.
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_mdd
— Methodxcsp_mdd(; list, diagram)
Return a function that checks if the list of values list
satisfies the MDD diagram
.
Arguments
list::Vector{Int}
: list of values to check.diagram::MDD
: MDD to check.
Variants
:mdd
: Multi-valued Decision Diagram (MDD) constraint.
The MDD constraint is a constraint that can be used to model a wide range of problems. It is a directed graph where each node is labeled with a value and each edge is labeled with a value. The constraint is satisfied if there is a path from the first node to the last node such that the sequence of edge labels is a valid sequence of the value labels.
concept(:mdd, x; language)
concept(:mdd)(x; language)
Examples
c = concept(:mdd)
states = [
Dict( # level x1
(:r, 0) => :n1,
(:r, 1) => :n2,
(:r, 2) => :n3,
),
Dict( # level x2
(:n1, 2) => :n4,
(:n2, 2) => :n4,
(:n3, 0) => :n5,
),
Dict( # level x3
(:n4, 0) => :t,
(:n5, 0) => :t,
),
]
a = MDD(states)
c([0,2,0]; language = a)
c([1,2,0]; language = a)
c([2,0,0]; language = a)
c([2,1,2]; language = a)
c([1,0,2]; language = a)
c([0,1,2]; language = a)
Constraints.xcsp_minimum
— Methodxcsp_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.
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_no_overlap
— Methodxcsp_no_overlap(; origins, lengths, zero_ignored)
Return true
if the nooverlap constraint is satisfied, false
otherwise. The nooverlap constraint is a global constraint used in constraint programming, often in scheduling problems. It ensures that tasks do not overlap in time, i.e., for any two tasks, either the first task finishes before the second task starts, or the second task finishes before the first task starts.
Arguments
origins::AbstractVector
: list of origins of the tasks.lengths::AbstractVector
: list of lengths of the tasks.zero_ignored::Bool
: whether to ignore zero-length tasks.
Variants
:no_overlap
: The no_overlap constraint is a global constraint used in constraint programming, often in scheduling problems. It ensures that tasks do not overlap in time, i.e., for any two tasks, either the first task finishes before the second task starts, or the second task finishes before the first task starts.
concept(:no_overlap, x; pair_vars, bool)
concept(:no_overlap)(x; pair_vars, bool)
:no_overlap_no_zero
: The no_overlap constraint is a global constraint used in constraint programming, often in scheduling problems. It ensures that tasks do not overlap in time, i.e., for any two tasks, either the first task finishes before the second task starts, or the second task finishes before the first task starts. This variant ignores zero-length tasks.
concept(:no_overlap_no_zero, x; pair_vars)
concept(:no_overlap_no_zero)(x; pair_vars)
:no_overlap_with_zero
: The no_overlap constraint is a global constraint used in constraint programming, often in scheduling problems. It ensures that tasks do not overlap in time, i.e., for any two tasks, either the first task finishes before the second task starts, or the second task finishes before the first task starts. This variant includes zero-length tasks.
concept(:no_overlap_with_zero, x; pair_vars)
concept(:no_overlap_with_zero)(x; pair_vars)
Examples
c = concept(:no_overlap)
c([1, 2, 3, 4, 5])
c([1, 2, 3, 4, 1])
c([1, 2, 4, 6, 3]; pair_vars = [1, 1, 1, 1, 1])
c([1, 2, 4, 6, 3]; pair_vars = [1, 1, 1, 3, 1])
c([1, 2, 4, 6, 3]; pair_vars = [1, 1, 3, 1, 1])
c([1, 1, 1, 3, 5, 2, 7, 7, 5, 12, 8, 7]; pair_vars = [2, 4, 1, 4 ,2 ,3, 5, 1, 2, 3, 3, 2], dim = 3)
c([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]; pair_vars = [2, 4, 1, 4 ,2 ,3, 5, 1, 2, 3, 3, 2], dim = 3)
Constraints.xcsp_nvalues
— Methodxcsp_nvalues(list, condition, except)
Return true
if the number of distinct values in list
satisfies the given condition, false
otherwise.
Arguments
list::Vector{Int}
: list of values to check.condition
: condition to satisfy.except::Union{Nothing, Vector{Int}}
: list of values to exclude. Default isnothing
.
Variants
:nvalues
: The nValues constraint specifies that the number of distinct values in the list of variables x is equal to a given value. The constraint is defined by the following expression: nValues(x, op, val) where x is a list of variables, op is a comparison operator, and val is an integer value.
concept(:nvalues, x; op, val)
concept(:nvalues)(x; op, val)
Examples
c = concept(:nvalues)
c([1, 2, 3, 4, 5]; op = ==, val = 5)
c([1, 2, 3, 4, 5]; op = ==, val = 2)
c([1, 2, 3, 4, 3]; op = <=, val = 5)
c([1, 2, 3, 4, 3]; op = <=, val = 3)
Constraints.xcsp_ordered
— Methodxcsp_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 an increasing order.
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.
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.
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.
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.
concept(:strictly_decreasing, x; op=>, pair_vars=nothing)
concept(:strictly_decreasing)(x; op=>, pair_vars=nothing)
Examples
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=<)
Constraints.xcsp_regular
— Methodxcsp_regular(; list, automaton)
Ensures that a sequence x
(interpreted as a word) is accepted by the regular language represented by a given automaton. This constraint verifies the compliance of x
with the language rules encoded within the automaton
parameter, which must be an instance of <:AbstractAutomaton
.
Arguments
list::Vector{Int}
: A list of variablesautomaton<:AbstractAutomaton
: An automaton representing the regular language
Variants
:regular
: Ensures that a sequencex
(interpreted as a word) is accepted by the regular language represented by a given automaton. This constraint verifies the compliance ofx
with the language rules encoded within theautomaton
parameter, which must be an instance of<:AbstractAutomaton
.
concept(:regular, x; language)
concept(:regular)(x; language)
Examples
c = concept(:regular)
states = Dict(
(:a, 0) => :a,
(:a, 1) => :b,
(:b, 1) => :c,
(:c, 0) => :d,
(:d, 0) => :d,
(:d, 1) => :e,
(:e, 0) => :e,
)
start = :a
finish = :e
a = Automaton(states, start, finish)
c([0,0,1,1,0,0,1,0,0]; language = a)
c([1,1,1,0,1]; language = a)
Constraints.xcsp_sum
— Methodxcsp_sum(list, coeffs, condition)
Return true
if the sum of the variables in list
satisfies the given condition, false
otherwise.
Arguments
list::Vector{Int}
: list of values to check.coeffs::Vector{Int}
: list of coefficients to use.condition
: condition to satisfy.
Variants
:sum
: Global constraint ensuring that the sum of the variables inx
satisfies a given condition.
concept(:sum, x; op===, pair_vars=ones(x), val)
concept(:sum)(x; op===, pair_vars=ones(x), val)
Examples
c = concept(:sum)
c([1, 2, 3, 4, 5]; op===, val=15)
c([1, 2, 3, 4, 5]; op===, val=2)
c([1, 2, 3, 4, 3]; op=≤, val=15)
c([1, 2, 3, 4, 3]; op=≤, val=3)
Constraints.@usual
— Macrousual(ex::Expr)
This macro is used to define a new constraint or update an existing one in the USUAL_CONSTRAINTS dictionary. It takes an expression ex as input, which represents the definition of a constraint.
Here's a step-by-step explanation of what the macro does:
- It first extracts the symbol of the concept from the input expression. This symbol is expected to be the first argument of the first argument of the expression. For example, if the expression is @usual alldifferent(x; y=1), the symbol would be :alldifferent.
- It then calls the shrink_concept function on the symbol to get a simplified version of the concept symbol.
- It initializes a dictionary defaults to store whether each keyword argument of the concept has a default value or not.
- It checks if the expression has more than two arguments. If it does, it means that there are keyword arguments present. It then loops over these keyword arguments. If a keyword argument is a symbol, it means it doesn't have a default value, so it adds an entry to the defaults dictionary with the keyword argument as the key and false as the value. If a keyword argument is not a symbol, it means it has a default value, so it adds an entry to the defaults dictionary with the keyword argument as the key and true as the value.
- It calls the make_error function on the simplified concept symbol to generate an error function for the constraint.
- It evaluates the input expression to get the concept function.
- It checks if the USUALCONSTRAINTS dictionary already contains an entry for the simplified concept symbol. If it does, it adds the defaults dictionary to the parameters of the existing constraint. If it doesn't, it creates a new constraint with the concept function, a description, the error function, and the defaults dictionary as the parameters, and adds it to the USUALCONSTRAINTS dictionary.
This macro is used to make it easier to define and update constraints in a consistent and possibly automated way.
Arguments
ex::Expr
: expression to parse.
Example
@usual concept_all_different(x; vals=nothing) = xcsp_all_different(list=x, except=vals)