Ejemplo n.º 1
0
        public Organism[] Solve(bool prune)
        {
            var leaderboard = new Leaderboard(5, prune);

            var organisms   = new List <Organism>();
            var generations = 0;

            for (int i = 0; i < 50; i++)
            {
                Organism organism = new Organism(this, problem);
                organisms.Add(organism);
                leaderboard.AddOrganism(organism);
            }

            if (includedOrganisms != null)
            {
                foreach (var organism in includedOrganisms.Select(i => i.Clone(this)))
                {
                    organisms.Add(organism);
                    leaderboard.AddOrganism(organism);
                }
            }

            var best      = default(Organism);
            var worst     = default(Organism);
            var bestCost  = double.MaxValue;
            var worstCost = 0d;

            var newBest = false;

            while (generations < 2000)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }

                generations++;

                newBest = false;

                foreach (var organism in organisms)
                {
                    organism.Tick();

                    leaderboard.AddOrganism(organism);

                    if (organism.Solution.CostTotal < bestCost)
                    {
                        bestCost = organism.Solution.CostTotal;
                        best     = organism;

                        newBest = true;
                    }

                    if (organism.Solution.CostTotal > worstCost)
                    {
                        worstCost = organism.Solution.CostTotal;
                        worst     = organism;
                    }
                }

                if (RandomHelper.GetShort(10) == 1)
                {
                    organisms.Remove(worst);
                    Organism clone = best.Clone(this);
                    organisms.Add(clone);
                }
                else if (RandomHelper.GetShort(30) == 1)
                {
                    organisms.Remove(worst);

                    var      index = RandomHelper.GetShort(organisms.Count);
                    Organism clone = organisms[index].Clone(this);
                    organisms.Add(clone);
                }
                else if (RandomHelper.GetShort(100) == 1)
                {
                    organisms.Remove(worst);

                    int      index  = RandomHelper.GetShort(organisms.Count);
                    Organism mother = best;
                    Organism father = organisms[index];

                    Organism child = mother.MakeCrossOver(father);
                    organisms.Add(child);
                }

                worst     = null;
                worstCost = 0d;

                if (newBest)
                {
                    Console.WriteLine(generations + ": Current (" + bestCost + ", " + best.Solution.IncompletenessPenalty + "): " + best.ToString());
                }
            }

            Console.WriteLine("Done.");

            return(leaderboard.GetBest());
        }