コード例 #1
0
        protected Population crossoverHelper(Population parents, int random_gens1, int random_gens2, int[] permutation)
        {
            List<Individual> offsprings1 = new List<Individual>();
            List<Individual> offsprings2 = new List<Individual>();
            List<int> used = new List<int>();

            if (random_gens2 < random_gens1)
            {
                int temp = random_gens1;
                random_gens1 = random_gens2;
                random_gens2 = temp;
            }

            for (int i = 0; i < parents.Count / 2; i++)
            {
                Tuple<Individual, Individual> children = crossover(parents.getIndividual(permutation[i]), parents.getIndividual(permutation[parents.Count - 1 - i]), random_gens1, random_gens2);
                offsprings1.Add(children.Item1);
                offsprings2.Add(children.Item2);
            }
            offsprings2.Reverse(0, offsprings2.Count);
            offsprings1.AddRange(offsprings2);
            Population offspring = new Population(offsprings1);
            return offspring;
        }
コード例 #2
0
 public void AddRange(Population population)
 {
     this.population.AddRange(population.population);
 }
コード例 #3
0
        public Population UniformCrossover(Population parents)
        {
            Population offspring = new Population();
            List<int> unused = new List<int>();

            for (int i = 0; i < parents.Count; i++)
            {
                unused.Add(i);
            }

            int chromosomeSize = getChromosomeSize();

            for (int i = 0; i < parents.Count; i++)
            {
                if (!unused.Contains(i))
                    continue;

                unused.Remove(i);

                int randomIndex = unused.ElementAt(GA_GT.random.Next() % unused.Count);
                unused.Remove(randomIndex);

                if (GA_GT.random.NextDouble() >= GA_GT.crossoverRate)
                {
                    offspring.Add(parents.getIndividual(i));
                    offspring.Add(parents.getIndividual(randomIndex));
                }

                else
                {
                    Chromosome ch1 = new Chromosome(chromosomeSize);
                    Chromosome ch2 = new Chromosome(chromosomeSize);

                    for (int j = 0; j < chromosomeSize; j++)
                    {
                        if (GA_GT.random.NextDouble() < 0.5)
                        {
                            ch1[j] = parents.getIndividual(i)[j];
                            ch2[j] = parents.getIndividual(randomIndex)[j];
                        }
                        else
                        {
                            ch1[j] = parents.getIndividual(randomIndex)[j];
                            ch2[j] = parents.getIndividual(i)[j];
                        }
                    }

                    Individual ind1 = new Individual(ch1);
                    ind1.strategy = parents.getIndividual(i).strategy;
                    ind1.Update(GA_GT.knapsackList);

                    Individual ind2 = new Individual(ch2);
                    ind2.strategy = parents.getIndividual(randomIndex).strategy;
                    ind2.Update(GA_GT.knapsackList);

                    offspring.Add(ind1);
                    offspring.Add(ind2);
                }
            }

            return offspring;
        }
コード例 #4
0
        // TODO: Maybe random_gens should be randomize for each pair separately?
        public Population TwoPointsCrossover(Population parents)
        {
            int[] permutation = generateRandomPairs(parents.Count);

            int random_gens1 = GA_GT.random.Next() % parents.getChromosomeSize();
            int random_gens2 = GA_GT.random.Next() % parents.getChromosomeSize();

            while (random_gens1 == random_gens2)
            {
                random_gens2 = GA_GT.random.Next() % parents.getChromosomeSize();
            }
            return crossoverHelper(parents, random_gens1, random_gens2, permutation);
        }
コード例 #5
0
 public Population(Population p)
 {
     this.population = new List<Individual>(p.population);
 }
コード例 #6
0
        public Population TournamentSelection()
        {
            Population parents = new Population();
            int nonFeasibleAllowed = (int)(Count * (1.0 - GA_GT.feasibleRate));
            int nonFeasibleInParents = 0;

            while (parents.Count != Count) {

                int x = GA_GT.random.Next() % Count;
                int y = GA_GT.random.Next() % Count;

                while (y == x)
                {
                    y = GA_GT.random.Next() % Count;
                }

                Individual chosen = null;
                if (getIndividual(x).fitness > getIndividual(y).fitness)
                {
                    chosen = getIndividual(x);
                }
                else
                {
                    chosen = getIndividual(y);
                }

                if (!chosen.isFeasible)
                {
                    if (nonFeasibleInParents == nonFeasibleAllowed)
                    {
                        chosen = GetBestFeasible();
                    }
                    else
                    {
                        nonFeasibleInParents++;
                    }
                }

                parents.Add(chosen);
            }
            return parents;
        }
コード例 #7
0
        public Individual RunGA_GT()
        {
            population = new Population();
            population.RandomPopulation(cheaterRate, chromosomeLength, populationSize);

            maxFitness = knapsackList.MaxFitness();     //maxFitness only calculated once (sum of the values of the objects)
            maxPayoff = gameModel.maxPayoff;

            population.Evaluation();

            for (int epoch = 0; epoch < numberOfEpochs; epoch++)
            {

                Population parents = population.TournamentSelection();

                Population offspring = population.UniformCrossover(parents);//population.TwoPointsCrossover(parents);

                offspring.Mutation();

                parents.Sort();
                offspring.Sort();

                // Another way of choosing the new generation:
                //population.Clear();
                //population.AddRange(parents);
                //population.AddRange(offspring);
                //population.Sort();
                //population.RemoveRange(populationSize, population.Count - populationSize);

                //// TODO: number of cheaters should be less than a cheater rate? or some other value?
                //population.Clear();
                //for (int i = 0; i < populationSize; i++)
                //{
                //    //population.Add(parents.getIndividual(i));
                //    population.Add(offspring.getIndividual(i));
                //}

                population.Clear();
                int nonFeasibleAllowed = (int)(populationSize * (1.0 - feasibleRate));
                int nonFeasibleInPopulation = 0;

                for (int i = 0; i < offspring.Count; i++)
                {
                    if (!offspring.getIndividual(i).isFeasible)
                    {
                        if (nonFeasibleInPopulation == nonFeasibleAllowed)
                            continue;

                        else
                        {
                            population.Add(offspring.getIndividual(i));
                            nonFeasibleInPopulation++;
                        }
                    }

                    else
                    {
                        population.Add(offspring.getIndividual(i));
                    }
                }

                if (population.Count < populationSize)
                {
                    for (int i = 0; i < parents.Count; i++)
                    {
                        if (population.Count == populationSize)
                            break;

                        if (parents.getIndividual(i).isFeasible)
                            population.Add(parents.getIndividual(i));
                    }
                }

                population.GetBestFeasible().ShowValue();

                population.Evaluation();

            }

            population.Sort();
            population.Show();
            Console.ReadLine();
            return population.GetBestFeasible();
        }