EvolvingObjects
eoSyncEasyPSO.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoSyncEasyPSO.h
00005 // (c) OPAC 2007
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: thomas.legrand@lifl.fr
00022  */
00023 //-----------------------------------------------------------------------------
00024 
00025 #ifndef _EOSYNCEASYPSO_H
00026 #define _EOSYNCEASYPSO_H
00027 
00028 //-----------------------------------------------------------------------------
00029 #include <eoContinue.h>
00030 #include <eoPopEvalFunc.h>
00031 #include <eoPSO.h>
00032 #include <eoVelocity.h>
00033 #include <eoFlight.h>
00034 //-----------------------------------------------------------------------------
00035 
00049 template < class POT > class eoSyncEasyPSO:public eoPSO < POT >
00050 {
00051 public:
00052 
00061     eoSyncEasyPSO (
00062         eoInitializerBase <POT> &_init,
00063         eoContinue < POT > &_continuator,
00064         eoEvalFunc < POT > &_eval,
00065         eoVelocity < POT > &_velocity,
00066         eoFlight < POT > &_flight):
00067             init(_init),
00068             continuator (_continuator),
00069             eval (_eval),
00070             loopEval(_eval),
00071             popEval(loopEval),
00072             velocity (_velocity),
00073             flight (_flight)
00074     {}
00075 
00076 
00083     eoSyncEasyPSO (
00084         eoInitializerBase <POT> &_init,
00085         eoContinue < POT > &_continuator,
00086         eoEvalFunc < POT > &_eval,
00087         eoVelocity < POT > &_velocity):
00088             init(_init),
00089             continuator (_continuator),
00090             eval (_eval),
00091             loopEval(_eval),
00092             popEval(loopEval),
00093             velocity (_velocity),
00094             flight (dummyFlight)
00095     {}
00096 
00104     eoSyncEasyPSO (
00105         eoInitializerBase <POT> &_init,
00106         eoContinue < POT > &_continuator,
00107         eoPopEvalFunc < POT > &_eval,
00108         eoVelocity < POT > &_velocity,
00109         eoFlight <POT> &_flight):
00110             init(_init),
00111             continuator (_continuator),
00112             eval (dummyEval),
00113             loopEval(dummyEval),
00114             popEval(_eval),
00115             velocity (_velocity),
00116             flight (_flight)
00117     {}
00118 
00119 
00127     eoSyncEasyPSO (
00128         eoContinue < POT > &_continuator,
00129         eoEvalFunc < POT > &_eval,
00130         eoVelocity < POT > &_velocity,
00131         eoFlight < POT > &_flight):
00132             init(dummyInit),
00133             continuator (_continuator),
00134             eval (_eval),
00135             loopEval(_eval),
00136             popEval(loopEval),
00137             velocity (_velocity),
00138             flight (_flight)
00139     {}
00140 
00141 
00147     eoSyncEasyPSO (
00148         eoContinue < POT > &_continuator,
00149         eoEvalFunc < POT > &_eval,
00150         eoVelocity < POT > &_velocity):
00151             init(dummyInit),
00152             continuator (_continuator),
00153             eval (_eval),
00154             loopEval(_eval),
00155             popEval(loopEval),
00156             velocity (_velocity),
00157             flight (dummyFlight)
00158     {}
00159 
00166     eoSyncEasyPSO (
00167         eoContinue < POT > &_continuator,
00168         eoPopEvalFunc < POT > &_eval,
00169         eoVelocity < POT > &_velocity,
00170         eoFlight <POT> &_flight):
00171             init(dummyInit),
00172             continuator (_continuator),
00173             eval (dummyEval),
00174             loopEval(dummyEval),
00175             popEval(_eval),
00176             velocity (_velocity),
00177             flight (_flight)
00178     {}
00179 
00181     virtual void operator  () (eoPop < POT > &_pop)
00182     {
00183 
00184         try
00185         {
00186             // initializes the topology, velocity, best particle(s)
00187             init();
00188 
00189             // just to use a loop eval
00190             eoPop<POT> empty_pop;
00191 
00192             do
00193             {
00194                 // perform velocity evaluation
00195                 velocity.apply (_pop);
00196 
00197                 // apply the flight
00198                 flight.apply (_pop);
00199 
00200                 // evaluate the position (with a loop eval, empty_swarm IS USELESS)
00201                 popEval(empty_pop,_pop);
00202 
00203                 // update the topology (particle and local/global best(s))
00204                 velocity.updateNeighborhood(_pop);
00205 
00206             }
00207             while (continuator (_pop));
00208 
00209         }
00210         catch (std::exception & e)
00211         {
00212             std::string s = e.what ();
00213             s.append (" in eoSyncEasyPSO");
00214             throw std::runtime_error (s);
00215         }
00216 
00217     }
00218 
00219 private:
00220 
00221     eoInitializerBase <POT> &init;
00222     eoContinue < POT > &continuator;
00223 
00224     eoEvalFunc < POT > &eval;
00225     eoPopLoopEval<POT>        loopEval;
00226     eoPopEvalFunc<POT>&       popEval;
00227 
00228     eoVelocity < POT > &velocity;
00229     eoFlight < POT > &flight;
00230 
00231     // if the eval does not need to be used, use the dummy eval instance
00232         class eoDummyEval : public eoEvalFunc<POT>
00233             {
00234             public:
00235                 void operator()(POT &)
00236                 {}
00237             }
00238             dummyEval;
00239 
00240          class eoDummyFlight:public eoFlight < POT >
00241         {
00242                 public:
00243         eoDummyFlight () {}
00244         void operator  () (POT & /*_po*/) {}
00245         }dummyFlight;
00246 
00247         // if the initializer does not need to be used, use the dummy one instead
00248         class eoDummyInitializer:public eoInitializerBase < POT >
00249         {
00250          public:
00251         eoDummyInitializer () {}
00252         void operator  () (POT & _po) {}
00253         }dummyInit;
00254 
00255 };
00260 #endif /*_EOSYNCEASYPSO_H*/
 All Classes Namespaces Files Functions Variables Typedefs Friends