示例#1
0
        public async Task <Genome> FindPathAsync(Roteiro map, Genome seed = null)
        {
            if (Mutate == null || Crossover == null || Fitness == null || Selection == null)
            {
                throw new System.Exception("GA cant run without all operators");
            }

            var locals = map.Destinations.ToList();

            locals.Add(map.Depot);
            await routeService.Prepare(locals);

            var rand      = RandomSingleton.Instance;
            var startNode = map.Depot;

            Populations.Clear();

            var popusize = PopulationSize;

            if (seed != null)
            {
                Populations.Add(seed);
                popusize--;
            }

            Populations.AddRange(
                Enumerable.Range(0, popusize)
                .Select(_ => Genome.Generator(map, Settings)));

            await CalcFitness();

            for (int i = 0; i < GenerationLimit; i++)
            {
                var newpopulations = new List <Genome>();

                for (int j = 0; j < BestSolutionToPick; j++)
                {
                    newpopulations.Add(Populations[j]);
                }

                while (newpopulations.Count < Populations.Count)
                {
                    if (newpopulations.Any(e => e.Trucks.SelectMany(l => l.Locals).Count() != map.Destinations.Count))
                    {
                        throw new System.Exception();
                    }

                    // Selection
                    var(nodemom, nodedad) = Selection.SelectCouple(Populations);

                    // CrossOver
                    var sons = Crossover.Make(nodemom, nodedad);

                    // Mutation
                    sons = sons.Select(s => Mutate.Apply(s)).ToArray();

                    newpopulations.AddRange(sons);
                }
                Populations = newpopulations.ToList();

                await CalcFitness();

                Best = Populations.First();
            }

            return(Best);
        }
        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);
        }