// Creates and returns a new node.
        public static StateSpaceNode CreateNode(Planner planner, Domain domain, Problem problem, Plan plan, State state)
        {
            // Create a node for the current state.
            StateSpaceNode root = new StateSpaceNode();

            // Record that a node was created.
            StateSpaceMediator.NodeCount++;

            // Check if the node is a leaf.
            if (StateSpaceSearchTools.CheckLeaf(plan, state, root))
            {
                return(root);
            }

            // Set the node's domain.
            root.domain = domain;

            // Set the node's problem.
            root.problem = problem;

            // Set the node's plan.
            root.plan = plan;

            // Set the node's state.
            root.state = state;

            // Find out what the player knows.
            root.problem.Initial = RobertsonMicrotheory.Annotate(problem.Initial, problem.Player);

            // Find all outgoing user actions from this state.
            root.outgoing = StateSpaceTools.GetAllPossibleActions(domain, problem, plan, state);

            // Record the number of outgoing edges.
            StateSpaceMediator.OutgoingEdgesCount = StateSpaceMediator.OutgoingEdgesCount + root.outgoing.Count;

            // Return the current node.
            return(root);
        }
Example #2
0
        // Build the mediation tree using a depth-first strategy.
        public static StateSpaceNode BuildTree(Planner planner, Domain domain, Problem problem, Plan plan, State state, int depth)
        {
            // Create a node for the current state.
            StateSpaceNode root = new StateSpaceNode();

            // Record that a node was created.
            NodeCount++;

            // Return any empty plans.
            if (plan.Steps.Count == 0)
            {
                // If the goal is satisfied...
                if (state.Satisfies(plan.GoalStep.Preconditions))
                {
                    // Differentiate a satisfied goal.
                    root.satisfiesGoal = true;

                    // Record that a goal state was reached.
                    GoalStateCount++;
                }
                else
                {
                    // Record that a dead end state was reached.
                    DeadEndCount++;
                }

                // Return the leaf node.
                return(root);
            }

            // Set the node's domain.
            root.domain = domain;

            // Set the node's problem.
            root.problem = problem;

            // Set the node's plan.
            root.plan = plan;

            // Set the node's state.
            root.state = state;

            // Find out what the player knows.
            root.problem.Initial = RobertsonMicrotheory.Annotate(problem.Initial, problem.Player);

            // Find all outgoing user actions from this state.
            root.outgoing = StateSpaceTools.GetAllPossibleActions(domain, problem, plan, state);

            // Return the node if the depth limit has been reached.
            if (depth <= 0)
            {
                return(root);
            }

            // Loop through the possible actions.
            foreach (StateSpaceEdge edge in root.outgoing)
            {
                StateSpaceNode child = ExpandTree(planner, domain, problem, plan.Clone() as Plan, state.Clone() as State, edge, depth);

                // Set the child's parent to this node.
                child.parent = root;

                // Add the child to the parent's collection of children, by way of the current action.
                root.children[edge] = child;
            }

            // Return the current node.
            return(root);
        }