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 //Evaluation function
00042 double f (const Indi & _indi)
00043 {
00044   // Rosenbrock function f(x) = 100*(x[1]-x[0]^2)^2+(1-x[0])^2
00045   // => optimal : f* = 0 , with x* =(1,1)
00046 
00047   double sum;
00048   sum=_indi[1]-pow(_indi[0],2);
00049   sum=100*pow(sum,2);
00050   sum+=pow((1-_indi[0]),2);
00051   return (-sum);
00052 }
00053 
00054 int main (int __argc, char *__argv[])
00055 {
00056 
00057 // Initialization of the parallel environment : thanks this instruction, ParadisEO-PEO can initialize himself
00058   peo :: init( __argc, __argv );
00059 
00060 //Parameters
00061   eoParser parser(__argc, __argv);
00062   unsigned int POP_SIZE = parser.createParam((unsigned int)(20), "popSize", "Population size",'P',"Param").value();
00063   unsigned int MAX_GEN = parser.createParam((unsigned int)(100), "maxGen", "Maximum number of generations",'G',"Param").value();
00064   unsigned int VEC_SIZE = parser.createParam((unsigned int)(2), "vecSize", "Vector size",'V',"Param").value();
00065   double INIT_POSITION_MIN = parser.createParam(-2.0, "pMin", "Init position min",'N',"Param").value();
00066   double INIT_POSITION_MAX = parser.createParam(2.0, "pMax", "Init position max",'X',"Param").value();
00067   double INIT_VELOCITY_MIN = parser.createParam(-1.0, "vMin", "Init velocity min",'n',"Param").value();
00068   double INIT_VELOCITY_MAX = parser.createParam(1.0, "vMax", "Init velocity max",'x',"Param").value();
00069   double weight = parser.createParam(1.0, "weight", "Weight",'w',"Param").value();
00070   double C1 = parser.createParam(0.5, "c1", "C1",'1',"Param").value();
00071   double C2 = parser.createParam(2.0, "c2t", "C2",'2',"Param").value();
00072   unsigned int NEIGHBORHOOD_SIZE = parser.createParam((unsigned int)(6), "neighSize", "Neighborhood size",'H',"Param").value();
00073   rng.reseed (time(0));
00074 
00075 // Stopping
00076   eoGenContinue < Indi > genContPara (MAX_GEN);
00077   eoCombinedContinue <Indi> continuatorPara (genContPara);
00078   eoCheckPoint<Indi> checkpoint(continuatorPara);
00079 
00080 // For a parallel evaluation
00081   peoEvalFunc<Indi, double, const Indi& > plainEval(f);
00082   peoPopEval< Indi > eval(plainEval);
00083 
00084 // Initialization
00085   eoUniformGenerator < double >uGen (INIT_POSITION_MIN, INIT_POSITION_MAX);
00086   eoInitFixedLength < Indi > random (VEC_SIZE, uGen);
00087 
00088 // Velocity
00089   eoUniformGenerator < double >sGen (INIT_VELOCITY_MIN, INIT_VELOCITY_MAX);
00090   eoVelocityInitFixedLength < Indi > veloRandom (VEC_SIZE, sGen);
00091 
00092 // Initializing the best
00093   eoFirstIsBestInit < Indi > localInit;
00094 
00095 // Flight
00096   eoRealVectorBounds bndsFlight(VEC_SIZE,INIT_POSITION_MIN,INIT_POSITION_MAX);
00097   eoStandardFlight < Indi > flight(bndsFlight);
00098 
00099 // Creation of the population
00100   eoPop < Indi > pop;
00101   pop.append (POP_SIZE, random);
00102 
00103 // Topology
00104   eoLinearTopology<Indi> topology(NEIGHBORHOOD_SIZE);
00105   eoRealVectorBounds bnds(VEC_SIZE,INIT_VELOCITY_MIN,INIT_VELOCITY_MAX);
00106   eoStandardVelocity < Indi > velocity (topology,weight,C1,C2,bnds);
00107 
00108 // Initialization
00109   eoInitializer <Indi> init(eval,veloRandom,localInit,topology,pop);
00110 
00111 //Parallel algorithm
00112   eoSyncEasyPSO <Indi> psa(init,checkpoint,eval, velocity, flight);
00113   peoWrapper parallelPSO( psa, pop);
00114   eval.setOwner(parallelPSO);
00115 
00116   peo :: run();
00117   peo :: finalize();
00118   if (getNodeRank()==1)
00119     {
00120       pop.sort();
00121       std::cout << "Final population :\n" << pop << std::endl;
00122     }
00123 }

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