public void recursive_depth(State state) { if (ClosedSet.ContainsKey(state.Key) == false) { ClosedSet.Add(state.Key, state); } if (state.IsEqualToGoal()) { watch.Stop(); SolutionFound(state); } else { var list = state.BuildChildren(); #if DEBUG Console.WriteLine("Children of node: {0}", state); foreach(var item in list){ item.Format(); Console.WriteLine(); } #endif foreach (var item in list) { if (ClosedSet.ContainsKey(item.Key)) { #if DEBUG Console.WriteLine("ClosedSet already contains key {0}", item.Key); #endif continue; } OpenSet.Push(item); if(!parents.ContainsKey(item.Key)) parents.Add(item.Key,state); } state = OpenSet.Pop(); //Debugger.Break(); if (OpenSet.Count == 0) { throw new Exception("no solution"); } recursive_depth(state); } }
public BiDirectional(int[] arr) { CurrentState = new State(arr); }
private void SolutionFound(State FinalState) { //Debugger.Break(); var temp = FinalState; while(true){ if(!frontparents.ContainsKey(temp.Key)) break; moves.AddLast(frontparents[temp.Key]); temp = frontparents[temp.Key]; } temp = FinalState; while(true){ if(!backparents.ContainsKey(temp.Key)) break; moves.AddLast(backparents[temp.Key]); temp = backparents[temp.Key]; } Console.WriteLine("Move list: "); var move = moves.First; int count = 0; while(move != null){ Console.WriteLine( move.Value ); move = move.Next; if(count++ > 100)break; } if(moves.Count > 100) Console.WriteLine("More than 100 ..."); Console.WriteLine("Goal: "); new State(GlobalVar.GOAL).Format(); Console.WriteLine("Number of expanded nodes: {0}", moves.Count); }
public BreadthFirst(int[] arr) { CurrentState = new State(arr); }
public DepthFirstSearch(int[] arr) { CurrentState = new State(arr); }
public astar(int[] arr, bool heuristic) { CurrentState = new State(arr); use_manhattan = heuristic; }
public greedy(int[] arr) { CurrentState = new State(arr); parents[CurrentState.Key] = null; }
public void SolutionFound(State FinalState) { var temp = FinalState; while(true){ if(parents[temp.Key] == null) break; moves.AddFirst(parents[temp.Key]); temp = parents[temp.Key]; } Console.WriteLine("Move list: "); var move = moves.First; int count = 0; while(move != null) { Console.WriteLine( move.Value ); move = move.Next; if(count++ > 100) break; } if(moves.Count > 100) Console.WriteLine("More than 100 ..."); Console.WriteLine("Goal: "); new State(GlobalVar.GOAL).Format(); Console.WriteLine("Number of expanded nodes: {0}", moves.Count); }