Skip to content

Commit 47d9c12

Browse files
authored
Update doc (#37)
* Doc for usual constraint. * Some doc improvement * Some doc improvement 2/? * Some doc improvement 3/? * Some doc improvement 4/? * Some doc improvement 5/? * Improves the doc * tag new version and CI
1 parent 3c48c33 commit 47d9c12

24 files changed

+878
-83
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: false
1010
matrix:
1111
version:
12-
- "1.9"
12+
- "1.10"
1313
- 'nightly'
1414
os:
1515
- ubuntu-latest

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Constraints"
22
uuid = "30f324ab-b02d-43f0-b619-e131c61659f7"
33
authors = ["Jean-François Baffier"]
4-
version = "0.5.3"
4+
version = "0.5.4"
55

66
[deps]
77
CompositionalNetworks = "4b67e4b5-442d-4ef5-b760-3f5df3a57537"
@@ -19,7 +19,7 @@ CompositionalNetworks = "0.5"
1919
ConstraintCommons = "0.1"
2020
ConstraintDomains = "0.3"
2121
DataFrames = "1"
22-
Dictionaries = "0.3"
22+
Dictionaries = "0.4"
2323
MacroTools = "0.5"
2424
PrettyTables = "2"
2525
TestItemRunner = "0.2"

src/Constraints.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ include("constraint.jl")
3939
include("usual_constraints.jl")
4040

4141
# SECTION - Generic Constraints: intension, extension
42-
include("constraints/intention.jl")
42+
include("constraints/intension.jl")
4343
include("constraints/extension.jl")
4444

4545
# SECTION - Constraints defined from Languages

src/constraint.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ Return the list of symmetries of `c`.
7272
"""
7373
symmetries(c::Constraint) = c.symmetries
7474

75+
"""
76+
make_error(symb::Symbol)
77+
78+
Create a function that returns an error based on the predicate of the constraint identified by the symbol provided.
79+
80+
## Arguments
81+
- `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.
82+
83+
## Returns
84+
- Function: A function that takes in a variable `x` and an arbitrary number of parameters `params`. The function returns a Float64.
85+
86+
# Examples
87+
```julia
88+
e = make_error(:all_different)
89+
e([1, 2, 3]) # Returns 0.0
90+
e([1, 1, 3]) # Returns 1.0
91+
```
92+
"""
7593
function make_error(symb::Symbol)
7694
isdefined(Constraints, Symbol("icn_$symb")) && (return eval(Symbol("icn_$symb")))
7795
isdefined(Constraints, Symbol("error_$symb")) && (return eval(Symbol("error_$symb")))
@@ -85,6 +103,25 @@ Simply delete the `concept_` part of symbol or string starting with it. TODO: ad
85103
"""
86104
shrink_concept(s) = Symbol(string(s)[9:end])
87105

106+
"""
107+
concept_vs_error(c, e, args...; kargs...)
108+
109+
Compare the results of a concept function and an error function for the same inputs. It is mainly used for testing purposes.
110+
111+
# Arguments
112+
- `c`: The concept function.
113+
- `e`: The error function.
114+
- `args...`: Positional arguments to be passed to both the concept and error functions.
115+
- `kargs...`: Keyword arguments to be passed to both the concept and error functions.
116+
117+
# Returns
118+
- 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.
119+
120+
# Examples
121+
```julia
122+
concept_vs_error(all_different, make_error(:all_different), [1, 2, 3]) # Returns false
123+
```
124+
"""
88125
function concept_vs_error(c, e, args...; kargs...)
89126
return c(args...; kargs...) (e(args...; kargs...) > 0.0)
90127
end

src/constraints/all_different.jl

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
#!SECTION - all_different
22

3+
const description_all_different = """
4+
Global constraint ensuring that all the values of `x` are all different.
5+
"""
6+
7+
"""
8+
xcsp_all_different(list::Vector{Int})
9+
10+
Return `true` if all the values of `list` are different, `false` otherwise.
11+
12+
## Arguments
13+
- `list::Vector{Int}`: list of values to check.
14+
15+
## Variants
16+
- `:all_different`: $description_all_different
17+
```julia
18+
concept(:all_different, x; vals)
19+
concept(:all_different)(x; vals)
20+
```
21+
22+
## Examples
23+
```julia
24+
c = concept(:all_different)
25+
26+
c([1, 2, 3, 4])
27+
c([1, 2, 3, 1])
28+
c([1, 0, 0, 4]; vals=[0])
29+
c([1, 0, 0, 1]; vals=[0])
30+
```
31+
"""
332
xcsp_all_different(list, ::Nothing) = allunique(list)
433

534
function xcsp_all_different(list, except)
@@ -8,8 +37,6 @@ end
837

938
xcsp_all_different(; list, except=nothing) = xcsp_all_different(list, except)
1039

11-
const description_all_different = """Global constraint ensuring that all the values of a given configuration are unique"""
12-
1340
@usual concept_all_different(x; vals=nothing) = xcsp_all_different(list=x, except=vals)
1441

1542
## SECTION - Test Items

src/constraints/all_equal.jl

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,39 @@
11
#!SECTION - all_equal
22

3-
const description_all_equal = """Global constraint ensuring that all the values of `x` are all equal"""
3+
const description_all_equal = """
4+
Global constraint ensuring that all the values of `x` are all equal.
5+
"""
46

57
concept_all_equal(x, val) = all(y -> y == val, x)
68

9+
"""
10+
xcsp_all_equal(list::Vector{Int}, val::Int)
11+
12+
Return `true` if all the values of `list` are equal to `val`, `false` otherwise.
13+
14+
## Arguments
15+
- `list::Vector{Int}`: list of values to check.
16+
- `val::Int`: value to compare to.
17+
18+
## Variants
19+
- `:all_equal`: $description_all_equal
20+
```julia
21+
concept(:all_equal, x; val=nothing, pair_vars=zeros(x), op=+)
22+
concept(:all_equal)(x; val=nothing, pair_vars=zeros(x), op=+)
23+
```
24+
25+
## Examples
26+
```julia
27+
c = concept(:all_equal)
28+
29+
c([0, 0, 0, 0])
30+
c([1, 2, 3, 4])
31+
c([3, 2, 1, 0]; pair_vars=[0, 1, 2, 3])
32+
c([0, 1, 2, 3]; pair_vars=[0, 1, 2, 3])
33+
c([1, 2, 3, 4]; op=/, val=1, pair_vars=[1, 2, 3, 4])
34+
c([1, 2, 3, 4]; op=*, val=1, pair_vars=[1, 2, 3, 4])
35+
```
36+
"""
737
xcsp_all_equal(; list) = concept_all_equal(list; val=first(list))
838

939
@usual function concept_all_equal(x; val=nothing, pair_vars=zero(x), op=+)

src/constraints/cardinality.jl

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,64 @@
11
# cardinality (or global_cardinality or gcc)
22

3-
const description_cardinality = """Global constraint ensuring that all ...`"""
4-
const description_cardinality_closed = """Global constraint ensuring that all ...`"""
5-
const description_cardinality_open = """Global constraint ensuring that all ...`"""
3+
const description_cardinality = """
4+
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.
5+
"""
6+
const description_cardinality_closed = """
7+
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.
8+
"""
69

10+
const description_cardinality_open = """
11+
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.
12+
"""
13+
14+
"""
15+
xcsp_cardinality(list, values, occurs, closed)
16+
17+
Return `true` if the number of occurrences of the values in `values` in `list` satisfies the given condition, `false` otherwise.
18+
19+
## Arguments
20+
- `list::Vector{Int}`: list of values to check.
21+
- `values::Vector{Int}`: list of values to check.
22+
- `occurs::Vector{Int}`: list of occurrences to check.
23+
- `closed::Bool`: whether the constraint is closed or not.
24+
25+
## Variants
26+
- `:cardinality`: $description_cardinality
27+
```julia
28+
concept(:cardinality, x; bool=false, vals)
29+
concept(:cardinality)(x; bool=false, vals)
30+
```
31+
- `:cardinality_closed`: $description_cardinality_closed
32+
```julia
33+
concept(:cardinality_closed, x; vals)
34+
concept(:cardinality_closed)(x; vals)
35+
```
36+
- `:cardinality_open`: $description_cardinality_open
37+
```julia
38+
concept(:cardinality_open, x; vals)
39+
concept(:cardinality_open)(x; vals)
40+
```
41+
42+
## Examples
43+
```julia
44+
c = concept(:cardinality)
45+
46+
c([2, 5, 10, 10]; vals=[2 0 1; 5 1 3; 10 2 3])
47+
c([8, 5, 10, 10]; vals=[2 0 1; 5 1 3; 10 2 3], bool=false)
48+
c([8, 5, 10, 10]; vals=[2 0 1; 5 1 3; 10 2 3], bool=true)
49+
c([2, 5, 10, 10]; vals=[2 1; 5 1; 10 2])
50+
c([2, 5, 10, 10]; vals=[2 0 1 42; 5 1 3 7; 10 2 3 -4])
51+
c([2, 5, 5, 10]; vals=[2 0 1; 5 1 3; 10 2 3])
52+
c([2, 5, 10, 8]; vals=[2 1; 5 1; 10 2])
53+
c([5, 5, 5, 10]; vals=[2 0 1 42; 5 1 3 7; 10 2 3 -4])
54+
55+
cc = concept(:cardinality_closed)
56+
cc([8, 5, 10, 10]; vals=[2 0 1; 5 1 3; 10 2 3])
57+
58+
co = concept(:cardinality_open)
59+
co([8, 5, 10, 10]; vals=[2 0 1; 5 1 3; 10 2 3])
60+
```
61+
"""
762
function xcsp_cardinality(; list, values, occurs, closed=false)
863
counts = zeros(Int, Indices(distinct(values)))
964
for t in list

src/constraints/channel.jl

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
const description = """
2+
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.
3+
"""
4+
5+
"""
6+
xcsp_channel(; list)
7+
8+
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.
9+
10+
## Arguments
11+
- `list::Union{AbstractVector, Tuple}`: list of values to check.
12+
13+
## Variants
14+
- `:channel`: $description
15+
```julia
16+
concept(:channel, x; dim=1, id=nothing)
17+
concept(:channel)(x; dim=1, id=nothing)
18+
```
19+
20+
## Examples
21+
```julia
22+
c = concept(:channel)
23+
24+
c([2, 1, 4, 3])
25+
c([1, 2, 3, 4])
26+
c([2, 3, 1, 4])
27+
c([2, 1, 5, 3, 4, 2, 1, 4, 5, 3]; dim=2)
28+
c([2, 1, 4, 3, 5, 2, 1, 4, 5, 3]; dim=2)
29+
c([false, false, true, false]; id=3)
30+
c([false, false, true, false]; id=1)
31+
```
32+
"""
133
function xcsp_channel(list::AbstractVector)
234
for (i, j) in enumerate(list)
335
if i j
@@ -32,8 +64,6 @@ end
3264

3365
concept_channel(x, ::Val) = xcsp_channel(list=x)
3466

35-
const description_channel = """Global constraint ensuring that all ...`"""
36-
3767
concept_channel(x, id) = count(!iszero, x) == 1 == x[id]
3868

3969
@usual function concept_channel(x; dim=1, id = nothing)

src/constraints/circuit.jl

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
const description_circuit = """
2+
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.
3+
"""
4+
5+
"""
6+
xcsp_circuit(; list, size)
7+
8+
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.
9+
10+
## Arguments
11+
- `list::AbstractVector`: list of values to check.
12+
- `size::Int`: size of the circuit.
13+
14+
## Variants
15+
- `:circuit`: $description_circuit
16+
```julia
17+
concept(:circuit, x; op, val)
18+
concept(:circuit)(x; op, val)
19+
```
20+
21+
## Examples
22+
```julia
23+
c = concept(:circuit)
24+
25+
c([1, 2, 3, 4])
26+
c([2, 3, 4, 1])
27+
c([2, 3, 1, 4]; op = ==, val = 3)
28+
c([4, 3, 1, 3]; op = >, val = 0)
29+
```
30+
"""
131
function xcsp_circuit(; list, size = nothing)
232
return if isnothing(size)
333
concept_circuit(list)
@@ -6,8 +36,6 @@ function xcsp_circuit(; list, size = nothing)
636
end
737
end
838

9-
const description_circuit = "Global constraint ensuring that the values of `x` form a circuit. If the indices of the variables are not `1:length(x)`, the indices can be indicated as the `param` collection"
10-
1139
# circuit (full circuit)
1240
@usual function concept_circuit(x; op = , val = length(x))
1341
V = Set(1:length(x))

0 commit comments

Comments
 (0)