예제 #1
0
파일: GA.cs 프로젝트: jakkarintiew/FYP
        public void NewGeneration()
        {
            int finalCount = Settings.PopulationSize + Settings.NumNewDNA;
            List <Chromosome> newPopulation = new List <Chromosome>();

            if (finalCount <= 0)
            {
                return;
            }

            if (Population.Count > 0)
            {
                CalculateAllFitness();
                Population = Population.OrderBy(chrmsm => chrmsm.Fitness).ToList();
            }

            for (int i = 0; i < finalCount; i++)
            {
                // Keep only top individuals of the previous generation
                if (i < Elitism && i < Settings.PopulationSize)
                {
                    newPopulation.Add(Population[i]);
                    //Console.WriteLine("Generation: " + Generation);
                    //Console.WriteLine("/n");
                    //Console.WriteLine("genes: [{0}]", string.Join(",\t", Population[i].genes));
                    //Console.WriteLine(Population[i].fitness);
                }
                else if (i < Settings.PopulationSize)
                {
                    Chromosome parent1 = Selection.PerformSelection(Population);
                    Chromosome parent2 = Selection.PerformSelection(Population);
                    Chromosome child   = Crossover.PerformCrossover(parent1, parent2);
                    child = Mutation.PerformMutation(child);
                    newPopulation.Add(child);
                }
                else if (Settings.EnableCrossoverNewDNA)
                {
                    newPopulation.Add(new Chromosome(shouldInitGenes: true));
                }
            }

            Population = newPopulation;
            Generation++;
        }
예제 #2
0
        public virtual void PerformIteration()
        {
            foreach (var chromosome in Population)
            {
                chromosome.FittnessValue = FittnessFunction.Calculate(chromosome);
            }

            var selection = Selection.Select(Population);
            var crossover = Crossover.Perform(selection);

            foreach (var chromosome in crossover)
            {
                Mutation.Mutate(chromosome);
            }

            Population.Remove(Population.FirstOrDefault(
                                  x => x.FittnessValue == Population.Min(y => y.FittnessValue)));
            Population.Remove(Population.FirstOrDefault(
                                  x => x.FittnessValue == Population.Min(y => y.FittnessValue)));

            Population.AddRange(crossover);
        }
예제 #3
0
        public GeneticAlgorithm(Selection <T> selectionMethod, Crossover <T> crossoverMethod, Mutation <T> mutationMethod, FitnessFunction <T> fitnessFunction, CreateNewSolution <T> createNewSolution, AcceptResult <T> acceptResult, int population, double mutationProbability, int selectionCount, int eliteCount = 0)
        {
            if (population < 2)
            {
                throw new Exception("Population must be greater or equal 2.");
            }

            if (eliteCount >= population)
            {
                throw new Exception("EliteCount must be less than population.");
            }

            selection      = selectionMethod;
            crossover      = crossoverMethod;
            mutation       = mutationMethod;
            fitness        = fitnessFunction;
            createSolution = createNewSolution;
            accept         = acceptResult;

            maxPopulation            = population;
            this.mutationProbability = mutationProbability;
            this.selectionCount      = selectionCount;
            this.eliteCount          = eliteCount;
        }
        private void start_Click(object sender, EventArgs e)
        {
            ReadFile.Read();

            Program.itr = 1000;
            Program.pM  = Double.Parse(mutationRate.Text, culture);
            Program.pC  = Double.Parse(crossoverRate.Text, culture);
            int tryCount = Convert.ToInt32(repeatTime.Text);

            Program.size = Program.weight.Count;
            Program.n    = Convert.ToInt32(populationSize.Text);

            for (int k = 0; k < tryCount; k++)
            {
                double[]  fit       = new double[Program.n]; // (fx/overall) or fx values
                double[]  norm      = new double[Program.n]; // fitness values get norm
                int[]     choosenId = new int[Program.n];    // choose for new generation
                int       bestId    = 0;
                int       worstId   = 0;
                int       bestFx    = 0;
                int       max;
                int       index;
                Stopwatch stopw = new Stopwatch();
                stopw.Start();

                // Generate Population
                Population population = new Population();

                bool       gotcha        = true;
                Population newGeneration = new Population(gotcha);

                for (int i = 0; i < Program.itr; i++)
                {
                    // Normalize
                    norm = Population.Normalize(population.curPopulation);

                    // Select from population
                    switch (selectionMethod.SelectedItem.ToString())
                    {
                    case "Tournament Selection":
                        choosenId = Selection.TournamentSelection(norm);
                        break;

                    case "Roulet Selection":
                        choosenId = Selection.RouletChark(norm);
                        break;
                    }

                    // Keep best
                    bestId = Population.GetBest(norm);

                    newGeneration.curPopulation = Population.CopyToNew(choosenId, population.curPopulation, newGeneration.curPopulation);

                    switch (crossoverMethod.SelectedItem.ToString())
                    {
                    case "One Point Crossover":
                        newGeneration.curPopulation = CrossingOver.Crossover(newGeneration.curPopulation, 1);
                        break;

                    case "Two Point Crossover":
                        newGeneration.curPopulation = CrossingOver.Crossover(newGeneration.curPopulation, 2);
                        break;
                    }

                    // Mutation
                    newGeneration.curPopulation = Mutation.Mutate(newGeneration.curPopulation);

                    norm = Population.Normalize(newGeneration.curPopulation);

                    // Get worst
                    worstId = Population.GetWorst(norm);

                    // Copy the best element to new gen
                    Array.Copy(population.curPopulation[bestId].genes, newGeneration.curPopulation[worstId].genes, newGeneration.curPopulation[worstId].genes.Length);

                    newGeneration.curPopulation[worstId].calcFitness();

                    Chromosome.Fitness(newGeneration.curPopulation);

                    // Copy new generation
                    max = -1;
                    for (int p = 0; p < population.curPopulation.Count; p++)
                    {
                        Array.Copy(newGeneration.curPopulation[p].genes, population.curPopulation[p].genes, population.curPopulation[p].genes.Length);

                        population.curPopulation[p].calcFitness();

                        if (population.curPopulation[p].value > max)
                        {
                            max   = population.curPopulation[p].value;
                            index = p;
                        }
                    }

                    if (max > bestFx)
                    {
                        bestFx = max;
                    }
                }

                Console.WriteLine(bestFx);
                stopw.Stop();
                TimeSpan ts          = stopw.Elapsed;
                string   elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
                Console.WriteLine("RunTime " + elapsedTime);
                Console.WriteLine("---------------------");
            }
            Console.WriteLine("Process Completed...");
        }
        private void RunAll_Click(object sender, EventArgs e)
        {
            string[] txts = new string[] { "test1.txt", "test2.txt", "test3.txt", "test4.txt", "test5.txt", "test6.txt", "test7.txt", "test8.txt", "test9.txt", "test10.txt" };

            foreach (string k in txts)
            {
                ReadFile.path = "../../../../Datasets/" + k;
                Console.WriteLine(ReadFile.path);
                ReadFile.Read();
                List <int>    bests = new List <int>();
                List <double> times = new List <double>();
                Program.itr = 1000;
                Program.pM  = Double.Parse(mutationRate.Text, culture);
                Program.pC  = Double.Parse(crossoverRate.Text, culture);
                int tryCount = Convert.ToInt32(repeatTime.Text);
                Program.size = Program.weight.Count;
                Program.n    = Convert.ToInt32(populationSize.Text);

                for (int t = 0; t < tryCount; t++)
                {
                    double[]  fit       = new double[Program.n]; // (fx/overall) or fx values
                    double[]  norm      = new double[Program.n]; // fitness values get norm
                    int[]     choosenId = new int[Program.n];    // choose for new generation
                    int       bestId    = 0;
                    int       worstId   = 0;
                    int       bestFx    = 0;
                    int       max;
                    int       index;
                    Stopwatch stopw = new Stopwatch();
                    stopw.Start();

                    // Create Population
                    Population population = new Population();

                    bool       gotcha        = true;
                    Population newGeneration = new Population(gotcha);

                    for (int i = 0; i < Program.itr; i++)
                    {
                        // Normalize
                        norm = Population.Normalize(population.curPopulation);

                        switch (selectionMethod.SelectedItem.ToString())
                        {
                        case "Tournament Selection":
                            choosenId = Selection.TournamentSelection(norm);
                            break;

                        case "Roulet Selection":
                            choosenId = Selection.RouletChark(norm);
                            break;
                        }
                        // Keep best of population
                        bestId = Population.GetBest(norm);

                        newGeneration.curPopulation = Population.CopyToNew(choosenId, population.curPopulation, newGeneration.curPopulation);

                        switch (crossoverMethod.SelectedItem.ToString())
                        {
                        case "One Point Crossover":
                            newGeneration.curPopulation = CrossingOver.Crossover(newGeneration.curPopulation, 1);
                            break;

                        case "Two Point Crossover":
                            newGeneration.curPopulation = CrossingOver.Crossover(newGeneration.curPopulation, 2);
                            break;
                        }

                        // Mutation
                        newGeneration.curPopulation = Mutation.Mutate(newGeneration.curPopulation);

                        norm = Population.Normalize(newGeneration.curPopulation);

                        worstId = Population.GetWorst(norm);

                        // Copy the best of prev generation to new generation
                        Array.Copy(population.curPopulation[bestId].genes, newGeneration.curPopulation[worstId].genes, newGeneration.curPopulation[worstId].genes.Length);

                        newGeneration.curPopulation[worstId].calcFitness();

                        Chromosome.Fitness(newGeneration.curPopulation);

                        // Copy the new generation to old generation
                        max = -1;
                        for (int p = 0; p < population.curPopulation.Count; p++)
                        {
                            Array.Copy(newGeneration.curPopulation[p].genes, population.curPopulation[p].genes, population.curPopulation[p].genes.Length);

                            population.curPopulation[p].calcFitness();

                            if (population.curPopulation[p].value > max)
                            {
                                max   = population.curPopulation[p].value;
                                index = p;
                            }
                        }

                        if (max > bestFx)
                        {
                            bestFx = max;
                        }
                    }

                    stopw.Stop();
                    Console.WriteLine(k.Split('.')[0] + " Try:" + (t + 1) + " " + bestFx + " " + stopw.ElapsedMilliseconds + " ms");

                    bests.Add(bestFx);
                    times.Add(stopw.ElapsedMilliseconds);
                }
                //Optional write file
                //ReadFile.Write(bests,times,k);
            }
            Console.WriteLine("Process Completed...");
        }