00001 #include "GLBaseOrganism.h"
00002 #include "GLBaseRanker.h"
00003 #include "GLBaseSelectionist.h"
00004 #include "GLLogicError.h"
00005 #include "GLRuntimeError.h"
00013 GLBaseSelectionist::GLBaseSelectionist(GLBaseRanker* ranker)
00014 {
00015 if (ranker == NULL)
00016 {
00017 char errormsg[] = "\nGLBaseSelectionist::GLBaseSelectionist\n"
00018 "Null argument is passed as a parameter 'ranker'\n";
00019 throw GLRuntimeError(errormsg);
00020 }
00021 m_ranker = ranker;
00022 }
00023
00027 GLBaseSelectionist::~GLBaseSelectionist()
00028 {
00029 if (m_ranker != NULL) delete m_ranker;
00030 }
00031
00042 void GLBaseSelectionist::assertNewSize(int new_size,
00043 const vector<GLBaseOrganism*>& current_population,
00044 const vector<GLBaseOrganism*>& candidates)
00045 {
00046 if ((new_size < 0) ||
00047 ((size_t)new_size > current_population.size() + candidates.size()))
00048 {
00049 char errormsg[1000];
00050 sprintf(errormsg, "\nGLBaseSelectionist::assertNewSize\n"
00051 "Passed parameter new_size (%i) is either < 0 or "
00052 "exceeds the sum of elements in current generation"
00053 " and candidates (%i and %i accordingly)\n",
00054 (int)new_size, (int)current_population.size(),
00055 (int)candidates.size());
00056 throw GLLogicError(errormsg);
00057 }
00058 }
00059
00074 int GLBaseSelectionist::countSurvivedCandidates(
00075 const vector<GLBaseOrganism*>& to_live,
00076 const vector<GLBaseOrganism*>& to_die,
00077 const vector<GLBaseOrganism*>& current_population,
00078 const vector<GLBaseOrganism*>& candidates)
00079 {
00080
00081 for(size_t i = 0; i < to_live.size(); i++)
00082 {
00083 to_live[i]->setTag(1);
00084 }
00085 for(size_t i = 0; i < to_die.size(); i++)
00086 {
00087 to_die[i]->setTag(0);
00088 }
00089
00090 int counter(0);
00091 for(size_t i = 0; i < candidates.size(); i++)
00092 {
00093 counter += candidates[i]->getTag();
00094 }
00095 return counter;
00096 }
00097
00102 void GLBaseSelectionist::reset()
00103 {
00104 getRanker()->reset();
00105 }