Exemplo n.º 1
0
        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);
              }
        }
Exemplo n.º 2
0
 public BiDirectional(int[] arr)
 {
     CurrentState = new State(arr);
 }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
 public BreadthFirst(int[] arr)
 {
     CurrentState = new State(arr);
 }
Exemplo n.º 5
0
 public DepthFirstSearch(int[] arr)
 {
     CurrentState = new State(arr);
 }
Exemplo n.º 6
0
 public astar(int[] arr, bool heuristic)
 {
     CurrentState = new State(arr);
       use_manhattan = heuristic;
 }
Exemplo n.º 7
0
 public greedy(int[] arr)
 {
     CurrentState = new State(arr);
       parents[CurrentState.Key] = null;
 }
Exemplo n.º 8
0
        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);
        }