Generic GA instance. More...

#include <rinst.h>

Inheritance diagram for RInst< cInst, cChromo, cFit, cThreadData >:
[legend]
Collaboration diagram for RInst< cInst, cChromo, cFit, cThreadData >:
[legend]

Public Member Functions

 RInst (size_t popsize, const RString &name, RDebug *debug=0)
 
virtual RCString GetClassName (void) const
 
void SetMutationParams (size_t agemut, size_t agebestmut, size_t nbmut)
 
void SetVerify (bool verify)
 
bool GetVerify (void) const
 
bool MustOptimisation (void) const
 
void SetOptimisation (bool opti)
 
cChromo * GetBestChromosome (void) const
 
void SetSeed (int seed)
 
int GetSeed (void) const
 
virtual void Init (void)
 
virtual void AnalyzePop (void)
 
virtual void PostEvaluate (void)
 
virtual void RandomConstruct (void)
 
virtual bool StopCondition (void)=0
 
virtual void PostRun (void)
 
virtual void DisplayInfos (void)
 
virtual void Crossover (void)
 
virtual void Mutation (void)
 
virtual void StrongMutation (void)
 
virtual void Inversion (void)
 
void Generation (void)
 
virtual void Run (void)
 
virtual void Verify (void)
 
virtual void HandlerNotFound (const RNotification &notification)
 
long RRand (long max)
 
template<class T >
void RandOrder (T *array, size_t size)
 
virtual ~RInst (void)
 
- Public Member Functions inherited from RObject
 RObject (const RString &name=RString::Null)
 
int Compare (const RObject &obj) const
 
RString GetName (void) const
 
void PostNotification (const hNotification handle)
 
void PostNotification (const RCString &name)
 
void PostNotification (const hNotification handle, T data)
 
void PostNotification (const RCString &name, T data)
 
void InsertObserver (tNotificationHandler handler, const hNotification handle, RObject *object)
 
void InsertObserver (tNotificationHandler handler, const RCString &name, RObject *object)
 
void InsertObserver (tNotificationHandler handler, const hNotification handle)
 
void InsertObserver (tNotificationHandler handler, const RCString &name)
 
void InsertObserver (tNotificationHandler handler, RObject *object)
 
void InsertObserver (tNotificationHandler handler)
 
void DeleteObserver (void)
 
void DeleteObserver (const hNotification handle, RObject *object)
 
void DeleteObserver (const RCString &name, RObject *object)
 
hNotification GetNotificationHandle (const RCString &name) const
 
RCString GetNotificationName (const hNotification handle) const
 
virtual ~RObject (void)
 
- Public Member Functions inherited from iRInst
size_t GetPopSize (void) const
 
size_t GetGen (void) const
 
size_t GetAgeBest (void) const
 
size_t GetAgeBestPop (void) const
 
virtual ~iRInst (void)
 

Static Public Member Functions

static int sort_function_cChromosome (const void *a, const void *b)
 

Public Attributes

RDebugDebug
 
RRandom Random
 
cChromo ** Chromosomes
 
cChromo ** Ranked
 
cChromo * BestChromosome
 
cChromo * BestInPop
 

Static Public Attributes

static const char instMaster =0
 
static const char instSlave =1
 
static const char instMultiProcess =2
 
static const char instMultiComputers =4
 
static const char instMultiEchos =8
 

Protected Attributes

cThreadData ** thDatas
 
bool bRandomConstruct
 
bool VerifyGA
 
bool DoPostEvaluation
 
bool DoOptimisation
 
- Protected Attributes inherited from RObject
RString Name
 
- Protected Attributes inherited from iRInst
size_t PopSize
 
size_t Gen
 
size_t AgeBest
 
size_t AgeBestPop
 
bool Pause
 

Private Attributes

cChromo ** tmpChrom1
 
cChromo ** tmpChrom2
 
size_t NbCross
 
size_t FreqMutation
 
size_t FreqBestMutation
 
size_t NbMutations
 
size_t FreqInversion
 
size_t AgeNextMutation
 
size_t AgeNextBestMutation
 
size_t AgeNextInversion
 

Additional Inherited Members

- Protected Member Functions inherited from iRInst
 iRInst (size_t popsize)
 

Detailed Description

template<class cInst, class cChromo, class cFit, class cThreadData>
class R::RInst< cInst, cChromo, cFit, cThreadData >

Generic GA instance.

Instance for the generic GA.

The instance emit several signals:

  • PostNotification("RInst::Interact") : Possible interactions from the outside.
  • PostNotification("RInst::Best",size_t) : The best chromosome has changed and the generation number is given as parameter.
  • PostNotification("RInst::Generation",size_t) : A new generation has be done which number is passed as parameter.

Constructor & Destructor Documentation

RInst ( size_t  popsize,
const RString name,
RDebug debug = 0 
)

Construct the instance.

Parameters
popsizeThe size of the population.
nameName of the genetic algorithm.
debugDebugger.
virtual ~RInst ( void  )
virtual

Destruct the instance.

Member Function Documentation

virtual RCString GetClassName ( void  ) const
virtual

Return the name of the class.

Reimplemented from RObject.

Reimplemented in RInstH< cInst, cChromo, cFit, cThreadData, cNode, cObj >.

void SetMutationParams ( size_t  agemut,
size_t  agebestmut,
size_t  nbmut 
)

Set the parameters of the mutation operator.

Parameters
agemutNumber of generations between mutations.
agebestmutNumber of generations between mutations of the best chromosome.
nbmutNumber of mutations to do at each mutation operation.
void SetVerify ( bool  verify)

Set if the verifications must be done.

Parameters
verifyVerify?
bool GetVerify ( void  ) const

See if the verifications are made by the GA.

bool MustOptimisation ( void  ) const

Look if a optimization is performed.

Returns
true or false.
void SetOptimisation ( bool  opti)

Set if a optimization must be done or not. By default, the GA does optimization.

Parameters
optiPerform optimization?
cChromo* GetBestChromosome ( void  ) const
Returns
the best chromosome.
void SetSeed ( int  seed)

Set the seed value of the internal random number generator.

By default,the generator is always seeded with the same value, which means that the generated sequence is always the same. The GA will therefore always produce an identical result for the same problem. This is wanted most of the time (in particular for debugging purposes).

Parameters
seedThe seeded value.
int GetSeed ( void  ) const

Return the seed value of the internal random number generator.

static int sort_function_cChromosome ( const void *  a,
const void *  b 
)
static

This function is used to make a sort of the chromosomes based on their fitness where the best chromosomes are at the top.

Parameters
aThe first chromosome.
bThe second chromosome.
Returns
a-b
virtual void AnalyzePop ( void  )
virtual

Analyze the population to find the best chromosome of the population and to verify if the best chromosome ever calculated has to replaced. When needed, the chromosomes are evaluated.

Reimplemented in RInstG< cInst, cChromo, cFit, cThreadData, cGroup, cObj >, RInstG< cInst, cChromo, RFitnessNN, cThreadData, cGroup, cObj >, and RInstG< cInst, cChromo, RFitnessSG, cThreadData, cGroup, cObj >.

virtual void PostEvaluate ( void  )
virtual

Do some post evaluation treatment. It can be used to implement a multi-criteria approach like PROMETHEE to classify the chromosomes.

Reimplemented in RInstSG< cInst, cChromo, cThreadData, cGroup, cObj >, and RInstNN< cInst, cChromo, cThreadData, cGroup, cObj >.

virtual void RandomConstruct ( void  )
virtual

Random construction of the chromosomes.

Returns
The function returns true if all chromosomes are constructed.

Reimplemented in RInstG< cInst, cChromo, cFit, cThreadData, cGroup, cObj >, RInstG< cInst, cChromo, RFitnessNN, cThreadData, cGroup, cObj >, and RInstG< cInst, cChromo, RFitnessSG, cThreadData, cGroup, cObj >.

virtual bool StopCondition ( void  )
pure virtual

This function determines if the GA must stop. It is called after each generation. This function is a pure one and has to be implement.

Returns
When the function returns true, the GA is stop.

Implemented in RInstSG< cInst, cChromo, cThreadData, cGroup, cObj >, and RInstNN< cInst, cChromo, cThreadData, cGroup, cObj >.

virtual void PostRun ( void  )
virtual
virtual void DisplayInfos ( void  )
virtual

This function can be used to display some information. It is called after each generation.

Reimplemented in RInstH< cInst, cChromo, cFit, cThreadData, cNode, cObj >.

virtual void Crossover ( void  )
virtual

This function does the crossovers for a generation. Actually, the tournament strategy is implemented.

Reimplemented in RInstG< cInst, cChromo, cFit, cThreadData, cGroup, cObj >, RInstG< cInst, cChromo, RFitnessNN, cThreadData, cGroup, cObj >, and RInstG< cInst, cChromo, RFitnessSG, cThreadData, cGroup, cObj >.

virtual void Mutation ( void  )
virtual
virtual void StrongMutation ( void  )
virtual

This function does the necessary mutation of the best solution ever computed.

virtual void Inversion ( void  )
virtual
void Generation ( void  )

This function does a generation.

virtual void Run ( void  )
virtual

This functions runs the GA.

Implements iRInst.

virtual void Verify ( void  )
virtual

This functions verifies all the chromosomes after each generation.

virtual void HandlerNotFound ( const RNotification notification)
virtual

This is the handler that is called when an object does not find any handler for a send notification. By default, it prints the name of the message and the generation numbers.

Parameters
notificationNotification.

Reimplemented from RObject.

Reimplemented in RInstSG< cInst, cChromo, cThreadData, cGroup, cObj >, and RInstNN< cInst, cChromo, cThreadData, cGroup, cObj >.

long RRand ( long  max)

Return a number in the interval [0,max[ using the current random generator.

Parameters
maxVariable used to calculate the number.
void RandOrder ( T *  array,
size_t  size 
)

Random the position of elements of a vector using the current random generator.

Parameters
arrayA pointer to the array representing the vector.
sizeThe size of the vector.

Member Data Documentation

RDebug* Debug

Pointer to a "debugger" holding information about the GA when it is running.

RRandom Random

Random number generator

cChromo** tmpChrom1
private

Temporary data for internal use.

cChromo** tmpChrom2
private

Temporary data for internal use.

cThreadData** thDatas
protected

"Thread-dependent" data of the instance.

bool bRandomConstruct
protected

This variable is true if the random construction where done.

bool VerifyGA
protected

Set if the GA must verify the chromosomes during the different steps of the run.

bool DoPostEvaluation
protected

Define if the post evaluation must be done.

bool DoOptimisation
protected

Must an optimization be performed.

cChromo** Chromosomes

The chromosomes.

cChromo** Ranked

The chromosomes ranked by fitness.

cChromo* BestChromosome

The best chromosome ever calculated.

cChromo* BestInPop

The best chromosome in the population.

size_t NbCross
private

Number of crossovers to be done in a generation.

size_t FreqMutation
private

Frequency of mutation.

size_t FreqBestMutation
private

Frequency of mutation of the best chromosome.

size_t NbMutations
private

Number of mutations to do at each mutation operation.

size_t FreqInversion
private

Frequency of inversion.

size_t AgeNextMutation
private

Number of generation left before next mutation.

size_t AgeNextBestMutation
private

Number of generation left before mutation of the best chromosome.

size_t AgeNextInversion
private

Number of generation left before next inversion.

const char instMaster =0
static

The instance is a master.

const char instSlave =1
static

The instance is a slave.

const char instMultiProcess =2
static

The instance can use multi-processors.

const char instMultiComputers =4
static

The instance can use slaves on other computers.

const char instMultiEchos =8
static

The echo of the slaves is received by the master.