Member-only story
Decision Making in AI Agents: From Rules to Intelligence
Hey folks! 👋 Today we’re diving deep into how AI agents make decisions. We’re going way beyond simple if-else statements and building something that can actually think through problems.
This is the 5th article in the 30-day AI Agents from Zero to Hero series. Check out the first 3 articles if you haven’t already.
Previous Articles: ( Day 1, Day 2, Day 3, Day 4)
What We’re Building
We’re creating a decision-making system that can:
- Evaluate multiple options
- Learn from experience
- Handle uncertainty
- Adapt to new situations
Let’s get coding!
Starting Simple: Rule-Based Decision Making
First, let’s look at a basic rule-based system to understand the foundations:
from enum import Enum
from typing import Dict, List, Any, Tuple
import random
from dataclasses import dataclass
class Action(Enum):
MOVE_LEFT = "left"
MOVE_RIGHT = "right"
MOVE_UP = "up"
MOVE_DOWN = "down"
WAIT = "wait"
@dataclass
class State:
position: Tuple[int, int]
energy: float
has_goal: bool
obstacles: List[Tuple[int, int]]
class SimpleDecisionMaker:
def decide(self, state: State) -> Action:
# Avoid obstacles
for obstacle in state.obstacles:
if self._is_adjacent(state.position, obstacle):
return self._avoid_obstacle(state.position, obstacle)
# If low energy, wait to recharge
if state.energy < 0.2:
return Action.WAIT
# Move towards goal if we have one
if state.has_goal:
return self._move_to_goal(state.position)
# Explore randomly
return random.choice(list(Action))
def _is_adjacent(self, pos1: Tuple[int, int],
pos2: Tuple[int, int]) -> bool:
return abs(pos1[0] - pos2[0]) + abs(pos1[1] - pos2[1]) == 1
def _avoid_obstacle(self, position: Tuple[int, int],
obstacle: Tuple[int, int]) -> Action:
x_diff = position[0] - obstacle[0]
y_diff = position[1] - obstacle[1]
if abs(x_diff) > abs(y_diff):
return Action.MOVE_RIGHT if x_diff < 0 else Action.MOVE_LEFT
else:
return Action.MOVE_DOWN if y_diff < 0 else…