Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
ConstraintSatisfactionProblems in
Python
MichaelSioutis
Outline
Introduction
Constraints inPython
Examples
Questions
Constraint Satisfaction Problems in Python
Michael Sioutis
Department of Informatics and TelecommunicationsNational and Kapodistrian University of Athens
July 14, 2011
Michael Sioutis Constraint Satisfaction Problems in Python
ConstraintSatisfactionProblems in
Python
MichaelSioutis
Outline
Introduction
Constraints inPython
Examples
Questions
Table of Contents
1 Introduction
2 Constraints in Python
3 Examples
4 Questions
Michael Sioutis Constraint Satisfaction Problems in Python
ConstraintSatisfactionProblems in
Python
MichaelSioutis
Outline
Introduction
Constraints inPython
Examples
Questions
Definition
A Constraint Satisfaction Problem consists of:
A Finite set of variables: V1,V2, ...,Vn.
A Nonempty domain of possible values for each variable:DV1 ,DV2 , ...,DVn .
A Finite set of constraints: C1,C2, ...,Cn.
Each constraint Ci limits the values that variables can take(e.g., V1 6= V2).
Michael Sioutis Constraint Satisfaction Problems in Python
ConstraintSatisfactionProblems in
Python
MichaelSioutis
Outline
Introduction
Constraints inPython
Examples
Questions
Definition
A state is defined as an assignment of values to some orall variables.
A consistent assignment is an assignment that does notviolate the constraints.
A complete assignment is an assignment that includes allvariables.
A problem solution is a complete and consistentassignment.
Michael Sioutis Constraint Satisfaction Problems in Python
ConstraintSatisfactionProblems in
Python
MichaelSioutis
Outline
Introduction
Constraints inPython
Examples
Questions
Benefits
Standard representation pattern.
Generic goal and successor functions.
Generic heuristics (no domain specific expertise).
Michael Sioutis Constraint Satisfaction Problems in Python
ConstraintSatisfactionProblems in
Python
MichaelSioutis
Outline
Introduction
Constraints inPython
Examples
Questions
Applications
Scheduling the time of observations on the Hubble SpaceTelescope
Airline schedules
Cryptography
Computer vision → image interpretationScheduling your MS or PhD thesis exam ,
Michael Sioutis Constraint Satisfaction Problems in Python
ConstraintSatisfactionProblems in
Python
MichaelSioutis
Outline
Introduction
Constraints inPython
Examples
Questions
Constraint Programming In Python Possible?
Constraint satisfaction problems are mathematicalproblems defined as a set of objects whose state mustsatisfy a number of constraints or limitations.We only need to specify the problem, even better if wecould do it in Python...
We can!
Michael Sioutis Constraint Satisfaction Problems in Python
ConstraintSatisfactionProblems in
Python
MichaelSioutis
Outline
Introduction
Constraints inPython
Examples
Questions
Python Constraint Library
Python module python-constraint1 offers solvers forConstraint Satisfaction Problems over finite domains insimple and pure Python.
Download and install python-constraint from here:http://labix.org/download/python-constraint/
python-constraint-1.1.tar.bz2
After you setup, you should be able to run the followingcommand on a python shell:
from constraint import *
1http://labix.org/python-constraint
Michael Sioutis Constraint Satisfaction Problems in Python
http://labix.org/download/python-constraint/python-constraint-1.1.tar.bz2http://labix.org/download/python-constraint/python-constraint-1.1.tar.bz2http://labix.org/python-constraint
ConstraintSatisfactionProblems in
Python
MichaelSioutis
Outline
Introduction
Constraints inPython
Examples
Questions
Features Of Python-Constraint
Solvers
Backtracking solverRecursive backtracking solverMinimum conflicts solver
Predefined constraint types
FunctionConstraintAllDifferentConstraintAllEqualConstraintExactSumConstraintMaxSumConstraintMinSumConstraintInSetConstraintNotInSetConstraintSomeInSetConstraintSomeNotInSetConstraint
Michael Sioutis Constraint Satisfaction Problems in Python
ConstraintSatisfactionProblems in
Python
MichaelSioutis
Outline
Introduction
Constraints inPython
Examples
Questions
Basic Example
Solve the a + b = 5, a ∗ b = 6 algebra relation.from constraint import *
problem = Problem()
problem.addVariable(’a’, range(5))
problem.addVariable(’b’, range(5))
problem.addConstraint(lambda a, b: a + b == 5)
problem.addConstraint(lambda a, b: a * b == 6)
solutions = problem.getSolutions()
print solutions
[{’a’: 3, ’b’: 2}, {’a’: 2, ’b’: 3}]
Michael Sioutis Constraint Satisfaction Problems in Python
ConstraintSatisfactionProblems in
Python
MichaelSioutis
Outline
Introduction
Constraints inPython
Examples
Questions
Advanced Example
Solve the 3× 3 Magic Squares2 problem.from constraint import *
problem = Problem()
problem.addVariables(range(0, 9), range(1, 9+1))
problem.addConstraint(AllDifferentConstraint(), range(0, 9))
problem.addConstraint(ExactSumConstraint(15), [0,4,8])
problem.addConstraint(ExactSumConstraint(15), [2,4,6])
for row in range(3):
problem.addConstraint(ExactSumConstraint(15),
[row*3+i for i in range(3)])
for col in range(3):
problem.addConstraint(ExactSumConstraint(15),
[col+3*i for i in range(3)])
solution = problem.getSolution()
print solution
{0: 6, 1: 7, 2: 2, 3: 1, 4: 5, 5: 9, 6: 8, 7: 3, 8: 4}
2http://en.wikipedia.org/wiki/Magic_square
Michael Sioutis Constraint Satisfaction Problems in Python
http://en.wikipedia.org/wiki/Magic_square
ConstraintSatisfactionProblems in
Python
MichaelSioutis
Outline
Introduction
Constraints inPython
Examples
Questions
The End
Thank you!-
Any Questions?
Michael Sioutis Constraint Satisfaction Problems in Python
OutlineIntroductionConstraints in PythonExamplesQuestions