A.9 library(clpfd): CLP(FD): Constraint Logic Programming over Finite Domains
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
      • The SWI-Prolog library
        • library(clpfd): CLP(FD): Constraint Logic Programming over Finite Domains
          • Introduction
          • Arithmetic constraints
          • Declarative integer arithmetic
          • Example: Factorial relation
          • Combinatorial constraints
          • Domains
          • Example: Sudoku
          • Residual goals
          • Core relations and search
          • Example: Eight queens puzzle
          • Optimisation
          • Reification
          • Enabling monotonic CLP(FD)
          • Custom constraints
          • Applications
          • Acknowledgments
          • CLP(FD) predicate index
          • Closing and opening words about CLP(FD)
    • Packages

A.9.11 Optimisation

We can use labeling/2 to minimize or maximize the value of a CLP(FD) expression, and generate solutions in increasing or decreasing order of the value. See the labeling options min(Expr) and max(Expr), respectively.

Again, to easily try different labeling options in connection with optimisation, we recommend to introduce a dedicated predicate for posting constraints, and to use labeling/2 in a separate goal. This way, we can observe properties of the core relation in isolation, and try different labeling options without recompiling our code.

If necessary, we can use once/1 to commit to the first optimal solution. However, it is often very valuable to see alternative solutions that are also optimal, so that we can choose among optimal solutions by other criteria. For the sake of purity and completeness, we recommend to avoid once/1 and other constructs that lead to impurities in CLP(FD) programs.

Related to optimisation with CLP(FD) constraints are library(simplex) and CLP(Q) which reason about linear constraints over rational numbers.