# 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
    00037
    00039
    00048 template <class EOT>
    00049 eoPop<EOT>&  do_make_pop(eoParser & _parser, eoState& _state, eoInit<EOT> & _init)
    00050 {
    00051   // random seed
    00052     eoValueParam<uint32_t>& seedParam = _parser.createParam(uint32_t(0), "seed", "Random number seed", 'S');
    00053     if (seedParam.value() == 0)
    00054         seedParam.value() = time(0);
    00055     eoValueParam<unsigned>& popSize = _parser.createParam(unsigned(20), "popSize", "Population Size", 'P', "Evolution Engine");
    00056
    00057   // Either load or initialize
    00058   // create an empty pop and let the state handle the memory
    00059   eoPop<EOT>& pop = _state.takeOwnership(eoPop<EOT>());
    00060
    00061   eoValueParam<std::string>& loadNameParam = _parser.createParam(std::string(""), "Load","A save file to restart from",'L', "Persistence" );
    00062   eoValueParam<bool> & recomputeFitnessParam = _parser.createParam(false, "recomputeFitness", "Recompute the fitness after re-loading the pop.?", 'r',  "Persistence" );
    00063
    00064   if (loadNameParam.value() != "") // something to load
    00065     {
    00066       // create another state for reading
    00067       eoState inState;          // a state for loading - WITHOUT the parser
    00068       // register the rng and the pop in the state, so they can be loaded,
    00069       // and the present run will be the exact continuation of the saved run
    00070       // eventually with different parameters
    00071       inState.registerObject(pop);
    00072       inState.registerObject(rng);
    00073       inState.load(loadNameParam.value()); //  load the pop and the rng
    00074       // the fitness is read in the file:
    00075       // do only evaluate the pop if the fitness has changed
    00076       if (recomputeFitnessParam.value())
    00077         {
    00078           for (unsigned i=0; i<pop.size(); i++)
    00079             pop[i].invalidate();
    00080         }
    00081       if (pop.size() < popSize.value())
    00082         std::cerr << "WARNING, only " << pop.size() << " individuals read in file " << loadNameParam.value() << "\nThe remaining " << popSize.value() - pop.size() << " will be randomly drawn" << std::endl;
    00083       if (pop.size() > popSize.value())
    00084         {
    00085           std::cerr << "WARNING, Load file contained too many individuals. Only the best will be retained" << std::endl;
    00086           pop.resize(popSize.value());
    00087         }
    00088     }
    00089   else                          // nothing loaded from a file
    00090     {
    00091       rng.reseed(seedParam.value());
    00092     }
    00093
    00094   if (pop.size() < popSize.value()) // missing some guys
    00095     {
    00096       // Init pop from the randomizer: need to use the append function
    00097       pop.append(popSize.value(), _init);
    00098     }
    00099
    00100   // for future stateSave, register the algorithm into the state
    00101   _state.registerObject(_parser);
    00102   _state.registerObject(pop);
    00103   _state.registerObject(rng);
    00104
    00105   return pop;
    00106 }
    00107
    00108 #endif