Model based Reflex Agent

Example 1 : Model based Vacuum Cleaner Agent

Source Code :

class ModelBasedReflexAgent:
    def __init__(self):
        self.state = {"location": "A", "status": "Clean"}
        self.model = {"A": {"Clean": {"Suck": "Dirty", "MoveRight": "Clean"}},
                      "B": {"Clean": {"Suck": "Dirty", "MoveLeft": "Clean"}}}
        self.rules = [
            {"condition": {"location": "A", "status": "Dirty"}, "action": "Suck"},
            {"condition": {"location": "A", "status": "Clean"}, "action": "MoveRight"},
            {"condition": {"location": "B", "status": "Dirty"}, "action": "Suck"},
            {"condition": {"location": "B", "status": "Clean"}, "action": "MoveLeft"}
        ]
        self.action = None

    def update_state(self, action, percept):
        if action == "MoveRight":
            self.state["location"] = "B"
        elif action == "MoveLeft":
            self.state["location"] = "A"
        elif action == "Suck":
            self.state["status"] = "Clean"
            percept["status"] = "Clean"

    def rule_match(self, state, rules):
        for rule in rules:
            if rule["condition"] == state:
                return rule
        return None  # Return None if no matching rule is found

    def model_based_reflex_agent(self, percept):
        self.state = self.update_state(self.action, percept)
        rule = self.rule_match(self.state, self.rules)
        if rule is not None:
            self.action = rule["action"]
        else:
            self.action = None
        return self.action

# Example percept sequence
percept_sequence = [{"location": "A", "status": "Dirty"},
                    {"location": "A", "status": "Clean"},
                    {"location": "B", "status": "Dirty"},
                    {"location": "B", "status": "Clean"}]

# Create and run the agent
agent = ModelBasedReflexAgent()

for percept in percept_sequence:
    action = agent.model_based_reflex_agent(percept)
    print(f"Percept: {percept} - Action: {action}")

Output

Percept: {'location': 'A', 'status': 'Dirty'} - Action: None
Percept: {'location': 'A', 'status': 'Clean'} - Action: None
Percept: {'location': 'B', 'status': 'Dirty'} - Action: None
Percept: {'location': 'B', 'status': 'Clean'} - Action: None

Example 2: Linear Model based Simple Agent

Source Code

class LinearModelBasedAgent:
    def __init__(self, initial_state):
        self.state = initial_state
        self.model_coefficient = 2  # Example coefficient for the linear equation
        self.action = None

    def update_state(self, action):
        # Simple linear equation: next_state = coefficient * current_state + action
        next_state = self.model_coefficient * self.state + action
        self.state = next_state
        return next_state

    def model_based_agent(self, action):
        self.action = action
        next_state = self.update_state(action)
        return next_state

# Example usage
initial_state = 1
agent = LinearModelBasedAgent(initial_state)

# Perform actions and observe the next states
actions = [3, -2, 5, 1]
for action in actions:
    next_state = agent.model_based_agent(action)
    print(f"Action: {action} - Next State: {next_state}")

Output:

Action: 3 - Next State: 5
Action: -2 - Next State: 8
Action: 5 - Next State: 21
Action: 1 - Next State: 43

Example 3: Let’s consider a simple example of a model-based agent that predicts the temperature for the next day based on historical data and uses a mathematical equation as a model. The agent’s goal is to decide whether to bring an umbrella depending on the predicted temperature. Here’s a Python program demonstrating this model-based agent:

Source Code

class TemperaturePredictorAgent:
    def __init__(self):
        self.historical_data = [(25, "No"), (18, "Yes"), (30, "No"), (22, "Yes")]
        self.model_coefficients = {"intercept": 5, "slope": 0.8}
        self.threshold = 25  # If predicted temperature is above this threshold, don't bring an umbrella
        self.action = None

    def predict_temperature(self, historical_data):
        # Simple linear regression model: temperature = intercept + slope * day
        days = [i + 1 for i in range(len(historical_data))]
        predicted_temperature = self.model_coefficients["intercept"] + self.model_coefficients["slope"] * (len(historical_data) + 1)
        return predicted_temperature

    def decide_action(self, predicted_temperature):
        if predicted_temperature > self.threshold:
            self.action = "No Umbrella"
        else:
            self.action = "Bring Umbrella"

    def model_based_agent(self):
        predicted_temperature = self.predict_temperature(self.historical_data)
        self.decide_action(predicted_temperature)
        return predicted_temperature, self.action  # Return both temperature and action

# Create and run the agent
agent = TemperaturePredictorAgent()
temperature, action = agent.model_based_agent()
print(f"Predicted Temperature: {round(temperature)}°C - Action: {action}")

Output:

Predicted Temperature: 9°C - Action: Bring Umbrella