Exemplo n.º 1
0
 override protected WorldState CreateSearchRoot(int minDepth = -1, int minCost = -1, MDDNode mddNode = null)
 {
     return(new WorldStateForPartialExpansion(this.instance.agents, minDepth, minCost, mddNode)); // Consider using a WorldStateForBasicPartialExpansion that only has the IsAlreadyExpanded stuff
 }
Exemplo n.º 2
0
 public WorldStateWithOD(AgentState[] states, int minDepth = -1, int minCost = -1, MDDNode mddNode = null)
     : base(states, minDepth, minCost, mddNode)
 {
     this.agentTurn = 0;
 }
Exemplo n.º 3
0
 public void addParent(MDDNode parent)
 {
     parents.AddLast(parent);
 }
Exemplo n.º 4
0
 public void addChild(MDDNode child)
 {
     children.AddLast(child);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Builds an MDD by first performing a BFS from start_pos to the agent's goal,
        /// then deleting all nodes which don't lead to the goal at the given cost.
        /// </summary>
        /// <param name="mddNum"></param>
        /// <param name="agentNum"></param>
        /// <param name="start_pos"></param>
        /// <param name="cost">The MDD must be of this cost</param>
        /// <param name="numOfLevels">
        /// The MDD must be of this number of levels, not counting level zero.
        /// If higher than cost, the extra levels will be WAITs at the goal.
        /// </param>
        /// <param name="numOfAgents">Used for initializng coexistence lists</param>
        /// <param name="instance"></param>
        /// <param name="ignoreConstraints"></param>
        /// <param name="supportPruning"></param>
        public MDD(int mddNum, int agentNum, Move start_pos, int cost, int numOfLevels, int numOfAgents,
                   ProblemInstance instance, bool ignoreConstraints = false, bool supportPruning     = true, ISet <TimedMove> reserved = null,
                   ISet <CbsConstraint> constraints = null, ISet <CbsConstraint> positiveConstraints = null)
        {  // numOfLevels >= cost
            this.problem        = instance;
            this.mddNum         = mddNum;
            this.agentNum       = agentNum;
            this.numOfAgents    = numOfAgents;
            this.cost           = cost;
            this.levels         = new LinkedList <MDDNode> [numOfLevels + 1];
            this.supportPruning = supportPruning;

            Dictionary <int, TimedMove>[] mustConstraints = null;

            if (ignoreConstraints == false && constraints != null && constraints.Count != 0)
            {
                this.queryConstraint = new CbsConstraint();
                this.queryConstraint.queryInstance = true;
            }

            if (ignoreConstraints == false && positiveConstraints != null && positiveConstraints.Count != 0)
            {
                // TODO: Code dup with A_Star's constructor
                mustConstraints = new Dictionary <int, TimedMove> [positiveConstraints.Max(con => con.GetTimeStep()) + 1]; // To have index MAX, array needs MAX + 1 places.
                foreach (CbsConstraint con in positiveConstraints)
                {
                    int timeStep = con.GetTimeStep();
                    if (mustConstraints[timeStep] == null)
                    {
                        mustConstraints[timeStep] = new Dictionary <int, TimedMove>();
                    }
                    mustConstraints[timeStep][con.agentNum] = con.move;
                }
            }

            var perLevelClosedList = new Dictionary <MDDNode, MDDNode>();
            var toDelete           = new List <MDDNode>();

            for (int i = 0; i < levels.Length; i++)
            {
                levels[i] = new LinkedList <MDDNode>();
            }
            MDDNode root = new MDDNode(new TimedMove(start_pos, 0), numOfAgents, this, supportPruning); // Root
            LinkedListNode <MDDNode> llNode = new LinkedListNode <MDDNode>(root);

            root.setMyNode(llNode);
            llNode.Value.startOrGoal = true;
            levels[0].AddFirst(llNode);

            for (int i = 0; i < numOfLevels; i++)  // For each level, populate the _next_ level
            {
                int heuristicBound = cost - i - 1; // We want g+h <= cost, so h <= cost-g. -1 because it's the bound of the _children_.
                if (heuristicBound < 0)
                {
                    heuristicBound = 0;
                }

                // Go over each MDDNode in this level
                foreach (MDDNode currentMddNode in levels[i]) // Since we're not deleting nodes in this method, we can use the simpler iteration method :)
                {
                    List <MDDNode> children = this.GetAllChildren(currentMddNode, heuristicBound, numOfAgents,
                                                                  constraints, mustConstraints, reserved);
                    if (children.Count == 0) // Heuristic wasn't perfect because of constraints, illegal moves or other reasons
                    {
                        toDelete.Add(currentMddNode);
                    }

                    foreach (MDDNode child in children)
                    {
                        MDDNode toAdd = child; // The compiler won't let me assign to the foreach variable...
                        if (perLevelClosedList.ContainsKey(child))
                        {
                            toAdd = perLevelClosedList[child];
                        }
                        else
                        {
                            perLevelClosedList.Add(toAdd, toAdd);
                            llNode = new LinkedListNode <MDDNode>(toAdd);
                            toAdd.setMyNode(llNode);
                            levels[i + 1].AddLast(llNode);
                        }
                        currentMddNode.addChild(toAdd);  // forward edge
                        toAdd.addParent(currentMddNode); // backward edge
                    }
                }
                perLevelClosedList.Clear();
            }

            foreach (MDDNode goal in levels[numOfLevels]) // The goal may be reached in more than one direction
            {
                goal.startOrGoal = true;
            }

            foreach (MDDNode remove in toDelete)
            {
                remove.delete();
            }
        }
Exemplo n.º 6
0
        public MDD(MDD other)
        {
            this.problem        = other.problem;
            this.mddNum         = other.mddNum;
            this.agentNum       = other.agentNum;
            this.numOfAgents    = other.numOfAgents;
            this.cost           = other.cost;
            this.supportPruning = other.supportPruning;
            if (other.levels == null)
            {
                this.levels = null;
                return;
            }
            this.levels = new LinkedList <MDDNode> [other.levels.Length];
            int numOfLevels = other.levels.Length - 1;  // Level zero not counted

            for (int i = 0; i < levels.Length; i++)
            {
                levels[i] = new LinkedList <MDDNode>();
            }
            Dictionary <MDDNode, MDDNode> originals = new Dictionary <MDDNode, MDDNode>();
            Dictionary <MDDNode, MDDNode> copies    = new Dictionary <MDDNode, MDDNode>();
            MDDNode copiedRoot = new MDDNode(new TimedMove(other.levels[0].First.Value.move),
                                             numOfAgents, this, supportPruning); // Root
            LinkedListNode <MDDNode> llNode = new LinkedListNode <MDDNode>(copiedRoot);

            copiedRoot.setMyNode(llNode);
            llNode.Value.startOrGoal = true;
            levels[0].AddFirst(llNode);
            originals.Add(copiedRoot, other.levels[0].First.Value);
            copies.Add(other.levels[0].First.Value, copiedRoot);

            for (int i = 0; i < numOfLevels; i++) // For each level, populate the _next_ level
            {
                foreach (MDDNode copiedNode in levels[i])
                {
                    foreach (MDDNode originalChildNode in originals[copiedNode].children)
                    {
                        MDDNode copiedChild;
                        if (copies.ContainsKey(originalChildNode) == false)
                        {
                            copiedChild = new MDDNode(originalChildNode.move, numOfLevels, this, supportPruning);
                            originals.Add(copiedChild, originalChildNode);
                            copies.Add(originalChildNode, copiedChild);
                            llNode = new LinkedListNode <MDDNode>(copiedChild);
                            copiedChild.setMyNode(llNode);
                            levels[i + 1].AddLast(llNode);
                        }
                        else
                        {
                            copiedChild = copies[originalChildNode];
                        }
                        copiedNode.addChild(copiedChild);  // forward edge
                        copiedChild.addParent(copiedNode); // backward edge
                    }
                }
            }

            originals.Clear();
            copies.Clear();

            foreach (MDDNode goal in levels[numOfLevels]) // The goal may be reached in more than one direction
            {
                goal.startOrGoal = true;
            }
            this.queryConstraint = other.queryConstraint;
        }
Exemplo n.º 7
0
 /// <summary>
 /// Create a state with the given state for every agent.
 /// </summary>
 /// <param name="allAgentsState"></param>
 /// <param name="minDepth"></param>
 /// <param name="minCost"></param>
 /// <param name="mddNode"></param>
 public WorldState(AgentState[] allAgentsState, int minDepth = -1, int minCost = -1, MDDNode mddNode = null)
 {
     this.allAgentsState = allAgentsState.ToArray();
     this.makespan       = allAgentsState.Max(state => state.lastMove.time); // We expect to only find at most two G values within the agent group
     this.CalculateG();                                                      // G not necessarily zero when solving a partially solved problem.
     this.sumConflictCounts = 0;
     this.conflictCounts    = new Dictionary <int, int>();                   // Unused if not running under CBS, and we can't tell at this point easily
     this.conflictTimes     = new Dictionary <int, List <int> >();           // Unused if not running under CBS, and we can't tell at this point easily
     this.minGoalTimeStep   = minDepth;
     this.minGoalCost       = minCost;
     if (mddNode == null)
     {
         this.currentMoves = new Dictionary <TimedMove, int>();
     }
     this.goalCost        = NOT_SET;
     this.goalSingleCosts = null;
     this.singlePlans     = null;
     this.hBonus          = 0;
     this.mddNode         = mddNode;
 }
Exemplo n.º 8
0
 /// <summary>
 /// Factory method. Creates the initial state from which the search will start.
 /// This will be the first state to be inserted to OPEN.
 /// </summary>
 /// <returns>The root of the search tree</returns>
 protected virtual WorldState CreateSearchRoot(int minDepth = -1, int minCost = -1, MDDNode mddNode = null)
 {
     return(new WorldState(this.instance.agents, minDepth, minCost, mddNode));
 }
Exemplo n.º 9
0
        override protected WorldState CreateSearchRoot(int minDepth = -1, int minCost = -1, MDDNode mddNode = null)
        {
            var root = new WorldStateForPartialExpansion(this.instance.agents, minDepth, minCost, mddNode);

            root.sic = (int)SumIndividualCosts.h(root, this.instance);
            return(root);
        }