ArticleslgStudy

science

Constraint programming

Constraint programming is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Constraint programming rather than just read about it. In short: Constraint programming (CP) 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.

Key takeaways

  • Constraint programming belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Constraint programming to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Constraint programming from memory before moving on to harder problems.

Reference excerpt

Constraint programming (CP) 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 addition 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, 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 that limits the values these variables can take simultaneously.

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 <,>,≤,≥,=,≠,...; logical constraints: constraints are defined with an explicit semantics, i.e., AllDifferent, AtMost,...

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 has been assigned. A total assignment is when all the variables of the problem have been assigned.

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 COP, 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:

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 formula evaluation in 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.

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

Boolean domains, where only true/false constraints apply (SAT problem) integer domains, rational domains interval domains, in particular for scheduling problems linear domains, where only linear functions are described and analyzed (although approaches to non-linear problems do exist) finite domains, where constraints are defined over finite sets mixed domains, involving two or more of the above 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

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Constraint programming

Start with the simplest possible case. Write down what Constraint programming claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Constraint programming before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Constraint programming ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Constraint programming

In research
Constraint programming appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Constraint programming in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Constraint programming is common in secondary-school and first-year university syllabi. It links to neighbouring topics Constraint programming, Declarative programming, Programming paradigms, so understanding it makes those chapters shorter.
In everyday life
Look for Constraint programming outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Constraint programming” →

How to study Constraint programming in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Constraint programming means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Constraint programming out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Constraint programming in simple terms?

Constraint programming (CP) 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…

Why does Constraint programming matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Constraint programming?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Constraint programming.

Tags

  • Constraint programming
  • Declarative programming
  • Programming paradigms

Keep exploring