00001 #include <iostream> 00002 #include <stdio.h> 00003 #include <NLstl_utils.h> 00004 00005 #include "GLBaseOrganism.h" 00006 #include "GLInterfaceMoveGene.h" 00007 #include "GLMutatorGeneMove.h" 00008 #include "GLRandomNumbersGenerator.h" 00009 00019 bool GLMutatorGeneMove::mutateOrganism(GLBaseOrganism* organism) 00020 { 00021 //Get the reference to the gene-movable interface. 00022 GLInterfaceMoveGene* m_organism = 00023 nlutils::transform_pointer<GLInterfaceMoveGene, 00024 GLBaseOrganism>(organism, 00025 "GLMutatorGenesSwapping::mutateOrganism\n" 00026 "Passed pointer is either NULL or does not point " 00027 "to the organism with GLInterfaceMoveGene"); 00028 00029 //See if random mutation event should occur 00030 if (GLRandomNumbersGenerator::getRandomUniform(0, 1) > 00031 getMutationRate()) 00032 { 00033 return false; 00034 } 00035 //check, that size >= 2, otherwise there is nowhere to move. 00036 if (organism->getSize() < 2) 00037 { 00038 return false; 00039 } 00040 //Ok, now we need to move gene in position. 00041 //Randomly choose the start position 00042 int first_index = GLRandomNumbersGenerator:: 00043 getRandomInteger(organism->getSize()); 00044 //Then the second 00045 int second_index = GLRandomNumbersGenerator:: 00046 getRandomInteger(organism->getSize()); 00047 //To make sure, that first and second indices do not coincide 00048 while(first_index == second_index) 00049 { 00050 second_index = GLRandomNumbersGenerator:: 00051 getRandomInteger(organism->getSize()); 00052 } 00053 m_organism->moveGene(first_index, second_index); 00054 //printf("Exchanged indices %i and %i\n", first_index, second_index); 00055 return true; 00056 }