00001 #include <iostream>
00002 #include <vector>
00003
00004 #include <NLstl_utils.h>
00005
00006 #include "GLBaseCrossover.h"
00007 #include "GLBaseEvaluator.h"
00008 #include "GLBaseGeneticAlgorithm.h"
00009 #include "GLBaseOrganism.h"
00010 #include "GLBaseOrganismStorage.h"
00011 #include "GLBasePopulation.h"
00012 #include "GLBaseRanker.h"
00013 #include "GLBreederStandard.h"
00014 #include "GLLogicError.h"
00015 #include "GLRandomNumbersGenerator.h"
00016
00017
00029 GLBreederStandard::GLBreederStandard(GLBaseCrossover *crossover,
00030 GLBreederStandard::TParameters* parameters,
00031 GLBaseGeneticAlgorithm *parent_ga):
00032 GLBaseBreeder(crossover)
00033 {
00034
00035 if ((parameters->t_breeding_probability < 0) ||
00036 (parameters->t_breeding_probability > 1))
00037 {
00038 char errormsg[1000];
00039 sprintf(errormsg, "\nGLBreederStandard::GLBreederStandard\n"
00040 "Parameter breeding probability (= %e) should "
00041 "be in interval [0; 1]",
00042 parameters->t_breeding_probability);
00043 throw GLLogicError(errormsg);
00044 }
00045
00046 m_breeding_probability = parameters->t_breeding_probability;
00047 nlutils::assertPointer(parent_ga,
00048 "GLBreederStandard::GLBreederStandard, parent_ga");
00049 m_parent_ga = parent_ga;
00050 }
00051
00052 GLBreederStandard::~GLBreederStandard()
00053 {
00054 }
00055
00056
00073 int GLBreederStandard::breedOrganisms(GLBasePopulation* population,
00074 GLBaseOrganismStorage* potential_parents,
00075 GLBaseOrganismStorage* offsprings,
00076 GLConstants::TStorageFilling fill,
00077 GLConstants::TTwinsFlags twins,
00078 int size_limits)
00079 {
00080
00081 if (fill == GLConstants::REPLACE)
00082 {
00083 offsprings->emptyStorage();
00084 }
00085
00086 vector<GLBaseOrganism*> parents;
00087 potential_parents->getOrganismsVector(parents);
00088
00089 int parents_size = parents.size();
00090 for(int i = 0; i < parents_size; i++)
00091 {
00092 if (!parents[i]->isEvaluated())
00093 {
00094 parents[i]->evaluate(m_parent_ga->getEvaluator());
00095 }
00096 }
00097
00098 m_parent_ga->getRanker()->rankOrganisms(parents);
00099
00100 int initial_size = offsprings->getSize();
00101 vector<GLBaseOrganism*> children;
00102
00103 for(int i = 0; i < parents_size; i++)
00104 {
00105 vector<GLBaseOrganism*> couple(2);
00106 couple[0] = parents[i];
00107 for(int j = i + 1; j < parents_size; j++)
00108 {
00109
00110 if (GLRandomNumbersGenerator::getRandomUniform(0, 1)
00111 < m_breeding_probability)
00112 {
00113 couple[1] = parents[j];
00114 getCrossover()->makeOffsprings(couple, children);
00115
00116 for(size_t k = 0; k < children.size(); k++)
00117 {
00118
00119 if ((twins == GLConstants::NO_TWINS) &&
00120 !population->isOrganismTrulyNew(children[k]))
00121 {
00122 delete children[k];
00123 continue;
00124 }
00125
00126 offsprings->addOrganism(children[k]);
00127
00128 if ((size_limits > 0) &&
00129 (offsprings->getSize() - initial_size) == size_limits)
00130 {
00131
00132 for_each(children.begin() + k + 1, children.end(),
00133 nlutils::DeleteObjectSafely());
00134 return size_limits;
00135 }
00136 }
00137 }
00138 }
00139 }
00140 return offsprings->getSize() - initial_size;
00141 }