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

Artificial Intelligence

Lab Journal 8

SAMMA KHALIL
01-134191-099
BSCS-6B

Department of Computer Science


BAHRIA UNIVERSITY, ISLAMABAD
Lab # 8: Constraint Satisfaction Problems:

Objectives:

To implement the concept of CSP in Python.

Tools Used:

Spyder IDE.

Submission Date:14/4/2020

Evaluation: Signatures of Lab Engineer:


Task #1:

Apply forward checking to solve the problem of hotel room assignment. Map of rooms
is given below. Two adjacent rooms should not be allocated to persons of same
country. Persons x,y and z belongs to country P. Persons a and b belongs to country Q,
person s belong to country R.

A D F

B C

Procedure/Program:
Rooms = [
[0,1,0,0,0,0] ,
[1,0,1,0,0,0] ,
[0,1,0,0,0,0] ,
[0,0,0,0,0,1] ,
[0,0,0,0,0,0] ,
[0,0,0,1,0,0]
]

Persons = [['x' , 'y' , 'z'] , ['a' , 'b'] , ['s']]


Room = ['A' , 'B' , 'C' , 'D' , 'E' , 'F']
P = ['x' , 'y' , 'z']
Q = ['a' , 'b']
R = ['s']
Countries = [P , Q , R]
Solution = ['','','','','','']

def Select_Person():
for i in range(len(Countries)):
for j in range(len(Countries[i])):
return Countries[i][j]
def Select_Other_Country_Person(p):
for i in range(len(Countries)):
if p in Countries[i]:
if len(Countries[i-1]) !=0 :
return Countries[i-1][0]
def Remove_Person(p):
for i in range(len(Countries)):
if p in Countries[i]:
Countries[i].remove(p)

def Check_Neighbours(k,person):
for i in range (0,6):
if (Rooms[k][i]==1):
neighbour = Solution[i]
for i in range(0,len(Persons)):
if( person in Persons[i] ) and (neighbour in Persons[i]):
return False
return True

def Assign_Rooms(k):
p = Select_Person()
if(Check_Neighbours(k,p) == True):
Solution[k]=p
Remove_Person(p)
if(k+1<6):
Assign_Rooms(k+1)

else:
p = Select_Other_Country_Person(p)
if(Check_Neighbours(k,p) == True):
Solution[k]=p
Remove_Person(p)
if(k+1<6):
Assign_Rooms(k+1)

Assign_Rooms(0)
print('Person : ' , Solution[0] , " in Room A")
print('Person : ' , Solution[1] , " in Room B")
print('Person : ' , Solution[2] , " in Room C")
print('Person : ' , Solution[3] , " in Room D")
print('Person : ' , Solution[4] , " in Room E")
print('Person : ' , Solution[5] , " in Room F")

Result/Output:

Analysis/Conclusion:

In this task we learn Allocate the Rooms to Persons with the help of Forward Checking
Algorithm.
Task # 2:

In an exhi bi ti on, st al l s need t o get al l ocat ed.


The al l ocat i on shoul d be accordi ng fol l owi ng condi t i ons.
 There are tot al t hree bl ocks (A, B , C ).
 There are si x t ypes of st al l s. (food, cosm et i cs, garm ent s, t oys, shoes, j ewel l ery).
 (A)= (food,t oys,shoes,j ewel l ery)
 (B ) = (t oys, shoes, j ewel l ery).
 (C ) = (food, cosm et i cs, garm ent s, toys, shoes, j ewel l ery).
 B l ock A can have onl y t hose t ypes of st al l s whi ch are i n B l ock C .
 B l ock C shoul d have t hose t ypes of st al l s whi ch are i n Bl ock A.
 There shoul d be no st al l t ype i n B l ock A t hat exi st s i n Bl ock B .
 B l ock B can have m axi m um of t wo t ypes of st al l s t hat are com m on t o st all s i n bl ock C .

Procedure/Program:
from random import randint
Stalls=['food','cosmetics','garments','toys','shoes','jewellery']
A=list()
B=list()
C=list()

i=randint(0,5)
j=randint(0,5)

B.append(Stalls[i])
B.append(Stalls[j])

for k in range(0,len(Stalls)):
if(k!=i and k != j):
A.append(Stalls[k])
C.append(Stalls[k])

print("A : ",A)
print("B : ",B)
print("C : ",C)
Result/Output:

Analysis/Conclusion:

In this task we learn Allocate the stalls with Forward Checking Algorithm.

You might also like