java.lang.Object
java.lang.Number
io.jenetics.BitChromosome
- All Implemented Interfaces:
Chromosome<BitGene>,BaseSeq<BitGene>,Factory<Chromosome<BitGene>>,Verifiable,Serializable,Comparable<BitChromosome>,Iterable<BitGene>,RandomAccess
public final class BitChromosome
extends Number
implements Chromosome<BitGene>, Comparable<BitChromosome>, Serializable
Implementation of the classical BitChromosome.
- Since:
- 1.0
- Version:
- 7.0
- See Also:
- Implementation Note:
- This class is immutable and thread-safe. The bits of the bit chromosome are
backed by a
byte[]array with the following layout:Byte: 3 2 1 0 | | | | Array: |11110011|10011101|01000000|00101010| | | | | Bit: 23 15 7 0
-
Constructor Summary
ConstructorsConstructorDescriptionBitChromosome(byte[] bits) Create a newBitChromosomefrom the givenbytearray.BitChromosome(byte[] bits, int start, int end) Create a new bit chromosome from the given bit (byte) array.BitChromosome(byte[] bits, int start, int end, double p) Create a new bit chromosome from the given bit (byte) array. -
Method Summary
Modifier and TypeMethodDescriptionand(BitChromosome other) Returns aBitChromosomewhose value is (this & other).intbitCount()Returns the number of bits set to true in thisBitChromosome.booleanReturn the value of the first gene of this chromosome.booleanbooleanValue(int index) Return the value on the specified index.intcompareTo(BitChromosome that) doubleReturn the double value this BitChromosome represents.booleanfloatReturn the float value this BitChromosome represents.gene()Return the first gene of this chromosome.get(int index) Return the value at the givenindex.inthashCode()intintValue()Return the long value this BitChromosome represents.invert()Invert the ones and zeros of this bit chromosome.booleanisValid()Return alwaystrue.intlength()Return the length of this sequence.longReturn the long value this BitChromosome represents.Maps the gene alleles of this chromosome, given asBitSet, by applying the given mapper functionf.Create a new instance of type T.newInstance(ISeq<BitGene> genes) A factory method which creates a newChromosomeof specific type and the givengenes.static BitChromosomeof(int length) Constructing a new BitChromosome with the givenlengthand randomly set bits.static BitChromosomeof(int length, double p) Constructing a new BitChromosome with the givenlengthand randomly set bits.static BitChromosomeof(CharSequence value) Create a newBitChromosomefrom the given character sequence containing '0' and '1'; as created with thetoCanonicalString()method.static BitChromosomeof(CharSequence value, double p) Create a newBitChromosomefrom the given character sequence containing '0' and '1'; as created with thetoCanonicalString()method.static BitChromosomeof(CharSequence value, int length, double p) Create a newBitChromosomefrom the given character sequence containing '0' and '1'; as created with thetoCanonicalString()method.static BitChromosomeof(BigInteger value) Create a newBitChromosomefrom the given big integer value.static BitChromosomeof(BigInteger value, int length) Create a newBitChromosomefrom the given big integer value and ones' probability.static BitChromosomeof(BigInteger value, int length, double p) Create a newBitChromosomefrom the given big integer value and ones' probability.static BitChromosomeConstructing a new BitChromosome from a given BitSet.static BitChromosomeCreate a newBitChromosomewith the given parameters.static BitChromosomeCreate a newBitChromosomewith the given parameters.doubleReturn the one nominal probability of this chromosome.ones()Return the indexes of the ones of this bit-chromosome as stream.or(BitChromosome other) Returns aBitChromosomewhose value is (this | other).shiftLeft(int n) Returns a newBitChromosomewhose value is (this << n).shiftRight(int n) Returns a newBitChromosomewhose value is (this >> n).Return theBigIntegervalue thisBitChromosomerepresents.toBitSet()Return the corresponding BitSet of this BitChromosome.byte[]Returns the byte array, which represents the bit values ofthischromosome.Return the BitChromosome as String.toString()xor(BitChromosome other) Returns aBitChromosomewhose value is (this ^ other).zeros()Return the indexes of the zeros of this bit-chromosome as stream.Methods inherited from class java.lang.Number
byteValue, shortValueMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface io.jenetics.util.BaseSeq
forEach, isEmpty, iterator, listIterator, nonEmpty, spliterator, streamMethods inherited from interface io.jenetics.Chromosome
as
-
Constructor Details
-
BitChromosome
Create a new bit chromosome from the given bit (byte) array.- Parameters:
bits- the bit values of the new chromosome gene.start- the initial (bit) index of the range to be copied, inclusiveend- the final (bit) index of the range to be copied, exclusive. (This index may lie outside the array.)p- the one's probability- Throws:
ArrayIndexOutOfBoundsException- ifstart < 0orstart > bits.length*8IllegalArgumentException- ifstart > endNullPointerException- if thebitsarray isnull.- Since:
- 7.0
-
BitChromosome
Create a new bit chromosome from the given bit (byte) array.- Parameters:
bits- the bit values of the new chromosome gene.start- the initial (bit) index of the range to be copied, inclusiveend- the final (bit) index of the range to be copied, exclusive. (This index may lie outside the array.)- Throws:
ArrayIndexOutOfBoundsException- ifstart < 0orstart > bits.length*8IllegalArgumentException- ifstart > endNullPointerException- if thebitsarray isnull.
-
BitChromosome
Create a newBitChromosomefrom the givenbytearray. This is a shortcut fornew BitChromosome(bits, 0, bits.length*8).- Parameters:
bits- thebytearray.
-
-
Method Details
-
oneProbability
Return the one nominal probability of this chromosome. It's not the actual one-probability ofthischromosome.- Returns:
- the one probability of this chromosome.
- Since:
- 5.2
-
gene
Description copied from interface:ChromosomeReturn the first gene of this chromosome. Each chromosome must contain at least one gene.- Specified by:
genein interfaceChromosome<BitGene>- Returns:
- the first gene of this chromosome.
-
booleanValue
Return the value of the first gene of this chromosome.- Returns:
- the first value of this chromosome.
- Since:
- 4.2
-
get
Description copied from interface:BaseSeqReturn the value at the givenindex. -
length
Description copied from interface:BaseSeqReturn the length of this sequence. Once the sequence is created, the length can't be changed. -
booleanValue
Return the value on the specified index.- Parameters:
index- the gene index- Returns:
- the wanted gene value
- Throws:
IndexOutOfBoundsException- if the index is out of range (index < 1 || index >= length()).- Since:
- 4.2
-
bitCount
Returns the number of bits set to true in thisBitChromosome.- Returns:
- the number of bits set to true in this
BitChromosome
-
intValue
Return the long value this BitChromosome represents. -
longValue
Return the long value this BitChromosome represents. -
floatValue
Return the float value this BitChromosome represents.- Specified by:
floatValuein classNumber- Returns:
- float value this BitChromosome represents.
-
doubleValue
Return the double value this BitChromosome represents.- Specified by:
doubleValuein classNumber- Returns:
- double value this BitChromosome represents.
-
isValid
Return alwaystrue.- Specified by:
isValidin interfaceChromosome<BitGene>- Specified by:
isValidin interfaceVerifiable- Returns:
true, always
-
toBigInteger
Return theBigIntegervalue thisBitChromosomerepresents.- Returns:
BigIntegervalue thisBitChromosomerepresents.
-
toByteArray
Returns the byte array, which represents the bit values ofthischromosome.- Returns:
- a byte array which represents this
BitChromosome. The length of the array is(int)Math.ceil(length()/8.0).
-
toBitSet
Return the corresponding BitSet of this BitChromosome.- Returns:
- The corresponding BitSet of this BitChromosome.
-
ones
Return the indexes of the ones of this bit-chromosome as stream.- Returns:
- the indexes of the ones of this bit-chromosome
- Since:
- 3.0
-
zeros
Return the indexes of the zeros of this bit-chromosome as stream.- Returns:
- the indexes of the zeros of this bit-chromosome
- Since:
- 3.0
-
newInstance
Description copied from interface:ChromosomeA factory method which creates a newChromosomeof specific type and the givengenes.- Specified by:
newInstancein interfaceChromosome<BitGene>- Parameters:
genes- the genes of the new chromosome. The given genes array is not copied.- Returns:
- A new
Chromosomeof the same type with the given genes.
-
newInstance
Description copied from interface:FactoryCreate a new instance of type T.- Specified by:
newInstancein interfaceFactory<Chromosome<BitGene>>- Returns:
- a new instance of type T
-
map
Maps the gene alleles of this chromosome, given asBitSet, by applying the given mapper functionf. The mapped gene values are then wrapped into a newly created chromosome.- Parameters:
f- the mapper function- Returns:
- a newly created chromosome with the mapped gene values
- Throws:
NullPointerException- if the mapper function isnull.- Since:
- 6.1
-
toCanonicalString
Return the BitChromosome as String. A TRUE is represented by a 1 and a FALSE by a 0. The returned string can be used to create a new chromosome with theof(CharSequence)constructor.- Returns:
- String representation (containing only '1' and '0') of the BitChromosome.
-
compareTo
- Specified by:
compareToin interfaceComparable<BitChromosome>
-
and
Returns aBitChromosomewhose value is (this & other).- Parameters:
other- value to be AND'ed with thisBitChromosome.- Returns:
this & other- Since:
- 7.1
-
or
Returns aBitChromosomewhose value is (this | other).- Parameters:
other- value to be OR'ed with thisBitChromosome.- Returns:
this | other- Since:
- 7.1
-
xor
Returns aBitChromosomewhose value is (this ^ other).- Parameters:
other- value to be XOR'ed with thisBitChromosome.- Returns:
this ^ other- Since:
- 7.1
-
invert
Invert the ones and zeros of this bit chromosome.- Returns:
- a new BitChromosome with inverted ones and zeros.
-
shiftLeft
Returns a newBitChromosomewhose value is (this << n). The shift distance, n, may be negative, in which case this method performs a right shift.- Parameters:
n- shift distance, in bits- Returns:
this << n
-
shiftRight
Returns a newBitChromosomewhose value is (this >> n). The shift distance, n, may be negative, in which case this method performs a left shift.- Parameters:
n- shift distance, in bits- Returns:
this >> n
-
hashCode
-
equals
-
toString
-
of
Constructing a new BitChromosome with the givenlengthand randomly set bits. The TRUEs and FALSE in theChromosomeare equally distributed with one-probability ofp.- Parameters:
length- Length of the BitChromosome, number of bits.p- Probability of the TRUEs in the BitChromosome.- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NegativeArraySizeException- if thelengthis smaller than one.IllegalArgumentException- ifpis not a valid probability.
-
of
Constructing a new BitChromosome with the givenlengthand randomly set bits. The TRUEs and FALSE in theChromosomeare equally distributed with one-probability of 0.5.- Parameters:
length- Length of the BitChromosome.- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NegativeArraySizeException- if thelengthis smaller than one.
-
of
Create a newBitChromosomewith the given parameters.- Parameters:
bits- the bit-set which initializes the chromosomelength- length of the BitChromosome.p- Probability of the TRUEs in the BitChromosome.- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NegativeArraySizeException- if thelengthis smaller than one.NullPointerException- if thebitSetisnull.IllegalArgumentException- ifpis not a valid probability.
-
of
Create a newBitChromosomewith the given parameters. TheoneProbability()of the chromosome is set to0.5.- Parameters:
bits- the bit-set which initializes the chromosomelength- length of the BitChromosome.- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NegativeArraySizeException- if thelengthis smaller than one.NullPointerException- if thebitSetisnull.
-
of
Constructing a new BitChromosome from a given BitSet. The length of the constructedBitChromosomewill be (BitSet.length()).- Parameters:
bits- the bit-set which initializes the chromosome- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NullPointerException- if thebitSetisnull.- See Also:
-
of
Create a newBitChromosomefrom the given big integer value and ones' probability.- Parameters:
value- the value of the createdBitChromosomelength- length of the BitChromosomep- Probability of the TRUEs in the BitChromosome.- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NullPointerException- if the givenvalueisnull.IllegalArgumentException- ifpis not a valid probability.
-
of
Create a newBitChromosomefrom the given big integer value and ones' probability. TheoneProbability()of the chromosome is set to0.5.- Parameters:
value- the value of the createdBitChromosomelength- length of the BitChromosome- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NullPointerException- if the givenvalueisnull.IllegalArgumentException- ifpis not a valid probability.- Since:
- 7.0
-
of
Create a newBitChromosomefrom the given big integer value. TheoneProbability()of the chromosome is set to0.5.- Parameters:
value- the value of the createdBitChromosome- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NullPointerException- if the givenvalueisnull.
-
of
Create a newBitChromosomefrom the given character sequence containing '0' and '1'; as created with thetoCanonicalString()method.- Parameters:
value- the input string.length- length of the BitChromosomep- Probability of the TRUEs in the BitChromosome.- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NullPointerException- if thevalueisnull.IllegalArgumentException- if the length of the character sequence is zero or contains other characters than '0' or '1'.IllegalArgumentException- ifpis not a valid probability.
-
of
Create a newBitChromosomefrom the given character sequence containing '0' and '1'; as created with thetoCanonicalString()method.- Parameters:
value- the input string.p- Probability of the TRUEs in the BitChromosome.- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NullPointerException- if thevalueisnull.IllegalArgumentException- if the length of the character sequence is zero or contains other characters than '0' or '1'.IllegalArgumentException- ifpis not a valid probability.
-
of
Create a newBitChromosomefrom the given character sequence containing '0' and '1'; as created with thetoCanonicalString()method. TheoneProbability()of the chromosome is set to0.5.- Parameters:
value- the input string.- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NullPointerException- if thevalueisnull.IllegalArgumentException- if the length of the character sequence is zero or contains other characters than '0' or '1'.
-