Пример #1
0
        public override bool Solve()
        {
            CostTreeNodeSolverOldMatching nodeSolver = new CostTreeNodeSolverOldMatching(problem, runner); // TODO: This begs a factory.

            SinglePlan[] ans = null;
            int          sumSubGroupA;
            int          sumSubGroupB;

            //TODO if no solution found the algorithm will never stop
            while (runner.ElapsedMilliseconds() < Constants.MAX_TIME)
            {
                costTreeNode = openList.Peek();
                sumSubGroupA = costTreeNode.Sum(0, this.sizeOfA);
                sumSubGroupB = costTreeNode.Sum(this.sizeOfA, costTreeNode.costs.Length);

                if (maxCost != -1)
                {
                    //if we are above the given solution return no solution found
                    if (sumSubGroupA + sumSubGroupB > maxCost)
                    {
                        return(this.minCAViolations != int.MaxValue);
                    }
                    //if we are below the given solution no need to do goal test just expand node
                    if (sumSubGroupA + sumSubGroupB < maxCost)
                    {
                        costTreeNode.Expand(openList, closedList);
                        openList.Dequeue();
                        continue;
                    }
                }

                // Reuse optimal solutions to previously solved subproblems
                // Eli: Does this actually happen?
                if (sumSubGroupA >= costA && sumSubGroupB >= costB)
                {
                    nodeSolver.Setup(costTreeNode, syncSize);
                    expandedHL++;
                    ans          = nodeSolver.Solve(ID_CAT, CBS_CAT);
                    generatedLL += nodeSolver.generated;
                    expandedLL  += nodeSolver.expanded;
                    if (ans != null)
                    {
                        if (ans[0] != null)
                        {
                            if (Constants.EXHAUSTIVE_ICTS == false)
                            {
                                this.totalCost     = nodeSolver.totalCost;
                                this.solutionDepth = nodeSolver.totalCost - this.initialEstimate;
                                this.solution      = ans;
                                CostTreeSearchSolver.passed--;
                                return(true);
                            }

                            if (nodeSolver.caViolations < this.minCAViolations)
                            {
                                this.totalCost     = nodeSolver.totalCost;
                                this.solutionDepth = nodeSolver.totalCost - this.initialEstimate;
                                this.solution      = ans;
                                CostTreeSearchSolver.passed--;
                                this.minCAViolations = nodeSolver.caViolations;
                                this.maxCost         = totalCost;
                                if (nodeSolver.caViolations == 0)
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }

                costTreeNode.Expand(openList, closedList);
                generatedHL += costTreeNode.costs.Length;
                openList.Dequeue();
            }

            this.totalCost     = Constants.TIMEOUT_COST;
            this.solutionDepth = nodeSolver.totalCost - this.initialEstimate; // A lower bound
            Console.WriteLine("Out of time");
            return(false);
        }
Пример #2
0
        public override bool Solve()
        {
            //long time=0;
            CostTreeNodeSolverOldMatching next = new CostTreeNodeSolverOldMatching(problem, runner);

            LinkedList <Move>[] ans = null;
            int sumSubGroupA;
            int sumSubGroupB;

            //TODO if no solution found the algorithm will never stop
            while (runner.ElapsedMilliseconds() < Constants.MAX_TIME)
            {
                costTreeNode = openList.First.Value;
                sumSubGroupA = costTreeNode.sum(0, sizeOfA);
                sumSubGroupB = costTreeNode.sum(sizeOfA, costTreeNode.costs.Length);

                if (maxCost != -1)
                {
                    //if we are above the given solution return no solution found
                    if (sumSubGroupA + sumSubGroupB > maxCost)
                    {
                        return(this.minCAViolations != int.MaxValue);
                    }
                    //if we are below the given solution no need to do goal test just expand node
                    if (sumSubGroupA + sumSubGroupB < maxCost)
                    {
                        costTreeNode.expandNode(openList, closedList);
                        openList.RemoveFirst();
                        continue;
                    }
                }

                // Reuse optimal solutions to previously solved subproblems
                if (sumSubGroupA >= costA && sumSubGroupB >= costB)
                {
                    next.setup(costTreeNode, syncSize);
                    expandedHL++;
                    ans          = next.Solve(ID_CAT, CBS_CAT);
                    generatedLL += next.generated;
                    expandedLL  += next.expanded;
                    if (ans != null)
                    {
                        if (ans[0] != null)
                        {
                            if (Constants.exhaustiveIcts == false)
                            {
                                totalCost = next.costs.Sum();
                                solution  = ans;
                                passed--;
                                return(true);
                            }

                            if (next.caViolations < this.minCAViolations)
                            {
                                totalCost = next.costs.Sum();
                                solution  = ans;
                                passed--;
                                this.minCAViolations = next.caViolations;
                                maxCost = totalCost;
                                if (next.caViolations == 0)
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }
                costTreeNode.expandNode(openList, closedList);
                generatedHL += costTreeNode.costs.Length;
                openList.RemoveFirst();
            }
            totalCost = Constants.TIMEOUT_COST;
            Console.WriteLine("Out of time");
            return(false);
        }