00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 // "peoParaPopEval.h" 00004 00005 // (c) OPAC Team, LIFL, August 2005 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: paradiseo-help@lists.gforge.inria.fr 00022 */ 00023 00024 #ifndef __peoParaPopEval_h 00025 #define __peoParaPopEval_h 00026 00027 #include <queue> 00028 #include <eoEvalFunc.h> 00029 00030 #include "core/messaging.h" 00031 #include "core/peo_debug.h" 00032 #include "peoAggEvalFunc.h" 00033 #include "peoNoAggEvalFunc.h" 00034 00035 00037 00041 template< class EOT > class peoParaPopEval : public peoPopEval< EOT > { 00042 00043 public: 00044 00045 using peoPopEval< EOT > :: requestResourceRequest; 00046 using peoPopEval< EOT > :: resume; 00047 using peoPopEval< EOT > :: stop; 00048 using peoPopEval< EOT > :: getOwner; 00049 00054 peoParaPopEval( eoEvalFunc< EOT >& __eval_func ); 00055 00060 peoParaPopEval( const std :: vector< eoEvalFunc < EOT >* >& __funcs, peoAggEvalFunc< EOT >& __merge_eval ); 00061 00065 void operator()( eoPop< EOT >& __pop ); 00066 00069 void packData(); 00070 00073 void unpackData(); 00074 00076 void execute(); 00077 00080 void packResult(); 00081 00084 void unpackResult(); 00085 00088 void notifySendingData(); 00089 00092 void notifySendingAllResourceRequests(); 00093 00094 private: 00095 00096 00097 const std :: vector< eoEvalFunc < EOT >* >& funcs; 00098 std :: vector< eoEvalFunc < EOT >* > one_func; 00099 00100 peoAggEvalFunc< EOT >& merge_eval; 00101 peoNoAggEvalFunc< EOT > no_merge_eval; 00102 00103 std :: queue< EOT* >tasks; 00104 00105 std :: map< EOT*, std :: pair< unsigned, unsigned > > progression; 00106 00107 unsigned num_func; 00108 00109 EOT sol; 00110 00111 EOT *ad_sol; 00112 00113 unsigned total; 00114 }; 00115 00116 00117 template< class EOT > peoParaPopEval< EOT > :: peoParaPopEval( eoEvalFunc< EOT >& __eval_func ) : 00118 00119 funcs( one_func ), merge_eval( no_merge_eval ) 00120 { 00121 00122 one_func.push_back( &__eval_func ); 00123 } 00124 00125 00126 template< class EOT > peoParaPopEval< EOT > :: peoParaPopEval( 00127 00128 const std :: vector< eoEvalFunc< EOT >* >& __funcs, 00129 peoAggEvalFunc< EOT >& __merge_eval 00130 00131 ) : funcs( __funcs ), merge_eval( __merge_eval ) 00132 { 00133 00134 } 00135 00136 00137 template< class EOT > void peoParaPopEval< EOT >::operator()( eoPop< EOT >& __pop ) { 00138 00139 for ( unsigned i = 0; i < __pop.size(); i++ ) { 00140 00141 __pop[ i ].fitness( typename EOT :: Fitness() ); 00142 00143 progression[ &__pop[ i ] ].first = funcs.size() - 1; 00144 progression[ &__pop[ i ] ].second = funcs.size(); 00145 00146 for ( unsigned j = 0; j < funcs.size(); j++ ) { 00147 /* Queuing the 'invalid' solution and its associated owner */ 00148 tasks.push( &__pop[ i ] ); 00149 } 00150 } 00151 00152 total = funcs.size() * __pop.size(); 00153 requestResourceRequest( funcs.size() * __pop.size() ); 00154 stop(); 00155 } 00156 00157 00158 template< class EOT > void peoParaPopEval< EOT > :: packData() { 00159 00160 // printDebugMessage ("debut pakc data"); 00161 pack( progression[ tasks.front() ].first-- ); 00162 00163 /* Packing the contents :-) of the solution */ 00164 pack( *tasks.front() ); 00165 00166 /* Packing the addresses of both the solution and the owner */ 00167 pack( tasks.front() ); 00168 tasks.pop( ); 00169 } 00170 00171 00172 template< class EOT > void peoParaPopEval< EOT > :: unpackData() { 00173 00174 unpack( num_func ); 00175 /* Unpacking the solution */ 00176 unpack( sol ); 00177 /* Unpacking the @ of that one */ 00178 unpack( ad_sol ); 00179 } 00180 00181 00182 template< class EOT > void peoParaPopEval< EOT > :: execute() { 00183 00184 /* Computing the fitness of the solution */ 00185 funcs[ num_func ]->operator()( sol ); 00186 } 00187 00188 00189 template< class EOT > void peoParaPopEval< EOT > :: packResult() { 00190 00191 /* Packing the fitness of the solution */ 00192 pack( sol.fitness() ); 00193 /* Packing the @ of the individual */ 00194 pack( ad_sol ); 00195 } 00196 00197 00198 template< class EOT > void peoParaPopEval< EOT > :: unpackResult() { 00199 00200 typename EOT :: Fitness fit; 00201 00202 /* Unpacking the computed fitness */ 00203 unpack( fit ); 00204 00205 /* Unpacking the @ of the associated individual */ 00206 unpack( ad_sol ); 00207 00208 00209 /* Associating the fitness the local solution */ 00210 merge_eval( *ad_sol, fit ); 00211 00212 progression[ ad_sol ].second--; 00213 00214 /* Notifying the container of the termination of the evaluation */ 00215 if ( !progression[ ad_sol ].second ) { 00216 00217 progression.erase( ad_sol ); 00218 } 00219 00220 total--; 00221 if ( !total ) { 00222 00223 getOwner()->setActive(); 00224 resume(); 00225 } 00226 } 00227 00228 00229 template< class EOT > void peoParaPopEval< EOT > :: notifySendingData() { 00230 00231 } 00232 00233 00234 template< class EOT > void peoParaPopEval< EOT > :: notifySendingAllResourceRequests() { 00235 00236 getOwner()->setPassive(); 00237 } 00238 00239 00240 #endif
1.4.7