/** * @return a byte array which represents this {@code BitChromosome}. The * length of the array is {@code (int)Math.ceil(length()/8.0)}. * * @see #toByteArray(byte[]) */ public byte[] toByteArray() { final byte[] data = new byte[_genes.length]; toByteArray(data); return data; }
/** * 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); }
/** * 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. * @param p Probability of the TRUEs in the BitChromosome. * @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'. * @throws IllegalArgumentException if {@code p} is not a valid probability. */ public static BitChromosome of(final CharSequence value, final double p) { final byte[] bits = toByteArray(requireNonNull(value, "Input")); return new BitChromosome(bits, bits.length*8, require.probability(p)); }
/** * 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. * @param length length of the BitChromosome * @param p Probability of the TRUEs in the BitChromosome. * @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'. * @throws IllegalArgumentException if {@code p} is not a valid probability. */ public static BitChromosome of( final CharSequence value, final int length, final double p ) { final byte[] bits = toByteArray(requireNonNull(value, "Input")); return new BitChromosome(bits, length, require.probability(p)); }
@Test public void toByteArray() { byte[] data = new byte[16]; for (int i = 0; i < data.length; ++i) { data[i] = (byte)(Math.random()*256); } BitChromosome bc = new BitChromosome(data); Assert.assertEquals(bc.toByteArray(), data); }
@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 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); }