internal void GlobalPheromoneUpdate(List <Node> mainSolution, Fitness mainSolutionFitnessValue, List <Edge> edges) { foreach (var node in mainSolution) { if (mainSolution.Last() == node) { break; } var nextNode = mainSolution.First(n => n.NurseId == node.NurseId + 1); var solutionEdge = edges.First(ed => ed.IndexNurseA == node.NurseId && ed.IndexNurseB == nextNode.NurseId && ed.IndexNodeA == node.Index && ed.IndexNodeB == nextNode.Index); solutionEdge.Pheromone = (1.0 - PARAM_RHO) * solutionEdge.Pheromone + PARAM_RHO * (1 / (1 + mainSolutionFitnessValue.CompleteFitnessValue * PARAM_LAMBDA)); } }
static void Main(string[] args) { var inputData = InputData.GetObjectDataFromFile("Instances/Sprint/sprint02.xml"); ACOHandler handler = new ACOHandler(inputData); List <Node>[] nodes = handler.GenerationManager.GetShiftPatterns(); //List<Node>[] nodes = NodeTest(); List <Edge> edges = new List <Edge>(); handler.ComputeStaticHeuristic(nodes); List <Node> mainSolution = handler.ExtractSolution(nodes); Fitness mainSolutionFitnessValue = handler.ApplySolution(mainSolution).Item1; handler.InitializeStandardPheromone(mainSolutionFitnessValue); handler.ListOfEdgesUpdate(mainSolution, edges); int consecutiveNoImprovements = 0; Console.Write("Solution construction "); do { Console.Write("."); List <Ant> ants = Ant.GenerateAnts(NUM_ANTS, handler.CoverRequirements, handler.CoverRequirementsArray); foreach (var ant in ants) { Console.Write(":"); for (int i = 0; i < handler.NurseNumber; i++) { //Compute the product between static heuristic and dynamic heuristic var heuristicInformation = handler.ComputeHeuristicInfo(nodes[i], ant.CoverRequirementsArray); var selectedIndex = handler.NodeSelection(heuristicInformation, nodes, edges, i); var selectedNode = nodes[i].ElementAt(selectedIndex); ant.Solution.Add(selectedNode); ant.CoverRequirementsArray = heuristicInformation[selectedIndex].Item2; } Tuple <Fitness, int[]> antSolutionApplied = handler.ApplySolution(ant.Solution); int[] antSolutionUpdatedCoverReq = antSolutionApplied.Item2; //Edges update handler.ListOfEdgesUpdate(ant.Solution, edges); if (Fitness.FitnessCompare(antSolutionApplied.Item1, mainSolutionFitnessValue) == 1) { mainSolution = ant.Solution; mainSolutionFitnessValue = antSolutionApplied.Item1; consecutiveNoImprovements = 0; Console.Write($" {antSolutionApplied.Item1.UncoveredShifts} + {antSolutionApplied.Item1.TotalOverShift} + {antSolutionApplied.Item1.TotalSolutionCost} "); } else { consecutiveNoImprovements++; } handler.LocalPheromoneUpdate(mainSolution, edges); } handler.GlobalPheromoneUpdate(mainSolution, mainSolutionFitnessValue, edges); } while (consecutiveNoImprovements < NO_IMPROVEMENT_LIMIT); handler.Evaluator.CalculateSolutionPenalty(mainSolution); handler.GenerationManager.PrintSolution(mainSolution, mainSolutionFitnessValue); Console.ReadKey(); }
internal void InitializeStandardPheromone(Fitness fitness) { Pheromone_0 = 1.0 / (fitness.CompleteFitnessValue + 1.0); CurrentPheromone_0 = Pheromone_0; }