/** * 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); }
/** * Constructing a new BitChromosome with the given _length. The TRUEs and * FALSE in the {@code Chromosome} are equally distributed. * * @param length Length of the BitChromosome. * @return a new {@code BitChromosome} with the given parameter * @throws NegativeArraySizeException if the {@code _length} is smaller * than one. */ public static BitChromosome of(final int length) { return new BitChromosome(bit.newArray(length, 0.5), length, 0.5); }
/** * 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); }
/** * Constructing a new BitChromosome from a given BitSet. * The BitSet is copied while construction. The length of the constructed * BitChromosome will be {@code bitSet.length()} ({@link BitSet#length}). * * @param bits the bit-set which initializes the chromosome * @return a new {@code BitChromosome} with the given parameter * @throws NullPointerException if the {@code bitSet} is * {@code null}. */ public static BitChromosome of(final BitSet bits) { return new BitChromosome(bits.toByteArray(), -1); }
/** * Construct a new BitChromosome with the given _length. * * @param length Length of the BitChromosome, number of bits. * @param p Probability of the TRUEs in the BitChromosome. * @return a new {@code BitChromosome} with the given parameter * @throws NegativeArraySizeException if the {@code length} is smaller * than one. * @throws IllegalArgumentException if {@code p} is not a valid probability. */ public static BitChromosome of(final int length, final double p) { return new BitChromosome(bit.newArray(length, p), length, p); }
/** * Create a new {@code BitChromosome} from the given big integer value and * ones probability. * * @param value the value of the created {@code BitChromosome} * @param p Probability of the TRUEs in the BitChromosome. * @return a new {@code BitChromosome} with the given parameter * @throws NullPointerException if the given {@code value} is {@code null}. * @throws IllegalArgumentException if {@code p} is not a valid probability. */ public static BitChromosome of(final BigInteger value, final double p) { final byte[] bits = value.toByteArray(); return new BitChromosome(bits, bits.length*8, require.probability(p)); }
/** * Invert the ones and zeros of this bit chromosome. * * @return a new BitChromosome with inverted ones and zeros. */ public BitChromosome invert() { final byte[] data = _genes.clone(); bit.invert(data); return new BitChromosome(data, _length, 1.0 - _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 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); }
/** * Create a new {@code BitChromosome} with the given parameters. * * @param length length of the BitChromosome. * @param bits the bit-set which initializes the chromosome * @return a new {@code BitChromosome} with the given parameter * @throws NegativeArraySizeException if the {@code length} is smaller * than one. * @throws NullPointerException if the {@code bitSet} is * {@code null}. */ public static BitChromosome of(final BitSet bits, final int length) { final byte[] bytes = bit.newArray(length); for (int i = 0; i < length; ++i) { if (bits.get(i)) { bit.set(bytes, i); } } final double p = (double)bit.count(bytes)/(double)length; return new BitChromosome(bytes, length, p); }
/** * Create a new {@code BitChromosome} with the given parameters. * * @param length length of the BitChromosome. * @param bits the bit-set which initializes the chromosome * @param p Probability of the TRUEs in the BitChromosome. * @return a new {@code BitChromosome} with the given parameter * @throws NegativeArraySizeException if the {@code length} is smaller than * one. * @throws NullPointerException if the {@code bitSet} is {@code null}. * @throws IllegalArgumentException if {@code p} is not a valid probability. */ public static BitChromosome of( final BitSet bits, final int length, final double p ) { final byte[] bytes = bit.newArray(length); for (int i = 0; i < length; ++i) { if (bits.get(i)) { bit.set(bytes, i); } } return new BitChromosome(bytes, length, require.probability(p)); }
@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) ); }
@Override public BitChromosome newInstance(final ISeq<BitGene> genes) { requireNonNull(genes, "Genes"); if (genes.isEmpty()) { throw new IllegalArgumentException( "The genes sequence must contain at least one gene." ); } final BitChromosome chromosome = new BitChromosome( bit.newArray(genes.length()), genes.length() ); int ones = 0; if (genes instanceof BitGeneISeq) { final BitGeneISeq iseq = (BitGeneISeq)genes; iseq.copyTo(chromosome._genes); ones = bit.count(chromosome._genes); } else { for (int i = genes.length(); --i >= 0;) { if (genes.get(i).booleanValue()) { bit.set(chromosome._genes, i); ++ones; } } } chromosome._p = (double)ones/(double)genes.length(); return chromosome; }