Genotype class. More...

Inheritance diagram for Genotype:

Classes

class  nObjectiveGreaterThanComparator
 Utility class for generating a function for comparing genotype of an objective. More...
 

Public Member Functions

 Genotype (unsigned int size=0)
 Construct a un-initialized Genotype.
 
 Genotype (QString, bool compressed=false)
 Read the values from QString and set the size accordlying to length of QString.
 
 Genotype (const Genotype &genotype)
 Copy-Constructor (generate a clone of the genoma)
 
virtual ~Genotype ()
 Destructor.
 
void assign (const Genotype *genotype)
 Assign method (it call operator=)
 
bool bit (unsigned int i) const
 Return the i-th bit.
 
virtual unsigned int bitToGeneIndex (unsigned int bit) const
 Convert the index of the given bit to the index of the gene that this bit is part of.
 
virtual Genotypeclone () const
 Create an exact copy of this Genotype.
 
virtual void configure (ConfigurationParameters &params, QString prefix)
 Configure the object using the ConfigurationParameters specified.
 
bool dominatedBy (const Genotype *genotype)
 Return True if this Genotype is dominated by the Genotype specified by param.
 
unsigned int extractUInt (unsigned int startPos, unsigned int stopPos)
 Utility function for extracting an integer value.
 
double fitness () const
 Return the fitness of this Genotype.
 
bool fromCompressedString (QString str)
 Read from compressed data with toCompressedString()
 
void fromString (QString)
 Configure the bits accordlying to its string representation passed.
 
virtual unsigned int geneToBitIndex (unsigned int gene) const
 Convert the index of the given gene to the index of the start of the bit(s) that encode this gene.
 
int hammingDistance (const Genotype *)
 Calculate the Hamming distance from the Genotype.
 
void insertUInt (unsigned int value, unsigned int startPos, unsigned int stopPos)
 Utility function for settings an integer value into Genotype.
 
QString notes () const
 Return the notes of this Genotype.
 
int numOfObjectives () const
 Return the number of Objectives setted The number of objectives is automatically configured by setObjective method.
 
double objective (int i) const
 Return the value of the i-th objective of the fitness.
 
virtual Genotypeoperator= (const Genotype &genotype)
 Assign operator (performs same operation of Copy-constructor)
Due to virtual attribute, Subclasses can customize the behaviour of this operator=, for an easy way to copy Genotype data the protected method copyDataFrom is provided and it behaves like this implementation of operator=.
 
virtual void randomize ()
 Randomize the value contained (attention, this method destroys previous data)
 
double rank () const
 Return the rank of this Genotype.
 
virtual void save (ConfigurationParameters &params, QString prefix)
 Save the actual status of parameters into the ConfigurationParameters object passed.
 
void set (unsigned int i)
 Set to One the i-th bit.
 
void setFitness (double value)
 Set the fitness value of this Genotype.
 
void setNotes (QString notes)
 Set the notes of this Genotype.
 
void setObjective (int i, double value)
 Set the i-th objective value of the (multi-objective) fitness.
 
void setRank (double rank)
 Set the rank of the genotype.
 
unsigned int size () const
 Return the size of Genotype.
 
QString toCompressedString () const
 Compress the data; It return a printable String.
 
void toggle (unsigned int i)
 toggle the i-th bit
 
QString toString () const
 Return a QString representing this Genotype.
 
void unset (unsigned int i)
 set to zero the i-th bit
 
- Public Member Functions inherited from ParameterSettableWithConfigureFunction
void addObserver (RuntimeParameterObserver *obs)
 
getRuntimeParameter (QString paramName)
 
virtual ParameterSettableUIgetUIManager ()
 
 ParameterSettable ()
 
virtual void postConfigureInitialization ()
 
void removeObserver (RuntimeParameterObserver *obs)
 
void setRuntimeParameter (QString paramName, T newvalue)
 
QString typeName () const
 
- Public Member Functions inherited from ParameterSettable
void addObserver (RuntimeParameterObserver *obs)
 
getRuntimeParameter (QString paramName)
 
void removeObserver (RuntimeParameterObserver *obs)
 
void setRuntimeParameter (QString paramName, T newvalue)
 
QString typeName () const
 

Static Public Member Functions

static void describe (QString type)
 Add to Factory::typeDescriptions() the descriptions of all parameters and subgroups.
 
static bool rankGreaterThanComparator (const Genotype *g1, const Genotype *g2)
 Utility function comparing two genotype pointers on their rank.
 
- Static Public Member Functions inherited from ParameterSettableWithConfigureFunction
static void describe (QString type)
 
static QString fullParameterDescriptionPath (QString type, QString param)
 
static QString fullSubgroupDescriptionPath (QString type, QString sub)
 
- Static Public Member Functions inherited from ParameterSettable
static void describe (QString type)
 
static QString fullParameterDescriptionPath (QString type, QString param)
 
static QString fullSubgroupDescriptionPath (QString type, QString sub)
 

Protected Member Functions

void copyDataFrom (Genotype *source)
 Utility method for copying data among Genotype.
 
void resize (unsigned int newsize)
 Resize the Genotype.
 
- Protected Member Functions inherited from ParameterSettableWithConfigureFunction
void notifyChangesToParam (QString paramName)
 
- Protected Member Functions inherited from ParameterSettable
void notifyChangesToParam (QString paramName)
 

Protected Attributes

unsigned int allocated
 Allocated memory.
 
unsigned char * data
 Data represented as a char vector allocated in memory.
 
QVector< double > fitnessv
 Objective values of the Fitness.
 
QString notesv
 Notes about this Genotype.
 
double rankv
 Rank.
 
unsigned int sizev
 Size.
 

Additional Inherited Members

- Public Types inherited from ParameterSettableWithConfigureFunction
enum  Property
 
- Public Attributes inherited from ParameterSettableWithConfigureFunction
 AllowMultiple
 
 Default
 
 IsList
 
 IsMandatory
 
- Static Public Attributes inherited from ParameterSettableWithConfigureFunction
static const double Infinity
 
static const int MaxInteger
 
static const int MinInteger
 
- Static Protected Member Functions inherited from ParameterSettableWithConfigureFunction
static Descriptor addTypeDescription (QString type, QString shortHelp, QString longHelp=QString(""))
 
static void setGraphicalEditor (QString type)
 

Detailed Description

Genotype class.

Description
Represent a bit-string
Warnings

Definition at line 38 of file genotype.h.

Constructor & Destructor Documentation

Genotype ( unsigned int  size = 0)

Construct a un-initialized Genotype.

Warning
you must explicity call randomize() to get a randomized Genotype

Definition at line 28 of file genotype.cpp.

References Genotype::allocated, Genotype::data, Genotype::fitnessv, Genotype::notesv, Genotype::rankv, Genotype::size(), and Genotype::sizev.

Referenced by Genotype::clone().

~Genotype ( )
virtual

Destructor.

Definition at line 39 of file genotype.cpp.

References Genotype::data.

Genotype ( QString  str,
bool  compressed = false 
)

Read the values from QString and set the size accordlying to length of QString.

Definition at line 43 of file genotype.cpp.

References Genotype::allocated, Genotype::data, Genotype::fitnessv, Genotype::notesv, Genotype::rankv, Genotype::sizev, and Genotype::unset().

Genotype ( const Genotype genotype)

Copy-Constructor (generate a clone of the genoma)

Definition at line 77 of file genotype.cpp.

References Genotype::allocated, Genotype::data, Genotype::fitnessv, Genotype::notesv, Genotype::rankv, and Genotype::sizev.

Member Function Documentation

void assign ( const Genotype genotype)

Assign method (it call operator=)

Definition at line 144 of file genotype.cpp.

unsigned int bitToGeneIndex ( unsigned int  bit) const
virtual

Convert the index of the given bit to the index of the gene that this bit is part of.

Reimplemented in DoubleGenotype.

Definition at line 359 of file genotype.cpp.

References Genotype::bit().

Genotype * clone ( ) const
virtual

Create an exact copy of this Genotype.

Reimplemented in RealGenotype, and SignedRangeGenotype.

Definition at line 149 of file genotype.cpp.

References Genotype::Genotype().

Referenced by Genome::append(), Genome::configure(), StefanoSteadyStateGA::gaStep(), Genome::Genome(), Genome::operator=(), and Genome::set().

void configure ( ConfigurationParameters params,
QString  prefix 
)
virtual

Configure the object using the ConfigurationParameters specified.

Parameters
paramsthe object with configuration parameters
prefixthe group in which parameters are expected to be (this is terminated by a separator character). We expect to receive as prefix the group with genotype parameters

Implements ParameterSettableWithConfigureFunction.

Reimplemented in RealGenotype, and SignedRangeGenotype.

Definition at line 100 of file genotype.cpp.

References Genotype::fitnessv, Genotype::fromCompressedString(), ConfigurationParameters::getValue(), Genotype::notesv, Genotype::rankv, and Genotype::resize().

void copyDataFrom ( Genotype source)
protected

Utility method for copying data among Genotype.

Definition at line 363 of file genotype.cpp.

References Genotype::allocated, Genotype::data, Genotype::fitnessv, Genotype::notesv, Genotype::rankv, and Genotype::sizev.

Referenced by RealGenotype::operator=(), and SignedRangeGenotype::operator=().

bool dominatedBy ( const Genotype genotype)

Return True if this Genotype is dominated by the Genotype specified by param.

Warning
the two Genotypes should have the same number of objectives; if this constraint is not meet then the number of objectives compared will be the minimum between the two genotypes
Note
Definition of Domination: a Genotype A is dominated by Genotype B iff all objective values of A are less-equal of corresponding objective values of B and for at least one objective the value of A is strictly less that value in B

Definition at line 190 of file genotype.cpp.

References Genotype::fitnessv.

unsigned int extractUInt ( unsigned int  startPos,
unsigned int  stopPos 
)

Utility function for extracting an integer value.

Parameters
startPosis the index of starting position of the bits to decode
stopPosis the index of end position of the bits to decode
          +----------------------------+
          | 0 1 0 0 1 0 1 1 0 1 0 1 0  |
          +----------------------------+
      
if startPos is 4 and stopPos is 8 the bits 1011 is decode and this methods will returns 11
if stopPos is beyond the length of Genotype, ad example startPos 9 and stopPos 15 then the bits 1010 will be decoded and this methods will return 10.

Definition at line 325 of file genotype.cpp.

References Genotype::bit(), and Genotype::sizev.

Referenced by SignedRangeGenotype::at().

double fitness ( ) const

Return the fitness of this Genotype.

Note
it is the same of objective(0);

Definition at line 173 of file genotype.cpp.

References Genotype::objective().

Referenced by StefanoSteadyStateGA::gaStep().

bool fromCompressedString ( QString  str)

Read from compressed data with toCompressedString()

Returns
true on successfull decompression, false otherwise

Definition at line 308 of file genotype.cpp.

References Genotype::sizev, and Genotype::unset().

Referenced by RealGenotype::configure(), SignedRangeGenotype::configure(), and Genotype::configure().

void fromString ( QString  str)

Configure the bits accordlying to its string representation passed.

Definition at line 287 of file genotype.cpp.

References Genotype::sizev, and Genotype::unset().

unsigned int geneToBitIndex ( unsigned int  gene) const
virtual

Convert the index of the given gene to the index of the start of the bit(s) that encode this gene.

Reimplemented in DoubleGenotype.

Definition at line 355 of file genotype.cpp.

int hammingDistance ( const Genotype other)

Calculate the Hamming distance from the Genotype.

Definition at line 260 of file genotype.cpp.

References Genotype::bit(), and Genotype::sizev.

void insertUInt ( unsigned int  value,
unsigned int  startPos,
unsigned int  stopPos 
)

Utility function for settings an integer value into Genotype.

Parameters
valueis the value to be converted and inserted into genotype
startPosis the index of starting position of the bits to decode
stopPosis the index of end position of the bits to decode The parameters has the same meanings of extractUInt, and the behaviour is exactly the opposite.

Definition at line 340 of file genotype.cpp.

References Genotype::sizev, and Genotype::unset().

Referenced by SignedRangeGenotype::set().

QString notes ( ) const

Return the notes of this Genotype.

Definition at line 252 of file genotype.cpp.

References Genotype::notesv.

Referenced by Genotype::setNotes().

int numOfObjectives ( ) const

Return the number of Objectives setted The number of objectives is automatically configured by setObjective method.

Definition at line 186 of file genotype.cpp.

References Genotype::fitnessv.

Referenced by GeneticAlgo::updateStats().

double objective ( int  i) const

Return the value of the i-th objective of the fitness.

Note
fitness() return the same value of objective(0)

Definition at line 182 of file genotype.cpp.

References Genotype::fitnessv.

Referenced by Genotype::fitness(), Genotype::nObjectiveGreaterThanComparator::operator()(), and GeneticAlgo::updateStats().

Genotype & operator= ( const Genotype genotype)
virtual

Assign operator (performs same operation of Copy-constructor)
Due to virtual attribute, Subclasses can customize the behaviour of this operator=, for an easy way to copy Genotype data the protected method copyDataFrom is provided and it behaves like this implementation of operator=.

Reimplemented in SignedRangeGenotype, and RealGenotype.

Definition at line 87 of file genotype.cpp.

References Genotype::allocated, Genotype::data, Genotype::fitnessv, Genotype::notesv, Genotype::rankv, and Genotype::sizev.

void randomize ( )
virtual

Randomize the value contained (attention, this method destroys previous data)

Reimplemented in RealGenotype.

Definition at line 268 of file genotype.cpp.

References globalRNG, Genotype::sizev, and Genotype::unset().

double rank ( ) const

Return the rank of this Genotype.

Definition at line 209 of file genotype.cpp.

References Genotype::rankv.

Referenced by StefanoSteadyStateGA::gaStep(), Genotype::rankGreaterThanComparator(), and RouletteWheelSelection::setGenome().

static bool rankGreaterThanComparator ( const Genotype g1,
const Genotype g2 
)
inlinestatic

Utility function comparing two genotype pointers on their rank.

Definition at line 182 of file genotype.h.

References Genotype::rank().

Referenced by StefanoSteadyStateGA::finalize(), SimpleGA::gaStep(), ParallelGA::gaStep(), and LaralGA::gaStep().

void resize ( unsigned int  newsize)
protected

Resize the Genotype.

Parameters
newsizeis the number of bits after resizing
Warning
if the new dimension is shorter than the old one, some data will be discarded
The data pointer will be changed after this method execution

Definition at line 157 of file genotype.cpp.

References Genotype::allocated, Genotype::data, and Genotype::sizev.

Referenced by RealGenotype::configure(), SignedRangeGenotype::configure(), and Genotype::configure().

void save ( ConfigurationParameters params,
QString  prefix 
)
virtual

Save the actual status of parameters into the ConfigurationParameters object passed.

Parameters
paramsthe configuration parameters object on which save actual parameters
prefixthe prefix to use to access the object configuration parameters.

Implements ParameterSettableWithConfigureFunction.

Reimplemented in RealGenotype, and SignedRangeGenotype.

Definition at line 123 of file genotype.cpp.

References ConfigurationParameters::createParameter(), Genotype::fitnessv, Genotype::notesv, Genotype::rankv, Genotype::sizev, and Genotype::toCompressedString().

Referenced by Genome::save().

void set ( unsigned int  i)

Set to One the i-th bit.

Definition at line 222 of file genotype.cpp.

References Genotype::data.

Referenced by RandomBit::mutate(), and SignedRangeGenotype::set().

void setFitness ( double  value)

Set the fitness value of this Genotype.

Note
it is the same of setObjective( 0, value );

Definition at line 169 of file genotype.cpp.

References Genotype::setObjective().

void setNotes ( QString  notes)

Set the notes of this Genotype.

Definition at line 256 of file genotype.cpp.

References Genotype::notes(), and Genotype::notesv.

void setObjective ( int  i,
double  value 
)

Set the i-th objective value of the (multi-objective) fitness.

Note
multi-objective fitness are used only with the GeneticAlgo that support them
setFitness( aValue ) correspond to setObjectiveValue( 0, value );
in a single objective fitness, the setObjectiveValue could be used to print out some intermediate values used for calculating the total fitness

Definition at line 177 of file genotype.cpp.

References Genotype::fitnessv.

Referenced by Genotype::setFitness().

void setRank ( double  rank)

Set the rank of the genotype.

Note
the rank is a general single value used to order the genotypes inside the genome; it is different from fitness and it is GA-dependent. In simple cases, like SimpleGA, where the GA is driven by only one objective, then tipically the rank correspond to the fitness. But in multi-objective GAs (MOGA), like NSGA2, the rank depends on how the MOGA orders the genotypes
better genotype will have higher rank value

Definition at line 205 of file genotype.cpp.

References Genotype::rankv.

Referenced by StefanoSteadyStateGA::gaStep().

QString toCompressedString ( ) const

Compress the data; It return a printable String.

Definition at line 298 of file genotype.cpp.

References Genotype::bit(), and Genotype::sizev.

Referenced by RealGenotype::save(), SignedRangeGenotype::save(), and Genotype::save().

void toggle ( unsigned int  i)

toggle the i-th bit

Definition at line 242 of file genotype.cpp.

References Genotype::data.

Referenced by OnePoint::crossover(), and FlipBit::mutate().

QString toString ( ) const

Return a QString representing this Genotype.

Definition at line 278 of file genotype.cpp.

References Genotype::bit(), and Genotype::sizev.

void unset ( unsigned int  i)

Member Data Documentation

unsigned int allocated
protected

Allocated memory.

Definition at line 203 of file genotype.h.

Referenced by Genotype::copyDataFrom(), Genotype::Genotype(), Genotype::operator=(), and Genotype::resize().


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