What is a simple reflex agent?

Introduction

A simple reflex agent is the simplest of all the agent programs. Decisions or actions are taken based on the current percept, which does not depend on the rest of the percept history. These agents react only to the predefined rules. It works best when the environment is fully observable.

Example

The vacuum agent is a simple reflex agent because the decision is based only on the current location, and whether the place contains dirt. 

A simple reflex agent comprises the following parts:

  • Agent: The agent is the one who performs actions on the environment.

  • Sensors: Sensors are the things that sense the environment. They are devices that measure physical property.

  • Actuators: Actuators are devices that convert energy into motion.

  • Environment: The environment includes the surroundings of the agent.

A simple reflex agent

Explanation

Here is an explanation of the above diagram:

  • The actions are taken depending upon the condition. If the condition is true, the relevant action is taken. If it is false, the other action is taken.

  • The agent takes input from the environment through sensors, and delivers the output to the environment through actuators.

  • The colored rectangles denote the current internal state of the agent’s decision process.

  • The ovals represent the background information used in the process.

Simulation

Now let's enhance our knowledge with an executable example. Click the "Run" button to execute the code.

class VacuumAgent:
def __init__(self):
self.location = 0 # Initial location of the agent
self.environment = ['clean'] * 5 # Initialize a 5-cell environment, initially clean
def sense(self):
return self.environment[self.location] # Sense the current location's cleanliness
def act(self):
if self.sense() == 'dirty':
self.clean()
else:
self.move()
def clean(self):
print("Cleaning cell", self.location)
self.environment[self.location] = 'clean' # Mark the current cell as clean
def move(self):
print("Moving to the next cell")
self.location = (self.location + 1) % len(self.environment) # Move to the next cell cyclically
# Test the agent
def test_agent():
agent = VacuumAgent()
print("Initial environment:", agent.environment)
for _ in range(6): # Run for 6 iterations
agent.act()
print("Final environment:", agent.environment)
if __name__ == "__main__":
test_agent()

In this example, the VacuumAgent class represents our simple reflex agent. It has methods to sense the cleanliness of the current location (sense()), clean the current location (clean()), and move to the next location (move()). The act() method decides whether to clean or move based on the current percept.

The test_agent() function initializes the agent, prints the initial environment, and then runs the agent for 6 iterations, printing the actions it takes and the final environment state.