Ejemplo n.º 1
0
 /// <summary>
 /// Not used
 /// </summary>
 /// <param name="plan"></param>
 /// <param name="agentIndex"></param>
 /// <param name="agentNum"></param>
 public SinglePlan(Plan plan, int agentIndex, int agentNum)
 {
     this.agentNum        = agentNum;
     this.locationAtTimes = new List <Move>();
     foreach (List <Move> movesAtTimestep in plan.GetLocations())
     {
         this.locationAtTimes.Add(movesAtTimestep.ElementAt <Move>(agentIndex));
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Solve given instance with a list of algorithms
        /// </summary>
        /// <param name="instance">The instance to solve</param>
        public bool SolveGivenProblem(ProblemInstance instance, bool toExecute = false)
        {
            if (toExecute)
            {
                instanceId += 1;
            }
            bool success = true;

            //return; // add for generator
            // Preparing a list of agent indices (not agent nums) for the heuristics' Init() method
            List <uint> agentList = Enumerable.Range(0, instance.m_vAgents.Length).Select <int, uint>(x => (uint)x).ToList <uint>(); // FIXME: Must the heuristics really receive a list of uints?

            // Solve using the different algorithms
            Debug.WriteLine("Solving " + instance);

            //this.PrintProblemStatistics(instance);

            //double cr0 = instance.getConflictRation(0);
            //double cr1 = instance.getConflictRation(1);

            //Debug.WriteLine("Conflict ratio (first order): " + cr0);


            // Initializing all heuristics, whereever they're used
            for (int i = 0; i < heuristics.Count; i++)
            {
                heuristics[i].init(instance, agentList);
            }

            solutionCost = -1;
            int firstSolverToSolveIndex = -1;

            for (int i = 0; i < solvers.Count; i++)
            {
                //CbsNode.PRobustBound = 0.4;
                //CbsNode.PRobustBound = PRobustBound[i];
                if (outOfTimeCounters[i] < Constants.MAX_FAIL_COUNT) // After "MAX_FAIL_COUNT" consecutive failures of a given algorithm we stop running it.
                                                                     // Assuming problem difficulties are non-decreasing, if it consistently failed on several problems it won't suddenly succeed in solving the next problem.
                {
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    if (solvers[i].GetType() == typeof(CBS_LocalConflicts) || solvers[i].GetType() == typeof(CBS_GlobalConflicts))
                    {
                        if (((CBS_LocalConflicts)solvers[i]).mergeThreshold == 314159) // MAGIC NUMBER WHICH MAKES US ADJUST B according to map
                        {
                            ((CBS_LocalConflicts)solvers[i]).mergeThreshold = 0;
                        }
                    }


                    if (
                        (solvers[i].GetType() == typeof(IndependenceDetection) &&
                         ((IndependenceDetection)solvers[i]).groupSolver.GetType() == typeof(CBS_LocalConflicts)) ||
                        (solvers[i].GetType() == typeof(IndependenceDetection) &&
                         ((IndependenceDetection)solvers[i]).groupSolver.GetType() == typeof(CBS_GlobalConflicts))
                        )
                    {
                        if (((CBS_LocalConflicts)((IndependenceDetection)solvers[i]).groupSolver).mergeThreshold == 314159) // MAGIC NUMBER SEE ABOVE
                        {
                            ((CBS_LocalConflicts)((IndependenceDetection)solvers[i]).groupSolver).mergeThreshold = 0;
                        }
                    }


                    int       solverSolutionCost;
                    Stopwatch planningStopwatch = new Stopwatch();;

                    planningStopwatch.Start();
                    this.run(solvers[i], instance);
                    solverSolutionCost = solvers[i].GetSolutionCost();

                    if (solverSolutionCost < 0)
                    {
                        Console.WriteLine("TIMEOUT!!!!!!!!!!!!2");
                        solverSolutionCost = -1;
                        solutionCost       = -1;
                        planningStopwatch.Stop();
                        success = false;
                        continue;
                    }
                    if (toPrint)
                    {
                        Console.WriteLine();
                        printLinkedList(solvers[i].GetPlan().GetLocations());
                        Console.WriteLine();
                    }


                    success = true;

                    if (solverSolutionCost >= 0) // Solved successfully
                    {
                        plan = solvers[i].GetPlan();
                        int j = 0;
                        for (LinkedListNode <List <Move> > node = plan.GetLocations().First; node != null; node = node.Next)
                        {
                            foreach (Move mv in node.Value)
                            {
                                ((TimedMove)mv).time = j;
                            }
                            j++;
                        }



                        outOfTimeCounters[i] = 0;

                        // Validate solution:
                        if (solutionCost == -1)     // Record solution cost
                        {
                            solutionCost            = solverSolutionCost;
                            firstSolverToSolveIndex = i;
                        }
                        solutionCost = solverSolutionCost;

                        Console.WriteLine("+SUCCESS+ (:");
                    }
                    else
                    {
                        outOfTimeCounters[i]++;
                        Console.WriteLine("-FAILURE- ):");
                        success = false;
                    }

                    int originalCost = solutionCost;
                    planningStopwatch.Stop();
                    planningTime = planningStopwatch.Elapsed.TotalMilliseconds;
                    TimedMove[] lastMove = new TimedMove[instance.m_vAgents.Length];
                    for (int ass = 0; ass < instance.m_vAgents.Length; ass++)
                    {
                        lastMove[ass] = new TimedMove(instance.m_vAgents[ass].lastMove, 0);
                    }

                    WriteGivenProblem(instance, solvers[i], plan.GetLocations());
                }
                else if (toPrint)
                {
                    PrintNullStatistics(solvers[i]);
                }

                Console.WriteLine();
            }
            return(success);
        }