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

NUML UNIVERSITY OF MODERN

LANGUAGES AND SCIENCES

BSCS-36
RC-298
Hanzala amir

Artificial
Intelligence
(Lab6)
LAB 5: Intelligent Agents

5.1.3 TASK:
o Can you name few model-based reflex agents?
o Write a program for model-based reflex agent of your own choice.
o Consider the vacuum world shown in the figure below:

This particular world has just two locations: squares A and B. The vacuum agent perceives
which square it is in and whether there is dirt in the square. It can choose to move left, move
right, suck up the dirt, or do nothing. One very simple agent function is the following: if the
current square is dirty, then suck, otherwise move to the other square. Write a simple reflex agent
for the vacuum cleaner. (Hint: Agent has no initial states knowledge)

If the current square is dirty, then suck; otherwise, move to the other square.
Pseudocode to the task is as follows;
function Reflex-Vacuum-Agent( [location,status]) returns an
action if status = Dirty then return Suck else if location = A then
return Right else if location = B then return Left
class VacuumAgent:

def __init__(self):
self.location = 'A' # Starting location of the vacuum cleaner
self.percept_history = {'A': 'Clean', 'B': 'Clean'} # Percept
history to keep track of cleanliness

def act(self, percept):


# Update percept history
self.percept_history[self.location] = percept

# Determine action based on current percept


action = self.choose_action(percept)

# Execute action
if action == 'Suck':
print("Sucking dirt at location", self.location)
elif action == 'Left':
self.location = 'A'
print("Moving to location A")
elif action == 'Right':
self.location = 'B'
print("Moving to location B")

def choose_action(self, percept):


# Choose action based on current percept and history
if percept == 'Dirty':
return 'Suck'
elif self.location == 'A':
return 'Right'
elif self.location == 'B':
return 'Left'

# Main function to simulate the environment


def main():
agent = VacuumAgent()
environment = {'A': 'Dirty', 'B': 'Clean'} # Initial environment
state
env = {'A': 'Clean', 'B': 'Clean'}
print("Initial Environment State:", environment)

for _ in range(2): # Simulate 2 time steps


agent.act(environment[agent.location])
print("Environment State after action:", env)
if __name__ == "__main__":
main()

You might also like