src/GeneticCode.cpp

Go to the documentation of this file.
00001 
00008 /*
00009 This file is part of Teapot Colony Wars.
00010 
00011 Teapot Colony Wars is free software: you can redistribute it and/or modify
00012 it under the terms of the GNU General Public License as published by
00013 the Free Software Foundation, either version 2 of the License, or
00014 (at your option) any later version.
00015 
00016 Teapot Colony Wars is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 GNU General Public License for more details.
00020 
00021 You should have received a copy of the GNU General Public License
00022 along with Teapot Colony Wars.  If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 
00025 #include "GeneticCode.h"
00026 
00027 #include <iostream>
00028 
00029 using namespace std;
00030 
00031 #include "Random.h"
00032 #include "Constants.h"
00033 
00034 GeneticCode::GeneticCode() {
00035   //Zero initialization
00036   _code.value = 0;
00037 
00038   //For all bits
00039   for(int n = 0; n < NB_BITS_GENETICCODE; n++){
00040 
00041     //If the randomized number is odd, we put a 1 instead of the initial 0
00042     if(Random::value() & 1){
00043       _code.value |= (1 << n);
00044     }
00045   }
00046 
00047   if(_code.bitfield.size > MAX_WORSHIPER_SIZE)
00048     _code.bitfield.size = MAX_WORSHIPER_SIZE;
00049 }
00050 
00051 
00052 GeneticCode::GeneticCode(GeneticCode const * c) : _code(c->_code) {}
00053 
00054 
00055 GeneticCode::~GeneticCode() {}
00056 
00057 
00058 GeneticCode * GeneticCode::mutate() const{
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 }
00073 
00074 
00075 GeneticCode * GeneticCode::crossover(GeneticCode * c) const{
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 }
00096 
00097 
00098 void GeneticCode::print(){
00099   int bit;
00100   cout << "######### GeneticCode::print() ##########" << endl;
00101   for(int n = 0; n < NB_BITS_GENETICCODE; n++){
00102     bit = ((_code.value << n) >> (NB_BITS_GENETICCODE - 1)) & 1;
00103     cout << bit << " ";
00104   }
00105   cout << "= " << _code.value <<  endl;
00106   cout << "######### end GeneticCode::print() ##########" << endl;
00107 }

Generated on Sat Feb 2 22:22:54 2008 for Teapot Colony Wars by  doxygen 1.5.4