Ok, updated the Makefile.am again to use the

make check

Command I picked up in the automake documentation (RTFM, you know)

Tagged a lot of header functions in the GnuPlot files with 'inline',
so they can be used from more than one sourcefile.

Ok, now the interesting news. Started a new library libga (not to be confused
with Matthew's GaLib). Here I suggest we put a fairly complete and configurable
genetic algorithm. Just to see how far we can stretch ourselves and also to have
a GA-componenent that can be used in other applications without having to rebuild
the entire thing. test/t-eoGA.cpp tests this library
This commit is contained in:
maartenkeijzer 2001-02-12 13:58:51 +00:00
commit dea8a51f7e
12 changed files with 260 additions and 81 deletions

View file

@ -4,7 +4,7 @@
##
###############################################################################
SUBDIRS = src win tutorial app
SUBDIRS = src test win tutorial app
#Directory for documents
DOCDIR = ~/public_html/eodocs
#Directory for indices -- not useful for the user
@ -17,8 +17,9 @@ EXTRA_DIST=LICENSE
lib:
pushd src; $(MAKE) all; popd
test: test/run_tests
pushd test; $(MAKE) all; ./run_tests; touch run_tests; popd
# The test directory should be run explicitely, to check if nothing is broken
test: test/run_tests test/Makefile
pushd test; touch run_tests; $(MAKE) all; ./run_tests; popd
# so that make doc always compiles the doc ...
doc: doc/eo.cfg

View file

@ -4,5 +4,11 @@
##
###############################################################################
INCLUDES = -I$(top_builddir)/src
lib_LIBRARIES = libga.a
libga_a_SOURCES = ga.cpp
CPPFLAGS = -O2 -Wall
libeoincdir = $(includedir)/eo/ga
libeoinc_HEADERS = eoBit.h eoBitOp.h eoBitOpFactory.h
libeoinc_HEADERS = eoBit.h eoBitOp.h eoBitOpFactory.h ga.h

91
eo/src/ga/ga.cpp Normal file
View file

@ -0,0 +1,91 @@
#include <eo>
#include <ga/ga.h>
eoValueParam<float> xoverRate(0.6f, "xoverrate", "The crossover rate", 'x');
eoValueParam<float> mutRate(1.0f, "mutationrate", "The mutation rate", 'm');
eoValueParam<unsigned> chromSize(unsigned(10), "chromosomeSize", "The length of the bitstrings", 'n');
eoValueParam<unsigned> popSize(unsigned(20), "PopSize", "Population Size", 'P');
template <class FitT>
eoAlgo<eoBit<FitT> >& do_make_ga(eoParameterLoader& _parser, eoEvalFunc<eoBit<FitT> >& _eval, eoCheckPoint<eoBit<FitT> >& _checkpoint, eoState& _state)
{
typedef eoBit<FitT> EOT;
_parser.processParam(xoverRate, "genetics");
_parser.processParam(mutRate, "genetics");
_parser.processParam(chromSize, "initialization");
eoBitMutation<EOT>* mutOp = new eoBitMutation<EOT>(1. / float(chromSize.value()));
_state.storeFunctor(mutOp);
eo1PtBitXover<EOT>* crossOp = new eo1PtBitXover<EOT>;
_state.storeFunctor(crossOp);
eoSelectOne<EOT>* select = new eoDetTournamentSelect<EOT>(2);
_state.storeFunctor(select);
eoSGA<eoBit<FitT> >* sga = new eoSGA<EOT>(*select, *crossOp, xoverRate.value(), *mutOp, mutRate.value(), _eval, _checkpoint);
_state.storeFunctor(sga);
return *sga;
}
template <class FitT>
eoPop<eoBit<FitT> >& do_init_ga(eoParameterLoader& _parser, eoState& _state, FitT)
{
typedef eoBit<FitT> EOT;
_parser.processParam(chromSize, "initialization");
_parser.processParam(popSize, "initialization");
eoInitFixedLength<EOT, boolean_generator> init(chromSize.value(), boolean_generator());
// Let the state handle the memory
eoPop<EOT>& pop = _state.takeOwnership(eoPop<EOT>());
_state.registerObject(pop);
// initialize the population
pop.append(popSize.value(), init);
return pop;
}
template <class FitT>
void do_run_ga(eoAlgo<eoBit<FitT> >& _ga, eoPop<eoBit<FitT> >& _pop)
{
_ga(_pop);
}
/// The following function merely call the templatized do_* functions above
eoAlgo<eoBit<double> >& make_ga(eoParameterLoader& _parser, eoEvalFunc<eoBit<double> >& _eval, eoCheckPoint<eoBit<double> >& _checkpoint, eoState& _state)
{
return do_make_ga(_parser, _eval, _checkpoint, _state);
}
eoAlgo<eoBit<eoMinimizingFitness> >& make_ga(eoParameterLoader& _parser, eoEvalFunc<eoBit<eoMinimizingFitness> >& _eval, eoCheckPoint<eoBit<eoMinimizingFitness> >& _checkpoint, eoState& _state)
{
return do_make_ga(_parser, _eval, _checkpoint, _state);
}
eoPop<eoBit<double> >& init_ga(eoParameterLoader& _parser, eoState& _state, double _d)
{
return do_init_ga(_parser, _state, _d);
}
eoPop<eoBit<eoMinimizingFitness> >& init_ga(eoParameterLoader& _parser, eoState& _state, eoMinimizingFitness _d)
{
return do_init_ga(_parser, _state, _d);
}
void run_ga(eoAlgo<eoBit<double> >& _ga, eoPop<eoBit<double> >& _pop)
{
do_run_ga(_ga, _pop);
}
void run_ga(eoAlgo<eoBit<eoMinimizingFitness> >& _ga, eoPop<eoBit<eoMinimizingFitness> >& _pop)
{
do_run_ga(_ga, _pop);
}

24
eo/src/ga/ga.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef ga_h
#define ga_h
#include <eoAlgo.h>
#include <eoScalarFitness.h>
#include <utils/eoParser.h>
#include <eoEvalFunc.h>
#include <utils/eoCheckPoint.h>
#include <eoPop.h>
#include <ga/eoBit.h>
#include <ga/eoBitOp.h>
eoAlgo<eoBit<double> >& make_ga(eoParameterLoader& _parser, eoEvalFunc<eoBit<double> >& _eval, eoCheckPoint<eoBit<double> >& _checkpoint, eoState& state);
eoAlgo<eoBit<eoMinimizingFitness> >& make_ga(eoParameterLoader& _parser, eoEvalFunc<eoBit<eoMinimizingFitness> >& _eval, eoCheckPoint<eoBit<eoMinimizingFitness> >& _checkpoint, eoState& state);
eoPop<eoBit<double> >& init_ga(eoParameterLoader& _parser, eoState& _state, double);
eoPop<eoBit<eoMinimizingFitness> >& init_ga(eoParameterLoader& _parser, eoState& _state, eoMinimizingFitness);
void run_ga(eoAlgo<eoBit<double> >& _ga, eoPop<eoBit<double> >& _pop);
void run_ga(eoAlgo<eoBit<eoMinimizingFitness> >& _ga, eoPop<eoBit<eoMinimizingFitness> >& _pop);
#endif

View file

@ -5,7 +5,7 @@
###############################################################################
INCLUDES = -I$(top_builddir)/src
CPPFLAGS = -O2
CPPFLAGS = -O2 -Wall
lib_LIBRARIES = libeoutils.a
libeoutils_a_SOURCES = eoParser.cpp eoRNG.cpp eoState.cpp eoUpdater.cpp eoFileMonitor.cpp eoStdoutMonitor.cpp

View file

@ -78,7 +78,7 @@ class eoGnuplot
}
protected:
protected:
void initGnuPlot(std::string _title, std::string _extra);
// the private data
bool firstTime; // the stats might be unknown in Ctor
@ -86,12 +86,12 @@ protected:
private:
};
// the following should be placed in a separate eoGnuplot.cpp
// the following should be placed in a separate eoGnuplot.cpp
static unsigned numWindow=0;
////////////////////////////////////////////////////////////
void eoGnuplot::initGnuPlot(std::string _title, std::string _extra)
inline void eoGnuplot::initGnuPlot(std::string _title, std::string _extra)
/////////////////////////////////////////////////////////
{
char snum[255];
@ -103,7 +103,7 @@ void eoGnuplot::initGnuPlot(std::string _title, std::string _extra)
args[1] = strdup( "-geometry" );
args[2] = strdup( os.str() );
args[3] = strdup( "-title" );
args[4] = strdup( _title.c_str() );
args[4] = strdup( _title.c_str() );
args[5] = 0;
gpCom = PipeComOpenArgv( "gnuplot", args );
if( ! gpCom )
@ -115,21 +115,21 @@ void eoGnuplot::initGnuPlot(std::string _title, std::string _extra)
}
}
// the following should be placed in a separate file pipecom.c
// together with the corresponding pipecom.h
// but first their MSC equivalent must be written and tested
// but first their MSC equivalent must be written and tested
// or some #idef instructions put with clear message at compile time
// that this is for Unix only ???
/* ----------------------------------------------------------------------
* Where........: CMAP - Polytechnique
* Where........: CMAP - Polytechnique
* File.........: pipecom.c
* Author.......: Bertrand Lamy (Equipe genetique)
* Created......: Mon Mar 13 13:50:11 1995
* Description..: Communication par pipe bidirectionnel avec un autre process
*
* Ident........: $Id: eoGnuplot.h,v 1.2 2001-02-01 05:17:16 evomarc Exp $
*
* Ident........: $Id: eoGnuplot.h,v 1.3 2001-02-12 13:58:51 maartenkeijzer Exp $
* ----------------------------------------------------------------------
*/
@ -142,7 +142,7 @@ void eoGnuplot::initGnuPlot(std::string _title, std::string _extra)
// #include "pipecom.h"
int Check( PCom *com )
inline int Check( PCom *com )
{
if( ! com ) {
fprintf( stderr, "PipeCom: Null pointer.\n" );
@ -158,16 +158,16 @@ int Check( PCom *com )
}
PCom * PipeComOpen( char *prog )
inline PCom * PipeComOpen( char *prog )
{
char *args[2];
args[0] = prog;
args[1] = NULL;
return PipeComOpenArgv( prog, args );
return PipeComOpenArgv( prog, args );
}
PCom * PipeComOpenArgv( char *prog, char *argv[] )
inline PCom * PipeComOpenArgv( char *prog, char *argv[] )
{
int toFils[2];
int toPere[2];
@ -188,7 +188,7 @@ PCom * PipeComOpenArgv( char *prog, char *argv[] )
perror("PipeComOpen: fork failed" );
return ret;
break;
case 0:
/* --- Here's the son --- */
/* --- replace old stdin --- */
@ -197,7 +197,7 @@ PCom * PipeComOpenArgv( char *prog, char *argv[] )
exit( -1 );
/* --- AVOIR: kill my father --- */
}
if( dup2( toPere[1], fileno(stdout) ) < 0 ) {
if( dup2( toPere[1], fileno(stdout) ) < 0 ) {
perror( "PipeComOpen(son): could not connect" );
exit( -1 );
}
@ -211,7 +211,7 @@ PCom * PipeComOpenArgv( char *prog, char *argv[] )
ret = (PCom *) malloc( sizeof(PCom) );
if( ! ret )
return NULL;
ret->fWrit = (FILE *)fdopen( toFils[1], "w" );
ret->fRead = (FILE *)fdopen( toPere[0], "r" );
ret->pid = sonPid;
@ -220,7 +220,7 @@ PCom * PipeComOpenArgv( char *prog, char *argv[] )
}
int PipeComSend( PCom *to, const char *line )
inline int PipeComSend( PCom *to, const char *line )
{
int nb = 0;
if( ! Check(to ) )
@ -231,10 +231,10 @@ int PipeComSend( PCom *to, const char *line )
}
int PipeComSendn( PCom *to, const char *data, int n )
inline int PipeComSendn( PCom *to, const char *data, int n )
{
int nb = 0;
if( ! Check(to) )
if( ! Check(to) )
return nb;
nb = fwrite( data, 1, n, to->fWrit );
@ -243,9 +243,9 @@ int PipeComSendn( PCom *to, const char *data, int n )
}
int PipeComReceive( PCom *from, char *data, int max )
inline int PipeComReceive( PCom *from, char *data, int max )
{
if( ! Check(from) )
if( ! Check(from) )
return 0;
if( ! data ) {
fprintf( stderr, "PipeComReceive: Invalid data pointer\n" );
@ -259,7 +259,7 @@ int PipeComReceive( PCom *from, char *data, int max )
int PipeComClose( PCom *to )
inline int PipeComClose( PCom *to )
{
if( ! Check(to) )
return 0;
@ -271,7 +271,7 @@ int PipeComClose( PCom *to )
int PipeComWaitFor( PCom *from, char *what )
inline int PipeComWaitFor( PCom *from, char *what )
{
char buffer[256];
do {

View file

@ -48,34 +48,34 @@ This class plots through gnuplot the eoStat given as argument
/** eoGnuplot1DMonitor plots stats through gnuplot
* assumes that the same file is appened every so and so,
* assumes that the same file is appened every so and so,
* and replots it everytime
*/
class eoGnuplot1DMonitor: public eoFileMonitor, public eoGnuplot
{
public:
// Ctor
eoGnuplot1DMonitor(std::string _filename, bool _top=false) :
eoFileMonitor(_filename, " "),
eoGnuplot1DMonitor(std::string _filename, bool _top=false) :
eoFileMonitor(_filename, " "),
eoGnuplot(_filename,(_top?"":"set key bottom"))
{}
// Dtor
virtual ~eoGnuplot1DMonitor(){}
virtual eoMonitor& operator() (void) ;
virtual void FirstPlot();
virtual void FirstPlot();
/// Class name.
virtual string className() const { return "eoGnuplot1DMonitor"; }
private:
private:
};
// the following should be placed in a separate eoGnuplot1DMonitor.cpp
// the following should be placed in a separate eoGnuplot1DMonitor.cpp
// then the inline specifier should dissappear
////////////////////////////////////////////////////////////
eoMonitor& eoGnuplot1DMonitor::operator() (void)
inline eoMonitor& eoGnuplot1DMonitor::operator() (void)
/////////////////////////////////////////////////////////
{
// update file using the eoFileMonitor
@ -98,10 +98,10 @@ eoMonitor& eoGnuplot1DMonitor::operator() (void)
}
////////////////////////////////////////////////////////////
void eoGnuplot1DMonitor::FirstPlot()
inline void eoGnuplot1DMonitor::FirstPlot()
////////////////////////////////////////////////////////
{
if (vec.size() < 2)
if (vec.size() < 2)
{
throw runtime_error("Must have some stats to plot!\n");
}
@ -118,5 +118,5 @@ void eoGnuplot1DMonitor::FirstPlot()
os << '\0';
PipeComSend( gpCom, buff );
}
#endif _eoGnuplot1DMonitor_H

View file

@ -81,7 +81,7 @@ private:
// the following should be placed in a separate eoGnuplot1DMonitor.cpp
////////////////////////////////////////////////////////////
eoMonitor& eoGnuplot1DSnapshot::operator() (void)
inline eoMonitor& eoGnuplot1DSnapshot::operator() (void)
/////////////////////////////////////////////////////////
{
// update file using the eoFileMonitor

View file

@ -3,7 +3,7 @@
//-----------------------------------------------------------------------------
// eoState.h
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000
/*
/*
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
@ -32,6 +32,8 @@
#include <map>
#include <vector>
#include <eoFunctorStore.h>
class eoObject;
class eoPersistent;
@ -40,7 +42,7 @@ class eoPersistent;
* then in turn implement the persistence framework through members load
* and save, that will call readFrom and printOn for the registrated objects.
*/
class eoState
class eoState
{
public :
@ -54,7 +56,7 @@ public :
void registerObject(eoPersistent& registrant);
/**
* Copies the object (MUST be derived from eoPersistent)
* Copies the object (MUST be derived from eoPersistent)
* and returns a reference to the owned object.
* Note: it does not register the object, this must be done afterwards!
*/
@ -65,7 +67,13 @@ public :
ownedObjects.push_back(new T(persistent));
return static_cast<T&>(*ownedObjects.back());
}
void storeFunctor(eoFunctorBase* _functor)
{
// add it to the functorStore, fo
functorStore.add(_functor);
}
/**
* Loading error thrown when nothing seems to work.
*/
@ -82,21 +90,21 @@ public :
* @param _filename the name of the file to load from
*/
void load(const std::string& _filename);
/**
* Reads the file specified
*
* @param is the stream to load from
*/
void load(std::istream& is);
/**
* Saves the state in file specified
*
* @param _filename the name of the file to save into
*/
void save(const std::string& _filename) const;
/**
* Saves the state in file specified
*
@ -109,13 +117,15 @@ private :
// first is Persistent, second is the raw data associated with it.
typedef std::map<std::string, eoPersistent*> ObjectMap;
ObjectMap objectMap;
std::vector<ObjectMap::iterator> creationOrder;
std::vector<eoPersistent*> ownedObjects;
// And a functor store to boot
eoFunctorStore functorStore;
// private copy and assignment as eoState is supposed to be unique
eoState(const eoState&);
eoState& operator=(const eoState&);

View file

@ -13,16 +13,17 @@ LDADDS = $(top_builddir)/src/libeo.a $(top_builddir)/src/utils/libeoutils.a
CXXFLAGS = -g
###############################################################################
noinst_PROGRAMS = t-eofitness t-eobin t-eoStateAndParser t-eoCheckpointing t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp
check_PROGRAMS = t-eofitness t-eobin t-eoStateAndParser t-eoCheckpointing t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA
TESTS=run_tests
# removing temporarily t-eoESFull
# noinst_PROGRAMS = t-eofitness t-eobin t-eoStateAndParser t-eoCheckpointing t-eoExternalEO t-eoESFull t-eoSymreg t-eo t-eoReplacement t-eoSelect
#noinst_PROGRAMS = t-eofitness t-eobin t-eoStateAndParser t-eoCheckpointing t-eoExternalEO t-eoESFull t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA
###############################################################################
# t_eoESFull_SOURCES = t-eoESFull.cpp real_value.h
# t_eoESFull_DEPENDENCIES = $(DEPS)
# t_eoESFull_LDFLAGS = -lm
# t_eoESFull_LDADD = $(LDADDS)
#t_eoESFull_SOURCES = t-eoESFull.cpp real_value.h
#t_eoESFull_DEPENDENCIES = $(DEPS)
#t_eoESFull_LDFLAGS = -lm
#t_eoESFull_LDADD = $(LDADDS)
###############################################################################
@ -39,57 +40,64 @@ t_eobin_LDADD = $(LDADDS)
###############################################################################
t_eoStateAndParser_SOURCES = t-eoStateAndParser.cpp
t_eoStateAndParser_DEPENDENCIES = $(DEPS)
t_eoStateAndParser_LDFLAGS = -lm
t_eoStateAndParser_LDADD = $(LDADDS)
t_eoStateAndParser_DEPENDENCIES = $(DEPS)
t_eoStateAndParser_LDFLAGS = -lm
t_eoStateAndParser_LDADD = $(LDADDS)
###############################################################################
t_eoCheckpointing_SOURCES = t-eoCheckpointing.cpp
t_eoCheckpointing_DEPENDENCIES = $(DEPS)
t_eoCheckpointing_LDFLAGS = -lm
t_eoCheckpointing_LDADD = $(LDADDS)
t_eoCheckpointing_DEPENDENCIES = $(DEPS)
t_eoCheckpointing_LDFLAGS = -lm
t_eoCheckpointing_LDADD = $(LDADDS)
###############################################################################
t_eoReplacement_SOURCES = t-eoReplacement.cpp
t_eoReplacement_DEPENDENCIES = $(DEPS)
t_eoReplacement_LDFLAGS = -lm
t_eoReplacement_LDADD = $(LDADDS)
t_eoReplacement_DEPENDENCIES = $(DEPS)
t_eoReplacement_LDFLAGS = -lm
t_eoReplacement_LDADD = $(LDADDS)
###############################################################################
t_eoSelect_SOURCES = t-eoSelect.cpp
t_eoSelect_DEPENDENCIES = $(DEPS)
t_eoSelect_LDFLAGS = -lm
t_eoSelect_LDADD = $(LDADDS)
t_eoSelect_DEPENDENCIES = $(DEPS)
t_eoSelect_LDFLAGS = -lm
t_eoSelect_LDADD = $(LDADDS)
###############################################################################
t_eoExternalEO_SOURCES = t-eoExternalEO.cpp
t_eoExternalEO_DEPENDENCIES = $(DEPS)
t_eoExternalEO_LDFLAGS = -lm
t_eoExternalEO_LDADD = $(LDADDS)
t_eoExternalEO_DEPENDENCIES = $(DEPS)
t_eoExternalEO_LDFLAGS = -lm
t_eoExternalEO_LDADD = $(LDADDS)
###############################################################################
t_eoSymreg_SOURCES = t-eoSymreg.cpp
t_eoSymreg_DEPENDENCIES = $(DEPS)
t_eoSymreg_LDFLAGS = -lm
t_eoSymreg_LDADD = $(LDADDS)
t_eoSymreg_DEPENDENCIES = $(DEPS)
t_eoSymreg_LDFLAGS = -lm
t_eoSymreg_LDADD = $(LDADDS)
###############################################################################
t_eo_SOURCES = t-eo.cpp
t_eo_DEPENDENCIES = $(DEPS)
t_eo_LDFLAGS = -lm
t_eo_LDADD = $(LDADDS)
t_eo_DEPENDENCIES = $(DEPS)
t_eo_LDFLAGS = -lm
t_eo_LDADD = $(LDADDS)
###############################################################################
t_eoGenOp_SOURCES = t-eoGenOp.cpp
t_eoGenOp_DEPENDENCIES = $(DEPS)
t_eoGenOp_LDFLAGS = -lm
t_eoGenOp_LDADD = $(LDADDS)
t_eoGenOp_DEPENDENCIES = $(DEPS)
t_eoGenOp_LDFLAGS = -lm
t_eoGenOp_LDADD = $(LDADDS)
###############################################################################
t_eoGA_SOURCES = t-eoGA.cpp binary_value.h
t_eoGA_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a
t_eoGA_LDFLAGS = -lm
t_eoGA_LDADD = $(top_builddir)/src/ga/libga.a $(LDADDS)
###############################################################################

View file

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/sh
echo "Starting t-eo"
./t-eo > eo.log
@ -33,6 +33,9 @@ echo "Testing t-eobin"
echo "Testing t-eofitness"
./t-eofitness > fitness.log
echo "Testing t-eoGA"
./t-eoGA > t-eoGA.log
echo "Finished"
#TODO test if an error occured

36
eo/test/t-eoGA.cpp Normal file
View file

@ -0,0 +1,36 @@
#include <iostream>
#include <ga/ga.h>
#include "binary_value.h"
#include <apply.h>
using namespace std;
int main(int argc, char* argv[])
{
try
{
typedef eoBit<double> EoType;
eoParser parser(argc, argv);
eoState state; // keeps all things allocated, including eoEasyEA and eoPop!
eoEvalFuncPtr<EoType, float> eval( binary_value<EoType> );
eoGenContinue<EoType> term(20);
eoCheckPoint<EoType> checkpoint(term);
eoAlgo<EoType>& ga = make_ga(parser, eval, checkpoint, state);
eoPop<EoType>& pop = init_ga(parser, state, double());
apply(eval, pop);
run_ga(ga, pop); // run the ga
}
catch(exception& e)
{
cout << e.what() << endl;
}
}