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
colorslist "blue red green white yellow".split
nationalitieslist "Norwegian German Dane Swede English".split
petslist "birds dog cats horse fish".split
drinkslist "tea coffee milk beer water".split
cigaretteslist "Blend, Prince, Blue Master, Dunhill, Pall Mall".split
# There are five houses.
minhouse, maxhouse
problem Problem
# value of a variable is the number of a house with corresponding property
allvariables colorslist nationalitieslist petslist drinkslist cigaretteslist
problem.addVariablesallvariables, rangeminhouse, maxhouse
# 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 vargroup in colorslist, nationalitieslist, petslist, drinkslist, cigaretteslist:
problem.addConstraintAllDifferentConstraint vargroup
# In the middle house they drink milk.
# NOTE: interpret "middle" in a numerical sense not geometrical
problem.addConstraintInSetConstraintminhouse maxhousemilk
# The Norwegian lives in the first house.
# NOTE: interpret "the first" as a house number
problem.addConstraintInSetConstraintminhouseNorwegian
# The green house is on the left side of the white house.
# XXX: what is "the left side"? linear circular, two sides, D house arrangment
# NOTE: interpret it as 'green house number' 'white house number'
problem.addConstraintlambda a b: a bgreen "white"
def addconstraintsconstraint statements, variablesallvariables, problemproblem:
for statement in line for line in statements if line.strip:
problem.addConstraintconstraintv for v in variables if v in statement
andstatements
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
addconstraintslambda a b: a b andstatements
nexttostatements
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 tolinear circular, two sides, D house arrangment
addconstraintslambda a b: absa b nexttostatements
def solvevariablesallvariables, problemproblem:
from itertools import groupby
from operator import itemgetter
# find & print solutions
for solution in problem.getSolutionIter:
for key, group in groupbysortedsolutionitems keyitemgetter keyitemgetter:
printkey end
for v in sorteddictgroupkeys keyvariables.index:
printvljust end
print
if namemain:
solve
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
