peoPopEval.h

00001 /*
00002 * <peoPopEval.h>
00003 * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008
00004 * (C) OPAC Team, LIFL, 2002-2008
00005 *
00006 * Sebastien Cahon, Alexandru-Adrian Tantar, Clive Canape
00007 *
00008 * This software is governed by the CeCILL license under French law and
00009 * abiding by the rules of distribution of free software.  You can  use,
00010 * modify and/ or redistribute the software under the terms of the CeCILL
00011 * license as circulated by CEA, CNRS and INRIA at the following URL
00012 * "http://www.cecill.info".
00013 *
00014 * As a counterpart to the access to the source code and  rights to copy,
00015 * modify and redistribute granted by the license, users are provided only
00016 * with a limited warranty  and the software's author,  the holder of the
00017 * economic rights,  and the successive licensors  have only  limited liability.
00018 *
00019 * In this respect, the user's attention is drawn to the risks associated
00020 * with loading,  using,  modifying and/or developing or reproducing the
00021 * software by the user in light of its specific status of free software,
00022 * that may mean  that it is complicated to manipulate,  and  that  also
00023 * therefore means  that it is reserved for developers  and  experienced
00024 * professionals having in-depth computer knowledge. Users are therefore
00025 * encouraged to load and test the software's suitability as regards their
00026 * requirements in conditions enabling the security of their systems and/or
00027 * data to be ensured and,  more generally, to use and operate it in the
00028 * same conditions as regards security.
00029 * The fact that you are presently reading this means that you have had
00030 * knowledge of the CeCILL license and that you accept its terms.
00031 *
00032 * ParadisEO WebSite : http://paradiseo.gforge.inria.fr
00033 * Contact: paradiseo-help@lists.gforge.inria.fr
00034 *
00035 */
00036 
00037 #ifndef __peoPopEval_h
00038 #define __peoPopEval_h
00039 
00040 #include <queue>
00041 #include <eoEvalFunc.h>
00042 
00043 #include "core/messaging.h"
00044 #include "core/peo_debug.h"
00045 #include "peoAggEvalFunc.h"
00046 #include "peoNoAggEvalFunc.h"
00047 
00053 template< class EOT > class peoPopEval : public Service, public eoPopEvalFunc<EOT>
00054   {
00055 
00056   public:
00057 
00062     peoPopEval( eoEvalFunc< EOT >& __eval_func );
00063 
00068     peoPopEval( const std :: vector< eoEvalFunc < EOT >* >& __funcs, peoAggEvalFunc< EOT >& __merge_eval );
00069 
00073     void operator()(eoPop< EOT >& __pop);
00074 
00078     void operator()( eoPop< EOT >& __dummy, eoPop< EOT >& __pop );
00079 
00082     void packData();
00083 
00086     void unpackData();
00087 
00089     void execute();
00090 
00093     void packResult();
00094 
00097     void unpackResult();
00098 
00101     void notifySendingData();
00102 
00105     void notifySendingAllResourceRequests();
00106 
00107   private:
00108 
00119     const std :: vector< eoEvalFunc < EOT >* >& funcs;
00120     std :: vector< eoEvalFunc < EOT >* > one_func;
00121     peoAggEvalFunc< EOT >& merge_eval;
00122     peoNoAggEvalFunc< EOT > no_merge_eval;
00123     std :: queue< EOT* >tasks;
00124     std :: map< EOT*, std :: pair< unsigned, unsigned > > progression;
00125     unsigned num_func;
00126     EOT sol;
00127     EOT *ad_sol;
00128     unsigned total;
00129   };
00130 
00131 
00132 template< class EOT > peoPopEval< EOT > :: peoPopEval( eoEvalFunc< EOT >& __eval_func ) :
00133 
00134     funcs( one_func ), merge_eval( no_merge_eval )
00135 {
00136 
00137   one_func.push_back( &__eval_func );
00138 }
00139 
00140 
00141 template< class EOT > peoPopEval< EOT > :: peoPopEval(
00142 
00143   const std :: vector< eoEvalFunc< EOT >* >& __funcs,
00144   peoAggEvalFunc< EOT >& __merge_eval
00145 
00146 ) : funcs( __funcs ), merge_eval( __merge_eval )
00147 {}
00148 
00149 template< class EOT > void peoPopEval< EOT >::operator()(eoPop< EOT >& __dummy, eoPop< EOT >& __pop )
00150 {
00151   this->operator()(__pop);
00152 }
00153 
00154 template< class EOT > void peoPopEval< EOT >::operator()(eoPop< EOT >& __pop )
00155 {
00156   if ( __pop.size() && (funcs.size() * __pop.size()) )
00157     {
00158       for ( unsigned i = 0; i < __pop.size(); i++ )
00159         {
00160           __pop[ i ].fitness(typename EOT :: Fitness() );
00161           progression[ &__pop[ i ] ].first = funcs.size() - 1;
00162           progression[ &__pop[ i ] ].second = funcs.size();
00163           for ( unsigned j = 0; j < funcs.size(); j++ )
00164             {
00165               /* Queuing the 'invalid' solution and its associated owner */
00166               tasks.push( &__pop[ i ] );
00167             }
00168         }
00169       total = funcs.size() * __pop.size();
00170       requestResourceRequest( funcs.size() * __pop.size() );
00171       stop();
00172     }
00173 }
00174 
00175 
00176 template< class EOT > void peoPopEval< EOT > :: packData()
00177 {
00178   //  printDebugMessage ("debut pakc data");
00179   pack( progression[ tasks.front() ].first-- );
00180 
00181   /* Packing the contents :-) of the solution */
00182   pack( *tasks.front() );
00183 
00184   /* Packing the addresses of both the solution and the owner */
00185   pack( tasks.front() );
00186   tasks.pop(  );
00187 }
00188 
00189 
00190 template< class EOT > void peoPopEval< EOT > :: unpackData()
00191 {
00192   unpack( num_func );
00193   /* Unpacking the solution */
00194   unpack( sol );
00195   /* Unpacking the @ of that one */
00196   unpack( ad_sol );
00197 }
00198 
00199 
00200 template< class EOT > void peoPopEval< EOT > :: execute()
00201 {
00202 
00203   /* Computing the fitness of the solution */
00204   funcs[ num_func ]->operator()( sol );
00205 }
00206 
00207 
00208 template< class EOT > void peoPopEval< EOT > :: packResult()
00209 {
00210   /* Packing the fitness of the solution */
00211   pack( sol.fitness() );
00212   /* Packing the @ of the individual */
00213   pack( ad_sol );
00214 }
00215 
00216 
00217 template< class EOT > void peoPopEval< EOT > :: unpackResult()
00218 {
00219   typename EOT :: Fitness fit;
00220 
00221   /* Unpacking the computed fitness */
00222   unpack( fit );
00223 
00224   /* Unpacking the @ of the associated individual */
00225   unpack( ad_sol );
00226 
00227 
00228   /* Associating the fitness the local solution */
00229   merge_eval( *ad_sol, fit );
00230 
00231   progression[ ad_sol ].second--;
00232 
00233   /* Notifying the container of the termination of the evaluation */
00234   if ( !progression[ ad_sol ].second )
00235     {
00236 
00237       progression.erase( ad_sol );
00238     }
00239 
00240   total--;
00241   if ( !total )
00242     {
00243 
00244       getOwner()->setActive();
00245       resume();
00246     }
00247 }
00248 
00249 
00250 template< class EOT > void peoPopEval< EOT > :: notifySendingData()
00251 {}
00252 
00253 
00254 template< class EOT > void peoPopEval< EOT > :: notifySendingAllResourceRequests()
00255 {
00256   getOwner()->setPassive();
00257 }
00258 
00259 
00260 #endif

Generated on Thu Mar 13 15:55:03 2008 for ParadisEO-PEO-ParallelanddistributedEvolvingObjects by  doxygen 1.4.7