Package libai.genetics.chromosomes
Class BinaryChromosome
- java.lang.Object
-
- libai.genetics.chromosomes.Chromosome
-
- libai.genetics.chromosomes.BinaryChromosome
-
public class BinaryChromosome extends Chromosome
The binary form of the chromosome. This chromosome contains a sequence of bits. The mutation operation are supported by flipping a bit. And the cross by masking the bits:
offspring1: (this & mask) | (otherparent & ~mask)
offspring2: (otherparent & mask) | (this & ~mask)
-
-
Constructor Summary
Constructors Modifier Constructor Description BinaryChromosome()
protected
BinaryChromosome(int length, java.util.Random random)
protected
BinaryChromosome(BinaryChromosome c)
Initialize this chromosome with another one
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Chromosome[]
cross(Chromosome b, int position)
Split the genes by position and swap lower portion of this with the lower portion of b and vice versa to return 2 offsprings.long
decode()
Convert this chromosome in a long value.double
decode(double minValue, double maxValue)
Convert this chromosome in a double value using the minValue and maxValue as reference.Chromosome
getCopy()
Clone this chromosome.Chromosome
getInstance(int length, java.util.Random random)
Creates a new chromosome with a length oflength
protected Chromosome
getInstance(java.util.BitSet bs)
Create a new instance of a Chromosome with this BitSet.Chromosome
mutate(double pm)
Flip a bit of the genes.java.lang.String
toString()
-
Methods inherited from class libai.genetics.chromosomes.Chromosome
getChance, getFitness, getFitnessReal, setChance, setFitness, setFitnessReal
-
-
-
-
Constructor Detail
-
BinaryChromosome
public BinaryChromosome()
-
BinaryChromosome
protected BinaryChromosome(int length, java.util.Random random)
-
BinaryChromosome
protected BinaryChromosome(BinaryChromosome c)
Initialize this chromosome with another one- Parameters:
c
- The chromosome to copy.
-
-
Method Detail
-
cross
public Chromosome[] cross(Chromosome b, int position)
Split the genes by position and swap lower portion of this with the lower portion of b and vice versa to return 2 offsprings.- Specified by:
cross
in classChromosome
- Parameters:
b
- chromosome to crossposition
- Position to split the chromosomes.- Returns:
- A two position array with the new offsprings.
-
mutate
public Chromosome mutate(double pm)
Flip a bit of the genes. The bits are selected with probabilitypm
- Specified by:
mutate
in classChromosome
- Parameters:
pm
- Mutation probability.- Returns:
- The new mutated chromosome.
-
getInstance
protected Chromosome getInstance(java.util.BitSet bs)
Create a new instance of a Chromosome with this BitSet.- Parameters:
bs
- The genetic charge to copy.- Returns:
- A new chromosome with the same genetic charge.
-
decode
public long decode()
Convert this chromosome in a long value. If the chromosome is too large this value can be overflowed.- Returns:
- The integral representation of the chromosome.
-
decode
public double decode(double minValue, double maxValue)
Convert this chromosome in a double value using the minValue and maxValue as reference. If the chromosome is too large this value can be overflowed.- Parameters:
minValue
-minValue
maxValue
-maxValue
- Returns:
- The double representation of the chromosome.
-
getCopy
public Chromosome getCopy()
Clone this chromosome.- Specified by:
getCopy
in classChromosome
- Returns:
- A identical chromosome of this.
-
getInstance
public Chromosome getInstance(int length, java.util.Random random)
Creates a new chromosome with a length oflength
- Specified by:
getInstance
in classChromosome
- Parameters:
length
- The length of the new chromosome.- Returns:
- A new instance of length
length
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-