Question: How would I manually use backtracking algorithm with this: from constraint import AllDifferentConstraint, InSetConstraint, Problem # variables colors _ list = blue red green white

How would I manually use backtracking algorithm with this:
from constraint import AllDifferentConstraint, InSetConstraint, Problem
# variables
colors_list = "blue red green white yellow".split()
nationalities_list = "Norwegian German Dane Swede English".split()
pets_list = "birds dog cats horse fish".split()
drinks_list = "tea coffee milk beer water".split()
cigarettes_list = "Blend, Prince, Blue Master, Dunhill, Pall Mall".split(",")
# There are five houses.
min_house, max_house =1,5
problem = Problem()
# value of a variable is the number of a house with corresponding property
all_variables = colors_list + nationalities_list + pets_list + drinks_list + cigarettes_list
problem.addVariables(all_variables, range(min_house, max_house +1))
# Each house has its own unique color.
# All house owners are of different nationalities.
# They all have different pets.
# They all drink different drinks.
# They all smoke different cigarettes.
for var_group in (colors_list, nationalities_list, pets_list, drinks_list, cigarettes_list):
problem.addConstraint(AllDifferentConstraint(), var_group)
# In the middle house they drink milk.
# NOTE: interpret "middle" in a numerical sense (not geometrical)
problem.addConstraint(InSetConstraint([(min_house + max_house)//2]),["milk"])
# The Norwegian lives in the first house.
# NOTE: interpret "the first" as a house number
problem.addConstraint(InSetConstraint([min_house]),["Norwegian"])
# The green house is on the left side of the white house.
# XXX: what is "the left side"? (linear, circular, two sides, 2D house arrangment)
# NOTE: interpret it as 'green house number' +1== 'white house number'
problem.addConstraint(lambda a, b: a +1== b,["green", "white"])
def add_constraints(constraint, statements, variables=all_variables, problem=problem):
for statement in (line for line in statements if line.strip()):
problem.addConstraint(constraint,[v for v in variables if v in statement])
and_statements ="""
They drink coffee in the green house.
The man who smokes Pall Mall has birds.
The English man lives in the red house.
The Dane drinks tea.
In the yellow house they smoke Dunhill.
The man who smokes Blue Master drinks beer.
The German smokes Prince.
The Swede has a dog.
""".split("
")
add_constraints(lambda a, b: a == b, and_statements)
nextto_statements ="""
The man who smokes Blend lives in the house next to the house with cats.
In the house next to the house where they have a horse, they smoke Dunhill.
The Norwegian lives next to the blue house.
They drink water in the house next to the house where they smoke Blend.
""".split("
")
# XXX: what is "next to"?(linear, circular, two sides, 2D house arrangment)
add_constraints(lambda a, b: abs(a - b)==1, nextto_statements)
def solve(variables=all_variables, problem=problem):
from itertools import groupby
from operator import itemgetter
# find & print solutions
for solution in problem.getSolutionIter():
for key, group in groupby(sorted(solution.items(), key=itemgetter(1)), key=itemgetter(1)):
print(key, end='')
for v in sorted(dict(group).keys(), key=variables.index):
print(v.ljust(9), end='')
print()
if __name__=='__main__':
solve()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!