- Project Overview
- Key Features
- Usage Instructions
- Implementation Details
- Class Structure
- Search Strategies
- Performance Metrics
This project implements an AI-based search agent to solve the Water Sort Puzzle, a problem where liquids of different colors in various bottles must be sorted so that each bottle contains only one color. The solution leverages various search strategies to find an optimal plan to achieve this goal.
- Bottles: Each bottle has a maximum capacity and a unique identifier.
- Actions: The only permissible action is
pour(i, j)
which pours liquid from bottlei
to bottlej
, adhering to specific constraints. - Objective: Sort the liquids so that each bottle contains uniform layers of a single color.
A string defines the initial state as:
numberOfBottles;bottleCapacity;color0,1,color0,2,...;color1,1,color1,2,...;
Example:
"5;4;b,y,r,b;b,y,r,r;y,r,b,y;e,e,e,e;e,e,e,e;"
- Implements six search strategies: BFS, DFS, ID, UCS, Greedy Search (with 2 heuristics), and A* Search (with 2 admissible heuristics).
- Tracks and reports metrics: runtime, nodes expanded, memory usage, and CPU utilization.
- Visualizes the solution process via console output (optional).
- Validates the solution's correctness and optimality (for applicable strategies).
-
Clone the Repository:
git clone <repository-url> cd WaterSortPuzzleSolver
-
Compile the Code:
Ensure you have Java installed. Then, compile the project:javac -d bin src/code/*.java
-
Run the Program:
Use thesolve
method in theWaterSortSearch
class to solve a specific puzzle instance:java -cp bin code.WaterSortSearch
-
Parameters for
solve
:- initialState: String defining the initial puzzle setup.
- strategy: Search strategy to use (
BF
,DF
,ID
,UC
,GR1
,GR2
,AS1
, orAS2
). - visualize: Boolean to print step-by-step solution states.
Example:
String result = WaterSortSearch.solve("5;4;b,y,r,b;b,y,r,r;y,r,b,y;e,e,e,e;e,e,e,e;", "BF", true);
- Pour: Transfers liquid layers from one bottle to another. Ensures constraints on bottle capacity, color continuity, and emptiness are respected.
The goal is to minimize the total liquid layers poured across all actions.
-
GenericSearch
- Implements a generic search problem framework, including state management and frontier expansion.
-
WaterSortSearch
- Subclass of
GenericSearch
, specializing in the water-sort puzzle. - Implements the
solve
method to orchestrate the solution process.
- Subclass of
-
Node
- Represents a node in the search tree, encapsulating state, parent, action, path cost, and heuristic values.
Additional utility classes may be implemented for modularity and clarity.
- Breadth-First Search (BF): Explores all nodes at a given depth before proceeding deeper.
- Depth-First Search (DF): Explores as deep as possible along each branch before backtracking.
- Iterative Deepening Search (ID): Combines the benefits of BFS and DFS by incrementally deepening the search.
- Uniform Cost Search (UC): Expands the node with the lowest cumulative cost.
- Greedy Search (GR1, GR2): Uses heuristics to prioritize nodes likely leading to the goal.
- AStar Search (AS1, AS2): Combines path cost and heuristic values to find an optimal path.
Each strategy is evaluated on:
- Completeness: Does it always find a solution if one exists?
- Optimality: Does it guarantee the optimal solution?
- Runtime: Time taken to solve the puzzle.
- Memory Usage: RAM consumed during execution.
- CPU Utilization: Processor usage during execution.
- Nodes Expanded: Number of nodes explored to reach the solution.