@Override public BitChromosome newInstance() { return of(_length, _p); }
@Override protected Factory<Chromosome<BitGene>> factory() { return () -> BitChromosome.of(500, 0.3); }
public static BitChromosome nextBitChromosome() { return BitChromosome.of(20, 0.5); }
public static BitChromosome nextBitChromosome() { return BitChromosome.of(20, 0.5); }
@Test(invocationCount = 5) public void toBigInteger() { final Random random = new Random(); final BigInteger value = new BigInteger(1056, random); final BitChromosome chromosome = BitChromosome.of(value); assertEquals(chromosome.toBigInteger(), value); }
@Test public void fromBitSet() { final Random random = new Random(234); final BitSet bits = new BitSet(2343); for (int i = 0; i < bits.size(); ++i) { bits.set(i, random.nextBoolean()); } final BitChromosome c = BitChromosome.of(bits); Assert.assertEquals(c.toByteArray(), bits.toByteArray()); }
@Test public void bitChromosomeBitSet() { BitSet bits = new BitSet(10); for (int i = 0; i < 10; ++i) { bits.set(i, i % 2 == 0); } BitChromosome c = BitChromosome.of(bits); for (int i = 0; i < bits.length(); ++i) { assertEquals(c.getGene(i).getBit(), i % 2 == 0); } }
@Test public void fromByteArrayBitSet() { final Random random = new Random(123); final byte[] bytes = new byte[234]; random.nextBytes(bytes); final BitSet bits = BitSet.valueOf(bytes); final BitChromosome c = BitChromosome.of(bits); Assert.assertEquals(c.toByteArray(), bytes); Assert.assertEquals(bits.toByteArray(), bytes); }
@Override public Codec<ISeq<BitGene>, BitGene> codec() { return Codec.of( Genotype.of(BitChromosome.of(_length, _onesProbability)), gt -> gt.getChromosome().toSeq() ); }
@Test public void seqTypes() { final BitChromosome c = BitChromosome.of(100, 0.3); Assert.assertEquals(c.toSeq().getClass(), BitGeneISeq.class); Assert.assertEquals(c.toSeq().copy().getClass(), BitGeneMSeq.class); Assert.assertEquals(c.toSeq().copy().toISeq().getClass(), BitGeneISeq.class); }
@Test public void numValue() { BitChromosome c1 = BitChromosome.of(10); int value = c1.intValue(); assertEquals((short)value, c1.shortValue()); assertEquals(value, c1.longValue()); assertEquals((float)value, c1.floatValue()); assertEquals((double)value, c1.doubleValue()); }
@Test public void ones() { final BitChromosome c = BitChromosome.of(1000, 0.5); final int ones = (int)c.ones().count(); assertEquals(ones, c.bitCount()); assertTrue(c.ones().allMatch(c::booleanValue)); }
@Test public void zeros() { final BitChromosome c = BitChromosome.of(1000, 0.5); final int zeros = (int)c.zeros().count(); assertEquals(zeros, c.length() - c.bitCount()); assertTrue(c.zeros().noneMatch(c::booleanValue)); }
@Test public void invert() { final BitChromosome c1 = BitChromosome.of(100, 0.3); final BitChromosome c3 = c1.invert(); for (int i = 0; i < c1.length(); ++i) { Assert.assertTrue(c1.getGene(i).getBit() != c3.getGene(i).getBit()); } BitChromosome c4 = c3.invert(); Assert.assertEquals(c4, c1); }
@Test(invocationCount = 5) public void onesCountLimit() { final Problem<ISeq<BitGene>, BitGene, Integer> problem = Problem.of( genes -> (int)genes.stream().filter(BitGene::getBit).count(), Codec.of( Genotype.of(BitChromosome.of(20, 0.125)), gt -> gt.getChromosome().toSeq() ) ); final Engine<BitGene, Integer> engine = Engine.builder(problem) .build(); final EvolutionResult<BitGene, Integer> result = engine.stream() .limit(Limits.byPopulationConvergence(0.015)) .collect(toBestEvolutionResult()); Assert.assertTrue( result.getTotalGenerations() < 2901, "Gen: " + result.getTotalGenerations() ); }
public static void main(final String[] args) throws Exception { final Problem<BitChromosome, BitGene, Integer> count = Problem.of( BitChromosome::bitCount, Codec.of( Genotype.of(BitChromosome.of(10)), gt -> gt.getChromosome().as(BitChromosome.class) ) ); final Engine<BitGene, Integer> engine = Engine.builder(count).build(); final EvolutionResult<BitGene, Integer> result = engine.stream() .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); final ISeq<Genotype<BitGene>> genotypes = result.getGenotypes(); Writers.write(System.out, genotypes.asList(), Writers.BitChromosome.writer()); Readers.read(System.in, Readers.BitChromosome.reader()); engine.stream(genotypes); }