mainPSO.cpp

00001 /*
00002 * <mainPSO.cpp>
00003 * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008
00004 * (C) OPAC Team, INRIA, 2008
00005 *
00006 * 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 #include <peo>
00038 
00039 typedef eoRealParticle < double >Indi;
00040 
00041 double f (const Indi & _indi)
00042 {
00043   double sum;
00044   sum=_indi[1]-pow(_indi[0],2);
00045   sum=100*pow(sum,2);
00046   sum+=pow((1-_indi[0]),2);
00047   return (-sum);
00048 }
00049 
00050 int main (int __argc, char *__argv[])
00051 {
00052   peo :: init( __argc, __argv );
00053   eoParser parser(__argc, __argv);
00054   unsigned int POP_SIZE = parser.createParam((unsigned int)(20), "popSize", "Population size",'P',"Param").value();
00055   unsigned int MAX_GEN = parser.createParam((unsigned int)(100), "maxGen", "Maximum number of generations",'G',"Param").value();
00056   unsigned int VEC_SIZE = parser.createParam((unsigned int)(2), "vecSize", "Vector size",'V',"Param").value();
00057   double INIT_POSITION_MIN = parser.createParam(-2.0, "pMin", "Init position min",'N',"Param").value();
00058   double INIT_POSITION_MAX = parser.createParam(2.0, "pMax", "Init position max",'X',"Param").value();
00059   double INIT_VELOCITY_MIN = parser.createParam(-1.0, "vMin", "Init velocity min",'n',"Param").value();
00060   double INIT_VELOCITY_MAX = parser.createParam(1.0, "vMax", "Init velocity max",'x',"Param").value();
00061   double omega = parser.createParam(1.0, "weight", "Weight",'w',"Param").value();
00062   double C1 = parser.createParam(0.5, "c1", "C1",'1',"Param").value();
00063   double C2 = parser.createParam(2.0, "c2t", "C2",'2',"Param").value();
00064   unsigned int NEIGHBORHOOD_SIZE = parser.createParam((unsigned int)(6), "neighSize", "Neighborhood size",'H',"Param").value();
00065   unsigned int MIG_FREQ = parser.createParam((unsigned int)(10), "migFreq", "Migration frequency",'F',"Param").value();
00066   rng.reseed (time(0));
00067 
00068 // Island model
00069 
00070   RingTopology topologyMig;
00071 
00072 // First
00073   eoGenContinue < Indi > genContPara (MAX_GEN);
00074   eoCombinedContinue <Indi> continuatorPara (genContPara);
00075   eoCheckPoint<Indi> checkpoint(continuatorPara);
00076   peoEvalFunc<Indi, double, const Indi& > plainEval(f);
00077   peoPopEval< Indi > eval(plainEval);
00078   eoUniformGenerator < double >uGen (INIT_POSITION_MIN, INIT_POSITION_MAX);
00079   eoInitFixedLength < Indi > random (VEC_SIZE, uGen);
00080   eoUniformGenerator < double >sGen (INIT_VELOCITY_MIN, INIT_VELOCITY_MAX);
00081   eoVelocityInitFixedLength < Indi > veloRandom (VEC_SIZE, sGen);
00082   eoFirstIsBestInit < Indi > localInit;
00083   eoRealVectorBounds bndsFlight(VEC_SIZE,INIT_POSITION_MIN,INIT_POSITION_MAX);
00084   eoStandardFlight < Indi > flight(bndsFlight);
00085   eoPop < Indi > pop;
00086   pop.append (POP_SIZE, random);
00087   eoLinearTopology<Indi> topology(NEIGHBORHOOD_SIZE);
00088   eoRealVectorBounds bnds(VEC_SIZE,INIT_VELOCITY_MIN,INIT_VELOCITY_MAX);
00089   eoStandardVelocity < Indi > velocity (topology,omega,C1,C2,bnds);
00090   eoInitializer <Indi> init(eval,veloRandom,localInit,topology,pop);
00091 
00092 // Island model
00093 
00094   eoPeriodicContinue< Indi > mig_cont( MIG_FREQ );
00095   peoPSOSelect<Indi> mig_selec(topology);
00096   peoWorstPositionReplacement<Indi> mig_replac;
00097 
00098 // Specific implementation (peoData.h)
00099 
00100   eoContinuator<Indi> cont(mig_cont, pop);
00101   eoSelector <Indi, eoPop<Indi> > mig_select (mig_selec,1,pop);
00102   eoReplace <Indi, eoPop<Indi> > mig_replace (mig_replac,pop);
00103 
00104 
00105 // Second
00106 
00107   eoGenContinue < Indi > genContPara2 (MAX_GEN);
00108   eoCombinedContinue <Indi> continuatorPara2 (genContPara2);
00109   eoCheckPoint<Indi> checkpoint2(continuatorPara2);
00110   peoEvalFunc<Indi, double, const Indi& > plainEval2(f);
00111   peoPopEval< Indi > eval2(plainEval2);
00112   eoUniformGenerator < double >uGen2 (INIT_POSITION_MIN, INIT_POSITION_MAX);
00113   eoInitFixedLength < Indi > random2 (VEC_SIZE, uGen2);
00114   eoUniformGenerator < double >sGen2 (INIT_VELOCITY_MIN, INIT_VELOCITY_MAX);
00115   eoVelocityInitFixedLength < Indi > veloRandom2 (VEC_SIZE, sGen2);
00116   eoFirstIsBestInit < Indi > localInit2;
00117   eoRealVectorBounds bndsFlight2(VEC_SIZE,INIT_POSITION_MIN,INIT_POSITION_MAX);
00118   eoStandardFlight < Indi > flight2(bndsFlight2);
00119   eoPop < Indi > pop2;
00120   pop2.append (POP_SIZE, random2);
00121   eoLinearTopology<Indi> topology2(NEIGHBORHOOD_SIZE);
00122   eoRealVectorBounds bnds2(VEC_SIZE,INIT_VELOCITY_MIN,INIT_VELOCITY_MAX);
00123   eoStandardVelocity < Indi > velocity2 (topology2,omega,C1,C2,bnds2);
00124   eoInitializer <Indi> init2(eval2,veloRandom2,localInit2,topology2,pop2);
00125 
00126 // Island model
00127 
00128   eoPeriodicContinue< Indi > mig_cont2( MIG_FREQ );
00129   peoPSOSelect<Indi> mig_selec2(topology2);
00130   peoWorstPositionReplacement<Indi> mig_replac2;
00131 
00132 // Specific implementation (peoData.h)
00133 
00134   eoContinuator<Indi> cont2(mig_cont2,pop2);
00135   eoSelector <Indi, eoPop<Indi> > mig_select2 (mig_selec2,1,pop2);
00136   eoReplace <Indi, eoPop<Indi> > mig_replace2 (mig_replac2,pop2);
00137 
00138 
00139 // Asynchronous island
00140 
00141   peoAsyncIslandMig< eoPop<Indi>, eoPop<Indi> > mig(cont,mig_select, mig_replace, topologyMig);
00142   checkpoint.add( mig );
00143   peoAsyncIslandMig< eoPop<Indi>, eoPop<Indi> > mig2(cont2,mig_select2, mig_replace2, topologyMig);
00144   checkpoint2.add( mig2 );
00145 
00146 
00147 // Parallel algorithm
00148 
00149   eoSyncEasyPSO <Indi> psa(init,checkpoint,eval, velocity, flight);
00150   peoWrapper parallelPSO( psa, pop);
00151   eval.setOwner(parallelPSO);
00152   mig.setOwner(parallelPSO);
00153   eoSyncEasyPSO <Indi> psa2(init2,checkpoint2,eval2, velocity2, flight2);
00154   peoWrapper parallelPSO2( psa2, pop2);
00155   eval2.setOwner(parallelPSO2);
00156   mig2.setOwner(parallelPSO2);
00157   peo :: run();
00158   peo :: finalize();
00159   if (getNodeRank()==1)
00160     {
00161       pop.sort();
00162       pop2.sort();
00163       std::cout << "Final population :\n" << pop << std::endl;
00164       std::cout << "Final population :\n" << pop2        << std::endl;
00165     }
00166 }

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