# Constraint programming

Constraint programming (CP)[1] is a paradigm for solving combinatorial problems that draws on a wide range of techniques from artificial intelligence, computer science, and operations research. In constraint programming, users declaratively state the constraints on the feasible solutions for a set of decision variables. Constraints differ from the common primitives of imperative programming languages in that they do not specify a step or sequence of steps to execute, but rather the properties of a solution to be found. In additions to constraints, users also need to specify a method to solve these constraints. This typically draws upon standard methods like chronological backtracking and constraint propagation, but may use customized code like a problem specific branching heuristic.

Constraint programming takes its root from and can be expressed in the form of constraint logic programming, which embeds constraints into a logic program. This variant of logic programming is due to Jaffar and Lassez,[2] who extended in 1987 a specific class of constraints that were introduced in Prolog II. The first implementations of constraint logic programming were Prolog III, CLP(R), and CHIP.

Instead of logic programming, constraints can be mixed with functional programming, term rewriting, and imperative languages. Programming languages with built-in support for constraints include Oz (functional programming) and Kaleidoscope (imperative programming). Mostly, constraints are implemented in imperative languages via constraint solving toolkits, which are separate libraries for an existing imperative language.

## Constraint logic programming

Constraint programming is an embedding of constraints in a host language. The first host languages used were logic programming languages, so the field was initially called constraint logic programming. The two paradigms share many important features, like logical variables and backtracking. Today most Prolog implementations include one or more libraries for constraint logic programming.

The difference between the two is largely in their styles and approaches to modeling the world. Some problems are more natural (and thus, simpler) to write as logic programs, while some are more natural to write as constraint programs.

The constraint programming approach is to search for a state of the world in which a large number of constraints are satisfied at the same time. A problem is typically stated as a state of the world containing a number of unknown variables. The constraint program searches for values for all the variables.

Temporal concurrent constraint programming (TCC) and non-deterministic temporal concurrent constraint programming (MJV) are variants of constraint programming that can deal with time.

## Constraint satisfaction problem

A constraint is a relation between multiple variables which limits the values these variables can take simultaneously.

Definition  A constraint satisfaction problem on finite domains (or CSP) is defined by a triplet ${\displaystyle ({\mathcal {X}},{\mathcal {D}},{\mathcal {C}})}$ where:

• ${\displaystyle {\mathcal {X}}=\{x_{1},\dots ,x_{n}\}}$ is the set of variables of the problem;
• ${\displaystyle {\mathcal {D}}=\{{\mathcal {D}}_{1},\dots ,{\mathcal {D}}_{n}\}}$ is the set of domains of the variables, i.e., for all ${\displaystyle k\in [1;n]}$ we have ${\displaystyle x_{k}\in {\mathcal {D}}_{k}}$;
• ${\displaystyle {\mathcal {C}}=\{C_{1},\dots ,C_{m}\}}$ is a set of constraints. A constraint ${\displaystyle C_{i}=({\mathcal {X}}_{i},{\mathcal {R}}_{i})}$ is defined by a set ${\displaystyle {\mathcal {X}}_{i}=\{x_{i_{1}},\dots ,x_{i_{k}}\}}$ of variables and a relation ${\displaystyle {\mathcal {R}}_{i}\subset {\mathcal {D}}_{i_{1}}\times \dots \times {\mathcal {D}}_{i_{k}}}$ which defines the set of values allowed simultaneously for the variables of ${\displaystyle {\mathcal {X}}_{i}}$:

Three categories of constraints exist:

• extensional constraints: constraints are defined by enumerating the set of values that would satisfy them;
• arithmetic constraints: constraints are defined by an arithmetic expression, i.e., using ${\displaystyle <,>,\leq ,\geq ,=,\neq ,...}$;
• logical constraints: constraints are defined with an explicit semantic, i.e., AllDifferent, AtMost,...

Definition   An assignment (or model) ${\displaystyle {\mathcal {A}}}$ of a CSP ${\displaystyle P=({\mathcal {X}},{\mathcal {D}},{\mathcal {C}})}$ is defined by the couple ${\displaystyle {\mathcal {A}}=({\mathcal {X_{\mathcal {A}}}},{\mathcal {V_{\mathcal {A}}}})}$ where:

• ${\displaystyle {\mathcal {X_{\mathcal {A}}}}\subset {\mathcal {X}}}$ is a subset of variable;
• ${\displaystyle {\mathcal {V_{\mathcal {A}}}}=\{v_{{\mathcal {A}}_{1}},\dots ,v_{{\mathcal {A}}_{k}}\}\in \{{\mathcal {D}}_{{\mathcal {A}}_{1}},\dots ,{\mathcal {D}}_{{\mathcal {A}}_{k}}\}}$ is the tuple of the values taken by the assigned variables.

Assignment is the association of a variable to a value from its domain. A partial assignment is when a subset of the variables of the problem have been assigned. A total assignment is when all the variables of the problem have been assigned.

Property  Given ${\displaystyle {\mathcal {A}}=({\mathcal {X_{\mathcal {A}}}},{\mathcal {V_{\mathcal {A}}}})}$ an assignation (partial or total) of a CSP ${\displaystyle P=({\mathcal {X}},{\mathcal {D}},{\mathcal {C}})}$, and ${\displaystyle C_{i}=({\mathcal {X}}_{i},{\mathcal {R}}_{i})}$ a constraint of ${\displaystyle P}$ such as ${\displaystyle {\mathcal {X}}_{i}\subset {\mathcal {X_{\mathcal {A}}}}}$, the assignation ${\displaystyle {\mathcal {A}}}$ satisfies the constraint ${\displaystyle C_{i}}$ if and only if all the values ${\displaystyle {\mathcal {V}}_{{\mathcal {A}}_{i}}=\{v_{i}\in {\mathcal {V}}_{\mathcal {A}}{\mbox{ tel que }}x_{i}\in {\mathcal {X}}_{i}\}}$ of the variables of the constraint ${\displaystyle C_{i}}$ belongs to ${\displaystyle {\mathcal {R}}_{i}}$.

Definition  A solution of a CSP is a total assignation which satisfied all the constraints of the problem.

During the search of the solutions of a CSP, a user can wish for:

• finding a solution (satisfying all the constraints);
• finding all the solutions of the problem;
• proving the unsatisfiability of the problem.

## Constraint optimization problem

A constraint optimization problem (COP) is a constraint satisfaction problem associated to an objective function.

An optimal solution to a minimization (maximization) COP is a solution that minimizes (maximizes) the value of the objective function.

During the search of the solutions of a CSP, a user can wish for:

• finding a solution (satisfying all the constraints);
• finding the best solution with respect to the objective;
• proving the optimality of the best found solution;
• proving the unsatisfiability of the problem.

## Perturbation vs refinement models

Languages for constraint-based programming follow one of two approaches:[3]

• Refinement model: variables in the problem are initially unassigned, and each variable is assumed to be able to contain any value included in its range or domain. As computation progresses, values in the domain of a variable are pruned if they are shown to be incompatible with the possible values of other variables, until a single value is found for each variable.
• Perturbation model: variables in the problem are assigned a single initial value. At different times one or more variables receive perturbations (changes to their old value), and the system propagates the change trying to assign new values to other variables that are consistent with the perturbation.

Constraint propagation in constraint satisfaction problems is a typical example of a refinement model, and spreadsheets are a typical example of a perturbation model.

The refinement model is more general, as it does not restrict variables to have a single value, it can lead to several solutions to the same problem. However, the perturbation model is more intuitive for programmers using mixed imperative constraint object-oriented languages.[4]

## Domains

The constraints used in constraint programming are typically over some specific domains. Some popular domains for constraint programming are:

Finite domains is one of the most successful domains of constraint programming. In some areas (like operations research) constraint programming is often identified with constraint programming over finite domains.

## Constraint propagation

Local consistency conditions are properties of constraint satisfaction problems related to the consistency of subsets of variables or constraints. They can be used to reduce the search space and make the problem easier to solve. Various kinds of local consistency conditions are leveraged, including node consistency, arc consistency, and path consistency.

Every local consistency condition can be enforced by a transformation that changes the problem without changing its solutions. Such a transformation is called constraint propagation[5]. Constraint propagation works by reducing domains of variables, strengthening constraints, or creating new ones. This leads to a reduction of the search space, making the problem easier to solve by some algorithms. Constraint propagation can also be used as an unsatisfiability checker, incomplete in general but complete in some particular cases.

## Constraint solving

There are three main algorithmic techniques for solving constraint satisfaction problems: backtracking search, local search, and dynamic programming[1].

Backtracking search is a general algorithm for finding all (or some) solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons a candidate ("backtracks") as soon as it determines that the candidate cannot possibly be completed to a valid solution.

Local search is an incomplete method for finding a solution to a problem. It is based on iteratively improving an assignment of the variables until all constraints are satisfied. In particular, local search algorithms typically modify the value of a variable in an assignment at each step. The new assignment is close to the previous one in the space of assignment, hence the name local search.

### Dynamic programming

Dynamic programming is both a mathematical optimization method and a computer programming method. It refers to simplifying a complicated problem by breaking it down into simpler sub-problems in a recursive manner. While some decision problems cannot be taken apart this way, decisions that span several points in time do often break apart recursively. Likewise, in computer science, if a problem can be solved optimally by breaking it into sub-problems and then recursively finding the optimal solutions to the sub-problems, then it is said to have optimal substructure.

## Example

The syntax for expressing constraints over finite domains depends on the host language. The following is a Prolog program that solves the classical alphametic puzzle SEND+MORE=MONEY in constraint logic programming:

% This code works in both YAP and SWI-Prolog using the environment-supplied
% CLPFD constraint solver library.  It may require minor modifications to work
% in other Prolog environments or using other constraint solvers.
:- use_module(library(clpfd)).
sendmore(Digits) :-
Digits = [S,E,N,D,M,O,R,N,Y],   % Create variables
Digits ins 0..9,                % Associate domains to variables
S #\= 0,                        % Constraint: S must be different from 0
M #\= 0,
all_different(Digits),          % all the elements must take different values
1000*S + 100*E + 10*N + D     % Other constraints
+ 1000*M + 100*O + 10*R + E
#= 10000*M + 1000*O + 100*N + 10*E + Y,
label(Digits).                  % Start the search


The interpreter creates a variable for each letter in the puzzle. The operator ins is used to specify the domains of these variables, so that they range over the set of values {0,1,2,3, ..., 9}. The constraints S#\=0 and M#\=0 means that these two variables cannot take the value zero. When the interpreter evaluates these constraints, it reduces the domains of these two variables by removing the value 0 from them. Then, the constraint all_different(Digits) is considered; it does not reduce any domain, so it is simply stored. The last constraint specifies that the digits assigned to the letters must be such that "SEND+MORE=MONEY" holds when each letter is replaced by its corresponding digit. From this constraint, the solver infers that M=1. All stored constraints involving variable M are awakened: in this case, constraint propagation on the all_different constraint removes value 1 from the domain of all the remaining variables. Constraint propagation may solve the problem by reducing all domains to a single value, it may prove that the problem has no solution by reducing a domain to the empty set, but may also terminate without proving satisfiability or unsatisfiability. The label literals are used to actually perform search for a solution.

## Constraint programming libraries for general-purpose programming languages

Constraint programming is often realized in imperative programming via a separate library. Some popular libraries for constraint programming are:

## References

1. Rossi, Francesca; Beek, Peter van; Walsh, Toby (2006-08-18). Handbook of Constraint Programming. Elsevier. ISBN 9780080463803.
2. Jaffar, Joxan, and J-L. Lassez. "Constraint logic programming." Proceedings of the 14th ACM SIGACT-SIGPLAN symposium on Principles of programming languages. ACM, 1987.
3. Mayoh, Brian; Tyugu, Enn; Penjam, Jaan (1993). Constraint Programming. Springer Science+Business Media. p. 76. ISBN 9783642859830.
4. Lopez, G., Freeman-Benson, B., & Borning, A. (1994, January). Kaleidoscope: A constraint imperative programming language. In Constraint Programming (pp. 313-329). Springer Berlin Heidelberg.
5. Bessiere, Christian (2006), "Constraint Propagation", Handbook of Constraint Programming, Foundations of Artificial Intelligence, 2, Elsevier, pp. 29–83, doi:10.1016/s1574-6526(06)80007-6, ISBN 9780444527264
6. Laborie P, Rogerie J, Shaw P, Vilim P (2018). "IBM ILOG CP optimizer for scheduling". Constraints. 23 (2): 210–250. doi:10.1007/s10601-018-9281-x.
7. Francesca Rossi; Peter Van Beek; Toby Walsh (2006). Handbook of constraint programming. Elsevier. p. 157. ISBN 978-0-444-52726-4.
8. van Hoeve, Willem-Jan; Hunting, Marcel; Kuip, Chris (2012). "The AIMMS Interface to Constraint Programming" (PDF). AIMMS.
9. Robert Fourer; David M. Gay (2002). "Extending an Algebraic Modeling Language to Support Constraint Programming". INFORMS Journal on Computing. 14 (4): 322–344. CiteSeerX 10.1.1.8.9699. doi:10.1287/ijoc.14.4.322.2825.
10. Tim Felgentreff; Alan Borning; Robert Hirschfeld (2014). "Specifying and Solving Constraints on Object Behavior". Journal of Object Technology. 13 (4): 1–38. doi:10.5381/jot.2014.13.4.a1.
11. https://www.minizinc.org/
12. CHIP V5 COSYTEC
13. Press, The MIT. "The Reasoned Schemer, Second Edition". The MIT Press. Retrieved 2019-02-19.