Menu

Building a Simple Intelligent Agent

Building a Simple Intelligent Agent

In this lesson, you will create a basic rule-based agent that decides whether to water a plant based on soil moisture.

This example demonstrates the relationship between percepts, rules, actions, and performance goals.

Problem Statement

Build a simple agent that:

  • Receives a soil moisture reading.
  • Compares the reading with a threshold.
  • Recommends watering when the soil is too dry.
  • Avoids watering when the soil is sufficiently moist.

For this example, assume that moisture is represented as a number from 0 to 100 and that values below 30 indicate dry soil. This threshold is illustrative, not a universal recommendation for real plants.

Python Implementation

class PlantWateringAgent:

def __init__(self, moisture_threshold=30):
self.moisture_threshold = moisture_threshold
def choose_action(self, soil_moisture):
if soil_moisture < self.moisture_threshold:
return "Water the plant"
return "No watering needed"
agent = PlantWateringAgent()
readings = [15, 25, 35, 60]
for moisture in readings:
action = agent.choose_action(moisture)
print(f"Soil moisture: {moisture} -> {action}")

Expected Output

Soil moisture: 15 -> Water the plant

Soil moisture: 25 -> Water the plant

Soil moisture: 35 -> No watering needed

Soil moisture: 60 -> No watering needed

Understanding the Code

  • The PlantWateringAgent class defines the agent.
  • The constructor stores the moisture threshold.
  • The choose_action() method receives a percept: the soil moisture reading.
  • The conditional statement applies a predefined rule.
  • The returned message represents the agent's selected action.

This is a simple reflex agent because it chooses an action using the current input and a predefined rule.

Improving the Agent

A real irrigation system would require more than a single threshold. Possible improvements include:

  • Validate readings and handle sensor failures.
  • Consider plant type and growth stage.
  • Check recent rainfall or weather forecasts.
  • Prevent excessive watering.
  • Track previous readings to detect trends.
  • Include a maximum watering duration.
  • Provide a manual override.

A production system should also distinguish between recommending an action and physically activating a water valve. Hardware controls need appropriate safeguards.

Practical Challenge

Extend the program so that it accepts both soil moisture and a rain forecast.

Requirements:

  1. If the soil is dry and rain is not expected, recommend watering.
  2. If rain is expected, postpone watering.
  3. If the soil is already sufficiently moist, do not water.
  4. Handle invalid moisture readings.

Test your program with at least five different inputs and explain why the agent selects each action.


Artificial Intelligence Tutorial

VA

Vishalini A



Learn via Video Course

Artificial Intelligence and Machine Learning Course Logo

Artificial Intelligence and Machine Learning Course

55 hrs



Get in Touch For Details