Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

LAB TASK

Q1: Implement the color mapping of the following map using CSP.
Flow-Chart:
Code:
class CSP():
def __init__(self, variables, domains,constraints):
self.variables = variables # variables to be constrained
self.domains = domains # domain of each variable
self.constraints = constraints

def add_constraint(self, constraint):


for v in constraint.variables:
self.constraints[v].append(constraint)

def consistent(self, variable, assignment):


for c in self.constraints[variable]:
if not c.satisfied(assignment):
return False
return True

def backtracking_search(self, assignment={}):


if len(assignment) == len(variables):
return assignment
unassigned = [v for v in variables if v not in assignment]
first = unassigned[0]
for value in self.domains[first]:
local_assignment = assignment.copy()
local_assignment[first] = value
#print(local_assignment)
if self.consistent(first,local_assignment):
result = self.backtracking_search(local_assignment)

if result != None:
return result

class MapColoringConstraint():
def __init__(self, place1, place2):
self.variables = [place1, place2]

def __repr__(self):
return repr(self.variables)

def satisfied(self, assignment):


if self.variables[0] not in assignment or self.variables[1] not in assignment:
return True
if assignment[self.variables[0]] == assignment[self.variables[1]]:
return False
else: return True

variables = ['sindh', 'balochistan', 'punjab', 'north west', 'northern


areas']
domains = {}
for v in variables:
domains[v] = ['R','G','B']

constraints={}
for v in variables:
constraints[v] = []

csp = CSP(variables,domains,constraints)

#add contraints using add_constraint method

c1 = MapColoringConstraint("sindh", "balochistan")
csp.add_constraint(c1)
csp.add_constraint(MapColoringConstraint("sindh", "punjab"))
csp.add_constraint(MapColoringConstraint("balochistan", "punjab"))
csp.add_constraint(MapColoringConstraint("balochistan", "north west"))
csp.add_constraint(MapColoringConstraint("punjab", "north west"))
csp.add_constraint(MapColoringConstraint("punjab", "northern areas"))
csp.add_constraint(MapColoringConstraint("north west", "northern areas"))

#call backtracking_search method

assignment= {}
solution = csp.backtracking_search(assignment)

# print solution if found

if solution != None:
print("\nYour solution is:")
print(solution)
else: print ("No solution found")

Discussion and Results:


We have used the following map in above constraint satisfactory problem. It contains 5 variables
sindh, balochistan, punjab, northwest, and northern areas.
This is the solution we get by running our code:

It is the correct solution as no constraint is violated in this solution.

Q2: Solve the 8-Queen problem using CSP.


Code:
class EightQueensCSP():
def __init__(self):
self.variables = list(range(8))
self.domains = {var: list(range(8)) for var in self.variables}
self.constraints = []

for i in range(8):
for j in range(i + 1, 8):
self.constraints.append((i, j))

def consistent(self, assignment):


for col1, col2 in self.constraints:
if col1 in assignment and col2 in assignment:
row1, row2 = assignment[col1], assignment[col2]
if row1 == row2 or abs(row1 - row2) == abs(col1 - col2):
return False
return True
def backtracking_search(self, assignment={}):
if len(assignment) == 8:
return assignment
unassigned = [v for v in self.variables if v not in assignment]
first = unassigned[0]

for value in self.domains[first]:


local_assignment = assignment.copy()
local_assignment[first] = value

if self.consistent(local_assignment):
result = self.backtracking_search(local_assignment)

if result is not None:


return result

return None

# Solving the 8-Queens problem


csp_8queens = EightQueensCSP()
solution = csp_8queens.backtracking_search()

if solution is not None:


solution_str = ''.join(str(solution[col] + 1) for col in
sorted(solution))
print("Solution found:", solution_str)
else:
print("No solution found")

Result:

Discussion:
By running our algorithm, we get this solution ‘15863724’ which is correct as no constraint is
violated.

You might also like