Labeling constraints
Author: Daniel Diaz
Keywords: GNU Prolog, manual, Prolog, compiler, constraints, finite domains

### 9.9 Labeling constraints

- fd_labeling/2, fd_labeling/1, fd_labelingff/1

#### 9.9.1 fd_labeling/2, fd_labeling/1, fd_labelingff/1

Templates

  fd_labeling(+fd_variable_list, +fd_labeling_option_list)
  fd_labeling(+fd_variable, +fd_labeling_option_list)
  fd_labeling(+fd_variable_list)
  fd_labeling(+fd_variable)
  fd_labelingff(+fd_variable_list)
  fd_labelingff(+fd_variable)

Description

fd_labeling(Vars, Options) assigns a value to each variable X of the list Vars according to the list of labeling options given by Options. Vars can be also a single FD variable. This predicate is re-executable on backtracking.

FD labeling options: Options is a list of labeling options. If this list contains contradictory options, the rightmost option is the one which applies. Possible options are:

- variable_method(V): specifies the heuristics to select the variable to enumerate:
  - standard: no heuristics, the leftmost variable is selected.
  - first_fail (or ff): selects the variable with the smallest number of elements in its domain. If several variables have the same number of elements the leftmost variable is selected.
  - most_constrained: like first_fail but when several variables have the same number of elements selects the variable that appears in most constraints.
  - smallest: selects the variable that has the smallest value in its domain. If there is more than one such variable selects the variable that appears in most constraints.
  - largest: selects the variable that has the greatest value in its domain. If there is more than one such variable selects the variable that appears in most constraints.
  - max_regret: selects the variable that has the greatest difference between the smallest value and the next value of its domain. If there is more than one such variable selects the variable that appears in most constraints.
  - random: selects randomly a variable. Each variable is chosen only once.

  The default value is standard.

- reorder(true/false): specifies if the variable heuristics should dynamically reorder the list of variable (true) or not (false). Dynamic reordering is generally more efficient but in some cases a static ordering is faster. The default value is true.
- value_method(V): specifies the heuristics to select the value to assign to the chosen variable:
  - min: enumerates the values from the smallest to the greatest (default).
  - max: enumerates the values from the greatest to the smallest.
  - middle: enumerates the values from the middle to the bounds.
  - bounds: enumerates the values from the bounds to the middle.
  - random: enumerates the values randomly. Each value is tried only once.
  - bisect: recursively creates a choice between X #=< M and X #> M, where M is the midpoint of the domain of the variable. Values are thus tried from the smallest to the greatest. This is also known as domain splitting.

  The default value is min.

- backtracks(B): unifies B with the number of backtracks during the enumeration.

fd_labeling(Vars) is equivalent to fd_labeling(Vars, []).

fd_labelingff(Vars) is equivalent to fd_labeling(Vars, [variable_method(ff)]).

Errors

Vars is a partial list or a list with an element E which is a variable |  | instantiation_error
Vars is neither a partial list nor a list |  | type_error(list, Vars)
an element E of the Vars list is neither a variable nor an integer nor an FD variable |  | type_error(fd_variable, E)
Options is a partial list or a list with an element E which is a variable |  | instantiation_error
Options is neither a partial list nor a list |  | type_error(list, Options)
an element E of the Options list is neither a variable nor a labeling option |  | domain_error(fd_labeling_option, E)

Portability

GNU Prolog predicates.

Copyright (C) 1999-2018 Daniel Diaz Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. More about the copyright