Package io.jenetics
Class BitChromosome
- 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 class BitChromosome extends Number implements Chromosome<BitGene>, Comparable<BitChromosome>, Serializable
Implementation of the classical BitChromosome.- Since:
- 1.0
- Version:
- 6.1
- See Also:
BitGene, Serialized Form- Implementation Requirements:
- This class is immutable and thread-safe.
-
-
Constructor Summary
Constructors Constructor Description BitChromosome(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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description intbitCount()Returns the number of bits set to true in thisBitChromosome.booleanbooleanValue()Return the value of the first gene of this chromosome.booleanbooleanValue(int index)Return the value on the specified index.intcompareTo(BitChromosome that)doubledoubleValue()Return the double value this BitChromosome represents.booleanequals(Object obj)floatfloatValue()Return the float value this BitChromosome represents.BitGenegene()Return the first gene of this chromosome.BitGeneget(int index)Return the value at the givenindex.inthashCode()intintValue()Return the long value this BitChromosome represents.BitChromosomeinvert()Invert the ones and zeros of this bit chromosome.booleanisValid()Return alwaystrue.intlength()Return the length of this sequence.ListIterator<BitGene>listIterator()Return a list iterator over the bit-genes of this chromosome.longlongValue()Return the long value this BitChromosome represents.BitChromosomemap(Function<? super BitSet,? extends BitSet> f)Maps the gene alleles of this chromosome, given asBitSet, by applying the given mapper functionf.BitChromosomenewInstance()Create a new instance of type T.BitChromosomenewInstance(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 given _length.static BitChromosomeof(int length, double p)Construct a new BitChromosome with the given _length.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, double p)Create a newBitChromosomefrom the given big integer value and ones probability.static BitChromosomeof(BitSet bits)Deprecated, for removal: This API element is subject to removal in a future version.This method doesn't let you control the actual length of the createdBitChromosome.static BitChromosomeof(BitSet bits, int length)Create a newBitChromosomewith the given parameters.static BitChromosomeof(BitSet bits, int length, double p)Create a newBitChromosomewith the given parameters.doubleoneProbability()Return the one probability of this chromosome.IntStreamones()Return the indexes of the ones of this bit-chromosome as stream.BigIntegertoBigInteger()Return theBigIntegervalue thisBitChromosomerepresents.BitSettoBitSet()Return the corresponding BitSet of this BitChromosome.byte[]toByteArray()inttoByteArray(byte[] bytes)Returns the two's-complement binary representation of this large integer.StringtoCanonicalString()Return the BitChromosome as String.StringtoString()IntStreamzeros()Return the indexes of the zeros of this bit-chromosome as stream.-
Methods inherited from class java.lang.Number
byteValue, shortValue
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.jenetics.util.BaseSeq
isEmpty, iterator, nonEmpty, spliterator, stream
-
Methods inherited from interface io.jenetics.Chromosome
as
-
-
-
-
Constructor Detail
-
BitChromosome
public BitChromosome(byte[] bits, int start, int end)
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
public BitChromosome(byte[] bits)
Create a newBitChromosomefrom the givenbytearray. This is a shortcut fornew BitChromosome(bits, 0, bits.length*8).- Parameters:
bits- thebytearray.
-
-
Method Detail
-
oneProbability
public double oneProbability()
Return the one probability of this chromosome.- Returns:
- the one probability of this chromosome.
- Since:
- 5.2
-
gene
public BitGene 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
public boolean booleanValue()
Return the value of the first gene of this chromosome.- Returns:
- the first value of this chromosome.
- Since:
- 4.2
-
get
public BitGene get(int index)
Description copied from interface:BaseSeqReturn the value at the givenindex.
-
length
public int length()
Description copied from interface:BaseSeqReturn the length of this sequence. Once the sequence is created, the length can't be changed.
-
booleanValue
public boolean booleanValue(int index)
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
public int bitCount()
Returns the number of bits set to true in thisBitChromosome.- Returns:
- the number of bits set to true in this
BitChromosome
-
listIterator
public ListIterator<BitGene> listIterator()
Return a list iterator over the bit-genes of this chromosome.- Specified by:
listIteratorin interfaceBaseSeq<BitGene>- Returns:
- a list iterator over the bit-genes of this chromosome
-
intValue
public int intValue()
Return the long value this BitChromosome represents.
-
longValue
public long longValue()
Return the long value this BitChromosome represents.
-
floatValue
public float floatValue()
Return the float value this BitChromosome represents.- Specified by:
floatValuein classNumber- Returns:
- float value this BitChromosome represents.
-
doubleValue
public double doubleValue()
Return the double value this BitChromosome represents.- Specified by:
doubleValuein classNumber- Returns:
- double value this BitChromosome represents.
-
isValid
public boolean isValid()
Return alwaystrue.- Specified by:
isValidin interfaceChromosome<BitGene>- Specified by:
isValidin interfaceVerifiable- Returns:
true, always
-
toBigInteger
public BigInteger toBigInteger()
Return theBigIntegervalue thisBitChromosomerepresents.- Returns:
BigIntegervalue thisBitChromosomerepresents.
-
toByteArray
public int toByteArray(byte[] bytes)
Returns the two's-complement binary representation of this large integer. The output array is in big-endian byte-order: the most significant byte is at the offset position.Note: This representation is consistent with
java.lang.BigIntegerbyte array representation and can be used for conversion between the two classes.- Parameters:
bytes- the bytes to hold the binary representation (two's-complement) of this large integer.- Returns:
- the number of bytes written.
- Throws:
IndexOutOfBoundsException- ifbytes.length < (int)Math.ceil(length()/8.0)NullPointerException- it the give array isnull.
-
toByteArray
public byte[] toByteArray()
- Returns:
- a byte array which represents this
BitChromosome. The length of the array is(int)Math.ceil(length()/8.0). - See Also:
toByteArray(byte[])
-
toBitSet
public BitSet toBitSet()
Return the corresponding BitSet of this BitChromosome.- Returns:
- The corresponding BitSet of this BitChromosome.
-
ones
public IntStream 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
public IntStream 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
public BitChromosome newInstance(ISeq<BitGene> genes)
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
public BitChromosome 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
public BitChromosome map(Function<? super BitSet,? extends BitSet> f)
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
public String 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
public int compareTo(BitChromosome that)
- Specified by:
compareToin interfaceComparable<BitChromosome>
-
invert
public BitChromosome invert()
Invert the ones and zeros of this bit chromosome.- Returns:
- a new BitChromosome with inverted ones and zeros.
-
of
public static BitChromosome of(int length, double p)
Construct a new BitChromosome with the given _length.- 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
public static BitChromosome of(int length)
Constructing a new BitChromosome with the given _length. The TRUEs and FALSE in theChromosomeare equally distributed.- Parameters:
length- Length of the BitChromosome.- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NegativeArraySizeException- if the_lengthis smaller than one.
-
of
public static BitChromosome of(BitSet bits, int length)
Create a newBitChromosomewith the given parameters.- Parameters:
length- length of the BitChromosome.bits- the bit-set which initializes the chromosome- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NegativeArraySizeException- if thelengthis smaller than one.NullPointerException- if thebitSetisnull.
-
of
public static BitChromosome of(BitSet bits, int length, double p)
Create a newBitChromosomewith the given parameters.- Parameters:
length- length of the BitChromosome.bits- the bit-set which initializes the chromosomep- 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
@Deprecated(since="6.1", forRemoval=true) public static BitChromosome of(BitSet bits)
Deprecated, for removal: This API element is subject to removal in a future version.This method doesn't let you control the actual length of the createdBitChromosome. Useof(BitSet, int, double)orof(BitSet, int)instead.Constructing a new BitChromosome from a given BitSet. The BitSet is copied while construction. The length of the constructed BitChromosome will bebitSet.length()(BitSet.length()).- Parameters:
bits- the bit-set which initializes the chromosome- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NullPointerException- if thebitSetisnull.
-
of
public static BitChromosome of(BigInteger value)
Create a newBitChromosomefrom the given big integer value.- Parameters:
value- the value of the createdBitChromosome- Returns:
- a new
BitChromosomewith the given parameter - Throws:
NullPointerException- if the givenvalueisnull.
-
of
public static BitChromosome of(BigInteger value, double p)
Create a newBitChromosomefrom the given big integer value and ones probability.- Parameters:
value- the value of the createdBitChromosomep- 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
public static BitChromosome of(CharSequence value)
Create a newBitChromosomefrom the given character sequence containing '0' and '1'; as created with thetoCanonicalString()method.- 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'.
-
of
public static BitChromosome of(CharSequence value, double p)
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
public static BitChromosome of(CharSequence value, int length, double p)
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.
-
-