Adding Maarten's "snippet" to dump the whole pop to the screen: eoPopStat.h
Modified utils/checkpointing accordingly. Tested in tutorial/Lesson3/exercise3.1 BTW, the eoFileSnapShot does not work any more - I've commented it out and will look at that later...
This commit is contained in:
parent
ebca71e228
commit
1726a06861
3 changed files with 139 additions and 16 deletions
|
|
@ -1,15 +1,16 @@
|
|||
#include <utils/eoParser.h>
|
||||
#include <utils/eoState.h>
|
||||
#include <utils/eoUpdater.h>
|
||||
#include <utils/eoMonitor.h>
|
||||
#include <utils/eoFileMonitor.h>
|
||||
#include <utils/eoStdoutMonitor.h>
|
||||
#ifndef _MSC_VER
|
||||
#include <utils/eoGnuplot1DMonitor.h>
|
||||
#include <utils/eoGnuplot1DSnapshot.h>
|
||||
#endif
|
||||
#include <utils/eoCheckPoint.h>
|
||||
#include <utils/eoStat.h>
|
||||
#include <utils/eoScalarFitnessStat.h>
|
||||
#include <utils/eoFDCStat.h>
|
||||
#include <utils/eoParser.h>
|
||||
#include <utils/eoState.h>
|
||||
#include <utils/eoUpdater.h>
|
||||
#include <utils/eoMonitor.h>
|
||||
#include <utils/eoFileMonitor.h>
|
||||
#include <utils/eoStdoutMonitor.h>
|
||||
#ifndef _MSC_VER
|
||||
#include <utils/eoGnuplot1DMonitor.h>
|
||||
#include <utils/eoGnuplot1DSnapshot.h>
|
||||
#endif
|
||||
#include <utils/eoCheckPoint.h>
|
||||
#include <utils/eoStat.h>
|
||||
#include <utils/eoScalarFitnessStat.h>
|
||||
#include <utils/eoFDCStat.h>
|
||||
#include <utils/eoMOFitnessStat.h>
|
||||
#include <utils/eoPopStat.h>
|
||||
117
eo/src/utils/eoPopStat.h
Normal file
117
eo/src/utils/eoPopStat.h
Normal file
|
|
@ -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 <utils/eoStat.h>
|
||||
|
||||
|
||||
/** 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 EOT>
|
||||
class eoPopString : public eoStat<EOT, string>
|
||||
{
|
||||
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<EOT, string>("", _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<EOT>& _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 EOT>
|
||||
class eoSortedPopString : public eoSortedStat<EOT, string>
|
||||
{
|
||||
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<EOT, string>("", _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<const EOT*>& _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
|
||||
|
|
@ -292,6 +292,11 @@ void main_function(int argc, char **argv)
|
|||
monitor.add(SecondStat);
|
||||
monitor.add(fdcStat);
|
||||
|
||||
// test de eoPopStat
|
||||
eoPopString<Indi> 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<Indi> 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!!)
|
||||
|
|
|
|||
Reference in a new issue