#include <GeneticCode.h>
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. | |
GeneticCode * | mutate () const |
Mutate the code in order to produce another code. | |
GeneticCode * | crossover (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... |
Definition at line 45 of file GeneticCode.h.
GeneticCode::GeneticCode | ( | GeneticCode const * | c | ) |
Copy constructor.
c | genetic code to copy |
Definition at line 52 of file GeneticCode.cpp.
00052 : _code(c->_code) {}
unsigned int GeneticCode::getSize | ( | ) | [inline] |
Get the size.
Definition at line 106 of file GeneticCode.h.
References _code, GeneticCode::__code::bitfield, and GeneticCode::__code::_bitfield::size.
Referenced by GeneticReplication::createWorshiper().
unsigned int GeneticCode::getAggressiveness | ( | ) | [inline] |
Get the 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; }
unsigned int GeneticCode::getGreedy | ( | ) | [inline] |
Get the greedy.
Definition at line 120 of file GeneticCode.h.
References _code, GeneticCode::__code::bitfield, and GeneticCode::__code::_bitfield::greedy.
Referenced by GeneticBehaviour::think().
unsigned int GeneticCode::getReasonable | ( | ) | [inline] |
Get the 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; }
GeneticCode * GeneticCode::mutate | ( | ) | const |
Mutate the code in order to produce another 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 }
GeneticCode * GeneticCode::crossover | ( | GeneticCode * | c | ) | const |
Cross the current code with a second one to create a third one.
c | the second code with wich the current code will be cross |
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 }