GeneticCode Class Reference

#include <GeneticCode.h>

Collaboration diagram for GeneticCode:

Collaboration graph
[legend]

Public Member Functions

 GeneticCode ()
 Constructor which initializes all fields with randomized values.
 GeneticCode (GeneticCode const *c)
 Copy constructor.
virtual ~GeneticCode ()
 Destructor.
unsigned int getSize ()
 Get the size.
unsigned int getAggressiveness ()
 Get the aggressiveness.
unsigned int getGreedy ()
 Get the greedy.
unsigned int getReasonable ()
 Get the reasonable.
GeneticCodemutate () const
 Mutate the code in order to produce another code.
GeneticCodecrossover (GeneticCode *c) const
 Cross the current code with a second one to create a third one.
void print ()
 Debug function, print the genetic code.

Private Attributes

union GeneticCode::__code _code
 The genetic code.

Data Structures

union  __code
 The genetic code. More...

Detailed Description

< Genetic code definition

Definition at line 45 of file GeneticCode.h.


Constructor & Destructor Documentation

GeneticCode::GeneticCode ( GeneticCode const *  c  ) 

Copy constructor.

Parameters:
c genetic code to copy

Definition at line 52 of file GeneticCode.cpp.

00052 : _code(c->_code) {}


Member Function Documentation

unsigned int GeneticCode::getSize (  )  [inline]

Get the size.

Returns:
size

Definition at line 106 of file GeneticCode.h.

References _code, GeneticCode::__code::bitfield, and GeneticCode::__code::_bitfield::size.

Referenced by GeneticReplication::createWorshiper().

00106 { return _code.bitfield.size; }

Here is the caller graph for this function:

unsigned int GeneticCode::getAggressiveness (  )  [inline]

Get the aggressiveness.

Returns:
aggressiveness

Definition at line 113 of file GeneticCode.h.

References _code, GeneticCode::__code::_bitfield::aggressiveness, and GeneticCode::__code::bitfield.

Referenced by GeneticBehaviour::think().

00113 { return _code.bitfield.aggressiveness; }

Here is the caller graph for this function:

unsigned int GeneticCode::getGreedy (  )  [inline]

Get the greedy.

Returns:
greedy

Definition at line 120 of file GeneticCode.h.

References _code, GeneticCode::__code::bitfield, and GeneticCode::__code::_bitfield::greedy.

Referenced by GeneticBehaviour::think().

00120 { return _code.bitfield.greedy; }

Here is the caller graph for this function:

unsigned int GeneticCode::getReasonable (  )  [inline]

Get the reasonable.

Returns:
reasonable

Definition at line 128 of file GeneticCode.h.

References _code, GeneticCode::__code::bitfield, and GeneticCode::__code::_bitfield::reasonable.

Referenced by GeneticBehaviour::think().

00128 { return _code.bitfield.reasonable; }

Here is the caller graph for this function:

GeneticCode * GeneticCode::mutate (  )  const

Mutate the code in order to produce another code.

Returns:
a new genetic modified code

Definition at line 58 of file GeneticCode.cpp.

References _code, GeneticCode(), NB_BITS_GENETICCODE, GeneticCode::__code::value, and Random::value().

Referenced by GeneticReplication::getNewWorshipers().

00058                                        {
00059   GeneticCode * new_code = new GeneticCode(this);
00060 
00061   //Generate a number between 0 and the number of bits of the structure
00062   int n = Random::value() % NB_BITS_GENETICCODE;
00063 
00064   //Flip the nth bit of the code
00065   if((_code.value >> n) & 1){
00066     new_code->_code.value &= ~(1 << n);//1 -> 0
00067   } else {
00068     new_code->_code.value |= (1 << n);//0 -> 1
00069   }
00070 
00071   return new_code;
00072 }

Here is the call graph for this function:

Here is the caller graph for this function:

GeneticCode * GeneticCode::crossover ( GeneticCode c  )  const

Cross the current code with a second one to create a third one.

Parameters:
c the second code with wich the current code will be cross
Returns:
a new genetic modified code

Definition at line 75 of file GeneticCode.cpp.

References _code, GeneticCode(), NB_BITS_GENETICCODE, GeneticCode::__code::value, and Random::value().

00075                                                          {
00076   //Copy the current code into the new one
00077   GeneticCode * new_code = new GeneticCode(this);
00078 
00079   //For each bit of the new code, either we keep it, either we take the
00080   //corresponding bit of c (crossover operation)
00081   for(int n = 0; n < NB_BITS_GENETICCODE; n++){
00082 
00083     //Generate a number. If it is odd, we take the corresponding bit of c : c[n]
00084     if(Random::value() & 1){
00085       if((c->_code.value >> n) & 1){ //If c[n] == 1, then new_code[c] = 1
00086         new_code->_code.value |= (1 << n);
00087       } else { //If c[n] == 0, then new_code[c] = 0
00088         new_code->_code.value &= ~(1 << n);
00089       }
00090     } //else, we keep that bit
00091 
00092   }
00093 
00094   return new_code;
00095 }

Here is the call graph for this function:


The documentation for this class was generated from the following files:
Generated on Sat Feb 2 22:23:14 2008 for Teapot Colony Wars by  doxygen 1.5.4