Пример #1
0
      private static Individual crossover(Individual ind1, Individual ind2)
      {
          Individual offspring = new Individual();
          int        j         = 0;

          for (int i = 0; i < ind1.size(); i++)
          {
              if (rng.NextDouble() <= uniformRate)
              {
                  offspring.setGene(i, ind1.getGene(i));
              }
          }

          for (j = 0; j < ind2.size(); j++)
          {
              if (!offspring.containsGene(ind2.getGene(j)))
              {
                  for (int jj = 0; jj < offspring.size(); jj++)
                  {
                      if (offspring.getGene(jj) == null)
                      {
                          offspring.setGene(jj, ind2.getGene(j));
                          break;
                      }
                  }
              }
          }

          return(offspring);
      }
Пример #2
0
      private static void mutate(Individual ind)
      {
          for (int i = 0; i < ind.size(); i++)
          {
              if (rng.NextDouble() <= mutationRate)
              {
                  int j = (int)(ind.size() * rng.NextDouble());

                  Cube c1 = ind.getGene(i);
                  Cube c2 = ind.getGene(2);

                  ind.setGene(i, c2);
                  ind.setGene(j, c1);
              }
          }
      }
Пример #3
0
        public static int getFitness(Individual ind)
        {
            int fitness = 0;

            for (int i = 0; i < ind.size() - 1; i++)
            {
                for (int j = i + 1; j < ind.size(); j++)
                {
                    if (ind.getGene(i).size >= ind.getGene(j).size)
                    {
                        fitness += 2;
                    }
                }
                if (ind.getGene(i).color != ind.getGene(i + 1).color)
                {
                    fitness += 1;
                }
            }

            return(fitness);
        }