Exemple #1
0
        /**
         * Starts the genetic algorithm.
         * @param The background worker, so we can report progress.
         */
        public void Start(BackgroundWorker worker, DoWorkEventArgs args)
        {
            Population population = new Population(fitnessCalculator, PopulationSize, BitSize, GenotypeLength, GroupSize, true);
            int        steps      = 0;

            CandidateSolution solution;
            int max;
            Dictionary <int, List <int> > groups;

            while (steps < MaxStep)
            {
                population = EvolvePopulation(population);

                if (steps % 2 == 0)
                {
                    solution = population.getFittest();
                    max      = solution.Solution.Max();
                    groups   = new Dictionary <int, List <int> >();
                    for (int i = 1; i <= max; i++)
                    {
                        groups.Add(i, new List <int>());
                    }

                    for (int member = 0; member < solution.Solution.Length; member++)
                    {
                        groups[solution.Solution[member]].Add(member + 1);
                    }

                    worker.ReportProgress(steps * 100 / MaxStep, new ProblemResult()
                    {
                        Groups = groups, Fitness = solution.Fitness
                    });
                }

                steps++;
            }

            // Finished.
            solution = population.getFittest();
            max      = solution.Solution.Max();
            groups   = new Dictionary <int, List <int> >();
            for (int i = 1; i <= max; i++)
            {
                groups.Add(i, new List <int>());
            }

            for (int member = 0; member < solution.Solution.Length; member++)
            {
                groups[solution.Solution[member]].Add(member + 1);
            }
            args.Result = new ProblemResult()
            {
                Groups = groups, Fitness = solution.Fitness
            };
        }
        void optimizePortfolio(int populationSize, int optimalValue)
        {
            Random rn             = new Random(10);
            double genesSum       = 0;
            double secGenesSum    = 0;
            double thirdGenesSum  = 0;
            double fourthGenesSum = 0;
            double childGenesSum  = 0;
            double child2GenesSum = 0;


            stopWatch.Reset();
            Elapsed_Time = "Time: 00:00:00.00";

            stopWatch.Start();

            population.initializePopulation(populationSize);

            population.calculateFitness();

            Console.WriteLine("Generation: " + generationCount + " Fittest: " + population.fittest + " Second Fittest: " + population.secondFittest + " least fit: " + population.leastFittest);

            /*
             * for (int i = 0; i < 10; i++)
             * {
             *  Console.WriteLine("Genes: " + population.getFittest().genes[i]);
             * }
             */

            Read(population.fittest, generationCount);

            //Continue genetics until optimal fitness value is found
            while (population.fittest < optimalValue)
            {
                Thread.Sleep(50);
                ++generationCount;

                selection();

                crossover();

                if (rn.Next() % 10 < mutationRate)
                {
                    mutation();
                }

                replacement(populationSize);

                for (int i = 0; i < fittest.genes.Length; i++)
                {
                    genesSum       += fittest.genes[i];
                    secGenesSum    += secondFittest.genes[i];
                    thirdGenesSum  += thirdFittest.genes[i];
                    fourthGenesSum += fourthFittest.genes[i];
                    childGenesSum  += child.genes[i];
                    child2GenesSum += child2.genes[i];
                }

                for (int i = 0; i < fittest.genes.Length; i++)
                {
                    // make genes add to 1
                    //fittest.genes[i] = (fittest.genes[i] / genesSum) * 1;
                    //secondFittest.genes[i] = (secondFittest.genes[i] / secGenesSum) * 1;
                    //thirdFittest.genes[i] = (thirdFittest.genes[i] / thirdGenesSum) * 1;
                    //fourthFittest.genes[i] = (fourthFittest.genes[i] / fourthGenesSum) * 1;
                    child.genes[i]  = (child.genes[i] / childGenesSum) * 1;
                    child2.genes[i] = (child2.genes[i] / child2GenesSum) * 1;
                }

                genesSum       = 0;
                secGenesSum    = 0;
                thirdGenesSum  = 0;
                fourthGenesSum = 0;
                childGenesSum  = 0;
                child2GenesSum = 0;

                for (int i = 0; i < fittest.genes.Length; i++)
                {
                    genesSum       += fittest.genes[i];
                    secGenesSum    += secondFittest.genes[i];
                    thirdGenesSum  += thirdFittest.genes[i];
                    fourthGenesSum += fourthFittest.genes[i];
                    childGenesSum  += child.genes[i];
                    child2GenesSum += child2.genes[i];
                }

                Console.WriteLine("Genes Sum: " + genesSum);
                Console.WriteLine("SecGenes Sum: " + secGenesSum);
                Console.WriteLine("ThirdGenes Sum: " + thirdGenesSum);
                Console.WriteLine("FourthGenes Sum: " + fourthGenesSum);
                Console.WriteLine("Child Genes Sum: " + childGenesSum);
                Console.WriteLine("Child2 Genes Sum: " + child2GenesSum);


                genesSum       = 0;
                secGenesSum    = 0;
                thirdGenesSum  = 0;
                fourthGenesSum = 0;
                childGenesSum  = 0;
                child2GenesSum = 0;

                updateFitnessValues();

                population.calculateFitness();

                Console.WriteLine("Generation: " + generationCount + " Fittest: " + population.fittest + " Second Fittest: " + population.secondFittest);

                Appl_Weight = population.getFittest().genes[0] * 100;
                Msft_Weight = population.getFittest().genes[1] * 100;
                Amzn_Weight = population.getFittest().genes[2] * 100;
                Nflx_Weight = population.getFittest().genes[3] * 100;
                Fb_Weight   = population.getFittest().genes[4] * 100;
                Bxc_Weight  = population.getFittest().genes[5] * 100;
                Xom_Weight  = population.getFittest().genes[6] * 100;
                Jnj_Weight  = population.getFittest().genes[7] * 100;
                Mcd_Weight  = population.getFittest().genes[8] * 100;
                Jpm_Weight  = population.getFittest().genes[9] * 100;

                /*
                 * for (int i = 0; i < 10; i++)
                 * {
                 *  Console.WriteLine("Genes: " + population.getFittest().genes[i]);
                 * }
                 */

                //Update chart
                Read(population.fittest, generationCount);

                Console.WriteLine("\n\n");
            }

            Console.WriteLine("\nSolution found in generation " + generationCount);
            Console.WriteLine("Fitness: " + population.getFittest().fitness);
            Console.WriteLine("Genes: ");

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(population.getFittest().genes[i]);
            }

            Console.WriteLine("");

            stopWatch.Stop();

            TimeSpan ts = stopWatch.Elapsed;

            Elapsed_Time = "Time: " + String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                    ts.Hours, ts.Minutes, ts.Seconds,
                                                    ts.Milliseconds / 10);
        }