diff --git a/eo/src/utils/checkpointing b/eo/src/utils/checkpointing index 8af4df76..e570183a 100644 --- a/eo/src/utils/checkpointing +++ b/eo/src/utils/checkpointing @@ -1,15 +1,16 @@ -#include -#include -#include -#include -#include -#include -#ifndef _MSC_VER -#include -#include -#endif -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#ifndef _MSC_VER +#include +#include +#endif +#include +#include +#include +#include #include +#include \ No newline at end of file diff --git a/eo/src/utils/eoPopStat.h b/eo/src/utils/eoPopStat.h new file mode 100644 index 00000000..9c7dc076 --- /dev/null +++ b/eo/src/utils/eoPopStat.h @@ -0,0 +1,117 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoPopStat.h +// (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2001 +/* + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact: todos@geneura.ugr.es, http://geneura.ugr.es + Marc.Schoenauer@polytechnique.fr + mkeijzer@dhi.dk + */ +//----------------------------------------------------------------------------- + +/** WARNING: this file contains 2 classes: + +eoPopString and eoSortedPopString + +that transform the population into a string +that can be used to dump to the screen +*/ + +#ifndef _eoPopStat_h +#define _eoPopStat_h + +#include + + +/** Thanks to MS/VC++, eoParam mechanism is unable to handle vectors of stats. +This snippet is a workaround: +This class will "print" a whole population into a string - that you can later +send to any stream +This is the plain version - see eoPopString for the Sorted version + +Note: this Stat should probably be used only within eoStdOutMonitor, and not +inside an eoFileMonitor, as the eoState construct will work much better there. +*/ +template +class eoPopString : public eoStat +{ +public : + /** default Ctor, void string by default, as it appears + on the description line once at beginning of evolution. and + is meaningless there */ + eoPopString(string _desc ="") : eoStat("", _desc) {} + +/** Fills the value() of the eoParam with the dump of the population. +Adds a \n before so it does not get mixed up with the rest of the stats +that are written by the monitor it is probably used from. +*/ +void operator()(const eoPop& _pop) +{ + char buffer[1023]; // about one K of space per member + value() = "\n====== Pop dump =====\n"; + for (unsigned i = 0; i < _pop.size(); ++i) + { + std::ostrstream os(buffer, 1022); // leave space for emergency terminate + os << _pop[i] << endl << ends; + + // paranoid: + buffer[1022] = '\0'; + value() += buffer; + } +} +}; + +/** Thanks to MS/VC++, eoParam mechanism is unable to handle vectors of stats. +This snippet is a workaround: +This class will "print" a whole population into a string - that you can later +send to any stream +This is the Sorted version - see eoPopString for the plain version + +Note: this Stat should probably be used only within eoStdOutMonitor, and not +inside an eoFileMonitor, as the eoState construct will work much better there. +*/ +template +class eoSortedPopString : public eoSortedStat +{ +public : + /** default Ctor, void string by default, as it appears + on the description line once at beginning of evolution. and + is meaningless there */ + eoSortedPopString(string _desc ="") : eoSortedStat("", _desc) {} + +/** Fills the value() of the eoParam with the dump of the population. +Adds a \n before so it does not get mixed up with the rest of the stats +that are written by the monitor it is probably used from. +*/ +void operator()(const vector& _pop) +{ + char buffer[1023]; // about one K of space per member + value() = "\n====== Pop dump =====\n"; + for (unsigned i = 0; i < _pop.size(); ++i) + { + std::ostrstream os(buffer, 1022); // leave space for emergency terminate + os << *_pop[i] << endl << ends; + + // paranoid: + buffer[1022] = '\0'; + value() += buffer; + } +} +}; + +#endif diff --git a/eo/tutorial/Lesson3/exercise3.1.cpp b/eo/tutorial/Lesson3/exercise3.1.cpp index e80b43ed..3079ed12 100644 --- a/eo/tutorial/Lesson3/exercise3.1.cpp +++ b/eo/tutorial/Lesson3/exercise3.1.cpp @@ -292,6 +292,11 @@ void main_function(int argc, char **argv) monitor.add(SecondStat); monitor.add(fdcStat); + // test de eoPopStat + eoPopString popStat("Dump of whole population"); + checkpoint.add(popStat); + monitor.add(popStat); + // A file monitor: will print parameters to ... a File, yes, you got it! eoFileMonitor fileMonitor("stats.xg", " "); // and an eoGnuplot1DMonitor will 1-print to a file, and 2- plot on screen @@ -312,7 +317,7 @@ void main_function(int argc, char **argv) // send a scaling command to gnuplot gnuMonitor.gnuplotCommand("set yrange [0:500]"); - + /* // a specific plot monitor for FDC // first into a file (it adds everything ti itself eoFDCFileSnapshot fdcFileSnapshot(fdcStat); @@ -332,7 +337,7 @@ void main_function(int argc, char **argv) fitSnapshot.add(fitStat); // and of course add it to the checkpoint checkpoint.add(fitSnapshot); - + */ // Last type of item the eoCheckpoint can handle: state savers: eoState outState; // Register the algorithm into the state (so it has something to save!!)