EvolvingObjects
make_pop.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // make_pop.h
00005 // (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2001
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: todos@geneura.ugr.es, http://geneura.ugr.es
00022              Marc.Schoenauer@polytechnique.fr
00023              mkeijzer@dhi.dk
00024  */
00025 //-----------------------------------------------------------------------------
00026 
00027 #ifndef _make_pop_h
00028 #define _make_pop_h
00029 
00030 #include <ctime>  // for time(0) for random seeding
00031 #include <eoPop.h>
00032 #include <eoInit.h>
00033 #include <utils/eoRNG.h>
00034 #include <utils/eoParser.h>
00035 #include <utils/eoState.h>
00036 
00056 template <class EOT>
00057 eoPop<EOT>&  do_make_pop(eoParser & _parser, eoState& _state, eoInit<EOT> & _init)
00058 {
00059   // random seed
00060     eoValueParam<uint32_t>& seedParam = _parser.getORcreateParam(uint32_t(0), "seed", "Random number seed", 'S');
00061     if (seedParam.value() == 0)
00062         seedParam.value() = time(0);
00063     eoValueParam<unsigned>& popSize = _parser.getORcreateParam(unsigned(20), "popSize", "Population Size", 'P', "Evolution Engine");
00064 
00065   // Either load or initialize
00066   // create an empty pop and let the state handle the memory
00067   eoPop<EOT>& pop = _state.takeOwnership(eoPop<EOT>());
00068 
00069   eoValueParam<std::string>& loadNameParam = _parser.getORcreateParam(std::string(""), "Load","A save file to restart from",'L', "Persistence" );
00070   eoValueParam<bool> & recomputeFitnessParam = _parser.getORcreateParam(false, "recomputeFitness", "Recompute the fitness after re-loading the pop.?", 'r',  "Persistence" );
00071 
00072   if (loadNameParam.value() != "") // something to load
00073     {
00074       // create another state for reading
00075       eoState inState;          // a state for loading - WITHOUT the parser
00076       // register the rng and the pop in the state, so they can be loaded,
00077       // and the present run will be the exact continuation of the saved run
00078       // eventually with different parameters
00079       inState.registerObject(pop);
00080       inState.registerObject(rng);
00081       inState.load(loadNameParam.value()); //  load the pop and the rng
00082       // the fitness is read in the file:
00083       // do only evaluate the pop if the fitness has changed
00084       if (recomputeFitnessParam.value())
00085         {
00086           for (unsigned i=0; i<pop.size(); i++)
00087             pop[i].invalidate();
00088         }
00089       if (pop.size() < popSize.value())
00090         std::cerr << "WARNING, only " << pop.size() << " individuals read in file " << loadNameParam.value() << "\nThe remaining " << popSize.value() - pop.size() << " will be randomly drawn" << std::endl;
00091       if (pop.size() > popSize.value())
00092         {
00093           std::cerr << "WARNING, Load file contained too many individuals. Only the best will be retained" << std::endl;
00094           pop.resize(popSize.value());
00095         }
00096     }
00097   else                          // nothing loaded from a file
00098     {
00099       rng.reseed(seedParam.value());
00100     }
00101 
00102   if (pop.size() < popSize.value()) // missing some guys
00103     {
00104       // Init pop from the randomizer: need to use the append function
00105       pop.append(popSize.value(), _init);
00106     }
00107 
00108   // for future stateSave, register the algorithm into the state
00109   _state.registerObject(_parser);
00110   _state.registerObject(pop);
00111   _state.registerObject(rng);
00112 
00113   return pop;
00114 }
00115 
00116 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends