Example #1
0
        public IEnumerable <T> Expand(ISearchProblem <A, S, C> problem, T node)
        {
            var successors = problem.Successors(node.State);

            // Debug
            #if VERBOSE_DEBUG
            Console.WriteLine(String.Format("There are {0} successors for {1}", successors.Count(), node));
            Console.WriteLine(String.Format("This node has a current path cost of {0}", node.PathCost));
            #endif

            foreach (var successor in successors)
            {
                var action = successor.Item1;
                var state  = successor.Item2;
                var next   = CreateNode(node, state);

                next.Action    = action;
                next.StepCost  = problem.StepCost(node.State, action, state);
                next.Heuristic = problem.Heuristic(state);

                #if VERBOSE_DEBUG
                System.Diagnostics.Trace.WriteLine("   Action = " + next.Action + ", g(n') = " + next.PathCost + ", h(n') = " + next.Heuristic);
                #endif

                yield return(next);
            }
        }
        private C RBFS(T node, C F_N, C bound)
        {
            var f_N = problem.Heuristic(node.State);

            if (f_N.CompareTo(bound) > 0)
            {
                return(f_N);
            }

            if (problem.IsGoal(node.State))
            {
                throw new Exception();
            }

            var children = Expander.Expand(problem, node);

            if (!children.Any())
            {
                return(Cost.Maximum());
            }

            foreach (var N_i in children)
            {
                if (f_N.CompareTo(F_N) < 0)
                {
                    N_i.F = F_N.Max(N_i.EstCost);
                }
                else
                {
                    N_i.F = N_i.EstCost;
                }
            }

            children = children.OrderBy(x => x.F);


            /*
             * RBFS (node: N, value: F(N), bound: B)
             * IF f(N)>B, RETURN f(N)
             * IF N is a goal, EXIT algorithm
             * IF N has no children, RETURN infinity
             * FOR each child Ni of N,
             *  IF f(N)<F(N), F[i] := MAX(F(N),f(Ni))
             *  ELSE F[i] := f(Ni)
             * sort Ni and F[i] in increasing order of F[i]
             * IF only one child, F[2] := infinity
             * WHILE (F[1] <= B and F[1] < infinity)
             *  F[1] := RBFS(N1, F[1], MIN(B, F[2]))
             *  insert Ni and F[1] in sorted order
             * RETURN F[1]
             */
        }