private async Task CalcFitness() { await CalcGenomeRoutesAsync(); Populations.ForEach(e => e.CalcFitness(Fitness)); Populations = Populations .OrderBy(o => o.Fitness).ToList(); /*.OrderBy(e => * e.Trucks.Any(t => t.Routes.Any(r => r.Late))) * .ThenBy(o => o.Fitness).ToList();*/ }
public override bool Find(IMap map, IHeuristic heuristic) { if (Mutate == null || Crossover == null || Fitness == null || Selection == null) { throw new System.Exception("GA cant run without all operators"); } if (Fitness.Heuristic == null) { Fitness.Heuristic = heuristic; } var Adaptation = new Adaptation(map); var rand = RandomFactory.Rand; var startNode = map.StartNode; var endNode = map.EndNode; for (int i = 0; i < PopulationSize; i++) { Populations.Add(new Genome(map)); } CalcFitness(); var step = 0; OnStart(BuildArgs(step, map)); for (int i = 0; i < GenerationLimit; i++) { var newpopulations = new List <IGenome>(); Populations = Populations.OrderBy(o => o.Fitness).ToList(); for (int j = 0; j < BestSolutionToPick; j++) { Populations[j].Fitness = Fitness.Calc(Populations[j]); newpopulations.Add(Populations[j]); } var ran = rand.Next(1, Populations.Count); var best = Populations.First().ListNodes; var best2 = Selection.Select(Populations).ListNodes; endNode = best.Last(); if (endNode.Equals(map.EndNode)) { endNode.Direction = DirectionMovement.None; map.EndNode = endNode; map.UpdateClosedList(new List <Node>()); map.UpdateOpenList(new List <Node>()); //if (!best.First().Equals(map.StartNode)) OnEnd(BuildArgs(step, map, true)); Generations = i; return(true); } map.UpdateClosedList(best); map.UpdateOpenList(best2); while (newpopulations.Count < Populations.Count) { // Selection var nodemom = Selection.Select(Populations); var nodedad = Selection.Select(Populations); // CrossOver var cross = Crossover.Calc(new CrossoverOperation(nodemom, nodedad)); //if (!cross.Mom.ListNodes.First().Equals(map.StartNode)) { } //if (!cross.Dad.ListNodes.First().Equals(map.StartNode)) { } // Mutation nodemom = Mutate.Calc(cross.Mom); nodedad = Mutate.Calc(cross.Dad); //if (!nodemom.ListNodes.First().Equals(map.StartNode)) { } //if (!nodedad.ListNodes.First().Equals(map.StartNode)) { } // Adaptation nodemom = Adaptation.Calc(nodemom); nodedad = Adaptation.Calc(nodedad); nodemom.Fitness = Fitness.Calc(nodemom); nodedad.Fitness = Fitness.Calc(nodedad); //if (!nodemom.ListNodes.First().Equals(map.StartNode)) { } //if (!nodedad.ListNodes.First().Equals(map.StartNode)) { } // Add in new population newpopulations.Add(nodemom); newpopulations.Add(nodedad); } Populations = newpopulations.ToList(); OnStep(BuildArgs(step++, map)); } Generations = GenerationLimit; OnEnd(BuildArgs(step, map, false)); return(false); }