Exemple #1
0
 public void saveIndividual(int index, Individual indiv)
 {
     if(index < 0)
     {
         individuals[individuals.Length - 1] = indiv;
     } else
     {
         individuals[index] = indiv;
     }
 }
Exemple #2
0
 // Calculate inidividuals fittness by comparing it to our candidate solution
 public static int getFitness(Individual individual)
 {
     int fitness = 0;
     // Loop through our individuals genes and compare them to our cadidates
     for (int i = 0; i < individual.size() && i < solution.Length; i++) {
         if (individual.getGene(i) == solution[i]) {
             fitness++;
         }
     }
     return fitness;
 }
Exemple #3
0
 public Population(int populationSize, bool initialise)
 {
     individuals = new Individual[populationSize];
     if (initialise)
     {
         for (int i = 0; i<size();i++)
         {
             Individual newIndividual = new Individual();
             newIndividual.generateIndividual();
             saveIndividual(i, newIndividual);
         }
     }
 }
Exemple #4
0
 // Mutate an individual
 private static void mutate(Individual indiv)
 {
     // Loop through genes
     for (int i = 0; i < indiv.size(); i++) {
         if (Rnd.NextDouble() <= MutationRate) {
             // Create random gene
             byte gene = (byte)Math.Round(Rnd.NextDouble());
             indiv.setGene(i, gene);
         }
     }
 }
Exemple #5
0
 // Crossover individuals
 private static Individual crossover(Individual indiv1, Individual indiv2)
 {
     Individual newSol = new Individual();
     // Loop through genes
     for (int i = 0; i < indiv1.size(); i++) {
         // Crossover
         if (Rnd.NextDouble() <= UniformRate) {
             newSol.setGene(i, indiv1.getGene(i));
         } else {
             newSol.setGene(i, indiv2.getGene(i));
         }
     }
     return newSol;
 }