EXERCISE-01
Problem Statement:
Design and implement agent programs for Table-driven agents using the agent function of
vacuum-cleaner world. The agent cleans the current square if it is dirty, otherwise it moves to
the other square.
AIM: To design and implement agent programs for table driven agents using the agent function
of vacuum cleaner world. The agent cleans the current square if it is dirty otherwise it moves to
the other square.
A). program code: The agent cleans the current square if it is dirty, otherwise it moves to the
other square.
class VacuumAgent:
def __init__(self):
self.table = {
'Dirty': self.clean,
'Clean': self.move
self.current_state = 'Clean' # Initial state
def perceive(self, is_dirty):
if is_dirty:
self.current_state = 'Dirty'
else:
self.current_state = 'Clean'
def act(self):
action = self.table[self.current_state]
action()
def clean(self):
print("Cleaning the current square.")
def move(self):
print("Moving to the other square.")
# Example usage:
if __name__ == "__main__":
# Create a vacuum agent
vacuum_agent = VacuumAgent()
# Simulate the environment
dirty_states = [True, False, True, False] # Assume four squares, alternating dirty and clean
for dirty_state in dirty_states:
vacuum_agent.perceive(dirty_state)
vacuum_agent.act()
OUTPUT:
Cleaning the current square.
Moving to the other square.
Cleaning the current square.
Moving to the other square
B). Program code: Table-driven agents using the agent function of vacuum-cleaner world.
class TableDrivenVacuumAgent:
def __init__(self):
# Define the table with percept-action mappings
self.action_table = {
('Clean',): 'Suck', # If the current square is dirty, clean it
('Dirty',): 'Suck', # If the current square is dirty, clean it
('Clean', 'Left'): 'Right', # If the current square is clean and the agent is on the left, move to the
right
('Clean', 'Right'): 'Left', # If the current square is clean and the agent is on the right, move to the
left
('Dirty', 'Left'): 'Right', # If the current square is dirty and the agent is on the left, move to the
right
('Dirty', 'Right'): 'Left' # If the current square is dirty and the agent is on the right, move to the
left
def act(self, percept):
# Look up the action in the table based on the percept
action = self.action_table.get(percept, 'NoOp') # Default to NoOp if the percept is not in the table
return action
# Example of using the TableDrivenVacuumAgent
agent = TableDrivenVacuumAgent()
# Test cases
percept1 = ('Clean', 'Left')
percept2 = ('Dirty', 'Right')
action1 = agent.act(percept1)
action2 = agent.act(percept2)
print(f"Percept: {percept1}, Action: {action1}")
print(f"Percept: {percept2}, Action: {action2}")
OUTPUT:
Percept: ('Clean', 'Left'), Action: Right
Percept: ('Dirty', 'Right'), Action: Left