00001 #include <iostream> 00002 #include <stdio.h> 00003 #include <NLstl_utils.h> 00004 00005 #include "GLInterfaceExchangeableGenes.h" 00006 #include "GLMutatorGenesSwapping.h" 00007 #include "GLRandomNumbersGenerator.h" 00008 #include "GLRuntimeError.h" 00009 #include "gl_utils.h" 00019 bool GLMutatorGenesSwapping::mutateOrganism(GLBaseOrganism* organism) 00020 { 00021 //Get the reference to the exchangeable interface. 00022 GLInterfaceExchangeableGenes* x_organism = 00023 nlutils::transform_pointer<GLInterfaceExchangeableGenes, 00024 GLBaseOrganism>(organism, 00025 "GLMutatorGenesSwapping::mutateOrganism\n" 00026 "Passed pointer is either NULL or does not point " 00027 "to the organism with GLInterfaceExchangeableGenes"); 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 nothing to swap. 00036 if (organism->getSize() < 2) 00037 { 00038 return false; 00039 } 00040 //Ok, now we need to swap 2 elements. 00041 //Randomly choose the first one 00042 int first_index = GLRandomNumbersGenerator:: 00043 getRandomInteger(organism->getSize()); 00044 int second_index = GLRandomNumbersGenerator:: 00045 getRandomInteger(organism->getSize()); 00046 //To make sure, that first and second indices do not coincide 00047 while(first_index == second_index) 00048 { 00049 second_index = GLRandomNumbersGenerator:: 00050 getRandomInteger(organism->getSize()); 00051 } 00052 x_organism->exchangeGenes(first_index, second_index); 00053 //printf("Exchanged indices %i and %i\n", first_index, second_index); 00054 return true; 00055 }//mutateOrganism