00001 #include <iostream>
00002 #include "NLstl_utils.h"
00003
00004 #include "GLBaseRanker.h"
00005 #include "GLGeneticAlgorithmStandard.h"
00006 #include "GLGaStatusStandard.h"
00007 #include "GLPopulationStandard.h"
00008 #include "GLUtils.h"
00009 using namespace std;
00010
00016 GLGeneticAlgorithmStandard::GLGeneticAlgorithmStandard(
00017 const GLParametersGeneticAlgorithm& parameters, GLFactory* factory):
00018 GLBaseGeneticAlgorithm(parameters, factory)
00019 {
00020 m_status = new GLGaStatusStandard();
00021 }
00022
00023
00027 GLGeneticAlgorithmStandard::~GLGeneticAlgorithmStandard()
00028 {
00029 if (m_status != NULL) delete m_status;
00030 }
00031
00039 void GLGeneticAlgorithmStandard::reset()
00040 {
00041 GLBaseGeneticAlgorithm::reset();
00042 delete m_status;
00043 m_status = new GLGaStatusStandard();
00044 }
00045
00061 const GLBaseGaStatus* GLGeneticAlgorithmStandard::runGA(
00062 GLBaseOrganismInitialiser *initialiser,
00063 GLBaseEvaluator* evaluator,
00064 const TListOfStoppers& stoppers,
00065 bool bool_operator)
00066 {
00067 m_status->m_timer.reset();
00068
00069 #if VERBOSE > 1
00070 cout << "Init population" << endl;
00071 #endif
00072
00073 nlutils::assertPointer(evaluator,
00074 "\nGLGeneticAlgorithmStandard::runGA\n"
00075 "evaluator cannot be NULL pointer");
00076 m_evaluator = evaluator;
00077 int num_of_objectives = m_evaluator->getNumberOfObjectives();
00078 m_status->m_current_state = GLConstants::GA_STATE_RUNNING;
00079
00080
00081 int evaluator_initial_counter = m_evaluator->getCounter();
00082 GLBasePopulation *population = new GLPopulationStandard();
00083 population->populate(getParameters()->m_initial_population_size,
00084 initialiser, GLConstants::NO_TWINS);
00085
00086 #if DEBUG > 2
00087 cout << "Initial population" << endl;
00088 population->printRankedPopulation(cout, evaluator, getRanker());
00089 #endif
00090
00091 m_status->m_current_generation = 0;
00092 m_status->m_generations_since_population_changed = 0;
00093 m_status->m_generations_since_new_best_found = 0;
00094 m_status->m_generations_since_solution_improvement = 0;
00095
00096 GLBaseFitness* current_fitness(NULL);
00097
00098
00099
00100
00101
00102
00103 #if VERBOSE==1
00104 cout << "Generations\n";
00105 #endif
00106 while (!toFinish(stoppers, bool_operator))
00107 {
00108 if (population->createNewGeneration(
00109 getBreeder(),
00110 getVariator(),
00111 evaluator,
00112 getSelectionist(),
00113 m_status->m_best_organisms_in_population,
00114 getParameters()->m_children_per_generation,
00115 getParameters()->m_mutants_per_generation,
00116 getParameters()->m_mutation_policy) == 0)
00117 {
00118 ++m_status->m_generations_since_population_changed;
00119 }
00120 else
00121 {
00122 m_status->m_generations_since_population_changed = 0;
00123 }
00124 #if VERBOSE == 1
00125 cout << m_status->m_current_generation << "; ";
00126 if (m_status->m_current_generation % 15 == 14) cout << endl;
00127 #endif
00128 #if VERBOSE > 1
00129 cout << "Generation " << m_status->m_current_generation << endl;
00130 #endif
00131 #if DEBUG > 4
00132 population->printRankedPopulation(cout, evaluator,
00133 getRanker());
00134 cout << endl;
00135 #endif
00136
00137 ++m_status->m_current_generation;
00138
00139 if (getSelectionist()->getRanker()->updateFittest(
00140 m_status->m_best_organisms_in_population,
00141 m_status->m_best_ever_organisms))
00142 {
00143 m_status->m_generations_since_new_best_found = 0;
00144 #if VERBOSE > 1
00145 cout << "Best elements (total "
00146 << m_status->m_best_ever_organisms.size() << ")\n";
00147 GLUtils::printVectorOfOrganisms(cout,
00148 m_status->m_best_ever_organisms);
00149 #endif
00150 }
00151 else
00152 {
00153 ++m_status->m_generations_since_new_best_found;
00154 }
00155 if (num_of_objectives > 1)
00156 {
00157
00158 m_status->m_generations_since_solution_improvement =
00159 m_status->m_generations_since_new_best_found;
00160 }
00161 else
00162 {
00163
00164
00165 if ((current_fitness == NULL) ||
00166 (m_status->m_best_ever_organisms.front()
00167 ->getFitness()->isLess(current_fitness)))
00168 {
00169 current_fitness = m_status->m_best_ever_organisms.front()
00170 ->getFitness()->copy();
00171 m_status->m_generations_since_solution_improvement = 0;
00172 }
00173 else
00174 {
00175 ++m_status->m_generations_since_solution_improvement;
00176 }
00177 }
00178 m_status->m_evaluator_calls = m_evaluator->getCounter() -
00179 evaluator_initial_counter;
00180 }
00181 #if VERBOSE==1
00182 cout << "\n";
00183 #endif
00184
00185
00186
00187
00188
00189 if (getParameters()->m_keep_last_generation)
00190 {
00191 population->getOrganismsVector(m_status->m_last_generation,
00192 GLConstants::REPLACE, -1, true);
00193 getRanker()->rankOrganisms(m_status->m_last_generation);
00194 }
00195 else
00196 {
00197 m_status->m_last_generation.clear();
00198 }
00199 if (current_fitness != NULL)
00200 {
00201 delete current_fitness;
00202 }
00203 delete population;
00204 m_status->m_current_state = GLConstants::GA_STATE_STOPPED_NORMALLY;
00205 m_status->m_time_required = m_status->m_timer.getTimeSec();
00206 #if VERBOSE > 2
00207 cout << endl << *m_status;
00208 #endif
00209 return m_status;
00210 }