Beispiel #1
0
        public State GetSuccessorSuperposition(MediationTreeEdge edge)
        {
            VirtualMediationTreeNode parent = GetNode(edge.Parent) as VirtualMediationTreeNode;
            Superposition            pred   = parent.State as Superposition;
            Superposition            super  = new Superposition();

            if (edge is VirtualMediationTreeEdge)
            {
                VirtualMediationTreeEdge vEdge = edge as VirtualMediationTreeEdge;
                foreach (State state in pred.States)
                {
                    foreach (Operator action in vEdge.Actions)
                    {
                        if (state.Satisfies(action.Preconditions))
                        {
                            super.States.Add(state.NewState(action, data.problem.Objects));
                        }
                    }
                }
            }
            else
            {
                foreach (State state in pred.States)
                {
                    if (state.Satisfies(edge.Action.Preconditions))
                    {
                        super.States.Add(state.NewState(edge.Action as Operator, data.problem.Objects));
                    }
                }
            }

            return(super);
        }
        /// <summary>
        /// Collapses the superposition given the player, the current tree node, and a strategy for choosing between unique state sets
        /// </summary>
        /// <param name="player">The player's name.</param>
        /// <param name="node">The current tree node.</param>
        /// <param name="chooser">A strategy for choosing between perceptually unique state sets.</param>
        /// <returns>A state superposition.</returns>
        public static HashSet <State> Collapse(string player, VirtualMediationTreeNode node, SuperpositionChooser.Choose chooser)
        {
            // Store the node's state as a superposition structure.
            Superposition super = node.State as Superposition;

            // Check to see if the node has more than one superposed state.
            if (super.States.Count > 0)
            {
                // Create a dictionary that maps sets of observed literals to sets of states that match the observation.
                Dictionary <List <IPredicate>, HashSet <State> > obs = new Dictionary <List <IPredicate>, HashSet <State> >(new PredicateListComparer());

                // Iterate through each state in the superposition.
                foreach (State state in super.States)
                {
                    // Find and store the set of literals observed by the player in the current state.
                    List <IPredicate> observed = KnowledgeAnnotator.FullKnowledgeState(node.Domain.Predicates, node.Problem.ObjectsByType, state.Predicates, player);

                    // If the current set of literals has been encountered before, add the state to its set.
                    if (obs.ContainsKey(observed))
                    {
                        obs[observed].Add(state);
                    }
                    // Otherwise, create a new dictionary key for the set of observed literals and initialize the state set with the current state.
                    else
                    {
                        obs.Add(observed, new HashSet <State> {
                            state
                        });
                    }
                }

                // Choose a set of states from the superposition according to the given strategy and return it.
                return(chooser(obs, node));
            }

            // Return the empty set of states.
            return(super.States);
        }
Beispiel #3
0
        /// <summary>
        /// Allows custom plans to be passed in.
        /// </summary>
        /// <param name="domain">The node's domain.</param>
        /// <param name="problem">The node's problem.</param>
        /// <param name="incoming">The node's incoming edge.</param>
        /// <returns>A new tree node.</returns>
        private MediationTreeNode CreateNode(Domain domain, Problem problem, MediationTreeEdge incoming, Plan plan)
        {
            // Create a placeholder for the new node object.
            MediationTreeNode node = null;

            // If the node is a root, initialize a root node.
            if (incoming == null)
            {
                if (!data.superpositionManipulation)
                {
                    node = new MediationTreeNode(domain, problem, 0);
                }
                else
                {
                    node = new VirtualMediationTreeNode(domain, problem, 0);
                }
            }
            // Otherwise, it is a child node...
            else
            {
                // Store the current node's ID in the incoming edge.
                incoming.Child = ++data.nodeCounter;

                if (!data.superpositionManipulation)
                {
                    node = new MediationTreeNode(domain, problem, incoming, GetSuccessorState(incoming), plan, incoming.Child, GetDepth(incoming.Parent) + 1);
                }
                else
                {
                    node = new VirtualMediationTreeNode(domain, problem, incoming, GetSuccessorSuperposition(incoming), plan, incoming.Child, GetDepth(incoming.Parent) + 1);
                }

                // Add the edge to the tree hashtable.
                data.tree[incoming.Child] = incoming.Parent;

                MediationTreeNode parent = GetNode(incoming.Parent);
                if (incoming.Action != null)
                {
                    parent.Outgoing.Find(x => x.Action.Equals(incoming.Action)).Child = node.ID;
                }
                else
                {
                    foreach (MediationTreeEdge edge in parent.Outgoing)
                    {
                        if (edge is VirtualMediationTreeEdge)
                        {
                            if ((edge as VirtualMediationTreeEdge).Equals(incoming as VirtualMediationTreeEdge))
                            {
                                edge.Child = node.ID;
                            }
                        }
                    }
                }

                SetNode(parent);

                if (data.superpositionManipulation)
                {
                    Superposition super = node.State as Superposition;
                    super.States = SuperpositionManipulator.Collapse(data.player, node as VirtualMediationTreeNode, SuperpositionChooser.ChooseUtility);
                    node.State   = super;
                }
            }

            // If the node is a goal state, iterate the goal state counter.
            if (node.IsGoal)
            {
                data.goalStateCount++;
            }

            // If the node is a dead end, iterate the dead end counter.
            if (node.DeadEnd)
            {
                if (data.eventRevision)
                {
                    EventRevisor.EventRevision(Planner.FastDownward, node, this);
                }

                if (node.DeadEnd && data.domainRevision)
                {
                    DomainRevisor.DomainRevision(Planner.FastDownward, node, this);
                }

                if (node.DeadEnd)
                {
                    data.deadEndCount++;
                }
            }

            // If the node is at a lower depth than the previous record holder, update the depth counter.
            if (node.Depth > data.lowestDepth)
            {
                data.lowestDepth = node.Depth;
            }

            // If the node is not a dead end.
            if (!node.DeadEnd)
            {
                // Find and store the node's outgoing edges.
                node.Outgoing = GetOutgoingEdges(node, GetCurrentTurn(node));
            }

            // Save the current node to disk.
            SetNode(node);

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