static void Main(string[] args) { AOGraph graph = new AOGraph(); graph.CreateNode(0, 0, 50, false); graph.CreateNode(1, double.MaxValue, 30, false); graph.CreateNode(2, double.MaxValue, 20, false); graph.CreateNode(3, double.MaxValue, 20, false); graph.CreateNode(4, double.MaxValue, 20, false); graph.CreateNode(5, double.MaxValue, 20, false); graph.CreateNode(6, double.MaxValue, 15, true); graph.CreateNode(7, double.MaxValue, 15, true); graph.CreateNode(8, double.MaxValue, 15, true); graph.CreateNode(9, double.MaxValue, 15, true); graph.CreateOperator(0, new int[] { 1 }, 0); graph.CreateOperator(0, new int[] { 2, 3 }, 38); graph.CreateOperator(1, new int[] { 4, 5 }, 17); graph.CreateOperator(2, new int[] { 6, 7 }, 9); graph.CreateOperator(3, new int[] { 8, 9 }, 27); /* * HashSet<Operator> ops = graph.Nodes.ElementAt(0).Operators; * foreach(Operator op in ops) * Console.WriteLine(op.Ends.Count); */ AOStar.Find(graph, 0); Console.ReadKey(); }
private void Run(AOGraph mGraph, int start) { foreach (Node node in mGraph.Nodes) { this.costs.Add(node.Id, double.MaxValue); } this.opened.Add(mGraph.Nodes.ElementAt(start)); this.costs[start] = 0; while (opened.Count > 0) { /// current => is the best node to continue search from it's sub graph Node current = opened.Min(); opened.Remove(current); closed.Add(current); if (current.IsTerminal) { this.terminals.Add(current); Refresh(current.Parent); continue; } else { foreach (Operator op in current.Operators) { bool solveable = true; foreach (Node successor in op.Ends) { if (!successor.IsTerminal && (successor.Operators == null || successor.Operators.Count == 0)) { solveable = false; break; } } if (solveable) { foreach (Node successor in op.Ends) { successor.Parent = current; successor.Cost = current.Cost + G(current, successor); ///Here we must to override the comparator. if (!opened.Contains(successor) && !closed.Contains(successor)) { costs[successor.Id] = successor.Cost; opened.Add(successor); } } } else { Refresh(current); } } } } if (costs[start] >= double.MaxValue) { Console.WriteLine("No available paths from start = [{0:D}]", start); return; } Console.WriteLine("Best solution = " + this.costs[start]); foreach (Node node in mGraph.Nodes) { Console.WriteLine(node.Cost + ", " + costs[node.Id]); } }
/// <summary> /// Find all available paths from start node in the defined graph /// </summary> /// <param name="graph">The data set where graph is exist</param> /// <param name="start">Id of node which will consider as root</param> public static void Find(AOGraph graph, int start) { new AOStar().Run(graph, start); }