panga
Public Member Functions | Static Public Member Functions | List of all members
panga::Chromosome Class Reference

#include <Chromosome.h>

Inheritance diagram for panga::Chromosome:
Inheritance graph
[legend]
Collaboration diagram for panga::Chromosome:
Collaboration graph
[legend]

Public Member Functions

 Chromosome (const Genome &genome)
 
 Chromosome (const Chromosome &rhs)=delete
 
 Chromosome (Chromosome &&rhs)=default
 
Chromosomeoperator= (const Chromosome &rhs)=delete
 
const GenomeGetGenome () const
 
void Randomize (RandomWrapper *random)
 
template<typename IntegerType = uint64_t, bool use_gray_encoding = true>
IntegerType DecodeIntegerGene (size_t gene_index, IntegerType min=0, IntegerType max=std::numeric_limits< IntegerType >::max()) const
 
template<typename IntegerType = uint64_t, bool use_gray_encoding = true>
void EncodeIntegerGene (size_t gene_index, IntegerType value)
 
template<typename FloatType = double, bool use_gray_encoding = true>
FloatType DecodeFloatGene (size_t gene_index, FloatType min, FloatType max) const
 
template<typename FloatType = double, bool use_gray_encoding = true>
void EncodeFloatGene (size_t gene_index, FloatType value, FloatType min, FloatType max)
 
bool DecodeBooleanGene (size_t gene_index) const
 
void EncodeBooleanGene (size_t gene_index, bool value)
 
std::byte * GetRawGene (size_t gene_index, size_t *gene_bit_width)
 
- Public Member Functions inherited from panga::BitVector
 BitVector (size_t bit_count=0)
 
 BitVector (const BitVector &source)
 
BitVectoroperator= (const BitVector &rhs)
 
bool Equals (const BitVector &rhs, size_t bits_to_compare) const
 
bool Equals (const BitVector &rhs) const
 
void SetBitCount (size_t bit_count)
 
size_t GetBitCount () const
 
void Clip (size_t bit_count)
 
void Clear ()
 
void Set (size_t index)
 
void Unset (size_t index)
 
bool Get (size_t index) const
 
void Flip (size_t index)
 
template<typename IntegerType = uint64_t>
IntegerType GetInt (size_t bit_index, size_t bit_width) const
 
template<typename IntegerType = uint64_t>
void SetInt (IntegerType value, size_t bit_index, size_t bit_width)
 
void SubVector (BitVector *destination, size_t destination_start_bit_offset, size_t source_start_bit_offset, size_t bits_to_copy) const
 
size_t HammingDistance (const BitVector &rhs) const
 
size_t ToString (char *buffer, size_t buffer_length) const
 
void FromString (const char *buffer, size_t buffer_length)
 
size_t ToStringHex (char *buffer, size_t buffer_length) const
 
void FromStringHex (const char *buffer, size_t buffer_length)
 

Static Public Member Functions

template<typename IntegerType = uint64_t>
static IntegerType DecodeGray (IntegerType gray_value)
 
template<typename IntegerType = uint64_t>
static IntegerType EncodeGray (IntegerType binary_value)
 
template<typename FloatType = double, typename IntegerType = uint64_t>
static FloatType DecodeFloat (IntegerType int_value, size_t bit_width, FloatType min, FloatType max)
 
template<typename FloatType = double, typename IntegerType = uint64_t>
static IntegerType EncodeFloat (FloatType float_value, size_t bit_width, FloatType min, FloatType max)
 
static void UniformCrossover (const Chromosome &parent1, const Chromosome &parent2, Chromosome *offspring, RandomWrapper *random, bool ignore_gene_boundaries=true)
 
static void KPointCrossover (size_t k, const Chromosome &parent1, const Chromosome &parent2, Chromosome *offspring, RandomWrapper *random, bool ignore_gene_boundaries=true)
 
static void FlipMutator (Chromosome *chromosome, double mutation_percentage, RandomWrapper *random)
 

Additional Inherited Members

- Static Public Attributes inherited from panga::BitVector
static HexFormat_t HexFormat
 
- Protected Member Functions inherited from panga::BitVector
const std::vector< std::byte > & GetBytes () const
 
std::vector< std::byte > & GetBytesWritable ()
 
void Resize (size_t bit_count)
 
void WriteToStream (std::ostream &out) const
 
void WriteToStreamHex (std::ostream &out) const
 
- Static Protected Member Functions inherited from panga::BitVector
template<typename IntegerType = uint64_t>
static void WriteInt (std::byte *bytes, size_t start_bit_index, size_t bits_to_copy, IntegerType value)
 
template<typename IntegerType = uint64_t>
static IntegerType ReadInt (const std::byte *bytes, size_t start_bit_index, size_t bits_to_copy)
 
static void WriteBytes (const std::byte *source, size_t source_start_bit_offset, std::byte *destination, size_t destination_start_bit_offset, size_t bits_to_copy)
 
static bool Compare (const std::byte *left, const std::byte *right, size_t bits_to_compare)
 

Detailed Description

The binary data acting as an instance of a Genome.
Length of Chromosome in bits will be the same as the bits required to encode the Genome.
With the Genome, we know where all the genes are located in our binary data and we can interpret the bits making up each gene.

See also
Genome

Member Function Documentation

◆ DecodeBooleanGene()

bool panga::Chromosome::DecodeBooleanGene ( size_t  gene_index) const

Return true if the gene at |gene_index| is set and false if the gene is not set.
Note: |gene_index| must be the index of a boolean gene and not a gene with a bit width - even if that bit width is 1.

◆ DecodeFloat()

template<typename FloatType = double, typename IntegerType = uint64_t>
static FloatType panga::Chromosome::DecodeFloat ( IntegerType  int_value,
size_t  bit_width,
FloatType  min,
FloatType  max 
)
inlinestatic

Decode a floating point number from an integer representation.

Parameters
int_valueThe integer representation we will decode.
bit_widthHow many bits of int_value do we want to decode.
minMin value for the float.
maxMax value for the float.
See also
EncodeFloat

◆ DecodeFloatGene()

template<typename FloatType = double, bool use_gray_encoding = true>
FloatType panga::Chromosome::DecodeFloatGene ( size_t  gene_index,
FloatType  min,
FloatType  max 
) const
inline

Fetch the binary data representing a gene and interpret it as a floating point number scaled between min and max.
If use_gray_encoding is true, we will treat the gene data as a gray-coded number and decode it into a binary representation before scaling.

Parameters
gene_indexThe index of the gene we should interpret.
minThe min value which the float could assume.
maxThe max value which the float could assume.
Returns
Floating point representation of the gene in this Chromosome at gene_index.

◆ DecodeGray()

template<typename IntegerType = uint64_t>
static IntegerType panga::Chromosome::DecodeGray ( IntegerType  gray_value)
inlinestatic

Decode a binary integer from a gray-encoded value.

Parameters
gray_valueGray-encoded value
Returns
Binary integer value

◆ DecodeIntegerGene()

template<typename IntegerType = uint64_t, bool use_gray_encoding = true>
IntegerType panga::Chromosome::DecodeIntegerGene ( size_t  gene_index,
IntegerType  min = 0,
IntegerType  max = std::numeric_limits<IntegerType>::max() 
) const
inline

Fetch the binary data representing a gene and interpret it as an integer scaled between min and max.
If use_gray_encoding is true, we will treat the gene data as a gray-coded number and decode it into a binary representation before scaling.

Parameters
gene_indexThe index of the gene we should interpret.
minThe min value which the integer could assume.
maxThe max value which the integer could assume.
Returns
Integer representation of the gene in this Chromosome at |gene_index|.

◆ EncodeBooleanGene()

void panga::Chromosome::EncodeBooleanGene ( size_t  gene_index,
bool  value 
)

Set the bit value at |gene_index|.
Note: |gene_index| must be the index of a boolean gene and not a gene with a bit width - even if that bit width is 1.

Parameters
valueIf true, the bit at |gene_index| is set. If false, the bit at |gene_index| is unset.

◆ EncodeFloat()

template<typename FloatType = double, typename IntegerType = uint64_t>
static IntegerType panga::Chromosome::EncodeFloat ( FloatType  float_value,
size_t  bit_width,
FloatType  min,
FloatType  max 
)
inlinestatic

Encode a floating point number into an integer representation.

Parameters
float_valueFloat value to encode.
bit_widthHow many bits should we limit the integer representation to.
minMin value for the float.
maxMax value for the float.
See also
DecodeFloat

◆ EncodeFloatGene()

template<typename FloatType = double, bool use_gray_encoding = true>
void panga::Chromosome::EncodeFloatGene ( size_t  gene_index,
FloatType  value,
FloatType  min,
FloatType  max 
)
inline

Take a floating point value and encode it into the binary data representing a gene in the Chromosome.
If use_gray_encoding is true, we will encode value into a gray-coded value before writing it into the gene data.

Parameters
gene_indexThe index of the gene we should write to.
valueThe floating point value we want to write into the Chromosome.

◆ EncodeGray()

template<typename IntegerType = uint64_t>
static IntegerType panga::Chromosome::EncodeGray ( IntegerType  binary_value)
inlinestatic

Encode a binary integer value into a gray representation.

Parameters
binary_valueBinary integer value
Returns
Gray-encoded value

◆ EncodeIntegerGene()

template<typename IntegerType = uint64_t, bool use_gray_encoding = true>
void panga::Chromosome::EncodeIntegerGene ( size_t  gene_index,
IntegerType  value 
)
inline

Take an integer value and encode it into the binary data representing a gene in the Chromosome.
If use_gray_encoding is true, we will encode the integer into a gray-coded value before writing it into the gene data.

Parameters
gene_indexThe index of the gene we should write to.
valueThe integer value we want to write into the Chromosome.

◆ FlipMutator()

void panga::Chromosome::FlipMutator ( Chromosome chromosome,
double  mutation_percentage,
RandomWrapper random 
)
static

Perform flip mutation on |chromosome|.
Every bit in |chromosome| will have |mutation_percentage| chance of flipping.

◆ GetGenome()

const Genome & panga::Chromosome::GetGenome ( ) const

Get a read-only reference to the Genome used by this Chromosome.

◆ GetRawGene()

std::byte * panga::Chromosome::GetRawGene ( size_t  gene_index,
size_t *  gene_bit_width 
)

Get the raw binary data from the Chromosome where the gene at |gene_index| is located. Doesn't interpret the binary data.

Parameters
gene_bit_widthAn out param which receives the bit width of the gene at |gene_index|.

◆ KPointCrossover()

void panga::Chromosome::KPointCrossover ( size_t  k,
const Chromosome parent1,
const Chromosome parent2,
Chromosome offspring,
RandomWrapper random,
bool  ignore_gene_boundaries = true 
)
static

Perform k-point crossover between |parent1| and |parent2| and store the result in |offspring|.

Parameters
ignore_gene_boundariesIf false, gene boundaries will be respected. Instead of some bits from a gene being copied from |parent1| and some from |parent2|, all of the bits containing the gene will be copied together from a single parent. Genes are copied in chunks according to the cut points.
If true, gene boundaries are ignored and the chromosome is treated as a single chunk of bits which is split into chunks according to the cut points.

◆ Randomize()

void panga::Chromosome::Randomize ( RandomWrapper random)

Randomize all bits in the Chromosome.

◆ UniformCrossover()

void panga::Chromosome::UniformCrossover ( const Chromosome parent1,
const Chromosome parent2,
Chromosome offspring,
RandomWrapper random,
bool  ignore_gene_boundaries = true 
)
static

Perform uniform crossover between |parent1| and |parent2| and store the result in |offspring|.

Parameters
ignore_gene_boundariesIf false, gene boundaries will be respected. Instead of some bits from a gene being copied from |parent1| and some from |parent2|, all of the bits containing the gene will be copied together from a single parent. Each gene still has uniformly equal chance of being copied from either parent.
If true, gene boundaries are ignored and every bit in the chromosome has an equal chance to be copied from either parent.

The documentation for this class was generated from the following files: