00001 #include <NLstl_utils.h>
00002
00003 #include "GLCrossoverTwoPoints.h"
00004 #include "GLInterfaceSwapSection.h"
00005 #include "GLLogicError.h"
00006 #include "GLRandomNumbersGenerator.h"
00007
00016 void GLCrossoverTwoPoints::makeOffsprings(
00017 const vector<GLBaseOrganism*>& parents,
00018 vector<GLBaseOrganism*>& children)
00019 {
00020
00021 if (parents.size() != 2)
00022 {
00023 char errormsg[1000];
00024 sprintf(errormsg,
00025 "\nGLCrossoverTwoPoints<T>::makeOffspring\n"
00026 "Can accept 2 parents only, while vector of %i parents "
00027 "was passed\n",
00028 (int)parents.size());
00029 throw GLLogicError(errormsg);
00030 }
00031
00032 int min_size = min(parents[0]->getSize(), parents[1]->getSize());
00033 int leftPos = GLRandomNumbersGenerator::getRandomInteger(min_size - 1);
00034 int rightPos =
00035 GLRandomNumbersGenerator::getRandomInteger(leftPos + 1, min_size);
00036
00037
00038 children.clear();
00039 children[0] = parents[0]->makeClone();
00040 children[1] = parents[1]->makeClone();
00041
00042
00043 GLInterfaceSwapSection *swapper =
00044 nlutils::transform_pointer<GLInterfaceSwapSection,
00045 GLBaseOrganism>(children[0],
00046 "GLCrossoverOnePoint::makeOffspring\n"
00047 "first parent does not implement the "
00048 "InterfaceSwapSection\n");
00049 swapper->swapSections(children[1], leftPos, rightPos);
00050 }