You are on page 1of 30

CONSTRAINT SATISFACTION

PROBLEMS

CHAPTER 4
RECALL THE PUZZLES BELOW
• Would the problem solving approaches we have gone
through solve them? i.e. path finding search and
adversarial searches
CONSTRAINT SATISFACTION
PROBLEMS (CSPS)

• Imperative programming -the developer describing the


step for achieving a goal (some kind of result). E.g.
Procedural programming (e.g. C, Java).
• Declarative programming - we describe the goal, and the
computer gives us a solution. E.g. in SQL and Functional
Programming (e.g. Go, Haskell…).
• Constraint programming is an example of the declarative
programming paradigm, as opposed to the usual imperative
paradigm that we use most of the time.
CONSTRAINT SATISFACTION
PROBLEMS (CSPS)
• A constraint satisfaction problem (CSP) is a problem
that requires its solution within some (constraints). It
consists of the following:
• A finite set of variables which stores the solution. (V = {V1,
V2, V3,....., Vn} )
• A set of discrete values known as domain from which the
solution is picked. (D = {D1, D2, D3,.....,Dn} )
• A finite set of constraints. (C = {C1, C2, C3,......, Cn} )
CSP EXAMPLE - SUDOKU

EMPTY CELLS=53

• Other popular CSP examples are:


• -n-Queen (In an n-queen problem, n queens should be placed in a nXn matrix
such that no queen shares the same row, column or diagonal.)
• -Map Coloring (Coloring different regions of map ensuring no adjacent regions
have the same color.)
• -Crosswords (Everyday puzzles appearing in newspapers.)
CPS VERSUS STANDARD SEARCH
ALGORITHMS
In Standard search problem we used any data structure
to represent the environment (state) i.e. heuristic
function, and goal test
• In CSP the environment is defined by variables Xi
with values from domain Di. The goal test is a set of
constraints specifying allowable combinations of
values for subsets of variables
• This approach allows useful general-purpose
algorithms with more power than standard search
algorithms
SIMPLE EXAMPLE

• A student did two quizzes and their sum total was above
5. The second test is out of 10. The scores of the first test
were either 1, 2, or 3. What was the actual score of each
test?
• The problem can be represented as follows:
INBUILT CLASS CONSTRAINT

• We need to import constraint from


this python-constraint package. If
its not installed then install it: pip
install python-constraint
• You might get a warning
“unresolved constraint” due to
multiple occurrence of the class.
Ignore it, the default is what we
need.
FUNCTIONS IN CLASS
CONSTRAINT
• User defined functions –
our_constraint() x+y=5
• AddVariable() - Needs variable and
Domain
• addConstraint() - Needs user
defined constraints, list of variables
• getSolutions() - returns
combinations of variable values
that satisfy all the conditions.
PYTHON-CONSTRAINT LIBRARY

• Predefined constraint types currently available in the


library:
AllDifferentConstraint Enforces that values of all given variables are different
AllEqualConstraint Enforces that values of all given variables are equal

MaxSumConstraint Enforces that values of given variables sum up to a given amount

ExactSumConstraint Enforces that values of given variables sum exactly to a given amount

Constraint enforcing that values of given variables sum at least to a


MinSumConstraint
given amount
Constraint enforcing that values of given variables are present in the
InSetConstraint
given set
Constraint enforcing that values of given variables are not present in the
NotInSetConstraint
given set

Constraint enforcing that at least some of the values of given variables


SomeInSetConstraint
must be present in a given set

Constraint enforcing that at least some of the values of given variables


SomeNotInSetConstraint
must not be present in a given set
EXAMPLE - CRYPTO ARITHMETIC
PUZZLES

• In the above puzzles letters are used to represent digits


from 0 to 9 such that the arithmetic operations would be
correct.
INBUILT CONSTRAINTS

• Variables - 'T' and 'F' can't be zero since


they're the first digits in a number
(range 1-10). The rest can include a 0
(range 0-10). The convention is that
variables start with a capital letter--X,
Y, Name, ..., while constants start with
a lower case letter--ball, node, graph, a,
b,
• Constraints
• (1) user defined– must add up to the
solution
• (2) in built – all letters are different
AllDifferentConstraint()
EXAMPLE II- CRYPTO
ARITHMETIC PUZZLES
EXERCISE

• Solve the following problem.


VARIETIES OF VARIABLES

• CSPs work with different types of variables:


• Discrete variables
• finite domains: contain specific n variables e.g. Boolean {true,
false}
• infinite domains: can have any integers or strings e.g. date

• Continuous variables
• Any real number between two values e.g. temperatures
between 0 and 1 can have values such as 0.10, 0.11, 0.12..
VARIETIES OF CONSTRAINTS
• CSPs work with different types number of variables:
• Unary constraints involve a single variable,
• e.g., x=1, SA ≠ green
• Binary constraints involve pairs of variables,
• e.g., x ≠y, SA ≠ WA
• Higher-order constraints involve 3 or more variables,
• e.g., cryptarithmetic column constraints, (a*100 + b*45 +
c*10 + d*25) <= 3000:
PROCUREMENT CSP EXAMPLE

• A head teacher sends the schools procurement officer to buy


exercise books with $100. With this $100, the officer must buy at
least 80 exercise books and use up all the cash. A plain book
costs $1, a ruled book costs $5, and a squared book costs $10.
The officer must buy at least one book of each type. The officer
notices that these constraints remain the same, each time he
receives the request. However, the figures keep varying. He
decides to write a program to help him solve this problem by
informing him about the number of books required for each type.
SOLUTION
• A head teacher sends the schools
procurement officer to buy exercise books
with $100. With this $100, the officer must
buy at least 80 exercise books and use up
all the cash. A plain book costs $1, a
ruled book costs $5, and a squared book
costs $10. The officer must buy at least
one book of each type. The officer notices
that these constraints remain the same, each
time he receives the request. However, the
figures keep varying. He decides to write a
program to help him solve this problem by
informing him about the number of books
required for each type.
COMPLEX EXAMPLES
• Your are on a school trip to a chocolate
factory. The factory allows you to take
home some freebies
COMPLEX EXAMPLES
• But there are some rules:
• The amount of chocolate you take cant be over 3kg, should be worth
less than $300 and should be able to fit in a 10dm3 pack which they
have provided
• You have your own goal-you want to get the most sweetness 
• Name Weight(g) Dimensions(cm) Sweetness Value($)

• Chocolate A
• Chocolate B
• Chocolate C
• Chocolate D
CONSTRAINTS

• The sum of weights, the number of bars x weight of each bar (a*100
+ b*45 + c*10 + d*25), should be less than 3kg (<=3000g=3kgs)
• We need to generate similar constraints for the rest of the variables
• volume -->(a*8*2.5*0.5 + b*6*2*0.5 * c*2*2*0.5 + d*3*3*0.5) <=
1000cm3=10dm
• value--> (a*8 + b*6.8 + c*4 + d*3) < 300
VARIABLES & DOMAINS

• Based on the weight limit you cant carry more than 30 bars of
Chocolate A. Based on value we cant bring more than 37 bars,
and based on volume we cant bring more than 100bars.

• If we select 37 bars or 100 bars based on the value or volume we


will be violating the weight rule. Therefore the safest bet is to
pick the smallest number 30 bars. Our first variable therefore is
that we cant pick more than 30 bars of Chocolate A (range(30)
• Similar reasoning will give us the maximum of the rest as
• B -> 44, C -> 75, D -> 100
• Be patient, this is a complex program therefore it will
take time to run!!!
EXAMPLE - SUDOKU

• VARIABLES – we give them names 11 through 99

• Domains – range of [1..9]

• constraint
• all values in a row must be different
• all values in a columns must be different
• each nine (3x3) squares must have all different values,
• Cells already solved should not be solved
SUDOKU
EXERCISE
EXERCISE - OPTIMIZATION
• You have been requested to write a program for a drinks vending
machine at your college that assists in providing change. The user
inserts coins upto a total of Ksh.50, then they select a drink. Simulate
this by prompting for input of the drink cost and cash given. Change
is provided in denomination of 1, 5, 10, 20 and 40 shillings coins.
The machine cannot issue more than 5 pieces of 1 shilling coins, 4
pieces of 5 shillings coins, 2 pieces of 10 shillings coins, 2 pieces of
20 shillings coins, and 1 piece of 40 shillings coins.
• Write a program that can determine how much change the user
should get. It should recommend the combination that uses the least
number of coins.
ZEBRA PUZZLE
TIMETABLING PROBLEM
• Assume that you are in charge of creating a laboratory schedule for the classes in the Department of
Computing at UISU-A. Begin by finding out how many classes are offered in a semester for all programs
running in the department, how many laboratories are used, and how many faulty members teach in a
semester. A class schedule prepared by the academic advisor, indicates when a specific class should run
and who will be teaching it. They ensure that no collisions occur i.e. no faculty selects two courses that
run at the same time. However, the responsibility of ensuring that these classes are allocated a laboratory
with no collisions is your responsibility. Create a tentative class schedule and use it to write a program
that can generate the lab schedule – The lab schedule allocates each class to a lab. Factor in constraints
such as the number of labs, and permitted time slots (based on the class schedule).

• For extra credit: Write a program that can generate the class schedule when provided with the list of
courses and faculty teaching the courses. Assume undergraduate courses run twice a week (Mon/Wed or
Tue/Thur 7:00am – 9:00pm) and graduate courses run one a week from (Mon-Friday 5:40pm).
Undergraduate classes should run at the same time.

You might also like