Ejemplo n.º 1
0
 public Individual(Individual i)
 {
     this.chromosome_ = new Chromosome(i.chromosome);
     this.strategy_ = i.strategy;
     this.isFeasible_ = i.isFeasible;
     this.fitness_ = i.fitness;
 }
Ejemplo n.º 2
0
 public void Add(Individual individual)
 {
     Individual i = new Individual(individual);
     population.Add(i);
 }
Ejemplo n.º 3
0
 int Compare(Individual x, Individual y)
 {
     if (x.fitness > y.fitness)
         return -1;
     else if (x.fitness == y.fitness)
         return 0;
     else
         return 1;
 }
Ejemplo n.º 4
0
 protected Tuple<Individual, Individual> crossover(Individual ind1, Individual ind2, int index1, int index2)
 {
     // we forgot about crossover probability
     if (GA_GT.random.NextDouble() < GA_GT.crossoverRate)
     {
         for (int i = index1; i <= index2; i++)
         {
             bool temp = ind1.chromosome[i];
             ind1.chromosome[i] = ind2.chromosome[i];
             ind2.chromosome[i] = temp;
         }
     }
     return new Tuple<Individual, Individual>(ind1, ind2);
 }
Ejemplo n.º 5
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;
        }
Ejemplo n.º 6
0
        public void RandomPopulation(double cheaterRate, int chromosomeSize, int populationSize)
        {
            int numberOfCheaters = (int)(populationSize * cheaterRate);

            for (int i = 0; i < populationSize; i++)
            {
                Chromosome chromosome = new Chromosome(chromosomeSize);

                Individual temp;
                if (i < numberOfCheaters)
                    temp = new Individual(chromosome, false, (double)0, true);   // cheater
                else
                    temp = new Individual(chromosome, true, (double)0, true);    // cooperator

                population.Add(temp);
            }
        }
        public static void WriteOutput(string path, Individual ind)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);  //dots instead of commas as decimal marks

            //StreamWriter(path) to overwrite the file, StreamWriter(path, true) to append
            using (StreamWriter sw = new StreamWriter(path, true))
            {
                sw.Write(ind.Value + " " + ind.fitness + " " + ind.strategy + " ");

                for (int i = 0; i < ind.chromosome.Count; i++)
                {
                    if (ind[i])
                        sw.Write("1 ");
                    else
                        sw.Write("0 ");
                }

                sw.WriteLine();
            }
        }
Ejemplo n.º 8
0
        public double FitnessValue(Individual secondInd, KnapsackList knapsackList)
        {
            bool strategy1 = this.strategy;
            bool strategy2 = secondInd.strategy;

            if (strategy1)
            {
                if (strategy2)
                {
                    return fitnessCooperativeCooperative(knapsackList);
                }
                else
                {
                    return fitnessCooperativeDefector(knapsackList);
                }
            }
            else
            {
                if (strategy2)
                {
                    return fitnessDefectorCooperative(knapsackList);
                }
                else
                {
                    return fitnessDefectorDefector(knapsackList);
                }
            }
        }