/// <summary> /// Tries to find a plan for this group, that will not conflict with the given plan, /// and still has the same solution cost as the current solution cost. /// This is used in the ImprovedID() method. /// </summary> /// <param name="plan"></param> /// <param name="runner"></param> /// <returns></returns> public bool ReplanUnderConstraints(Plan plan, Run runner) { int oldCost = this.solutionCost; Plan oldPlan = this.plan; HashSet <TimedMove> reserved = new HashSet <TimedMove>(); plan.AddPlanToHashSet(reserved, Math.Max(plan.GetSize(), this.plan.GetSize()) - 1); // TODO: Why -1? this.instance.parameters[IndependenceDetection.ILLEGAL_MOVES_KEY] = reserved; this.instance.parameters[IndependenceDetection.MAXIMUM_COST_KEY] = solutionCost; bool success = this.Solve(runner); this.instance.parameters.Remove(IndependenceDetection.ILLEGAL_MOVES_KEY); this.instance.parameters.Remove(IndependenceDetection.MAXIMUM_COST_KEY); if (success == false) { this.solutionCost = oldCost; this.plan = oldPlan; } return(success); }
/// <summary> /// Tries to find a plan for this group, that will not conflict with the given plan, /// and still has the same solution cost as the current solution cost. /// This is used in the IndependenceDetection() method. /// </summary> /// <param name="plan"></param> /// <param name="runner"></param> /// <returns></returns> public bool ReplanUnderConstraints(Plan plan, Run runner) { bool success; int oldCost = this.solutionCost; WorldState oldSolution = this.solution; Plan oldPlan = this.plan; HashSet <TimedMove> reserved = new HashSet <TimedMove>(); plan.AddPlanToHashSet(reserved, Math.Max(plan.GetSize(), this.plan.GetSize()) - 1); this.instance.parameters[Trevor.ILLEGAL_MOVES_KEY] = reserved; this.instance.parameters[Trevor.MAXIMUM_COST_KEY] = solutionCost; success = this.Solve(runner); this.instance.parameters.Remove(Trevor.ILLEGAL_MOVES_KEY); this.instance.parameters.Remove(Trevor.MAXIMUM_COST_KEY); if (success == false) { this.solutionCost = oldCost; this.solution = oldSolution; this.plan = oldPlan; } return(success); }
/// <summary> /// Solve given instance with a list of algorithms /// </summary> /// <param name="instance">The instance to solve</param> public void SolveGivenProblem(ProblemInstance instance) { //maybe i need timer for this function int stopTimeMS = 1000 * 60 * 1; Stopwatch debugSW = new Stopwatch(); debugSW.Start(); //---------------------------------------- // 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); //Debug.WriteLine("Conflict ratio (second order): " + cr1); //this.resultsWriter.Write(cr0 + RESULTS_DELIMITER); //this.resultsWriter.Write(cr1 + RESULTS_DELIMITER); // Initializing all heuristics, whereever they're used for (int i = 0; i < heuristics.Count; i++) { heuristics[i].init(instance, agentList); if (debugSW.ElapsedMilliseconds > stopTimeMS) { Debug.WriteLine("Stop"); break; } } for (int i = 0; i < solvers.Count; 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(); this.run(solvers[i], instance); Console.WriteLine(); if (solvers[i].GetSolutionCost() >= 0) // Solved successfully { Plan plan = solvers[i].GetPlan(); int planSize = plan.GetSize(); if (planSize < 200) { plan.PrintPlan(); } else { Console.WriteLine("Plan is too long to print (" + planSize + " steps)."); } outOfTimeCounters[i] = 0; // Validate solution: if (i != 0 && solvers[0].GetSolutionCost() >= 0) { Debug.Assert(solvers[0].GetSolutionCost() == solvers[i].GetSolutionCost(), solvers[0] + " solution cost is different than that of " + solvers[i]); // Assuming algs are supposed to find an optimal solution, this is an error. //Debug.Assert(solvers[0].GetExpanded() == solvers[i].GetExpanded(), "Different Expanded"); //Debug.Assert(solvers[0].GetGenerated() == solvers[i].GetGenerated(), "Different Generated"); //Debug.Assert(solvers[0].GetSolutionDepth() == solvers[i].GetSolutionDepth(), "Depth Bug " + solvers[i]); } Console.WriteLine("+SUCCESS+ (:"); } else { outOfTimeCounters[i]++; Console.WriteLine("-FAILURE- ):"); } } else { PrintNullStatistics(solvers[i]); } Console.WriteLine(); if (debugSW.ElapsedMilliseconds > stopTimeMS) { Debug.WriteLine("Stop"); break; } } this.ContinueToNextLine(); }