# Prolog restrictions [TOC] ----- ## Materialization of restrictions A restriction can be set as boolean if a variable is not set: ```prolog |? - X #= 10, B #= (X#>= 2) + (X#>= 4) + (X #>=8). B=3, X=10 ``` ----- ## list_to_fdset(+List, -FD_Set). Transforms a list in a domain (Finite domain set). ### Example ``` prolog Numbers = [4,8,15,16,23,42], list_to_fdset(Numbers, FD_Set), Vars in FD_Set. ``` The elements inside `Var` are in the specified range. ----- ## domain(+List_of_variables, + Min, +Max) Set a numeric range domain for a list of variables. ### Example: ```prolog domain([A,B,C], 5, 12). ``` ----- ## fd_batch(+Constraints) When is set a restriction to a variable, the domain of all the other variables is updated. With `fd_batch` we first set all the variables domain, then update all the domains at once. ### Example: ```prolog domain([A,B,C], 5, 12), fd_batch([A#>8, B+C#<12, A+B+C #= 20]). ``` ----- ## sum(+Xs, +RelOp, ?Value). Corresnpond to `sumlist` from the `list` library. ### Example ```prolog |?- domain([X,Y], 1, 10), sum([X,Y], #<, 10). ``` ----- ## scalar_product - `Coeffs` is a list of size `n` of integers. - `Xs` is a list of domain integers with size n. - `RelOp` is a operational operator. - `Value` is the answer (domain or variable). - It's true if sum(Coeffs*Xs) RelOp Value. ### Usage ```prolog scalar_product(+Coeffs, +Xs, +RelOp, ?Value, +Options) scalar_product(+Coeffs, +Xs, +RelOp, ?Value, ?Reif, +Options) ``` ### Options `among(Least, Most, Range)` indicates thar in the maximum or min interval `Xs` has values in the range given. `consistency(Cons)` indicates the level of consistence used by the restriction. Cons can be `domain`, `bounds` or `value`. ### Example ```prolog |?- domain([A,B,C], 1,5 ), scalar_product([1,2,3], [A,B,C], #=, 10). A in 1..5, B in 1..3, C in 1..2 ``` ----- ## minimum(?Value, +Xs) and maximum(?Value, +Xs) - `Xs` is a list of integers and/or domain variables. - `Value` is the integer or domain variable. - It's true if `Value` is the min or max value of `Xs`. - Corresponds for min_member and max_member of the library(lists). ### Example 1 ```prolog |? - domain([A,B], 1, 10), C in 5..15, minimum(C, [A,B]). A in 5..10 B in 5..10 C in 5..10 ``` ### Example 2 ```prolog |? - domain([A,B,C], 1, 5), sum([A,B,C], #=, 10), maximum(3, [A,B]). A in 2..3 B in 2..3 C in 4..5 ``` ----- ## maximum_arg(+Xs, ?Index) and minimum_arg(+Xs, ?Index) - Xs is a list of integers in a domain. - Index is a variable of domain or integer. - It's true if the index of the max/min variable in `Xs` is `Index`. - If the max/min value is repeated `Index` will be the first occurrence. - Can't be materialized. ### Example ```prolog |? - domain([A,B,C], 1,5), sum([A,B], #=, 10), minimum_arg([A,B,C], X). A in 5 B in 5 C in 1..5 X = {1} \/ {3} % since C can be 5 or 1. ``` ----- ## global_cardinality(+Xs, +Vals, + Options) - Restricts the number of occurrences for each value in a list. - Xs is a list o integers or domain variables - Vals is a list of terms `K-V` where K in an unique integer and V is an integer of domain variable - True if each element of Xs is equals to a K and for each pair K-V, exactly V elements of Xs is equal to K. ### Example ```prolog global_cardinality([A,B,C], [1-2, 3-1]). ``` ## nvalue(?N, +Variables) - Restricts the list of variables `Variables` in a way that just exists exactly `N` distict values. - `Variables` is a list of integers an/or variables of domain with finit limis and N is an integer or domain variable. ### Example 1 ```prolog domain([X,Y], 1, 3), domain([Z], 3, 5). nvalue(2, [X,Y,Z]), X#=Y, X#=1. X = 1, Y = 3 Z = 3 ``` ## all_distinct_except_0(+Variables) - True whe all the variables have distinct values except if the variables has the number 0. ### Example ```prolog |? - L = [A,B, 1, D], domain(L, 0, 2), all_distinct_except_0(L). A in {0} \/ {2} B in {0} \/ {2} C in {0} \/ {2} ```` ## symmetric_all_distinct(+Variables) - True when all th variables have distinct values and all the variables $X_i = j <=> X_j = i$ . - Variables is a list of integers or domain variables. ### Example ```prolog |? - L [A,B,C,D], symmetric_all_distinct(L), A#=3. A = 3, C = 1, B in {2} \/ {4}, D in {2} \/ {4} ``` ## assignment(+Xs, +Ys, +Options) -