- Main Page
- Classes
- Files

- Search for |

# moeoEnvironmentalReplacement.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // moeoEnvironmentalReplacement.h
    00005 // (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
    00006 /*
    00007     This library...
    00008
    00009     Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
    00010  */
    00011 //-----------------------------------------------------------------------------
    00012
    00013 #ifndef MOEOENVIRONMENTALREPLACEMENT_H_
    00014 #define MOEOENVIRONMENTALREPLACEMENT_H_
    00015
    00016 #include <comparator/moeoComparator.h>
    00017 #include <comparator/moeoFitnessThenDiversityComparator.h>
    00018 #include <diversity/moeoDiversityAssignment.h>
    00019 #include <fitness/moeoFitnessAssignment.h>
    00020 #include <replacement/moeoReplacement.h>
    00021
    00026 template < class MOEOT > class moeoEnvironmentalReplacement:public moeoReplacement < MOEOT >
    00027 {
    00028 public:
    00029
    00031     typedef typename MOEOT::ObjectiveVector ObjectiveVector;
    00032
    00033
    00040     moeoEnvironmentalReplacement (moeoFitnessAssignment < MOEOT > & _fitnessAssignment, moeoDiversityAssignment < MOEOT > & _diversityAssignment, moeoComparator < MOEOT > & _comparator) :
    00041             fitnessAssignment (_fitnessAssignment), diversityAssignment (_diversityAssignment), comparator (_comparator)
    00042     {}
    00043
    00044
    00050     moeoEnvironmentalReplacement (moeoFitnessAssignment < MOEOT > & _fitnessAssignment, moeoDiversityAssignment < MOEOT > & _diversityAssignment) :
    00051             fitnessAssignment (_fitnessAssignment), diversityAssignment (_diversityAssignment), comparator (defaultComparator)
    00052     {}
    00053
    00054
    00060     moeoEnvironmentalReplacement (moeoFitnessAssignment < MOEOT > & _fitnessAssignment, moeoComparator < MOEOT > & _comparator) :
    00061             fitnessAssignment (_fitnessAssignment), diversityAssignment (defaultDiversity), comparator (_comparator)
    00062     {}
    00063
    00064
    00070     moeoEnvironmentalReplacement (moeoFitnessAssignment < MOEOT > & _fitnessAssignment) :
    00071             fitnessAssignment (_fitnessAssignment), diversityAssignment (defaultDiversity), comparator (defaultComparator)
    00072     {}
    00073
    00074
    00080     void operator () (eoPop < MOEOT > &_parents, eoPop < MOEOT > &_offspring)
    00081     {
    00082         unsigned int sz = _parents.size();
    00083         // merges offspring and parents into a global population
    00084         _parents.reserve (_parents.size() + _offspring.size());
    00085         std::copy (_offspring.begin(), _offspring.end(), back_inserter(_parents));
    00086         // evaluates the fitness and the diversity of this global population
    00087         fitnessAssignment (_parents);
    00088         diversityAssignment (_parents);
    00089         // remove individuals 1 by 1 and update the fitness values
    00090         unsigned int worstIdx;
    00091         ObjectiveVector worstObjVec;
    00092         while (_parents.size() > sz)
    00093         {
    00094             // the individual to delete
    00095             worstIdx = std::min_element(_parents.begin(), _parents.end(), comparator) - _parents.begin();
    00096             worstObjVec = _parents[worstIdx].objectiveVector();
    00097             // remove the woorst individual
    00098             _parents[worstIdx] = _parents.back();
    00099             _parents.pop_back();
    00100             // update of the fitness and diversity values
    00101             fitnessAssignment.updateByDeleting(_parents, worstObjVec);
    00102             diversityAssignment.updateByDeleting(_parents, worstObjVec);
    00103
    00104         }
    00105         // clear the offspring population
    00106         _offspring.clear ();
    00107     }
    00108
    00109
    00110 protected:
    00111
    00113     moeoFitnessAssignment < MOEOT > & fitnessAssignment;
    00115     moeoDiversityAssignment < MOEOT > & diversityAssignment;
    00117     moeoDummyDiversityAssignment < MOEOT > defaultDiversity;
    00119     moeoFitnessThenDiversityComparator < MOEOT > defaultComparator;
    00121     class Cmp
    00122     {
    00123     public:
    00128         Cmp(moeoComparator < MOEOT > & _comp) : comp(_comp)
    00129         {}
    00135         bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2)
    00136         {
    00137             return comp(_moeo1,_moeo2);
    00138         }
    00139     private:
    00141         moeoComparator < MOEOT > & comp;
    00142     } comparator;
    00143
    00144 };
    00145
    00146 #endif /*MOEOENVIRONMENTALREPLACEMENT_H_ */