@Override public CharacterGene newInstance() { return of(_validCharacters); }
@Override public char charAt(final int index) { return getGene(index).charValue(); }
/** * Create a new character gene from the given character. If the character * is not within the {@link #DEFAULT_CHARACTERS}, an invalid gene will be * created. * * @param character the character value of the created gene. * @return a new character gene. */ public static CharacterGene of(final char character) { return new CharacterGene(character, DEFAULT_CHARACTERS); }
@Test public void testCharacterGene() { CharacterGene gene = CharacterGene.of(); assertTrue(gene.isValidCharacter(gene.getAllele())); }
@Test public void testCompareTo() { CharacterGene g1 = CharacterGene.of('1'); CharacterGene g2 = CharacterGene.of('2'); CharacterGene g3 = CharacterGene.of('3'); assertTrue(g1.compareTo(g2) < 0); assertTrue(g2.compareTo(g3) < 0); assertTrue(g3.compareTo(g2) > 0); assertEquals(0, g2.compareTo(g2)); }
@Test public void testIsValidCharacter() { for (Character c : CharacterGene.DEFAULT_CHARACTERS) { assertTrue(CharacterGene.of(c.charValue()).isValidCharacter(c)); } }
private void writeObject(final ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeInt(length()); out.writeObject(_validCharacters); for (CharacterGene gene : _genes) { out.writeChar(gene.getAllele()); } }
/** * Create a new chromosome from the given {@code genes} array. The genes * array is copied, so changes to the given genes array doesn't effect the * genes of this chromosome. * * @since 4.0 * * @param genes the genes that form the chromosome. * @param lengthRange the allowed length range of the chromosome. * @throws NullPointerException if the given gene array is {@code null}. * @throws IllegalArgumentException if the length of the gene array is * smaller than one. */ protected CharacterChromosome( final ISeq<CharacterGene> genes, final IntRange lengthRange ) { super(genes, lengthRange); _validCharacters = genes.get(0).getValidCharacters(); }
/** * Create a new chromosome with the {@code validCharacters} char set as * valid characters. * * @since 4.0 * * @param validCharacters the valid characters for this chromosome. * @param lengthRange the allowed length range of the chromosome. * @throws NullPointerException if the {@code validCharacters} is * {@code null}. * @throws IllegalArgumentException if the length of the gene sequence is * empty, doesn't match with the allowed length range, the minimum * or maximum of the range is smaller or equal zero or the given * range size is zero. * * @deprecated Use {@link #of(CharSeq, IntRange)} instead. */ @Deprecated public CharacterChromosome( final CharSeq validCharacters, final IntRange lengthRange ) { this(CharacterGene.seq(validCharacters, lengthRange), lengthRange); _valid = true; }
@Test(invocationCount = 20, successPercentage = 95) public void newInstanceDistribution() { using(new Random(12345), r -> { final CharSeq characters = new CharSeq("0123456789"); final CharacterChromosome chromosome = CharacterChromosome.of(characters, 5000); final Histogram<Long> histogram = Histogram.ofLong(0L, 10L, 10); chromosome.stream() .map(g -> Long.valueOf(g.getAllele().toString())) .forEach(histogram); final double[] expected = dist.uniform(histogram.length()); assertDistribution(histogram, expected); }); }
/** * Return a {@link Writer} for {@link io.jenetics.CharacterChromosome} * objects. * * @return a chromosome writer */ public static Writer<io.jenetics.CharacterChromosome> writer() { return elem(ROOT_NAME, attr(LENGTH_NAME).map(io.jenetics.CharacterChromosome::length), elem(VALID_ALLELES_NAME, text().map(ch -> ch.getGene().getValidCharacters())), elem(ALLELES_NAME, text().map(io.jenetics.CharacterChromosome::toString)) ); }
/** * Create a new chromosome with the {@code validCharacters} char set as * valid characters. * * @since 4.3 * * @param validCharacters the valid characters for this chromosome. * @param lengthRange the allowed length range of the chromosome. * @return a new {@code CharacterChromosome} with the given parameter * @throws NullPointerException if the {@code validCharacters} is * {@code null}. * @throws IllegalArgumentException if the length of the gene sequence is * empty, doesn't match with the allowed length range, the minimum * or maximum of the range is smaller or equal zero or the given * range size is zero. */ public static CharacterChromosome of( final CharSeq validCharacters, final IntRange lengthRange ) { return new CharacterChromosome( CharacterGene.seq(validCharacters, lengthRange), lengthRange ); }
@Test(invocationCount = 20, successPercentage = 95) public void newInstanceDistribution() { using(new Random(12345), r -> { final CharSeq characters = new CharSeq("0123456789"); final Factory<CharacterGene> factory = CharacterGene.of(characters); final Histogram<Long> histogram = Histogram.ofLong(0L, 10L, 10); final int samples = 100000; for (int i = 0; i < samples; ++i) { final CharacterGene g1 = factory.newInstance(); final CharacterGene g2 = factory.newInstance(); Assert.assertNotSame(g1, g2); histogram.accept(Long.valueOf(g1.getAllele().toString())); histogram.accept(Long.valueOf(g2.getAllele().toString())); } assertUniformDistribution(histogram); }); }
/** * Create a new character gene from the given character. If the character * is not within the {@link #getValidCharacters()}, an invalid gene will be * created. * * @param character the character value of the created gene. * @return a new character gene. * @throws NullPointerException if the given {@code character} is * {@code null}. */ @Override public CharacterGene newInstance(final Character character) { return of(character, _validCharacters); }
@Test public void crossover() { final ISeq<CharacterGene> g1 = CharacterChromosome.of("1234567890").toSeq(); final ISeq<CharacterGene> g2 = CharacterChromosome.of("abcdefghij").toSeq(); final int rv1 = 12; using(new Random(10), r -> { final UniformCrossover<CharacterGene, Double> crossover = new UniformCrossover<>(0.5, 0.5); MSeq<CharacterGene> g1c = g1.copy(); MSeq<CharacterGene> g2c = g2.copy(); final int changed = crossover.crossover(g1c, g2c); Assert.assertEquals(changed, IntStream.range(0, g2c.length()) .filter(i -> Character.isDigit(g2c.get(i).charValue())) .count() ); }); }
/** * Create a new CharacterGene from the give character. * * @param character The allele. * @param validCharacters the valid characters fo the new gene * @return a new {@code CharacterGene} with the given parameter * @throws NullPointerException if one of the arguments is {@code null}. * @throws IllegalArgumentException if the {@code validCharacters} are empty. */ public static CharacterGene of( final char character, final CharSeq validCharacters ) { return new CharacterGene(character, validCharacters); }
public static CharacterGene nextCharacterGene() { return CharacterGene.of(); }
@Test public void crossoverChanges() { final ISeq<CharacterGene> g1 = CharacterChromosome.of("1234567890").toSeq(); final ISeq<CharacterGene> g2 = CharacterChromosome.of("abcdefghij").toSeq(); final int rv1 = 12; using(new Random(10), r -> { final UniformCrossover<CharacterGene, Double> crossover = new UniformCrossover<>(0.5, 0.5); final DoubleMomentStatistics statistics = new DoubleMomentStatistics(); for (int j = 0; j < 1000; ++j) { MSeq<CharacterGene> g1c = g1.copy(); MSeq<CharacterGene> g2c = g2.copy(); final int changed = crossover.crossover(g1c, g2c); Assert.assertEquals(changed, IntStream.range(0, g2c.length()) .filter(i -> Character.isDigit(g2c.get(i).charValue())) .count() ); statistics.accept(changed); } Assert.assertEquals(statistics.getMean(), 5.0, 0.0001); }); }