EvolvingObjects
|
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 00039 template <class EOT> class eoCellularEasyEA : public eoAlgo <EOT> { 00040 00041 public : 00042 00047 eoCellularEasyEA (eoContinue <EOT> & _cont, // Stop. criterion 00048 eoEvalFunc <EOT> & _eval, // Evaluation function 00049 eoSelectOne <EOT> & _sel_neigh, // To choose a partner 00050 eoBinOp <EOT> & _cross, // Cross-over operator 00051 eoMonOp <EOT> & _mut, // Mutation operator 00052 eoSelectOne <EOT> & _sel_repl /* Which to keep between the new 00053 child and the old individual ? */ 00054 ) : 00055 cont (_cont), 00056 eval (_eval), 00057 popEval (_eval), 00058 sel_neigh (_sel_neigh), 00059 cross (_cross), 00060 mut (_mut), 00061 sel_child (eoSelectFirstOne ()), 00062 sel_repl (_sel_repl) { 00063 00064 } 00065 00066 eoCellularEasyEA (eoContinue <EOT> & _cont, 00067 eoEvalFunc <EOT> & _eval, 00068 eoSelectOne <EOT> & _sel_neigh, 00069 eoQuadOp <EOT> & _cross, 00070 eoMonOp <EOT> & _mut, 00071 eoSelectOne <EOT> & _sel_child, /* To choose one from 00072 the both children */ 00073 eoSelectOne <EOT> & _sel_repl 00074 ) : 00075 cont (_cont), 00076 eval (_eval), 00077 popEval (_eval), 00078 sel_neigh (_sel_neigh), 00079 cross (_cross), 00080 mut (_mut), 00081 sel_child (_sel_child), 00082 sel_repl (_sel_repl) { 00083 00084 } 00085 00090 void operator () (eoPop <EOT> & pop) { 00091 00092 do { 00093 00094 for (unsigned i = 0 ; i < pop.size () ; i ++) { 00095 00096 // Who are neighbouring to the current individual ? 00097 eoPop <EOT> neigh = neighbours (pop, i) ; 00098 00099 // To select a partner 00100 EOT part, old_sol = pop [i] ; 00101 part = sel_neigh (neigh) ; 00102 00103 // To perform cross-over 00104 cross (pop [i], part) ; 00105 00106 // To perform mutation 00107 mut (pop [i]) ; 00108 mut (part) ; 00109 00110 pop [i].invalidate () ; 00111 part.invalidate () ; 00112 eval (pop [i]) ; 00113 eval (part) ; 00114 00115 // To choose one of the two children ... 00116 eoPop <EOT> pop_loc ; 00117 pop_loc.push_back (pop [i]) ; 00118 pop_loc.push_back (part) ; 00119 00120 pop [i] = sel_child (pop_loc) ; 00121 00122 // To choose only one between the new made child and the old individual 00123 pop_loc.clear () ; 00124 pop_loc.push_back (pop [i]) ; 00125 00126 pop_loc.push_back (old_sol) ; 00127 00128 pop [i] = sel_repl (pop_loc) ; 00129 } 00130 00131 } while (cont (pop)) ; 00132 } 00133 00134 protected : 00135 00136 virtual eoPop <EOT> neighbours (const eoPop <EOT> & pop, int rank) = 0 ; 00137 00138 private : 00139 00140 eoContinue <EOT> & cont ; 00141 eoEvalFunc <EOT> & eval ; 00142 eoPopLoopEval <EOT> popEval ; 00143 eoSelectOne <EOT> & sel_neigh ; 00144 eoBF <EOT &, EOT &, bool> & cross ; 00145 eoMonOp <EOT> & mut ; 00146 eoSelectOne <EOT> & sel_child ; 00147 eoSelectOne <EOT> & sel_repl ; 00148 00149 class eoSelectFirstOne : public eoSelectOne <EOT> { 00150 00151 public : 00152 00153 const EOT & operator () (const eoPop <EOT> & pop) { 00154 00155 return pop [0] ; 00156 } 00157 00158 } ; 00159 00160 } ; 00161 00162 #endif