Programming lesson
Intro to AI: Building a Tic-Tac-Toe Agent with Search Algorithms
Learn the fundamentals of AI by designing a tic-tac-toe agent, exploring PEAS, search algorithms, and heuristic functions. This tutorial covers breadth-first, depth-first, uniform cost, greedy best-first, and A* search with a timely fruit-sorting robot example.
Introduction to AI Agents and Search
Artificial intelligence (AI) is reshaping how we interact with technology, from AI-powered apps like ChatGPT to autonomous robots in warehouses. In this tutorial, we'll dive into the core concepts of AI by designing a tic-tac-toe agent—a classic problem that illustrates search algorithms and heuristic functions. Whether you're a student tackling an Intro to AI assignment or a curious learner, this guide will help you master the foundations.
PEAS Description for a Tic-Tac-Toe Agent
The PEAS framework (Performance, Environment, Actuators, Sensors) defines the task environment. For our tic-tac-toe robot:
- Performance: Win as many games as possible, minimize losses and draws.
- Environment: A 3x3 grid on paper, with X and O markers; the opponent (human or robot) and the game rules.
- Actuators: A robotic arm that draws X or O in a grid cell.
- Sensors: Camera or scanner to detect markers and empty cells.
The environment is fully observable (the robot sees the entire board), multi-agent (two players), deterministic (moves have predictable outcomes), sequential (each move affects future moves), static (board doesn't change while the robot thinks), discrete (finite number of states), and known (rules are known).
Search Algorithms in AI
Search is a fundamental AI problem-solving technique. Given a search tree, we explore nodes to find a goal. Let's apply breadth-first search (BFS), depth-first search (DFS), iterative deepening, uniform cost search (UCS), greedy best-first search, and A* search to a sample tree with nodes A through K, where F and K are goals. Path costs and heuristic values are provided.
Breadth-First Search (BFS)
BFS expands nodes level by level. Starting from A, we expand in order: A, B, C, D, E, F. Goal F is reached after expanding A, B, C, D, E, F.
Depth-First Search (DFS)
DFS goes deep first. Expand: A, B, E, K (goal reached). Order: A, B, E, K.
Iterative Deepening Search (IDS)
IDS combines BFS and DFS. Depth 0: A; Depth 1: A, B, C; Depth 2: A, B, E, F (goal). Expanded: A, B, C, A (repeated), B, E, F.
Uniform Cost Search (UCS)
UCS expands the node with the lowest path cost. Costs: A(0), B(2), C(4), D(6), E(5), F(7), G(9), H(8), I(10), J(11), K(9). Expand: A, B, C, E, D, F, G, K (goal). Order: A, B, C, E, D, F, G, K.
Greedy Best-First Search
Uses heuristic h(n). h(A)=6, B=4, C=4, D=3, E=3, F=0, G=2, H=1, I=2, J=1, K=0. Expand: A, B (tie with C, B first), D (tie with E, D first), H, J, K (goal). Order: A, B, D, H, J, K.
A* Search
Uses f(n)=g(n)+h(n). Compute f: A=0+6=6, B=2+4=6, C=4+4=8, D=6+3=9, E=5+3=8, F=7+0=7, G=9+2=11, H=8+1=9, I=10+2=12, J=11+1=12, K=9+0=9. Expand: A, B (tie with A, B first), E (f=8), C (f=8), F (goal). Order: A, B, E, C, F.
Fruit Sorting Robot: State Space and Search
Imagine a robot arm sorting crates of fruit—a timely example in AI for agriculture. The state space includes: positions of each crate, contents of each crate (fruit types), position of the robot arm, and whether the arm holds a fruit (and which type). Actions: move left/right, grab fruit from crate below, place fruit onto crate below. Goal test: each crate contains exactly one type of fruit, and all fruits are sorted. The optimal non-heuristic search algorithm is breadth-first search (BFS) because it guarantees the shortest path in terms of number of actions. The frontier is a FIFO queue. A reached set (closed set) is beneficial to avoid revisiting states, reducing time complexity.
Heuristic Functions and Consistency
A heuristic h(s) is consistent if it satisfies the triangle inequality. For a grid world with Manhattan distance M(s) and Euclidean distance E(s), a heuristic that returns a random number r between 0 and 1 is not consistent because it violates the condition h(s) ≤ cost(s, s') + h(s') for all s, s'. The random value does not relate to actual distances.
Hill Climbing for PIN Cracking
Hill climbing is a local search algorithm. For a 2-digit PIN (00-99), the agent tries candidates and moves to a neighbor with a better score. However, hill climbing can get stuck in local optima. This example mirrors AI in cybersecurity for password recovery. The algorithm's success depends on the evaluation function.
By understanding these AI search techniques, you can build intelligent agents for tic-tac-toe, robot sorting, and beyond. These concepts are foundational for machine learning and autonomous systems used in self-driving cars, game AI, and AI-powered apps like virtual assistants.