AO* algorithm is a search algorithm used in AI to find optimal solutions in AND‑OR graphs. It extends the A Star algorithm by handling more complex decision structures. In a regular OR graph (used by A Star), only one optimal path is selected.
Evaluates multiple interconnected subproblems to construct an optimal solution.
Uses heuristic information to guide the search efficiently.
Commonly applied in planning, decision-making, and problem-solving tasks.
Some problems require completing multiple subtasks before achieving a goal.
Traditional search techniques struggle with hierarchical and decomposable problem structures.
Complex planning and decision-making tasks often involve interdependent actions and outcomes.
An approach is needed to efficiently evaluate and solve such structured problems.
Understanding AND–OR Graph
An AND–OR graph is a problem representation structure used when decisions involve both alternatives and mandatory combinations. Unlike simple trees, some nodes require selecting one option, while others require completing multiple tasks together.
An OR node represents a decision point where multiple alternatives are available, but only one option needs to be selected to move forward.
The solution depends on choosing the most optimal or least costly branch among the available choices.
Example: To reach a destination, you may choose: Route A or Route B. Selecting any one route is sufficient.
2. AND Node
An AND node represents a situation where multiple sub-tasks must be completed together.
The solution is valid only when all child nodes are solved.
Example: To cook food, you must: Buy vegetables and Cook them. Both steps are necessary.
Heuristic and Cost Modelling in AO*
1. Heuristic Function
The heuristic function estimates the remaining cost from a node to the goal:
= estimated cost from node to goal
It guides the algorithm toward promising paths and reduces unnecessary exploration.
2. Evaluation Function
Each node is assigned an evaluation value
Where:
: actual cost from start node to node
: heuristic estimate from node to goal
: total estimated cost
3. Cost Calculation for OR Node
For an OR node, only one child is selected. The cost is the minimum among all children.
Where:
: Cost from node to child
: Evaluation cost of child
4. Cost Calculation for AND Node
For an AND node, all children must be solved. The total cost is the sum of all child costs.
Where:
All successors contribute to the final cost
Every child must be included
5. Backpropagation Rule
After expanding a node, updated costs are propagated upward:
This continues until the root node is updated.
Working
Initialization: Start with the initial (root) node and assign its heuristic value. This node represents the overall problem to be solved.
Path Selection: Follow the currently most promising path based on heuristic cost values to identify nodes that are not yet expanded.
Node Expansion: Select one of the unexpanded nodes and generate its successor nodes. Compute heuristic values for each successor.
Solution Check: If a node has no successors or satisfies the goal condition, mark it as solved.
Cost Backpropagation: Update the cost values of parent nodes based on the newly expanded successors. This adjustment moves from bottom to top.
Best Path Update: Re evaluate the graph and choose the most promising path according to updated costs.
Termination: Repeat the process until the start node is marked as solved or no better solution path exists.
Implementation of AO* in Python
Step 1: Define the Graph
Defines the AND, OR graph structure. It Contains:
type: whether node is AND or OR
children: list of (child, edge_cost)
Step 2: Define Heuristic values
Stores estimated remaining cost from each node to the goal. These values guide the search.
Goal nodes (D, E, F) have heuristic 0
Other nodes have estimated future cost
Step 3: Storage Structures
cost: Stores computed cost of each node.
solved: Marks whether a node is completely evaluated.
solution_graph: Stores only the selected optimal branches.
Step 4: Cost Computation
if not graph[node]['children']: If the node has no children, it is a terminal (goal) node. Its cost is set to 0, marked as solved and recursion stops.
if solved.get(node, False): If a node has already been solved, its stored cost is reused to avoid recomputation.
if node_type == 'OR': For an OR node, the child with the minimum total cost is selected and added to the solution graph.
elif node_type == 'AND': For an AND node, the function computes the cost of all children, adds their edge costs, and sums them. Since all subproblems must be solved, every child is included in the solution graph.
After computing the cost, the node is marked as solved and its value is returned for backpropagation.
Step 5: Extracting Optimal Solution Graph
After computing costs, the function reconstructs the optimal solution graph. It selects the minimum-cost child for OR nodes and includes all children for AND nodes, producing the final optimal solution structure.
Step 6: Run the Algorithm
The algorithm starts from the root node A, computes the minimum total cost and then extracts the final optimal solution graph.
Output:
👁 AO-output Minimum cost and path according to AO* algo
Step 7: Visualizing Graph
Here, we visualize comparison of the AND OR graph before applying AO Star and the optimized solution graph after the algorithm selects the minimum cost branches.
1. AND OR Graph Before Applying AO Star*
This diagram shows the complete problem before applying AO Star, where all possible branches are still present and no optimal choice has been made. A is an OR node that can choose between B and C, B is an AND node that requires both D and E to be solved and C is an OR node that leads to F, while D, E and F are goal nodes.
This diagram shows the optimized structure after applying AO Star, where the algorithm has compared costs and removed the higher cost branch. Since B is an AND node, it requires solving both D and E, making its total cost higher, while C only requires solving F, resulting in a lower cost.