Skip to content

MrCoolGh/smart-traffic-light

Repository files navigation

🚦 Smart Traffic Light AI Simulation

This project is an AI simulation of a smart traffic light system built in Clojure. It demonstrates the decision-making processes of the four foundational AI agent architectures (as defined in standard Artificial Intelligence textbooks like Russell & Norvig's AIMA).

The simulation models an intersection where cars arrive randomly, and the traffic light agent must decide whether to switch the light to keep traffic flowing efficiently.

🧠 The Four AI Agents

This simulation runs the same environment through four different agent types to observe how their complexity changes their behavior:

1. Simple Reflex Agent

  • How it works: Makes decisions based only on the current environment state, ignoring history.
  • Logic: IF cars waiting > 5, THEN switch the light. Otherwise, do nothing.
  • Flaw: It has no memory. If 4 cars are waiting, it will let them sit at a red light forever because the rule hasn't been triggered.

2. Model-Based Reflex Agent

  • How it works: Maintains an internal state (memory) to track how the world evolves.
  • Logic: Looks at cars waiting and how long the light has been in its current state.
  • Advantage: Prevents "starvation." If the light has been red for more than 5 ticks, it forces a switch, even if there are only a few cars waiting.

3. Goal-Based Agent

  • How it works: Acts to achieve a specific, defined goal by projecting into the future.
  • Logic: Goal: Keep the number of waiting cars strictly below 4. It asks, "If I do nothing and more cars arrive, will I violate my goal?" If yes, it switches the light preemptively.
  • Advantage: Proactive rather than reactive.

4. Utility-Based Agent

  • How it works: Uses a numerical utility function to weigh the "costs" and "benefits" of its actions to maximize overall efficiency.
  • Logic: Calculates a mathematical score. It weighs the penalty of making cars wait against the penalty of switching the light too quickly (which causes wear-and-tear or disrupts flow). It chooses the action with the highest utility score.
  • Advantage: Can handle conflicting goals by finding the best possible compromise.

💻 How to Run the Project

Prerequisites

  1. Java Development Kit (JDK 11+)
  2. Leiningen (The Clojure build tool)

Running the Simulation

  1. Open your terminal (or PowerShell).
  2. Navigate to the root directory of this project.
  3. Run the following command:
    lein run

The application will run the traffic simulation loop 4 times—once for each agent. Watch the terminal output to see how each agent reacts to the randomly generated traffic!

📂 Project Structure

  • src/smart_traffic_light/core.clj - Contains the environment state, the simulation loop, and the logic for all four AI agents.
  • project.clj - The Leiningen project configuration file.

About

A Clojure-based AI simulation of a smart traffic light demonstrating 4 foundational agent architectures (Simple Reflex, Model-Based, Goal-Based, and Utility-Based).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors