- Main Page
- Classes
- Files

- Search for |

# moeoElitistReplacement.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // moeoElitistReplacement.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 MOEOELITISTREPLACEMENT_H_
    00014 #define MOEOELITISTREPLACEMENT_H_
    00015
    00016 #include <moeoReplacement.h>
    00017 #include <moeoComparator.h>
    00018 #include <moeoFitnessAssignment.h>
    00019 #include <moeoDiversityAssignment.h>
    00020
    00024 template < class MOEOT > class moeoElitistReplacement:public moeoReplacement < MOEOT >
    00025 {
    00026 public:
    00027
    00034     moeoElitistReplacement (moeoFitnessAssignment < MOEOT > & _evalFitness, moeoDiversityAssignment < MOEOT > & _evalDiversity, moeoComparator < MOEOT > & _comparator) :
    00035             evalFitness (_evalFitness), evalDiversity (_evalDiversity), comparator (_comparator)
    00036     {}
    00037
    00038
    00044     moeoElitistReplacement (moeoFitnessAssignment < MOEOT > & _evalFitness, moeoDiversityAssignment < MOEOT > & _evalDiversity) :
    00045             evalFitness (_evalFitness), evalDiversity (_evalDiversity), comparator (*(new moeoFitnessThenDiversityComparator < MOEOT >))
    00046     {}
    00047
    00048
    00054     moeoElitistReplacement (moeoFitnessAssignment < MOEOT > & _evalFitness, moeoComparator < MOEOT > & _comparator) :
    00055             evalFitness (_evalFitness), evalDiversity (*(new moeoDummyDiversityAssignment < MOEOT >)), comparator (_comparator)
    00056     {}
    00057
    00058
    00064     moeoElitistReplacement (moeoFitnessAssignment < MOEOT > & _evalFitness) :
    00065             evalFitness (_evalFitness), evalDiversity (*(new moeoDummyDiversityAssignment < MOEOT >)), comparator (*(new moeoFitnessThenDiversityComparator < MOEOT >))
    00066     {}
    00067
    00068
    00074     void operator () (eoPop < MOEOT > &_parents, eoPop < MOEOT > &_offspring)
    00075     {
    00076         unsigned sz = _parents.size ();
    00077         // merges offspring and parents into a global population
    00078         _parents.reserve (_parents.size () + _offspring.size ());
    00079         copy (_offspring.begin (), _offspring.end (), back_inserter (_parents));
    00080         // evaluates the fitness and the diversity of this global population
    00081         evalFitness (_parents);
    00082         evalDiversity (_parents);
    00083         // sorts the whole population according to the comparator
    00084         Cmp cmp(comparator);
    00085         std::sort(_parents.begin(), _parents.end(), cmp);
    00086         // finally, resize this global population
    00087         _parents.resize (sz);
    00088         // and clear the offspring population
    00089         _offspring.clear ();
    00090     }
    00091
    00092
    00093 protected:
    00094
    00096     moeoFitnessAssignment < MOEOT > & evalFitness;
    00098     moeoDiversityAssignment < MOEOT > & evalDiversity;
    00100     moeoComparator < MOEOT > & comparator;
    00101
    00105     class Cmp
    00106     {
    00107     public:
    00108
    00113         Cmp(moeoComparator < MOEOT > & _comparator) : comparator(_comparator)
    00114         {}
    00115
    00116
    00122         bool operator()(const MOEOT & _moeo1, const MOEOT & _moeo2)
    00123         {
    00124             return comparator(_moeo1,_moeo2);
    00125         }
    00126
    00127
    00128     private:
    00129
    00131         moeoComparator < MOEOT > & comparator;
    00132
    00133     };
    00134
    00135 };
    00136
    00137 #endif /*MOEOELITISTREPLACEMENT_H_ */