Ejemplo n.º 1
0
        private void InitializePopulation()
        {
            var c = new List<Chromosome>();

            for (int i = 0; i < PopulationSize; i++)
            {
                var gList = new List<Gene>();
                foreach (Gene g in ChromosomeFootprint)
                {
                    Gene gene = g.Clone();
                    gene.InitializeGene();
                    gList.Add(gene);
                }

                var cx = new Chromosome(gList);
                c.Add(cx);
            }

            _currentPopulation = new Population(c);
            _currentPopulation.CalculateAllFitness(_fitnessFunction);
        }
Ejemplo n.º 2
0
        public static Population SimpleRouletteSelection(this Population pCopy, int number, Random r)
        {
            var result = new List<Chromosome>();
            var p = new Population(new List<Chromosome>(pCopy.Chromosomes));

            for (int n = 0; n < number; n++)
            {
                var fitnessProportion = new double[p.PopulationSize];
                double totalFitness = 0;
                double totalProbability = 0;

                for (int i = 0; i < p.PopulationSize; i++)
                {
                    totalFitness += p.Chromosomes[i].Fitness;
                }

                for (int i = 0; i < p.PopulationSize; i++)
                {
                    fitnessProportion[i] = totalProbability + (p.Chromosomes[i].Fitness/totalFitness);
                    totalProbability = fitnessProportion[i];
                }

                double random = r.NextDouble();
                for (int i = 0; i < p.PopulationSize; i++)
                {
                    if (random <= fitnessProportion[i])
                    {
                        result.Add(p.Chromosomes[i]);
                        p.Chromosomes.Remove(p.Chromosomes[i]);
                        break;
                    }
                }
            }

            return new Population(result);
        }
Ejemplo n.º 3
0
 public void JoinPopulation(Population p)
 {
     Chromosomes.AddRange(p.Chromosomes);
 }
Ejemplo n.º 4
0
        public void Run()
        {
            InitializePopulation();

            double maxFitness = 0;

            while (_currentTrial++ < Trials)
            {
                
                Console.WriteLine("Starting trial " + _currentTrial + "...");

                string filename = QSConstants.DEFAULT_DATA_FILEPATH + "result" + _currentTrial + ".txt";

                using (var file = new StreamWriter(@filename))
                {
                    for (int generation = 0; generation < Generations; generation++)
                    {
                        Console.WriteLine(generation);
                        var newPop = new Population();

                        //elitism
                        Population elite = _currentPopulation.ElitismSelection(2);

                        newPop.JoinPopulation(elite);

                        //populate rest of population with children
                        while (newPop.PopulationSize < PopulationSize)
                        {
                            //selection
                            Population parents = _currentPopulation.SimpleRouletteSelection(2, _randomSeed);

                            //crossover
                            Population children = Crossover.BinaryCrossover(parents[0], parents[1], _randomSeed);

                            newPop.JoinPopulation(children);
                        }

                        newPop.MutatePopulation(MutationRate, _randomSeed);

                        double average = 0;

                        //calculate fitness for new population
                        foreach (Chromosome chromosome in newPop.Chromosomes)
                        {
                            if (chromosome.Fitness.Equals(double.NaN))
                                chromosome.CalculateFitness(_fitnessFunction);
                            if (chromosome.Fitness > maxFitness)
                            {
                                maxFitness = chromosome.Fitness;
                                maxC = chromosome;

                                file.WriteLine("Generation: {0}", generation);
                                file.WriteLine("Fitness: {0}", maxFitness);
                                for (int i = 0; i < maxC.Count(); i++)
                                {
                                    file.WriteLine("Gene " + i + ":" + maxC[i]);
                                }

                                file.WriteLine();
                                file.Flush();
                            }

                            average += chromosome.Fitness;
                            //Console.WriteLine(chromosome.Fitness + ": x:" + (double)chromosome.GeneList[0].geneValue + "; y:" + (double)chromosome.GeneList[1].geneValue);
                        }

                        //Console.WriteLine(average/newPop.Chromosomes.Count);
                        //Console.WriteLine("-----------------------------------");

                        _currentPopulation = newPop;
                    }
                }

                maxC.CalculateFitness(_fitnessFunction);
            }

            Console.WriteLine("Execution complete.");
        }