Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint...

27
Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in Python Example Questions Constraint Satisfaction Problems in Python Michael Sioutis Department of Informatics and Telecommunications National and Kapodistrian University of Athens July 18, 2011 Michael Sioutis Constraint Satisfaction Problems in Python

Transcript of Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint...

Page 1: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Constraint Satisfaction Problems in Python

Michael Sioutis

Department of Informatics and TelecommunicationsNational and Kapodistrian University of Athens

July 18, 2011

Michael Sioutis Constraint Satisfaction Problems in Python

Page 2: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Table of Contents

1 Introduction

2 Constraints in Python

3 Example

4 Questions

Michael Sioutis Constraint Satisfaction Problems in Python

Page 3: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

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

Page 4: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

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

Page 5: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

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

Page 6: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

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

Page 7: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

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

Page 8: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

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

Page 9: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

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

Page 10: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Benefits

Standard representation pattern

Generic goal and successor functions

Generic heuristics (no domain specific expertise)

Michael Sioutis Constraint Satisfaction Problems in Python

Page 11: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Benefits

Standard representation pattern

Generic goal and successor functions

Generic heuristics (no domain specific expertise)

Michael Sioutis Constraint Satisfaction Problems in Python

Page 12: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Benefits

Standard representation pattern

Generic goal and successor functions

Generic heuristics (no domain specific expertise)

Michael Sioutis Constraint Satisfaction Problems in Python

Page 13: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Applications

Scheduling the time of observations on the Hubble SpaceTelescope

Airline schedules

Map coloring

Cryptography

Scheduling your MS or PhD thesis exam ,

Michael Sioutis Constraint Satisfaction Problems in Python

Page 14: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Applications

Scheduling the time of observations on the Hubble SpaceTelescope

Airline schedules

Map coloring

Cryptography

Scheduling your MS or PhD thesis exam ,

Michael Sioutis Constraint Satisfaction Problems in Python

Page 15: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Applications

Scheduling the time of observations on the Hubble SpaceTelescope

Airline schedules

Map coloring

Cryptography

Scheduling your MS or PhD thesis exam ,

Michael Sioutis Constraint Satisfaction Problems in Python

Page 16: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Applications

Scheduling the time of observations on the Hubble SpaceTelescope

Airline schedules

Map coloring

Cryptography

Scheduling your MS or PhD thesis exam ,

Michael Sioutis Constraint Satisfaction Problems in Python

Page 17: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Applications

Scheduling the time of observations on the Hubble SpaceTelescope

Airline schedules

Map coloring

Cryptography

Scheduling your MS or PhD thesis exam ,

Michael Sioutis Constraint Satisfaction Problems in Python

Page 18: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

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 and make use of its powerfulfeatures...

We can! With the python-contraint1 module.

1http://labix.org/python-constraintMichael Sioutis Constraint Satisfaction Problems in Python

Page 19: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

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 and make use of its powerfulfeatures...

We can! With the python-contraint1 module.

1http://labix.org/python-constraintMichael Sioutis Constraint Satisfaction Problems in Python

Page 20: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Python-Constraint Library

Python module python-constraint 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 *

Michael Sioutis Constraint Satisfaction Problems in Python

Page 21: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Python-Constraint Library

Python module python-constraint 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 *

Michael Sioutis Constraint Satisfaction Problems in Python

Page 22: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Features Of Python-Constraint

Solvers

Backtracking solverRecursive backtracking solverMinimum conflicts solver

Predefined constraint types (e.g., AllDifferentConstraint,FunctionConstraint)

Michael Sioutis Constraint Satisfaction Problems in Python

Page 23: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Features Of Python-Constraint

Solvers

Backtracking solverRecursive backtracking solverMinimum conflicts solver

Predefined constraint types (e.g., AllDifferentConstraint,FunctionConstraint)

Michael Sioutis Constraint Satisfaction Problems in Python

Page 24: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Solving An Algebraic Relation

Solve the a + b = 5, a ∗ b = 6 algebraic 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

Page 25: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Solving An Algebraic Relation

Solve the a + b = 5, a ∗ b = 6 algebraic 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

Page 26: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

Solving An Algebraic Relation

Solve the a + b = 5, a ∗ b = 6 algebraic 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

Page 27: Constraint Satisfaction Problems in Pythonsioutis/files/CSPsPy.pdf · 2013-12-06 · Constraint Satisfaction Problems in Python Michael Sioutis Outline Introduction Constraints in

ConstraintSatisfactionProblems in

Python

MichaelSioutis

Outline

Introduction

Constraints inPython

Example

Questions

The End

Thank you!-

Any Questions?

Michael Sioutis Constraint Satisfaction Problems in Python