0% found this document useful (0 votes)
195 views4 pages

Ai Week-1

The document describes two programs for implementing table-driven agents for a vacuum cleaner world. The first program defines a VacuumAgent class that uses an internal state variable to determine whether to clean or move based on if the current square is dirty or clean. The second program defines a TableDrivenVacuumAgent class that uses a lookup table to map percept-action pairs to actions like sucking or moving left/right. Both programs are demonstrated with examples of running the agent on a simulated environment.

Uploaded by

sadiqali4244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
195 views4 pages

Ai Week-1

The document describes two programs for implementing table-driven agents for a vacuum cleaner world. The first program defines a VacuumAgent class that uses an internal state variable to determine whether to clean or move based on if the current square is dirty or clean. The second program defines a TableDrivenVacuumAgent class that uses a lookup table to map percept-action pairs to actions like sucking or moving left/right. Both programs are demonstrated with examples of running the agent on a simulated environment.

Uploaded by

sadiqali4244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like