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 #Directory for documents
DOCDIR = ~/public_html/eodocs DOCDIR = ~/public_html/eodocs
#Directory for indices -- not useful for the user #Directory for indices -- not useful for the user
@ -17,8 +17,9 @@ EXTRA_DIST=LICENSE
lib: lib:
pushd src; $(MAKE) all; popd pushd src; $(MAKE) all; popd
test: test/run_tests # The test directory should be run explicitely, to check if nothing is broken
pushd test; $(MAKE) all; ./run_tests; touch run_tests; popd test: test/run_tests test/Makefile
pushd test; touch run_tests; $(MAKE) all; ./run_tests; popd
# so that make doc always compiles the doc ... # so that make doc always compiles the doc ...
doc: doc/eo.cfg 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 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 INCLUDES = -I$(top_builddir)/src
CPPFLAGS = -O2 CPPFLAGS = -O2 -Wall
lib_LIBRARIES = libeoutils.a lib_LIBRARIES = libeoutils.a
libeoutils_a_SOURCES = eoParser.cpp eoRNG.cpp eoState.cpp eoUpdater.cpp eoFileMonitor.cpp eoStdoutMonitor.cpp libeoutils_a_SOURCES = eoParser.cpp eoRNG.cpp eoState.cpp eoUpdater.cpp eoFileMonitor.cpp eoStdoutMonitor.cpp

View file

@ -91,7 +91,7 @@ private:
static unsigned numWindow=0; 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]; char snum[255];
@ -129,7 +129,7 @@ void eoGnuplot::initGnuPlot(std::string _title, std::string _extra)
* Created......: Mon Mar 13 13:50:11 1995 * Created......: Mon Mar 13 13:50:11 1995
* Description..: Communication par pipe bidirectionnel avec un autre process * 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" // #include "pipecom.h"
int Check( PCom *com ) inline int Check( PCom *com )
{ {
if( ! com ) { if( ! com ) {
fprintf( stderr, "PipeCom: Null pointer.\n" ); fprintf( stderr, "PipeCom: Null pointer.\n" );
@ -158,7 +158,7 @@ int Check( PCom *com )
} }
PCom * PipeComOpen( char *prog ) inline PCom * PipeComOpen( char *prog )
{ {
char *args[2]; char *args[2];
args[0] = prog; args[0] = prog;
@ -167,7 +167,7 @@ PCom * PipeComOpen( char *prog )
} }
PCom * PipeComOpenArgv( char *prog, char *argv[] ) inline PCom * PipeComOpenArgv( char *prog, char *argv[] )
{ {
int toFils[2]; int toFils[2];
int toPere[2]; int toPere[2];
@ -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; int nb = 0;
if( ! Check(to ) ) if( ! Check(to ) )
@ -231,7 +231,7 @@ 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; int nb = 0;
if( ! Check(to) ) if( ! Check(to) )
@ -243,7 +243,7 @@ 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; return 0;
@ -259,7 +259,7 @@ int PipeComReceive( PCom *from, char *data, int max )
int PipeComClose( PCom *to ) inline int PipeComClose( PCom *to )
{ {
if( ! Check(to) ) if( ! Check(to) )
return 0; 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]; char buffer[256];
do { do {

View file

@ -73,9 +73,9 @@ 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 // update file using the eoFileMonitor
@ -98,7 +98,7 @@ eoMonitor& eoGnuplot1DMonitor::operator() (void)
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void eoGnuplot1DMonitor::FirstPlot() inline void eoGnuplot1DMonitor::FirstPlot()
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
{ {
if (vec.size() < 2) if (vec.size() < 2)

View file

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

View file

@ -32,6 +32,8 @@
#include <map> #include <map>
#include <vector> #include <vector>
#include <eoFunctorStore.h>
class eoObject; class eoObject;
class eoPersistent; class eoPersistent;
@ -66,6 +68,12 @@ public :
return static_cast<T&>(*ownedObjects.back()); 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. * Loading error thrown when nothing seems to work.
*/ */
@ -113,9 +121,11 @@ private :
ObjectMap objectMap; ObjectMap objectMap;
std::vector<ObjectMap::iterator> creationOrder; std::vector<ObjectMap::iterator> creationOrder;
std::vector<eoPersistent*> ownedObjects; std::vector<eoPersistent*> ownedObjects;
// And a functor store to boot
eoFunctorStore functorStore;
// private copy and assignment as eoState is supposed to be unique // private copy and assignment as eoState is supposed to be unique
eoState(const eoState&); eoState(const eoState&);
eoState& operator=(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 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 # 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_SOURCES = t-eoESFull.cpp real_value.h
# t_eoESFull_DEPENDENCIES = $(DEPS) #t_eoESFull_DEPENDENCIES = $(DEPS)
# t_eoESFull_LDFLAGS = -lm #t_eoESFull_LDFLAGS = -lm
# t_eoESFull_LDADD = $(LDADDS) #t_eoESFull_LDADD = $(LDADDS)
############################################################################### ###############################################################################
@ -93,3 +94,10 @@ t_eoGenOp_LDFLAGS = -lm
t_eoGenOp_LDADD = $(LDADDS) 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

@ -33,6 +33,9 @@ echo "Testing t-eobin"
echo "Testing t-eofitness" echo "Testing t-eofitness"
./t-eofitness > fitness.log ./t-eofitness > fitness.log
echo "Testing t-eoGA"
./t-eoGA > t-eoGA.log
echo "Finished" echo "Finished"
#TODO test if an error occured #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;
}
}