Exemple #1
0
  /** Create a population of DoubleGenes */
  public static Population<DoubleGene, Double> newDoubleGenePopulation(
      final int ngenes, final int nchromosomes, final int npopulation) {
    final MSeq<DoubleChromosome> chromosomes = MSeq.ofLength(nchromosomes);

    for (int i = 0; i < nchromosomes; ++i) {
      chromosomes.set(i, DoubleChromosome.of(0, 10, ngenes));
    }

    final Genotype<DoubleGene> genotype = new Genotype<>(chromosomes.toISeq());
    final Population<DoubleGene, Double> population = new Population<>(npopulation);

    for (int i = 0; i < npopulation; ++i) {
      population.add(Phenotype.of(genotype.newInstance(), 0, FF).evaluate());
    }

    return population;
  }
  static ISeq<DoubleGene> seq(final Double minimum, final Double maximum, final int length) {
    final double min = minimum;
    final double max = maximum;
    final Random r = RandomRegistry.getRandom();

    return MSeq.<DoubleGene>ofLength(length)
        .fill(() -> new DoubleGene(nextDouble(r, min, max), minimum, maximum))
        .toISeq();
  }
Exemple #3
0
  public static Population<EnumGene<Double>, Double> newPermutationDoubleGenePopulation(
      final int ngenes, final int nchromosomes, final int npopulation) {
    final Random random = new Random(122343);
    final MSeq<Double> alleles = MSeq.ofLength(ngenes);
    for (int i = 0; i < ngenes; ++i) {
      alleles.set(i, random.nextDouble() * 10);
    }
    final ISeq<Double> ialleles = alleles.toISeq();

    final MSeq<PermutationChromosome<Double>> chromosomes = MSeq.ofLength(nchromosomes);

    for (int i = 0; i < nchromosomes; ++i) {
      chromosomes.set(i, PermutationChromosome.of(ialleles));
    }

    final Genotype<EnumGene<Double>> genotype = new Genotype<>(chromosomes.toISeq());
    final Population<EnumGene<Double>, Double> population = new Population<>(npopulation);

    for (int i = 0; i < npopulation; ++i) {
      population.add(Phenotype.of(genotype.newInstance(), 0, PFF));
    }

    return population;
  }
  @Test
  public void crossover() {
    final CharSeq chars = CharSeq.of("a-zA-Z");

    final ISeq<CharacterGene> g1 = new CharacterChromosome(chars, 20).toSeq();
    final ISeq<CharacterGene> g2 = new CharacterChromosome(chars, 20).toSeq();

    final int rv1 = 12;
    using(
        new ConstRandom(rv1),
        r -> {
          final SinglePointCrossover<CharacterGene, Double> crossover =
              new SinglePointCrossover<>();

          MSeq<CharacterGene> g1c = g1.copy();
          MSeq<CharacterGene> g2c = g2.copy();
          crossover.crossover(g1c, g2c);

          Assert.assertEquals(g1c.subSeq(0, rv1), g1.subSeq(0, rv1));
          Assert.assertEquals(g1c.subSeq(rv1), g2.subSeq(rv1));
          Assert.assertNotEquals(g1c, g2);
          Assert.assertNotEquals(g2c, g1);

          final int rv2 = 0;
          using(
              new ConstRandom(rv2),
              r2 -> {
                MSeq<CharacterGene> g1c2 = g1.copy();
                MSeq<CharacterGene> g2c2 = g2.copy();
                crossover.crossover(g1c2, g2c2);
                Assert.assertEquals(g1c2, g2);
                Assert.assertEquals(g2c2, g1);
                Assert.assertEquals(g1c2.subSeq(0, rv2), g1.subSeq(0, rv2));
                Assert.assertEquals(g1c2.subSeq(rv2), g2.subSeq(rv2));

                final int rv3 = 1;
                using(
                    new ConstRandom(rv3),
                    r3 -> {
                      MSeq<CharacterGene> g1c3 = g1.copy();
                      MSeq<CharacterGene> g2c3 = g2.copy();
                      crossover.crossover(g1c3, g2c3);
                      Assert.assertEquals(g1c3.subSeq(0, rv3), g1.subSeq(0, rv3));
                      Assert.assertEquals(g1c3.subSeq(rv3), g2.subSeq(rv3));

                      final int rv4 = g1.length();
                      using(
                          new ConstRandom(rv4),
                          r4 -> {
                            MSeq<CharacterGene> g1c4 = g1.copy();
                            MSeq<CharacterGene> g2c4 = g2.copy();
                            crossover.crossover(g1c4, g2c);
                            Assert.assertEquals(g1c4, g1);
                            Assert.assertEquals(g2c4, g2);
                            Assert.assertEquals(g1c4.subSeq(0, rv4), g1.subSeq(0, rv4));
                            Assert.assertEquals(g1c4.subSeq(rv4), g2.subSeq(rv4));
                          });
                    });
              });
        });
  }