0% found this document useful (0 votes)
98 views

AI Lab Assignment # 3

This document contains code for an artificial intelligence assignment involving a vacuum cleaning agent in a two room environment. It defines an environment and agent as abstract base classes with required initialization and methods. It then implements a two room vacuum cleaning environment class with two rooms and methods for displaying perceptions and actions. It also implements a vacuum agent class with methods for sensing the environment and taking actions to clean rooms or move between rooms. The code provides an example of running the agent in the two room environment for 50 steps.

Uploaded by

Maryam Qazi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

AI Lab Assignment # 3

This document contains code for an artificial intelligence assignment involving a vacuum cleaning agent in a two room environment. It defines an environment and agent as abstract base classes with required initialization and methods. It then implements a two room vacuum cleaning environment class with two rooms and methods for displaying perceptions and actions. It also implements a vacuum agent class with methods for sensing the environment and taking actions to clean rooms or move between rooms. The code provides an example of running the agent in the two room environment for 50 steps.

Uploaded by

Maryam Qazi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ARTIFICIAL INTELLIGENCE

ASSIGNMENT # 3
Name: Anas Qazi
Reg#: 36444
Program: BSCS

from abc import abstractmethod


class Environment(object):
'''
classdocs
@abstractmethod
def __init__(self, n):
self.n = n
def executeStep(self,n=1):
raise NotImplementedError('action must be defined!')
def executeAll(self):
raise NotImplementedError('action must be defined!')
def delay(self, n=100):
self.delay = n
_________________________________________________________________________________
from abc import abstractmethod
class Agent(object):
'''
classdocs
'''
@abstractmethod
def __init__(self):
pass
@abstractmethod
def sense(self,environment):
pass
@abstractmethod
def act(self):
pass

____________________________________________________________________________________
from com.environment import Environment
from com.environment import Room
from com.agent import VaccumAgent
class TwoRoomVaccumCleanerEnvironment(Environment.Environment):
'''
classdocs
'''
def __init__(self, agent):
'''
Constructor
'''
self.r1 = Room.Room('A','dirty')
self.r2 = Room.Room('B','dirty')
self.agent = agent
self.currentRoom = self.r1
self.delay = 1000
self.step = 1
self.action = ""
def executeStep(self,n=1):
for _ in range(0,n):
self.displayPerception()
self.agent.sense(self)
res = self.agent.act()
self.action = res
if res == 'clean':
self.currentRoom.status = 'clean'
elif res == 'right':
self.currentRoom = self.r2
else:
self.currentRoom = self.r1
self.displayAction()
self.step += 1
def executeAll(self):
raise NotImplementedError('action must be defined!')
def displayPerception(self):
print("Perception at step %d is [%s,%s]" %
(self.step,self.currentRoom.status,self.currentRoom.location))

def displayAction(self):
print("------- Action taken at step %d is [%s]" %(self.step,self.action))

def delay(self, n=100):


self.delay = n

if __name__ == '__main__':
vcagent = VaccumAgent.VaccumAgent()
env = TwoRoomVaccumCleanerEnvironment(vcagent)
env.executeStep(50)

___________________________________________________________________________________
class Room:
def __init__(self,location,status="dirty"):
self.location = location
self.status = status
__________________________________________________________________________________
from com.agent import Agent
class VaccumAgent(Agent.Agent):
'''
classdocs
'''
def __init__(self):
'''
Constructor
'''
pass
def sense(self,env):
self.environment = env
def act(self):
if self.environment.currentRoom.status == 'dirty':
return 'clean'
if self.environment.currentRoom.location == 'A':
return 'right'
return 'left'

You might also like