Open In App

AO* algorithm – Artificial intelligence

Last Updated : 20 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Best-first search is what the AO* algorithm does. The AO* method divides any given difficult problem into a smaller group of problems that are then resolved using the AND-OR graph concept. AND OR graphs are specialized graphs that are used in problems that can be divided into smaller problems. The AND side of the graph represents a set of tasks that must be completed to achieve the main goal, while the OR side of the graph represents different methods for accomplishing the same main goal.

AO* Algorithm -Geeksforgeeks

AND-OR Graph

In the above figure, the buying of a car may be broken down into smaller problems or tasks that can be accomplished to achieve the main goal in the above figure, which is an example of a simple AND-OR graph. The other task is to either steal a car that will help us accomplish the main goal or use your own money to purchase a car that will accomplish the main goal. The AND symbol is used to indicate the AND part of the graphs, which refers to the need that all subproblems containing the AND to be resolved before the preceding node or issue may be finished.

                                      The start state and the target state are already known in the knowledge-based search strategy known as the AO* algorithm, and the best path is identified by heuristics. The informed search technique considerably reduces the algorithm’s time complexity. The AO* algorithm is far more effective in searching AND-OR trees than the A* algorithm.

Working of AO* algorithm:

The evaluation function in AO* looks like this:
f(n) = g(n) + h(n)
f(n) = Actual cost + Estimated cost
here,
          f(n) = The actual cost of traversal.
          g(n) = the cost from the initial node to the current node.
          h(n) = estimated cost from the current node to the goal state.
 

Difference between the A* Algorithm and AO* algorithm

  • A* algorithm and AO* algorithm both works on the best first search.
  • They are both informed search and works on given heuristics values.
  • A* always gives the optimal solution but AO* doesn’t guarantee to give the optimal solution.
  • Once AO* got a solution doesn’t explore all possible paths but A* explores all paths.
  • When compared to the A* algorithm, the AO* algorithm uses less memory.
  • opposite to the A* algorithm, the AO* algorithm cannot go into an endless loop.

Example:

AO* Algorithm (Questions) -Geeksforgeeks

AO* Algorithm – Question tree

Here in the above example below the Node which is given is the heuristic value i.e h(n). Edge length is considered as 1.

Step 1

AO* Algorithm (Step-1)

With help of  f(n) = g(n) + h(n) evaluation function,

Start from node A,
f(A⇢B) = g(B) + h(B)
       = 1   +  5                       ……here g(n)=1 is taken by default for path cost
       = 6
      
f(A⇢C+D) = g(c) + h(c) + g(d) + h(d)
          = 1 + 2 + 1 + 4                ……here we have added C & D because they are in AND
          = 8
  So, by calculation A⇢B path is chosen which is the minimum path, i.e f(A⇢B)

Step 2

AO* Algorithm (Step-2) -Geeksforgeeks

AO* Algorithm (Step-2)

According to the answer of step 1, explore node B
Here the value of E & F are calculated as follows,

f(B⇢E) = g(e) + h(e)
f(B⇢E) = 1 + 7
        = 8
   
f(B⇢f) = g(f) + h(f)
f(B⇢f) = 1 + 9
        = 10
  So, by above calculation B⇢E path is chosen which is minimum path, i.e f(B⇢E)
  because B's heuristic value is different from its actual value The heuristic is 
  updated and the minimum cost path is selected. The minimum value in our situation is 8.
  Therefore, the heuristic for A must be updated due to the change in B's heuristic.
  So we need to calculate it again.  
 
f(A⇢B) = g(B) + updated h(B) 
          = 1 + 8
          = 9
  We have Updated all values in the above tree.

Step 3

AO* Algorithm (Step-3) -Geeksforgeeks

AO* Algorithm (Step-3) -Geeksforgeeks

By comparing f(A⇢B) & f(A⇢C+D) 
f(A⇢C+D) is shown to be smaller. i.e 8 < 9
Now explore f(A⇢C+D) 
So, the current node is C

f(C⇢G) = g(g) + h(g)
f(C⇢G) = 1 + 3
        = 4
        
f(C⇢H+I) = g(h) + h(h) + g(i) + h(i)        
f(C⇢H+I) = 1 + 0 + 1 + 0                ……here we have added H & I because they are in AND
          = 2
          
f(C⇢H+I) is selected as the path with the lowest cost and the heuristic is also left unchanged
because it matches the actual cost. Paths H & I are solved because the heuristic for those paths is 0,
but Path A⇢D needs to be calculated because it has an AND.

f(D⇢J) = g(j) + h(j)
f(D⇢J) = 1 + 0 
        = 1 
the heuristic of node D needs to be updated to 1.

f(A⇢C+D) = g(c) + h(c) + g(d) + h(d)
          = 1 + 2 + 1 + 1
          = 5
        
as we can see that path f(A⇢C+D) is get solved and this tree has become a solved tree now.
In simple words, the main flow of this algorithm is that we have to find firstly level 1st heuristic
value and then level 2nd and after that update the values with going upward means towards the root node.
In the above tree diagram, we have updated all the values.   

Code implementations of the above example:

Java




import java.util.*;
 
public class Main {
  public static Map<String, Integer>
    Cost(Map<String, Integer> H,
         Map<String, List<String> > condition, int weight)
  {
    Map<String, Integer> cost = new HashMap<>();
    if (condition.containsKey("AND")) {
      List<String> AND_nodes = condition.get("AND");
      String Path_A = String.join(" AND ", AND_nodes);
      int PathA
        = AND_nodes.stream()
        .mapToInt(
        node -> H.get(node) + weight)
        .sum();
      cost.put(Path_A, PathA);
    }
    if (condition.containsKey("OR")) {
      List<String> OR_nodes = condition.get("OR");
      String Path_B = String.join(" OR ", OR_nodes);
      int PathB
        = OR_nodes.stream()
        .mapToInt(
        node -> H.get(node) + weight)
        .min()
        .getAsInt();
      cost.put(Path_B, PathB);
    }
    return cost;
  }
 
  public static Map<String, Map<String, Integer> >
    UpdateCost(
    Map<String, Integer> H,
    Map<String, Map<String, List<String> > > Conditions,
    int weight)
  {
    List<String> Main_nodes
      = new ArrayList<>(Conditions.keySet());
    Collections.reverse(Main_nodes);
    Map<String, Map<String, Integer> > least_cost
      = new HashMap<>();
    for (String key : Main_nodes) {
      Map<String, List<String> > condition
        = Conditions.get(key);
      System.out.printf("%s: %s >>> %s%n", key,
                        condition,
                        Cost(H, condition, weight));
      Map<String, Integer> c
        = Cost(H, condition, weight);
      H.put(key, Collections.min(c.values()));
      least_cost.put(key, Cost(H, condition, weight));
    }
    return least_cost;
  }
 
  public static String ShortestPath(
    String Start,
    Map<String, Map<String, Integer> > Updated_cost,
    Map<String, Integer> H)
  {
    String Path = Start;
    if (Updated_cost.containsKey(Start)) {
      int Min_cost = Collections.min(
        Updated_cost.get(Start).values());
      List<String> key = new ArrayList<>(
        Updated_cost.get(Start).keySet());
      List<Integer> values = new ArrayList<>(
        Updated_cost.get(Start).values());
      int Index = values.indexOf(Min_cost);
      List<String> Next
        = Arrays.asList(key.get(Index).split(" "));
      if (Next.size() == 1) {
        Start = Next.get(0);
        Path += "<--"
          + ShortestPath(Start, Updated_cost,
                         H);
      }
      else {
        Path += "<--(" + key.get(Index) + ") ";
        Start = Next.get(0);
        Path += "["
          + ShortestPath(Start, Updated_cost,
                         H)
          + " + ";
        Start = Next.get(Next.size() - 1);
        Path += ShortestPath(Start, Updated_cost, H)
          + "]";
      }
    }
    return Path;
  }
 
  public static void main(String[] args)
  {
    Map<String, Integer> H = new HashMap<>();
    H.put("A", -1);
    H.put("B", 5);
    H.put("C", 2);
    H.put("D", 4);
    H.put("E", 7);
    H.put("F", 9);
    H.put("G", 3);
    H.put("H", 0);
    H.put("I", 0);
    H.put("J", 0);
 
    Map<String, Map<String, List<String> > > Conditions
      = new HashMap<>();
    Map<String, List<String> > aConditions
      = new HashMap<>();
    aConditions.put("OR", Arrays.asList("B"));
    aConditions.put("AND", Arrays.asList("C", "D"));
    Conditions.put("A", aConditions);
    Map<String, List<String> > bConditions
      = new HashMap<>();
    bConditions.put("OR", Arrays.asList("E", "F"));
    Conditions.put("B", bConditions);
 
    Map<String, List<String> > cConditions
      = new HashMap<>();
    cConditions.put("OR", Arrays.asList("G"));
    cConditions.put("AND", Arrays.asList("H", "I"));
    Conditions.put("C", cConditions);
 
    Map<String, List<String> > dConditions
      = new HashMap<>();
    dConditions.put("OR", Arrays.asList("J"));
    Conditions.put("D", dConditions);
 
    // weight
    int weight = 1;
 
    // Updated cost
    System.out.println("Updated Cost :");
    Map<String, Map<String, Integer> > Updated_cost
      = UpdateCost(H, Conditions, weight);
    System.out.println("*".repeat(75));
    System.out.println("Shortest Path :");
    System.out.println(
      ShortestPath("A", Updated_cost, H));
  }
}


Python3




# Cost to find the AND and OR path
def Cost(H, condition, weight = 1):
    cost = {}
    if 'AND' in condition:
        AND_nodes = condition['AND']
        Path_A = ' AND '.join(AND_nodes)
        PathA = sum(H[node]+weight for node in AND_nodes)
        cost[Path_A] = PathA
 
    if 'OR' in condition:
        OR_nodes = condition['OR']
        Path_B =' OR '.join(OR_nodes)
        PathB = min(H[node]+weight for node in OR_nodes)
        cost[Path_B] = PathB
    return cost
 
# Update the cost
def update_cost(H, Conditions, weight=1):
    Main_nodes = list(Conditions.keys())
    Main_nodes.reverse()
    least_cost= {}
    for key in Main_nodes:
        condition = Conditions[key]
        print(key,':', Conditions[key],'>>>', Cost(H, condition, weight))
        c = Cost(H, condition, weight)
        H[key] = min(c.values())
        least_cost[key] = Cost(H, condition, weight)           
    return least_cost
 
# Print the shortest path
def shortest_path(Start,Updated_cost, H):
    Path = Start
    if Start in Updated_cost.keys():
        Min_cost = min(Updated_cost[Start].values())
        key = list(Updated_cost[Start].keys())
        values = list(Updated_cost[Start].values())
        Index = values.index(Min_cost)
         
        # FIND MINIMIMUM PATH KEY
        Next = key[Index].split()
        # ADD TO PATH FOR OR PATH
        if len(Next) == 1:
 
            Start =Next[0]
            Path += '<--' +shortest_path(Start, Updated_cost, H)
        # ADD TO PATH FOR AND PATH
        else:
            Path +='<--('+key[Index]+') '
 
            Start = Next[0]
            Path += '[' +shortest_path(Start, Updated_cost, H) + ' + '
 
            Start = Next[-1]
            Path +=  shortest_path(Start, Updated_cost, H) + ']'
 
    return Path
         
        
 
H = {'A': -1, 'B': 5, 'C': 2, 'D': 4, 'E': 7, 'F': 9, 'G': 3, 'H': 0, 'I':0, 'J':0}
 
Conditions = {
 'A': {'OR': ['B'], 'AND': ['C', 'D']},
 'B': {'OR': ['E', 'F']},
 'C': {'OR': ['G'], 'AND': ['H', 'I']},
 'D': {'OR': ['J']}
}
# weight
weight = 1
# Updated cost
print('Updated Cost :')
Updated_cost = update_cost(H, Conditions, weight=1)
print('*'*75)
print('Shortest Path :\n',shortest_path('A', Updated_cost,H))


C#




using System;
using System.Collections.Generic;
using System.Linq;
 
class Program
{
  static Dictionary<string, int> Cost(Dictionary<string, int> H,
                                      Dictionary<string, List<string>> condition,
                                      int weight = 1)
  {
    Dictionary<string, int> cost = new Dictionary<string, int>();
    if (condition.ContainsKey("AND"))
    {
      List<string> AND_nodes = condition["AND"];
      string Path_A = string.Join(" AND ", AND_nodes);
      int PathA = AND_nodes.Sum(node => H[node] + weight);
      cost[Path_A] = PathA;
    }
 
    if (condition.ContainsKey("OR"))
    {
      List<string> OR_nodes = condition["OR"];
      string Path_B = string.Join(" OR ", OR_nodes);
      int PathB = OR_nodes.Min(node => H[node] + weight);
      cost[Path_B] = PathB;
    }
    return cost;
  }
 
  static Dictionary<string, Dictionary<string, int>> UpdateCost(Dictionary<string, int> H, Dictionary<string, Dictionary<string, List<string>>> Conditions, int weight = 1)
  {
    List<string> Main_nodes = new List<string>(Conditions.Keys);
    Main_nodes.Reverse();
    Dictionary<string, Dictionary<string, int>> least_cost = new Dictionary<string, Dictionary<string, int>>();
    foreach (string key in Main_nodes)
    {
      Dictionary<string, List<string>> condition = Conditions[key];
      Console.WriteLine("{0}: {1} >>> {2}", key, condition, Cost(H, condition, weight));
      Dictionary<string, int> c = Cost(H, condition, weight);
      H[key] = c.Values.Min();
      least_cost[key] = Cost(H, condition, weight);
    }
    return least_cost;
  }
 
  static string ShortestPath(string Start, Dictionary<string, Dictionary<string, int>> Updated_cost, Dictionary<string, int> H)
  {
    string Path = Start;
    if (Updated_cost.ContainsKey(Start))
    {
      int Min_cost = Updated_cost[Start].Values.Min();
      List<string> key = new List<string>(Updated_cost[Start].Keys);
      List<int> values = new List<int>(Updated_cost[Start].Values);
      int Index = values.IndexOf(Min_cost);
 
      // FIND MINIMIMUM PATH KEY
      List<string> Next = key[Index].Split().ToList();
      // ADD TO PATH FOR OR PATH
      if (Next.Count == 1)
      {
 
        Start = Next[0];
        Path += "<--" + ShortestPath(Start, Updated_cost, H);
      }
      // ADD TO PATH FOR AND PATH
      else
      {
        Path += "<--(" + key[Index] + ") ";
 
        Start = Next[0];
        Path += "[" + ShortestPath(Start, Updated_cost, H) + " + ";
 
        Start = Next[Next.Count - 1];
        Path += ShortestPath(Start, Updated_cost, H) + "]";
      }
 
    }
    return Path;
  }
 
  static void Main(string[] args) {
    var H = new Dictionary<string, int> {
      {"A", -1},
      {"B", 5},
      {"C", 2},
      {"D", 4},
      {"E", 7},
      {"F", 9},
      {"G", 3},
      {"H", 0},
      {"I", 0},
      {"J", 0},
    };
 
    var Conditions = new Dictionary<string, Dictionary<string, List<string>>> {
      {"A", new Dictionary<string, List<string>> {
        {"OR", new List<string>{"B"}},
        {"AND", new List<string>{"C", "D"}},
      }
      },
      {"B", new Dictionary<string, List<string>> {
        {"OR", new List<string>{"E",
                                "F"}},
      }
      },
      {"C", new Dictionary<string, List<string>> {
        {"OR", new List<string>{"G"}},
        {"AND", new List<string>{"H", "I"}},
      }
      },
      {"D", new Dictionary<string, List<string>> {
        {"OR", new List<string>{"J"}},
      }
      },
    };
 
    // weight
    int weight = 1;
 
    // Updated cost
    Console.WriteLine("Updated Cost :");
    var Updated_cost = UpdateCost(H, Conditions, weight: 1);
    Console.WriteLine("*".PadLeft(75, '*'));
    Console.WriteLine("Shortest Path :");
    Console.WriteLine(ShortestPath("A", Updated_cost, H));
  }
}


Javascript




// Cost to find the AND and OR path
function Cost(H, condition, weight = 1) {
  let cost = {};
  if ('AND' in condition) {
    let AND_nodes = condition['AND'];
    let Path_A = AND_nodes.join(' AND ');
    let PathA = AND_nodes.reduce((total, node) => total + H[node] + weight, 0);
    cost[Path_A] = PathA;
  }
 
  if ('OR' in condition) {
    let OR_nodes = condition['OR'];
    let Path_B = OR_nodes.join(' OR ');
    let PathB = Math.min(...OR_nodes.map(node => H[node] + weight));
    cost[Path_B] = PathB;
  }
  return cost;
}
 
// Update the cost
function update_cost(H, Conditions, weight=1) {
  let Main_nodes = Object.keys(Conditions).reverse();
  let least_cost = {};
  for (let i = 0; i < Main_nodes.length; i++) {
    let key = Main_nodes[i];
    let condition = Conditions[key];
    console.log(key + ' : ' + JSON.stringify(Conditions[key]) + ' >>> ' + JSON.stringify(Cost(H, condition, weight)));
    let c = Cost(H, condition, weight);
    H[key] = Math.min(...Object.values(c));
    least_cost[key] = Cost(H, condition, weight);
  }
  return least_cost;
}
 
// Print the shortest path
function shortest_path(Start, Updated_cost, H) {
  let Path = Start;
  if (Start in Updated_cost) {
    let Min_cost = Math.min(...Object.values(Updated_cost[Start]));
    let keys = Object.keys(Updated_cost[Start]);
    let values = Object.values(Updated_cost[Start]);
    let Index = values.indexOf(Min_cost);
 
    // FIND MINIMIMUM PATH KEY
    let Next = keys[Index].split(' ');
    // ADD TO PATH FOR OR PATH
    if (Next.length == 1) {
      Start = Next[0];
      Path += '<--' + shortest_path(Start, Updated_cost, H);
    }
    // ADD TO PATH FOR AND PATH
    else {
      Path += '<--(' + keys[Index] + ') ';
      Start = Next[0];
      Path += '[' + shortest_path(Start, Updated_cost, H) + ' + ';
      Start = Next[Next.length - 1];
      Path += shortest_path(Start, Updated_cost, H) + ']';
    }
  }
  return Path;
}
 
let H = {'A': -1, 'B': 5, 'C': 2, 'D': 4, 'E': 7, 'F': 9, 'G': 3, 'H': 0, 'I':0, 'J':0};
 
let Conditions = {
  'A': {'OR': ['B'], 'AND': ['C', 'D']},
  'B': {'OR': ['E', 'F']},
  'C': {'OR': ['G'], 'AND': ['H', 'I']},
  'D': {'OR': ['J']}
};
 
// weight
let weight = 1;
 
// Updated cost
console.log('Updated Cost:');
let Updated_cost = update_cost(H, Conditions, weight);
console.log('*'.repeat(75));
console.log('Shortest Path:\n' + shortest_path('A', Updated_cost, H));


Output:

Updated Cost :
D : {'OR': ['J']} >>> {'J': 1}
C : {'OR': ['G'], 'AND': ['H', 'I']} >>> {'H AND I': 2, 'G': 4}
B : {'OR': ['E', 'F']} >>> {'E OR F': 8}
A : {'OR': ['B'], 'AND': ['C', 'D']} >>> {'C AND D': 5, 'B': 9}
***************************************************************************
Shortest Path :
 A<--(C AND D) [C<--(H AND I) [H + I] + D<--J]

Example 2:

Python3




# Cost to find the AND and OR path
def Cost(H, condition, weight = 1):
    cost = {}
    if 'AND' in condition:
        AND_nodes = condition['AND']
        Path_A = ' AND '.join(AND_nodes)
        PathA = sum(H[node]+weight for node in AND_nodes)
        cost[Path_A] = PathA
 
    if 'OR' in condition:
        OR_nodes = condition['OR']
        Path_B =' OR '.join(OR_nodes)
        PathB = min(H[node]+weight for node in OR_nodes)
        cost[Path_B] = PathB
    return cost
 
# Update the cost
def update_cost(H, Conditions, weight=1):
    Main_nodes = list(Conditions.keys())
    Main_nodes.reverse()
    least_cost= {}
    for key in Main_nodes:
        condition = Conditions[key]
        print(key,':', Conditions[key],'>>>', Cost(H, condition, weight))
        c = Cost(H, condition, weight)
        H[key] = min(c.values())
        least_cost[key] = Cost(H, condition, weight)           
    return least_cost
 
# Print the shortest path
def shortest_path(Start,Updated_cost, H):
    Path = Start
    if Start in Updated_cost.keys():
        Min_cost = min(Updated_cost[Start].values())
        key = list(Updated_cost[Start].keys())
        values = list(Updated_cost[Start].values())
        Index = values.index(Min_cost)
         
        # FIND MINIMIMUM PATH KEY
        Next = key[Index].split()
        # ADD TO PATH FOR OR PATH
        if len(Next) == 1:
 
            Start =Next[0]
            Path += ' = ' +shortest_path(Start, Updated_cost, H)
        # ADD TO PATH FOR AND PATH
        else:
            Path +='=('+key[Index]+') '
 
            Start = Next[0]
            Path += '[' +shortest_path(Start, Updated_cost, H) + ' + '
 
            Start = Next[-1]
            Path +=  shortest_path(Start, Updated_cost, H) + ']'
 
    return Path
         
 
# Heuristic values of Nodes 
H1 = {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J': 1, 'T': 3}
 
 
Conditions = {
 'A': {'OR': ['D'], 'AND': ['B', 'C']},
 'B': {'OR': ['G', 'H']},
 'C': {'OR': ['J']},
 'D': {'AND': ['E', 'F']},
 'G': {'OR': ['I']}
    
}
 
# weight
weight = 1
# Updated cost
print('Updated Cost :')
Updated_cost = update_cost(H1, Conditions, weight=1)
print('*'*75)
print('Shortest Path :\n',shortest_path('A', Updated_cost,H1))


Javascript




// Cost to find the AND and OR path
function Cost(H, condition, weight = 1) {
    const cost = {};
    if ('AND' in condition) {
        const AND_nodes = condition['AND'];
        const Path_A = AND_nodes.join(' AND ');
        const PathA = AND_nodes.reduce((acc, node) => acc + H[node] + weight, 0);
        cost[Path_A] = PathA;
    }
 
    if ('OR' in condition) {
        const OR_nodes = condition['OR'];
        const Path_B = OR_nodes.join(' OR ');
        const PathB = Math.min(...OR_nodes.map((node) => H[node] + weight));
        cost[Path_B] = PathB;
    }
 
    return cost;
}
 
 
// Update the cost
function update_cost(H, Conditions, weight = 1) {
    const Main_nodes = Object.keys(Conditions).reverse();
    const least_cost = {};
    Main_nodes.forEach((key) => {
        const condition = Conditions[key];
        console.log(key, ':', Conditions[key], '>>>', Cost(H, condition, weight));
        const c = Cost(H, condition, weight);
        H[key] = Math.min(...Object.values(c));
        least_cost[key] = Cost(H, condition, weight);
    });
    return least_cost;
}
 
 
// Print the shortest path
function shortest_path(Start, Updated_cost, H) {
    let Path = Start;
    if (Start in Updated_cost) {
        const Min_cost = Math.min(...Object.values(Updated_cost[Start]));
        const key = Object.keys(Updated_cost[Start]);
        const values = Object.values(Updated_cost[Start]);
        const Index = values.indexOf(Min_cost);
 
        // FIND MINIMUM PATH KEY
        const Next = key[Index].split(' ');
 
        // ADD TO PATH FOR OR PATH
        if (Next.length === 1) {
            Start = Next[0];
            Path += ' = ' + shortest_path(Start, Updated_cost, H);
        }
        // ADD TO PATH FOR AND PATH
        else {
            Path += '=(' + key[Index] + ') ';
            Start = Next[0];
            Path += '[' + shortest_path(Start, Updated_cost, H) + ' + ';
            Start = Next[Next.length - 1];
            Path += shortest_path(Start, Updated_cost, H) + ']';
        }
    }
    return Path;
}
 
// Heuristic values of Nodes
const H1 = {
    A: 1,
    B: 6,
    C: 2,
    D: 12,
    E: 2,
    F: 1,
    G: 5,
    H: 7,
    I: 7,
    J: 1,
    T: 3,
};
 
const Conditions = {
    A: {
        OR: ['D'],
        AND: ['B', 'C']
    },
    B: {
        OR: ['G', 'H']
    },
    C: {
        OR: ['J']
    },
    D: {
        AND: ['E', 'F']
    },
    G: {
        OR: ['I']
    },
};
 
// weight
const weight = 1;
 
// Updated cost
console.log('Updated Cost :');
const Updated_cost = update_cost(H1, Conditions, weight);
console.log('*'.repeat(75));
console.log('Shortest Path :\n', shortest_path('A', Updated_cost, H1));
 
// This code is contributed by rishabmalhdijo


Output:

Updated Cost :
G : {'OR': ['I']} >>> {'I': 8}
D : {'AND': ['E', 'F']} >>> {'E AND F': 5}
C : {'OR': ['J']} >>> {'J': 2}
B : {'OR': ['G', 'H']} >>> {'G OR H': 8}
A : {'OR': ['D'], 'AND': ['B', 'C']} >>> {'B AND C': 12, 'D': 6}
***************************************************************************
Shortest Path :
 A = D=(E AND F) [E + F]

Real-Life Applications of AO* algorithm:

Vehicle Routing Problem:

The vehicle routing problem is determining the shortest routes for a fleet of vehicles to visit a set of customers and return to the depot, while minimizing the total distance traveled and the total time taken. The AO* algorithm can be used to find the optimal routes that satisfy both objectives.

Portfolio Optimization: 

Portfolio optimization is choosing a set of investments that maximize returns while minimizing risks. The AO* algorithm can be used to find the optimal portfolio that satisfies both objectives, such as maximizing the expected return and minimizing the standard deviation.

In both examples, the AO* algorithm can be used to find the optimal solution that balances multiple conflicting objectives, such as minimizing distance and time in the vehicle routing problem, or maximizing returns and minimizing risks in the portfolio optimization problem. The algorithm starts with an initial solution and iteratively improves it by exploring alternative solutions and keeping the best solution that satisfies both objectives.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads