/// <summary>
 /// constructor
 /// </summary>
 public MddMatchAndPrune(Run runner, CostTreeNodeSolver nodeSolver)
 {
     this.openList   = new Queue <MddMatchAndPruneState>();
     this.closedList = new Dictionary <MddMatchAndPruneState, MddMatchAndPruneState>();
     conflicted      = new bool[4];
     this.runner     = runner;
     this.nodeSolver = nodeSolver;
 }
Exemple #2
0
        public override bool Solve()
        {
            //int time = 0;
            CostTreeNodeSolver next = CreateNodeSolver(problem, runner);

            SinglePlan[] ans = null;
            Stopwatch    sw  = new Stopwatch();
            int          sumSubGroupA;
            int          sumSubGroupB;

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

                if (maxCost != -1)
                {
                    //if we are above the given solution return no solution found
                    if (sumSubGroupA + sumSubGroupB > maxCost)
                    {
                        return(this.minConflictsNotAvoided != 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
                if (sumSubGroupA >= costParentGroupA && sumSubGroupB >= costParentGroupB)
                {
                    ((CostTreeNodeSolverRepeatedMatching)next).setup(costTreeNode, syncSize, reserved);
                    generatedLL += next.generated;
                    expandedLL  += next.expanded;
                    sw.Start();
                    ans = next.Solve(CAT);
                    sw.Stop();
                    Console.WriteLine(sw.ElapsedMilliseconds);
                    if (sw.ElapsedMilliseconds > 0)
                    {
                        Console.ReadLine();
                    }
                    generatedLL += next.getGenerated();
                    if (ans != null)
                    {
                        if (ans[0] != null)
                        {
                            if (this.exhaustive == false)
                            {
                                totalCost = next.totalCost;
                                solution  = ans;
                                costs     = costTreeNode.costs;
                                survivedPruningHL--;
                                return(true);
                            }

                            if (next.conflictsNotAvoided < this.minConflictsNotAvoided)
                            {
                                totalCost = next.totalCost;
                                solution  = ans;
                                costs     = costTreeNode.costs;
                                survivedPruningHL--;
                                this.minConflictsNotAvoided = next.conflictsNotAvoided;
                                maxCost = totalCost;
                                if (next.conflictsNotAvoided == 0)
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }
                costTreeNode.Expand(openList, closedList);
                generatedHL += costTreeNode.costs.Length;
                openList.Dequeue();
            }
            totalCost = Constants.TIMEOUT_COST;
            Console.WriteLine("Out of time");
            return(false);
        }
Exemple #3
0
        public override bool Solve()
        {
            CostTreeNodeSolver nodeSolver = CreateNodeSolver(this.problem, this.runner);

            SinglePlan[] ans = null;

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

                //if we are above the given solution return no solution found
                if (sumSubGroupA + sumSubGroupB > maxCost)               // TODO: For makespan, using Max of the group "sums"
                {
                    return(this.minConflictsNotAvoided != int.MaxValue); // If we're running exhaustive ICTS and have a solution and
                }
                // reached a node with a higher cost, it means we've exhausted
                // all goal nodes and can return that a solution was found
                // (with some external conflicts)

                // Goal test, unless any subproblem hasn't reached its previous optimal cost or we're below the minimum total cost
                if (sumSubGroupA >= costParentGroupA && sumSubGroupB >= costParentGroupB &&
                    sumSubGroupA + sumSubGroupB >= minCost  // TODO: Support other cost functions
                    )
                {
                    ((CostTreeNodeSolverOldMatching)nodeSolver).Setup(costTreeNode, syncSize, this.reserved);
                    expandedHL++;
                    ans          = nodeSolver.Solve(CAT);
                    generatedLL += nodeSolver.generated;
                    expandedLL  += nodeSolver.expanded;
                    this.survivedPruningHL--;
                    if (ans != null && ans[0] != null)  // A solution was found!
                    {
                        if (this.exhaustive == false)
                        {
                            this.totalCost      = nodeSolver.totalCost;
                            this.solutionDepth  = nodeSolver.totalCost - this.initialEstimate;
                            this.solution       = ans;
                            this.costs          = costTreeNode.costs;
                            this.conflictCounts = nodeSolver.GetExternalConflictCounts();
                            this.conflictTimes  = nodeSolver.GetConflictTimes();
                            return(true);
                        }

                        if (nodeSolver.conflictsNotAvoided < this.minConflictsNotAvoided)
                        {
                            this.totalCost              = nodeSolver.totalCost;
                            this.solutionDepth          = nodeSolver.totalCost - this.initialEstimate;
                            this.solution               = ans;
                            this.costs                  = costTreeNode.costs;
                            this.conflictCounts         = nodeSolver.GetExternalConflictCounts();
                            this.conflictTimes          = nodeSolver.GetConflictTimes();
                            this.minConflictsNotAvoided = nodeSolver.conflictsNotAvoided;
                            this.maxCost                = totalCost;
                            if (nodeSolver.conflictsNotAvoided == 0)
                            {
                                return(true);
                            }
                        }
                    }
                    else
                    {
                        // This would allow us to also prune nodes where a subproblem has the same cost as its optimal cost,
                        // but in a bad configuration
                        // make ID pass the group solver a persistance object it got from the previous unconstrained execution
                        //if (this.instance.parameters.ContainsKey(IndependenceDetection.ILLEGAL_MOVES_KEY) == false &&
                        //    this.instance.parameters.ContainsKey(CBS.CONSTRAINTS) == false)  // technically we could cache by the constraints etc. but nah
                        // persistance.Add(costTreeNode.costs.ToImmutableArray())
                    }
                }
                else
                {
                    ++goalTestSkipped;
                }

                costTreeNode.Expand(openList, closedList);
                generatedHL += costTreeNode.costs.Length;  // We generated a child per each individual cost, with that cost increased by 1
                openList.Dequeue();
            }

            this.totalCost     = Constants.TIMEOUT_COST;
            this.solutionDepth = nodeSolver.totalCost - this.initialEstimate; // A lower bound
            Console.WriteLine("Out of time");
            return(false);
        }