eoCellularEasyEA.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 // "eoCellularEasyEA.h"
00004 
00005 // (c) OPAC Team, LIFL, 2002
00006 
00007 /* This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011    
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016    
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with this library; if not, write to the Free Software
00019    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020    
00021    Contact: cahon@lifl.fr
00022 */
00023 
00024 #ifndef eoCellularEasyEA_h
00025 #define eoCellularEasyEA_h
00026 
00027 #include <eoContinue.h>
00028 #include <eoEvalFunc.h>
00029 #include <eoSelectOne.h>
00030 #include <eoPopEvalFunc.h>
00031 #include <eoAlgo.h>
00032 #include <eoOp.h>
00033 
00038 template <class EOT> class eoCellularEasyEA : public eoAlgo <EOT> {
00039   
00040 public :
00041   
00046   eoCellularEasyEA (eoContinue <EOT> & _cont, // Stop. criterion
00047                     eoEvalFunc <EOT> & _eval, // Evaluation function
00048                     eoSelectOne <EOT> & _sel_neigh, // To choose a partner
00049                     eoBinOp <EOT> & _cross, // Cross-over operator
00050                     eoMonOp <EOT> & _mut, // Mutation operator
00051                     eoSelectOne <EOT> & _sel_repl /* Which to keep between the new
00052                                                      child and the old individual ? */
00053                     ) :
00054     cont (_cont),
00055     eval (_eval),
00056     popEval (_eval),
00057     sel_neigh (_sel_neigh),
00058     cross (_cross),
00059     mut (_mut),
00060     sel_child (eoSelectFirstOne ()),
00061     sel_repl (_sel_repl) {
00062     
00063   }
00064   
00065   eoCellularEasyEA (eoContinue <EOT> & _cont,
00066                     eoEvalFunc <EOT> & _eval,
00067                     eoSelectOne <EOT> & _sel_neigh,
00068                     eoQuadOp <EOT> & _cross,
00069                     eoMonOp <EOT> & _mut,
00070                     eoSelectOne <EOT> & _sel_child, /* To choose one from
00071                                                        the both children */
00072                     eoSelectOne <EOT> & _sel_repl
00073                     ) :
00074     cont (_cont),
00075     eval (_eval),
00076     popEval (_eval),
00077     sel_neigh (_sel_neigh),
00078     cross (_cross),
00079     mut (_mut),
00080     sel_child (_sel_child),
00081     sel_repl (_sel_repl) {
00082     
00083   }
00084 
00089   void operator () (eoPop <EOT> & pop) {
00090     
00091     do {
00092       
00093       for (unsigned i = 0 ; i < pop.size () ; i ++) {
00094         
00095         // Who are neighbouring to the current individual ?
00096         eoPop <EOT> neigh = neighbours (pop, i) ;
00097         
00098         // To select a partner
00099         EOT part, old_sol = pop [i] ;
00100         part = sel_neigh (neigh) ;
00101 
00102         // To perform cross-over
00103         cross (pop [i], part) ;
00104 
00105         // To perform mutation
00106         mut (pop [i]) ;
00107         mut (part) ;
00108         
00109         pop [i].invalidate () ;
00110         part.invalidate () ;
00111         eval (pop [i]) ;
00112         eval (part) ;
00113 
00114         // To choose one of the two children ...
00115         eoPop <EOT> pop_loc ;
00116         pop_loc.push_back (pop [i]) ;
00117         pop_loc.push_back (part) ;
00118 
00119         pop [i] = sel_child (pop_loc) ;
00120 
00121         // To choose only one between the new made child and the old individual
00122         pop_loc.clear () ;
00123         pop_loc.push_back (pop [i]) ;
00124         
00125         pop_loc.push_back (old_sol) ;
00126         
00127         pop [i] = sel_repl (pop_loc) ;  
00128       }
00129       
00130     } while (cont (pop)) ;
00131   }
00132 
00133 protected :
00134   
00135   virtual eoPop <EOT> neighbours (const eoPop <EOT> & pop, int rank) = 0 ;
00136 
00137 private :
00138   
00139   eoContinue <EOT> & cont ;
00140   eoEvalFunc <EOT> & eval ;
00141   eoPopLoopEval <EOT> popEval ;
00142   eoSelectOne <EOT> & sel_neigh ;
00143   eoBF <EOT &, EOT &, bool> & cross ;
00144   eoMonOp <EOT> & mut ;
00145   eoSelectOne <EOT> & sel_child ;
00146   eoSelectOne <EOT> & sel_repl ;
00147   
00148   class eoSelectFirstOne : public eoSelectOne <EOT> {
00149     
00150   public :
00151     
00152     const EOT & operator () (const eoPop <EOT> & pop) {
00153       
00154       return pop [0] ; 
00155     }
00156     
00157   } ; 
00158   
00159 } ;
00160 
00161 #endif

Generated on Thu Oct 19 05:06:34 2006 for EO by  doxygen 1.3.9.1