Example #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);
            }
        }
Example #2
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);
              }
        }
Example #3
0
        private bool Recursive_Depth(State state, int depth)
        {
            if (depth > -1)
            {
                if (!ClosedSet.ContainsKey(state.Key))
                {
                    ClosedSet.Add(state.Key, state);
                }

                if (state.IsEqualToGoal())
                {
                    SolutionFound(state);
                    return(true);
                }
                else
                {
                    var children = state.BuildChildren();

                    foreach (var child in children)
                    {
                        if (!ClosedSet.ContainsKey(child.Key))
                        {
                            if (!parents.ContainsKey(child.Key))
                            {
                                parents.Add(child.Key, state);
                            }

                            var result = Recursive_Depth(child, depth - 1);

                            if (result)
                            {
                                return(result);
                            }
                        }
                    }
                }
                depth += 1;
                return(false);
            }
            else
            {
                depth += 1;
                return(false);
            }
        }
Example #4
0
        public State recursive_depth(State state, int depth)
        {
            if (depth == 0 && state.IsEqualToGoal())
            {
                watch.Stop();

                SolutionFound(state);
                return(state);
            }

            if (depth > 0)
            {
                if (ClosedSet.ContainsKey(state.Key) == false)
                {
                    ClosedSet.Add(state.Key, state);
                }

                if (state.IsEqualToGoal())
                {
                    watch.Stop();

                    SolutionFound(state);

                    return(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
                    List <State> children = new List <State>();
                    foreach (var item in list)
                    {
                        if (ClosedSet.ContainsKey(item.Key)) //the second depth won't be able to add its children into the open set thus never finding the solution.
                        {
#if DEBUG
                            Console.WriteLine("ClosedSet already contains key {0}", item.Key);
#endif
                            continue;
                        }
                        children.Add(item);
                        OpenSet.Push(item);
                        if (!parents.ContainsKey(item.Key))
                        {
                            parents.Add(item.Key, state);
                        }
                    }

                    state = OpenSet.Pop();


                    if (OpenSet.Count == 0)
                    {
                        throw new Exception("no solution");
                    }

                    foreach (var child in children)
                    {
                        found = recursive_depth(child, depth--);

                        if (found != null)
                        {
                            return(found);
                        }
                    }
                    return(null);
                }
            }
            return(null);
        }