示例#1
0
        /// <summary>
        /// Setup the relevant data structures for a run.
        /// </summary>
        public virtual void Setup(ProblemInstance problemInstance, int minDepth, Run runner)
        {
            minCAViolations  = int.MaxValue;
            passed           = 0;
            this.generatedHL = 1;
            this.expandedHL  = 1;
            this.generatedLL = 0;
            this.expandedLL  = 0;
            this.totalCost   = Constants.TIMEOUT_COST;

            // If there exists relevant previously solved subproblems - use their solution as a lower bound
            if (problemInstance.parameters.ContainsKey(CostTreeSearch.PARENT_GROUP1_KEY))
            {
                costA   = ((AgentsGroup)(problemInstance.parameters[CostTreeSearch.PARENT_GROUP1_KEY])).solutionCost;
                costB   = ((AgentsGroup)(problemInstance.parameters[CostTreeSearch.PARENT_GROUP2_KEY])).solutionCost;
                sizeOfA = ((AgentsGroup)(problemInstance.parameters[CostTreeSearch.PARENT_GROUP1_KEY])).Size();
            }
            else
            {
                costA   = problemInstance.m_vAgents[0].h;
                costB   = 0;
                sizeOfA = 1;
            }
            this.problem = problemInstance;
            this.runner  = runner;

            closedList = new HashSet <CostTreeNode>();
            openList   = new LinkedList <CostTreeNode>();
            int[]      costs = new int[problem.GetNumOfAgents()];
            AgentState temp;

            for (int i = 0; i < problem.GetNumOfAgents(); i++)
            {
                temp     = problem.m_vAgents[i];
                costs[i] = Math.Max(problem.GetSingleAgentOptimalCost(temp), minDepth);
            }

            openList.AddFirst(new CostTreeNode(costs));
            this.initialHeuristics = openList.First.Value.costs.Sum();

            // Store parameters used by Trevor's Independence Detection algorithm
            if (problemInstance.parameters.ContainsKey(Trevor.MAXIMUM_COST_KEY))
            {
                this.maxCost = (int)(problemInstance.parameters[Trevor.MAXIMUM_COST_KEY]);
            }
            else
            {
                this.maxCost = -1;
            }

            if (problemInstance.parameters.ContainsKey(Trevor.CONFLICT_AVOIDANCE))
            {
                ID_CAT = ((HashSet <TimedMove>)problemInstance.parameters[Trevor.CONFLICT_AVOIDANCE]);
            }
            if (problemInstance.parameters.ContainsKey(CBS_LocalConflicts.INTERNAL_CAT))
            {
                CBS_CAT = ((HashSet_U <TimedMove>)problemInstance.parameters[CBS_LocalConflicts.INTERNAL_CAT]);
            }
        }
示例#2
0
        /// <summary>
        /// Setup the relevant data structures for a run.
        /// </summary>
        /// <param name="problemInstance"></param>
        /// <param name="minDepth"></param>
        /// <param name="runner"></param>
        /// <param name="minCost">Not taken into account, just added to comply with ICbsSolver</param>
        public virtual void Setup(ProblemInstance problemInstance, int minDepth, Run runner, int minCost = -1)
        {
            this.minCAViolations        = int.MaxValue;
            CostTreeSearchSolver.passed = 0;
            this.generatedHL            = 1;
            this.expandedHL             = 1;
            this.generatedLL            = 0;
            this.expandedLL             = 0;
            this.totalCost = Constants.TIMEOUT_COST;

            // If there exists relevant previously solved subproblems - use their solution as a lower bound
            //if (problemInstance.parameters.ContainsKey(CostTreeSearch.PARENT_GROUP1_KEY))
            //{
            //    costA = ((AgentsGroup)(problemInstance.parameters[CostTreeSearch.PARENT_GROUP1_KEY])).solutionCost;
            //    costB = ((AgentsGroup)(problemInstance.parameters[CostTreeSearch.PARENT_GROUP2_KEY])).solutionCost;
            //    sizeOfA = ((AgentsGroup)(problemInstance.parameters[CostTreeSearch.PARENT_GROUP1_KEY])).Size();
            //}
            //else
            {
                costA   = problemInstance.GetSingleAgentOptimalCost(problemInstance.m_vAgents[0]);
                costB   = 0;
                sizeOfA = 1;
            }

            this.problem = problemInstance;
            this.runner  = runner;

            closedList = new HashSet <CostTreeNode>();
            openList   = new Queue <CostTreeNode>();
            int[] costs = new int[problem.GetNumOfAgents()];
            for (int i = 0; i < problem.GetNumOfAgents(); i++)
            {
                costs[i] = Math.Max(problem.GetSingleAgentOptimalCost(problem.m_vAgents[i]), minDepth);
            }

            openList.Enqueue(new CostTreeNode(costs)); // The root
            this.initialEstimate = openList.Peek().costs.Sum();

            // Store parameters used by IndependenceDetection's Independence Detection algorithm
            if (problemInstance.parameters.ContainsKey(IndependenceDetection.MAXIMUM_COST_KEY))
            {
                this.maxCost = (int)(problemInstance.parameters[IndependenceDetection.MAXIMUM_COST_KEY]);
            }
            else
            {
                this.maxCost = -1;
            }

            if (problemInstance.parameters.ContainsKey(IndependenceDetection.CONFLICT_AVOIDANCE))
            {
                ID_CAT = ((Dictionary <TimedMove, List <int> >)problemInstance.parameters[IndependenceDetection.CONFLICT_AVOIDANCE]);
            }

            if (problemInstance.parameters.ContainsKey(CBS_LocalConflicts.CAT))
            {
                CBS_CAT = ((Dictionary <TimedMove, List <int> >)problemInstance.parameters[CBS_LocalConflicts.CAT]);
            }
        }
示例#3
0
 public static uint h(WorldState s, ProblemInstance instance)
 {
     return((uint)s.allAgentsState.Sum(state => instance.GetSingleAgentOptimalCost(state)));
 }
示例#4
0
        /// <summary>
        /// Calculates for each agent and each direction it can go, the effect of that move on F. Illegal moves get byte.MaxValue.
        /// Also calcs maxDeltaF.
        /// Implicitly uses the SIC heuristic.
        /// </summary>
        /// <param name="problem">For GetSingleAgentOptimalCost</param>
        /// <returns></returns>
        public void calcSingleAgentDeltaFs(ProblemInstance problem, ValidityChecker isValid)
        {
            // Init
            this.singleAgentDeltaFs = new byte[allAgentsState.Length][];
            for (int i = 0; i < singleAgentDeltaFs.Length; i++)
            {
                this.singleAgentDeltaFs[i] = new byte[Constants.NUM_ALLOWED_DIRECTIONS];
            }

            int hBefore, hAfter;

            this.maxDeltaF = 0;

            // Set values
            for (int i = 0; i < allAgentsState.Length; i++)
            {
                hBefore = problem.GetSingleAgentOptimalCost(allAgentsState[i]); // According to SIC

                int singleAgentMaxLegalDeltaF = -1;

                foreach (TimedMove check in allAgentsState[i].lastMove.GetNextMoves())
                {
                    if (isValid(check, noMoves, this.makespan + 1, i, this, this) == false)
                    {
                        singleAgentDeltaFs[i][(int)check.direction] = byte.MaxValue;
                    }
                    else
                    {
                        hAfter = problem.GetSingleAgentOptimalCost(allAgentsState[i].agent.agentNum, check); // According to SIC

                        if (Constants.Variant == Constants.ProblemVariant.ORIG)
                        {
                            if (hBefore != 0)
                            {
                                singleAgentDeltaFs[i][(int)check.direction] = (byte)(hAfter - hBefore + 1); // h difference + g difference in this specific domain
                            }
                            else if (hAfter != 0)                                                           // If agent moved from its goal we must count and add all the steps it was stationed at the goal, since they're now part of its g difference
                            {
                                singleAgentDeltaFs[i][(int)check.direction] = (byte)(hAfter - hBefore + makespan - allAgentsState[i].arrivalTime + 1);
                            }
                            else
                            {
                                singleAgentDeltaFs[i][(int)check.direction] = 0; // This is a WAIT move at the goal.
                            }
                            singleAgentMaxLegalDeltaF = Math.Max(singleAgentMaxLegalDeltaF, singleAgentDeltaFs[i][(int)check.direction]);
                        }
                        else if (Constants.Variant == Constants.ProblemVariant.NEW)
                        {
                            if (hBefore == 0 && hAfter == 0)
                            {
                                singleAgentDeltaFs[i][(int)check.direction] = 0; // This is a WAIT move at the goal.
                            }
                            else
                            {
                                singleAgentDeltaFs[i][(int)check.direction] = (byte)(hAfter - hBefore + 1); // h difference + g difference in this specific domain
                            }
                            singleAgentMaxLegalDeltaF = Math.Max(singleAgentMaxLegalDeltaF, singleAgentDeltaFs[i][(int)check.direction]);
                        }
                    }
                }

                if (singleAgentMaxLegalDeltaF == -1) // No legal action for this agent, so no legal children exist for this node
                {
                    this.maxDeltaF = 0;              // Can't make it negative without widening the field.
                    break;
                }

                this.maxDeltaF += (byte)singleAgentMaxLegalDeltaF;
            }

            fLookup = new sbyte[allAgentsState.Length][];
            for (int i = 0; i < fLookup.Length; i++)
            {
                fLookup[i] = new sbyte[this.maxDeltaF + 1]; // Towards the last agents most of the row will be wasted (the last one can do delta F of 0 or 1),
                                                            // but it's easier than fiddling with array sizes
            }
        }