Ejemplo n.º 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);
      }
Ejemplo n.º 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);
              }
          }
      }