@Override public BitChromosome newInstance() { return of(_length, _p); }
private static Integer count(final Genotype<BitGene> gt) { return ((BitChromosome)gt.getChromosome()).bitCount(); }
/** * Create a new {@code BitChromosome} from the given character sequence * containing '0' and '1'; as created with the {@link #toCanonicalString()} * method. * * @param value the input string. * @return a new {@code BitChromosome} with the given parameter * @throws NullPointerException if the {@code value} is {@code null}. * @throws IllegalArgumentException if the length of the character sequence * is zero or contains other characters than '0' or '1'. */ public static BitChromosome of(final CharSequence value) { return new BitChromosome(toByteArray(requireNonNull(value, "Input")), -1); }
@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 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(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 toCanonicalString() { BitChromosome c = BitChromosome.of(BigInteger.valueOf(234902)); String value = c.toCanonicalString(); BitChromosome sc = BitChromosome.of(value); Assert.assertEquals(sc, c); }
/** * Create a new {@code BitChromosome} from the given big integer value. * * @param value the value of the created {@code BitChromosome} * @return a new {@code BitChromosome} with the given parameter * @throws NullPointerException if the given {@code value} is {@code null}. */ public static BitChromosome of(final BigInteger value) { return new BitChromosome(value.toByteArray(), -1); }
@Test public void chromosomeProbability() { final byte[] data = new byte[1234]; RandomRegistry.getRandom().nextBytes(data); final BitChromosome c = new BitChromosome(data); Assert.assertEquals( c.getOneProbability(), (double)bit.count(data)/(double)(data.length*8) ); }
/** * Return the float value this BitChromosome represents. * * @return float value this BitChromosome represents. */ @Override public float floatValue() { return (float)longValue(); }
@Override public int compareTo(final BitChromosome that) { return toBigInteger().compareTo(that.toBigInteger()); }