00001 #include <algorithm>
00002 #include <NLstl_utils.h>
00003
00004 #include "GLBaseGaStatus.h"
00005 #include "GLBaseGeneticAlgorithm.h"
00006 #include "GLLogicError.h"
00007 #include "GLMultipleGaRunner.h"
00008 #include "GLRandomNumbersGenerator.h"
00009
00010 using namespace std;
00014 GLMultipleGaRunner::GLMultipleGaRunner()
00015 {
00016 }
00017
00021 GLMultipleGaRunner::~GLMultipleGaRunner()
00022 {
00023 }
00024
00046 void GLMultipleGaRunner::run(
00047 const vector<GLBaseGeneticAlgorithm*>&
00048 genetic_algorithms,
00049 const vector<int>& number_of_runs,
00050 GLBaseOrganismInitialiser* initialiser,
00051 GLBaseEvaluator* evaluator,
00052 const TListOfStoppers& stoppers,
00053 bool bool_operator,
00054 vector< vector<GLBaseGaStatus*> >&
00055 results,
00056 bool same_random_seed)
00057 {
00058
00059 if (genetic_algorithms.size() > number_of_runs.size())
00060 {
00061 char errormsg[1000];
00062 sprintf(errormsg, "\nGLMultipleGaRunner::Run\n"
00063 "Size of array of genetic algorithms (==%i) "
00064 "cannot exceed the size of number_of_runs "
00065 "vector(== %i)",
00066 (int)(genetic_algorithms.size()),
00067 (int)(number_of_runs.size()));
00068 throw GLLogicError(errormsg);
00069 }
00070
00071 for(size_t i = 0; i < results.size(); ++i)
00072 {
00073 for_each(results[i].begin(), results[i].end(),
00074 nlutils::DeleteObject());
00075 }
00076 results.clear();
00077 results.resize(genetic_algorithms.size());
00078
00079 long int random_seed(0);
00080
00081 for(size_t i = 0; i < genetic_algorithms.size(); ++i)
00082 {
00083
00084 if (same_random_seed && (i == 0))
00085 {
00086 random_seed = GLRandomNumbersGenerator::initGenerator();
00087 }
00088 if (same_random_seed && (i > 0))
00089 {
00090 GLRandomNumbersGenerator::initGenerator(random_seed);
00091 }
00092 for(int j = 0; j < number_of_runs[i]; ++j)
00093 {
00094 #if VERBOSE > 0
00095 cout << "\n\n---- Running batch " << i + 1 << " (out of "
00096 << genetic_algorithms.size() << "), test " << j + 1
00097 << " (out of " << number_of_runs[i] << ") ----\n";
00098 #endif
00099 genetic_algorithms[i]->reset();
00100 results[i].push_back(
00101 genetic_algorithms[i]->runGA(initialiser,
00102 evaluator, stoppers, bool_operator)->copy());
00103 }
00104 }
00105 }