Doing libes.a in src/es and adding t-eoReal.cpp in test
I had to move eoRealBounds to utils dir because of global objects Some domino consequences here and there ...
This commit is contained in:
parent
511dc26623
commit
4968d64b05
19 changed files with 120 additions and 25 deletions
|
|
@ -48,9 +48,6 @@
|
||||||
#include <es/eoRealOp.h>
|
#include <es/eoRealOp.h>
|
||||||
#include <es/eoNormalMutation.h>
|
#include <es/eoNormalMutation.h>
|
||||||
|
|
||||||
// and their generic counterparts
|
|
||||||
#include <es/eoGenericRealOp.h>
|
|
||||||
|
|
||||||
// the ES-mutations
|
// the ES-mutations
|
||||||
#include <es/eoEsMutationInit.h>
|
#include <es/eoEsMutationInit.h>
|
||||||
#include <es/eoEsMutate.h>
|
#include <es/eoEsMutate.h>
|
||||||
|
|
|
||||||
|
|
@ -4,5 +4,16 @@
|
||||||
##
|
##
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
|
INCLUDES = -I$(top_builddir)/src
|
||||||
|
lib_LIBRARIES = libes.a
|
||||||
|
libes_a_SOURCES = make_algo_scalar_real.cpp \
|
||||||
|
make_checkpoint_real.cpp \
|
||||||
|
make_continue_real.cpp \
|
||||||
|
make_genotype_real.cpp \
|
||||||
|
make_help.cpp \
|
||||||
|
make_op_real.cpp \
|
||||||
|
make_pop_real.cpp \
|
||||||
|
make_run_real.cpp
|
||||||
|
CPPFLAGS = -Wall
|
||||||
libeoincdir = $(includedir)/eo/es
|
libeoincdir = $(includedir)/eo/es
|
||||||
libeoinc_HEADERS = eoEsChromInit.h eoEsFull.h eoEsMutate.h eoEsMutationInit.h eoEsSimple.h eoEsStdev.h eoEsStdevXOver.h eoGenericRealOp.h eoNormalMutation.h eoReal.h eoRealBounds.h eoRealOp.h
|
libeoinc_HEADERS = eoEsChromInit.h eoEsFull.h eoEsMutate.h eoEsMutationInit.h eoEsSimple.h eoEsStdev.h eoEsStdevXOver.h eoGenericRealOp.h eoNormalMutation.h eoReal.h eoRealBounds.h eoRealOp.h
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@
|
||||||
#ifndef _eoEsChromInit_H
|
#ifndef _eoEsChromInit_H
|
||||||
#define _eoEsChromInit_H
|
#define _eoEsChromInit_H
|
||||||
|
|
||||||
#include <es/eoRealBounds.h>
|
#include <utils/eoRealBounds.h>
|
||||||
#include <es/eoEsSimple.h>
|
#include <es/eoEsSimple.h>
|
||||||
#include <es/eoEsStdev.h>
|
#include <es/eoEsStdev.h>
|
||||||
#include <es/eoEsFull.h>
|
#include <es/eoEsFull.h>
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@
|
||||||
#include <es/eoEsSimple.h>
|
#include <es/eoEsSimple.h>
|
||||||
#include <es/eoEsStdev.h>
|
#include <es/eoEsStdev.h>
|
||||||
#include <es/eoEsFull.h>
|
#include <es/eoEsFull.h>
|
||||||
#include <es/eoRealBounds.h>
|
#include <utils/eoRealBounds.h>
|
||||||
|
|
||||||
#include <eoOp.h>
|
#include <eoOp.h>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@
|
||||||
#define _eoEsStdevXOver_h
|
#define _eoEsStdevXOver_h
|
||||||
|
|
||||||
#include <es/eoEsStdev.h>
|
#include <es/eoEsStdev.h>
|
||||||
#include <es/eoGenericRealOp.h>
|
#include <es/eoRealOp.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
\ingroup EvolutionStrategies
|
\ingroup EvolutionStrategies
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@
|
||||||
#include <utils/eoUpdatable.h>
|
#include <utils/eoUpdatable.h>
|
||||||
#include <eoEvalFunc.h>
|
#include <eoEvalFunc.h>
|
||||||
#include <es/eoReal.h>
|
#include <es/eoReal.h>
|
||||||
#include <es/eoRealBounds.h>
|
#include <utils/eoRealBounds.h>
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
/** Simple normal mutation of a vector of real values.
|
/** Simple normal mutation of a vector of real values.
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@
|
||||||
#include <algorithm> // swap_ranges
|
#include <algorithm> // swap_ranges
|
||||||
#include <utils/eoRNG.h>
|
#include <utils/eoRNG.h>
|
||||||
#include <es/eoReal.h>
|
#include <es/eoReal.h>
|
||||||
#include <es/eoRealBounds.h>
|
#include <utils/eoRealBounds.h>
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,15 +24,15 @@
|
||||||
*/
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
/** This file contains all ***INSTANCIATED DEFINITIONS*** of operators
|
/** This file contains all ***INSTANCIATED DEFINITIONS*** of pop. init.
|
||||||
* of the library for ***BISTRING*** evolution inside EO.
|
* of the library for ***BISTRING*** evolution inside EO.
|
||||||
* It should be included in the file that calls any of the corresponding fns
|
* It should be included in the file that calls any of the corresponding fns
|
||||||
* Compiling this file allows one to generate part of the library (i.e. object
|
* Compiling this file allows one to generate part of the library (i.e. object
|
||||||
* files that you just need to link with your own main and fitness code).
|
* files that you just need to link with your own main and fitness code).
|
||||||
*
|
*
|
||||||
* The corresponding ***INSTANCIATED DECLARATIONS*** are contained
|
* The corresponding ***INSTANCIATED DECLARATIONS*** are contained
|
||||||
* in make_algo_scalar_ga.h
|
* in src/ga/ga.h
|
||||||
* while the TEMPLATIZED code is define in make_algo_scalar.h in the do dir
|
* while the TEMPLATIZED code is define in make_algo_scalar.h in the src/do dir
|
||||||
*
|
*
|
||||||
* Unlike most EO .h files, it does not (and should not) contain any code,
|
* Unlike most EO .h files, it does not (and should not) contain any code,
|
||||||
* just declarations
|
* just declarations
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,8 @@
|
||||||
* files that you just need to link with your own main and fitness code).
|
* files that you just need to link with your own main and fitness code).
|
||||||
*
|
*
|
||||||
* The corresponding ***INSTANCIATED DECLARATIONS*** are contained
|
* The corresponding ***INSTANCIATED DECLARATIONS*** are contained
|
||||||
* in make_continue_ga.h
|
* in ga.h
|
||||||
* while the TEMPLATIZED code is define in make_contninue.h in the do dir
|
* while the TEMPLATIZED code is define in make_contninue.h in the src/do dir
|
||||||
*
|
*
|
||||||
* Unlike most EO .h files, it does not (and should not) contain any code,
|
* Unlike most EO .h files, it does not (and should not) contain any code,
|
||||||
* just declarations
|
* just declarations
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,8 @@
|
||||||
* files that you just need to link with your own main and fitness code).
|
* files that you just need to link with your own main and fitness code).
|
||||||
*
|
*
|
||||||
* The corresponding ***INSTANCIATED DECLARATIONS*** are contained
|
* The corresponding ***INSTANCIATED DECLARATIONS*** are contained
|
||||||
* in make_pop_ga.h
|
* in ga.h
|
||||||
* while the TEMPLATIZED code is define in make_pop.h in the do dir
|
* while the TEMPLATIZED code is define in make_pop.h in the src/do dir
|
||||||
*
|
*
|
||||||
* Unlike most EO .h files, it does not (and should not) contain any code,
|
* Unlike most EO .h files, it does not (and should not) contain any code,
|
||||||
* just declarations
|
* just declarations
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
INCLUDES = -I$(top_builddir)/src
|
INCLUDES = -I$(top_builddir)/src
|
||||||
CPPFLAGS = -O2 -Wall
|
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 eoRealBounds.cpp
|
||||||
|
|
||||||
libeoincdir = $(includedir)/eo/utils
|
libeoincdir = $(includedir)/eo/utils
|
||||||
libeoinc_HEADERS = checkpointing *.h
|
libeoinc_HEADERS = checkpointing *.h
|
||||||
|
|
|
||||||
|
|
@ -103,7 +103,7 @@ inline void eoGnuplot1DMonitor::FirstPlot()
|
||||||
{
|
{
|
||||||
if (vec.size() < 2)
|
if (vec.size() < 2)
|
||||||
{
|
{
|
||||||
throw runtime_error("eoGnuplot1DMonitor needs at least 2 stats (X and Y)!\n");
|
throw runtime_error("Must have some stats to plot!\n");
|
||||||
}
|
}
|
||||||
char buff[1024];
|
char buff[1024];
|
||||||
ostrstream os(buff, 1024);
|
ostrstream os(buff, 1024);
|
||||||
|
|
|
||||||
|
|
@ -470,7 +470,7 @@ public:
|
||||||
// so here we do have arguments
|
// so here we do have arguments
|
||||||
string t = _value.substr(pos+1);// the arguments
|
string t = _value.substr(pos+1);// the arguments
|
||||||
_value.resize(pos);
|
_value.resize(pos);
|
||||||
first = _value; // done for the keyword
|
first = _value; // done for the keyword (NOTE: may be empty string!)
|
||||||
|
|
||||||
// now all arguments
|
// now all arguments
|
||||||
string delim(" (),");
|
string delim(" (),");
|
||||||
|
|
|
||||||
7
eo/src/utils/eoRealBounds.cpp
Normal file
7
eo/src/utils/eoRealBounds.cpp
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
#include <ctime>
|
||||||
|
#include "eoRealBounds.h"
|
||||||
|
|
||||||
|
// the global dummy bounds
|
||||||
|
// (used for unbounded variables when bounds are required)
|
||||||
|
eoRealNoBounds eoDummyRealNoBounds;
|
||||||
|
eoRealVectorNoBounds eoDummyVectorNoBounds;
|
||||||
657
eo/src/utils/eoRealBounds.h
Normal file
657
eo/src/utils/eoRealBounds.h
Normal file
|
|
@ -0,0 +1,657 @@
|
||||||
|
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// eoRealBounds.h
|
||||||
|
// (c) Marc Schoenauer 2001, Maarten Keijzer 2000, GeNeura Team, 1998
|
||||||
|
/*
|
||||||
|
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
|
||||||
|
mak@dhi.dk
|
||||||
|
*/
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef _eoRealBounds_h
|
||||||
|
#define _eoRealBounds_h
|
||||||
|
|
||||||
|
#include <stdexcept> // exceptions!
|
||||||
|
#include <utils/eoRNG.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
\defgroup EvolutionStrategies
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
\class eoRealBounds eoRealBounds.h es/eoRealBounds.h
|
||||||
|
\ingroup EvolutionStrategies
|
||||||
|
|
||||||
|
Defines bound classes for real numbers.
|
||||||
|
|
||||||
|
Scalar type:
|
||||||
|
------------
|
||||||
|
Basic class is eoRealBounds, a pure virtual.
|
||||||
|
|
||||||
|
The following pure virtual methods are to be used in mutations:
|
||||||
|
- void foldsInBounds(double &) that folds any value that falls out of
|
||||||
|
the bounds back into the bounds, by bouncing on the limit (if any)
|
||||||
|
- bool isInBounds(double) that simply says whether or not the argument
|
||||||
|
is in the bounds
|
||||||
|
- void truncate(double &) that set the argument to the bound value it
|
||||||
|
it exceeds it
|
||||||
|
|
||||||
|
So mutation can choose
|
||||||
|
- iterate trying until they fall in bounds,
|
||||||
|
- only try once and "repair" by using the foldsInBounds method
|
||||||
|
- only try once and repair using the truncate method (will create a
|
||||||
|
huge bias toward the bound if the soluiton is not far from the bounds)
|
||||||
|
|
||||||
|
There is also a uniform() method that generates a uniform value
|
||||||
|
(if possible, i.e. if bounded) in the interval.
|
||||||
|
|
||||||
|
Derived class are
|
||||||
|
eoRealInterval that holds a minimum and maximum value,
|
||||||
|
eoRealNoBounds the "unbounded bounds" (-infinity, +infinity)
|
||||||
|
eoRealBelowBound the half-bounded interval [min, +infinity)
|
||||||
|
eoRealAboveBound the half-bounded interval (-infinity, max]
|
||||||
|
|
||||||
|
Vector type:
|
||||||
|
------------
|
||||||
|
Class eoRealVectorBounds implements the vectorized version:
|
||||||
|
it is basically a vector of eoRealBounds * and forwards all request
|
||||||
|
to the elements of the vector.
|
||||||
|
|
||||||
|
This file also contains te 2 global variables eoDummyRealNoBounds and
|
||||||
|
eoDummyVectorNoBounds that are used as defaults in ctors (i.e. when no
|
||||||
|
bounds are given, it is assumed unbounded values)
|
||||||
|
|
||||||
|
TODO: have an eoRealBounds.cpp with the longuish parts of the code
|
||||||
|
(and the 2 global variables).
|
||||||
|
*/
|
||||||
|
class eoRealBounds
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~eoRealBounds(){}
|
||||||
|
|
||||||
|
/** Self-Test: true if ***both*** a min and a max
|
||||||
|
*/
|
||||||
|
virtual bool isBounded(void) = 0;
|
||||||
|
|
||||||
|
/** Self-Test: true if no min ***and*** no max
|
||||||
|
* hence no further need to test/truncate/fold anything
|
||||||
|
*/
|
||||||
|
virtual bool hasNoBoundAtAll(void) = 0;
|
||||||
|
|
||||||
|
/** Self-Test: bounded from below???
|
||||||
|
*/
|
||||||
|
virtual bool isMinBounded(void) = 0;
|
||||||
|
|
||||||
|
/** Self-Test: bounded from above???
|
||||||
|
*/
|
||||||
|
virtual bool isMaxBounded(void) = 0;
|
||||||
|
|
||||||
|
/** Test on a value: is it in bounds?
|
||||||
|
*/
|
||||||
|
virtual bool isInBounds(double) = 0;
|
||||||
|
|
||||||
|
/** Put value back into bounds - by folding back and forth
|
||||||
|
*/
|
||||||
|
virtual void foldsInBounds(double &) = 0;
|
||||||
|
|
||||||
|
/** Put value back into bounds - by truncating to a boundary value
|
||||||
|
*/
|
||||||
|
virtual void truncate(double &) = 0;
|
||||||
|
|
||||||
|
/** get minimum value
|
||||||
|
* @exception if does not exist
|
||||||
|
*/
|
||||||
|
virtual double minimum() = 0;
|
||||||
|
/** get maximum value
|
||||||
|
* @exception if does not exist
|
||||||
|
*/
|
||||||
|
virtual double maximum() = 0;
|
||||||
|
/** get range
|
||||||
|
* @exception if unbounded
|
||||||
|
*/
|
||||||
|
virtual double range() = 0;
|
||||||
|
|
||||||
|
/** random generator of uniform numbers in bounds
|
||||||
|
* @exception if unbounded
|
||||||
|
*/
|
||||||
|
virtual double uniform(eoRng & _rng = eo::rng) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** A default class for unbounded variables
|
||||||
|
*/
|
||||||
|
class eoRealNoBounds : public eoRealBounds
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~eoRealNoBounds(){}
|
||||||
|
|
||||||
|
virtual bool isBounded(void) {return false;}
|
||||||
|
virtual bool hasNoBoundAtAll(void) {return true;}
|
||||||
|
virtual bool isMinBounded(void) {return false;}
|
||||||
|
virtual bool isMaxBounded(void) {return false;}
|
||||||
|
virtual void foldsInBounds(double &) {return;}
|
||||||
|
virtual void truncate(double &) {return;}
|
||||||
|
virtual bool isInBounds(double) {return true;}
|
||||||
|
|
||||||
|
virtual double minimum()
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to get minimum of unbounded eoRealBounds");
|
||||||
|
}
|
||||||
|
virtual double maximum()
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to get maximum of unbounded eoRealBounds");
|
||||||
|
}
|
||||||
|
virtual double range()
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to get range of unbounded eoRealBounds");
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual double uniform(eoRng & _rng = eo::rng)
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to generate uniform values in unbounded eoRealBounds");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fully bounded eoRealBound == interval
|
||||||
|
*/
|
||||||
|
class eoRealInterval : public eoRealBounds
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
virtual ~eoRealInterval(){}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Simple bounds = minimum and maximum (allowed)
|
||||||
|
*/
|
||||||
|
eoRealInterval(double _min=0, double _max=1) :
|
||||||
|
repMinimum(_min), repMaximum(_max), repRange(_max-_min)
|
||||||
|
{
|
||||||
|
if (repRange<=0)
|
||||||
|
throw std::logic_error("Void range in eoRealBounds");
|
||||||
|
}
|
||||||
|
|
||||||
|
// accessors
|
||||||
|
virtual double minimum() { return repMinimum; }
|
||||||
|
virtual double maximum() { return repMaximum; }
|
||||||
|
virtual double range() { return repRange; }
|
||||||
|
|
||||||
|
// description
|
||||||
|
virtual bool isBounded(void) {return true;}
|
||||||
|
virtual bool hasNoBoundAtAll(void) {return false;}
|
||||||
|
virtual bool isMinBounded(void) {return true;}
|
||||||
|
virtual bool isMaxBounded(void) {return true;}
|
||||||
|
|
||||||
|
virtual double uniform(eoRng & _rng = eo::rng)
|
||||||
|
{
|
||||||
|
return repMinimum + _rng.uniform(repRange);
|
||||||
|
}
|
||||||
|
|
||||||
|
// says if a given double is within the bounds
|
||||||
|
virtual bool isInBounds(double _r)
|
||||||
|
{
|
||||||
|
if (_r < repMinimum)
|
||||||
|
return false;
|
||||||
|
if (_r > repMaximum)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// folds a value into bounds
|
||||||
|
virtual void foldsInBounds(double & _r)
|
||||||
|
{
|
||||||
|
long iloc;
|
||||||
|
double dlargloc = 2 * range() ;
|
||||||
|
|
||||||
|
if (fabs(_r) > 1.0E9) // iloc too large!
|
||||||
|
{
|
||||||
|
_r = uniform();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( (_r > maximum()) )
|
||||||
|
{
|
||||||
|
iloc = (long) ( (_r-minimum()) / dlargloc ) ;
|
||||||
|
_r -= dlargloc * iloc ;
|
||||||
|
if ( _r > maximum() )
|
||||||
|
_r = 2*maximum() - _r ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_r < minimum())
|
||||||
|
{
|
||||||
|
iloc = (long) ( (maximum()-_r) / dlargloc ) ;
|
||||||
|
_r += dlargloc * iloc ;
|
||||||
|
if (_r < minimum())
|
||||||
|
_r = 2*minimum() - _r ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// truncates to the bounds
|
||||||
|
virtual void truncate(double & _r)
|
||||||
|
{
|
||||||
|
if (_r < repMinimum)
|
||||||
|
_r = repMinimum;
|
||||||
|
else if (_r > repMaximum)
|
||||||
|
_r = repMaximum;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
private :
|
||||||
|
double repMinimum;
|
||||||
|
double repMaximum;
|
||||||
|
double repRange; // to minimize operations ???
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* an eoRealBound bounded from below only
|
||||||
|
*/
|
||||||
|
class eoRealBelowBound : public eoRealBounds
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
virtual ~eoRealBelowBound(){}
|
||||||
|
/**
|
||||||
|
Simple bounds = minimum
|
||||||
|
*/
|
||||||
|
eoRealBelowBound(double _min=0) :
|
||||||
|
repMinimum(_min)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// accessors
|
||||||
|
virtual double minimum() { return repMinimum; }
|
||||||
|
|
||||||
|
virtual double maximum()
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to get maximum of eoRealBelowBound");
|
||||||
|
}
|
||||||
|
virtual double range()
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to get range of eoRealBelowBound");
|
||||||
|
}
|
||||||
|
|
||||||
|
// random generators
|
||||||
|
virtual double uniform(eoRng & _rng = eo::rng)
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to generate uniform values in eoRealBelowBound");
|
||||||
|
}
|
||||||
|
|
||||||
|
// description
|
||||||
|
virtual bool isBounded(void) {return false;}
|
||||||
|
virtual bool hasNoBoundAtAll(void) {return false;}
|
||||||
|
virtual bool isMinBounded(void) {return true;}
|
||||||
|
virtual bool isMaxBounded(void) {return false;}
|
||||||
|
|
||||||
|
// says if a given double is within the bounds
|
||||||
|
virtual bool isInBounds(double _r)
|
||||||
|
{
|
||||||
|
if (_r < repMinimum)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// folds a value into bounds
|
||||||
|
virtual void foldsInBounds(double & _r)
|
||||||
|
{
|
||||||
|
// easy as a pie: symmetry w.r.t. minimum
|
||||||
|
if (_r < repMinimum) // nothing to do otherwise
|
||||||
|
_r = 2*repMinimum - _r;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// truncates to the bounds
|
||||||
|
virtual void truncate(double & _r)
|
||||||
|
{
|
||||||
|
if (_r < repMinimum)
|
||||||
|
_r = repMinimum;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
private :
|
||||||
|
double repMinimum;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
An eoRealBound bounded from above only
|
||||||
|
*/
|
||||||
|
class eoRealAboveBound : public eoRealBounds
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
virtual ~eoRealAboveBound(){}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Simple bounds = minimum
|
||||||
|
*/
|
||||||
|
eoRealAboveBound(double _max=0) :
|
||||||
|
repMaximum(_max)
|
||||||
|
{}
|
||||||
|
|
||||||
|
// accessors
|
||||||
|
virtual double maximum() { return repMaximum; }
|
||||||
|
|
||||||
|
virtual double minimum()
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to get minimum of eoRealAboveBound");
|
||||||
|
}
|
||||||
|
virtual double range()
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to get range of eoRealAboveBound");
|
||||||
|
}
|
||||||
|
|
||||||
|
// random generators
|
||||||
|
virtual double uniform(eoRng & _rng = eo::rng)
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to generate uniform values in eoRealAboveBound");
|
||||||
|
}
|
||||||
|
|
||||||
|
// description
|
||||||
|
virtual bool isBounded(void) {return false;}
|
||||||
|
virtual bool hasNoBoundAtAll(void) {return false;}
|
||||||
|
virtual bool isMinBounded(void) {return false;}
|
||||||
|
virtual bool isMaxBounded(void) {return true;}
|
||||||
|
|
||||||
|
// says if a given double is within the bounds
|
||||||
|
virtual bool isInBounds(double _r)
|
||||||
|
{
|
||||||
|
if (_r > repMaximum)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// folds a value into bounds
|
||||||
|
virtual void foldsInBounds(double & _r)
|
||||||
|
{
|
||||||
|
// easy as a pie: symmetry w.r.t. maximum
|
||||||
|
if (_r > repMaximum) // nothing to do otherwise
|
||||||
|
_r = 2*repMaximum - _r;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// truncates to the bounds
|
||||||
|
virtual void truncate(double & _r)
|
||||||
|
{
|
||||||
|
if (_r > repMaximum)
|
||||||
|
_r = repMaximum;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
private :
|
||||||
|
double repMaximum;
|
||||||
|
};
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
// The Vectorized versions
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/**
|
||||||
|
Class eoRealVectorBounds implements the vectorized version:
|
||||||
|
it is basically a vector of eoRealBounds * and forwards all request
|
||||||
|
to the elements of the vector.
|
||||||
|
Probably it would have been cleaner if there had been an empty base class
|
||||||
|
from which eoRealVectorBounds AND eoRealVectorNoBounds would have derived.
|
||||||
|
This is because I started to write eoRealVectorNoBounds as a
|
||||||
|
vector<eoRealBounds *> whose compoenents would have been eoRealNoBounds
|
||||||
|
but then realize that you don't necessarily have the dimension
|
||||||
|
when construction this vector - hence I added the eoRealVectorNoBounds ...
|
||||||
|
Anyone with extra time in his agenda is welcome to change that :-)
|
||||||
|
*/
|
||||||
|
class eoRealVectorBounds : public vector<eoRealBounds *>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// virtual desctructor (to avoid warning?)
|
||||||
|
virtual ~eoRealVectorBounds(){}
|
||||||
|
|
||||||
|
/** Default Ctor
|
||||||
|
*/
|
||||||
|
eoRealVectorBounds() :
|
||||||
|
vector<eoRealBounds *>(0) {}
|
||||||
|
|
||||||
|
/** Simple bounds = minimum and maximum (allowed)
|
||||||
|
*/
|
||||||
|
eoRealVectorBounds(unsigned _dim, double _min, double _max) :
|
||||||
|
vector<eoRealBounds *>(_dim, new eoRealInterval(_min, _max))
|
||||||
|
{
|
||||||
|
if (_max-_min<=0)
|
||||||
|
throw std::logic_error("Void range in eoRealVectorBounds");
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Ctor: same bonds for everybody, given as an eoRealBounds
|
||||||
|
*/
|
||||||
|
eoRealVectorBounds(unsigned _dim, eoRealBounds & _bounds) :
|
||||||
|
vector<eoRealBounds *>(_dim, &_bounds)
|
||||||
|
{}
|
||||||
|
|
||||||
|
/** Ctor: different bonds for different variables, vectors of double
|
||||||
|
*/
|
||||||
|
eoRealVectorBounds(vector<double> _min, vector<double> _max)
|
||||||
|
{
|
||||||
|
if (_max.size() != _min.size())
|
||||||
|
throw std::logic_error("Dimensions don't match in eoRealVectorBounds");
|
||||||
|
for (unsigned i=0; i<_min.size(); i++)
|
||||||
|
{
|
||||||
|
push_back( new eoRealInterval(_min[i], _max[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Ctor, particular case of dim-2
|
||||||
|
*/
|
||||||
|
eoRealVectorBounds(eoRealBounds & _xbounds, eoRealBounds & _ybounds) :
|
||||||
|
vector<eoRealBounds *>(0)
|
||||||
|
{
|
||||||
|
push_back( &_xbounds);
|
||||||
|
push_back( &_ybounds);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** test: is i_th component bounded
|
||||||
|
*/
|
||||||
|
virtual bool isBounded(unsigned _i)
|
||||||
|
{
|
||||||
|
return (*this)[_i]->isBounded();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** test: bounded iff all are bounded
|
||||||
|
*/
|
||||||
|
virtual bool isBounded(void)
|
||||||
|
{
|
||||||
|
for (unsigned i=0; i<size(); i++)
|
||||||
|
if (! (*this)[i]->isBounded())
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Self-test: true iff i_th component has no bounds at all
|
||||||
|
*/
|
||||||
|
virtual bool hasNoBoundAtAll(unsigned _i)
|
||||||
|
{
|
||||||
|
return (*this)[_i]->hasNoBoundAtAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Self-test: true iff all components have no bound at all
|
||||||
|
*/
|
||||||
|
virtual bool hasNoBoundAtAll(void)
|
||||||
|
{
|
||||||
|
for (unsigned i=0; i<size(); i++)
|
||||||
|
if (! (*this)[i]->hasNoBoundAtAll())
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool isMinBounded(unsigned _i)
|
||||||
|
{ return (*this)[_i]->isMinBounded();} ;
|
||||||
|
|
||||||
|
virtual bool isMaxBounded(unsigned _i)
|
||||||
|
{ return (*this)[_i]->isMaxBounded();} ;
|
||||||
|
|
||||||
|
/** Folds a real value back into the bounds - i_th component
|
||||||
|
*/
|
||||||
|
virtual void foldsInBounds(unsigned _i, double & _r)
|
||||||
|
{
|
||||||
|
(*this)[_i]->foldsInBounds(_r);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Folds all variables of a vector of real values into the bounds
|
||||||
|
*/
|
||||||
|
virtual void foldsInBounds(vector<double> & _v)
|
||||||
|
{
|
||||||
|
for (unsigned i=0; i<size(); i++)
|
||||||
|
{
|
||||||
|
(*this)[i]->foldsInBounds(_v[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Truncates a real value to the bounds - i_th component
|
||||||
|
*/
|
||||||
|
virtual void truncate(unsigned _i, double & _r)
|
||||||
|
{
|
||||||
|
(*this)[_i]->truncate(_r);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** truncates all variables of a vector of real values to the bounds
|
||||||
|
*/
|
||||||
|
virtual void truncate(vector<double> & _v)
|
||||||
|
{
|
||||||
|
for (unsigned i=0; i<size(); i++)
|
||||||
|
{
|
||||||
|
(*this)[i]->truncate(_v[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** test: is i_th component within the bounds?
|
||||||
|
*/
|
||||||
|
virtual bool isInBounds(unsigned _i, double _r)
|
||||||
|
{ return (*this)[_i]->isInBounds(_r); }
|
||||||
|
|
||||||
|
/** test: are ALL components within the bounds?
|
||||||
|
*/
|
||||||
|
virtual bool isInBounds(vector<double> _v)
|
||||||
|
{
|
||||||
|
for (unsigned i=0; i<size(); i++)
|
||||||
|
if (! isInBounds(i, _v[i]))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Accessors: will raise an exception if these do not exist
|
||||||
|
*/
|
||||||
|
virtual double minimum(unsigned _i) {return (*this)[_i]->minimum();}
|
||||||
|
virtual double maximum(unsigned _i) {return (*this)[_i]->maximum();}
|
||||||
|
virtual double range(unsigned _i) {return (*this)[_i]->range();}
|
||||||
|
|
||||||
|
/** Computes the average range
|
||||||
|
* An exception will be raised if one of the component is unbounded
|
||||||
|
*/
|
||||||
|
virtual double averageRange()
|
||||||
|
{
|
||||||
|
double r=0.0;
|
||||||
|
for (unsigned i=0; i<size(); i++)
|
||||||
|
r += range(i);
|
||||||
|
return r/size();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Generates a random number in i_th range
|
||||||
|
* An exception will be raised if one of the component is unbounded
|
||||||
|
*/
|
||||||
|
virtual double uniform(unsigned _i, eoRng & _rng = eo::rng)
|
||||||
|
{
|
||||||
|
double r= (*this)[_i]->uniform();
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** fills a vector with uniformly chosen variables in bounds
|
||||||
|
* An exception will be raised if one of the component is unbounded
|
||||||
|
*/
|
||||||
|
void uniform(vector<double> & _v, eoRng & _rng = eo::rng)
|
||||||
|
{
|
||||||
|
_v.resize(size());
|
||||||
|
for (unsigned i=0; i<size(); i++)
|
||||||
|
{
|
||||||
|
_v[i] = uniform(i, _rng);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** the dummy unbounded eoRealVectorBounds: usefull if you don't need bounds!
|
||||||
|
* everything is inlined.
|
||||||
|
* Warning: we do need this class, and not only a vector<eoRealNoBounds *>
|
||||||
|
*/
|
||||||
|
class eoRealVectorNoBounds: public eoRealVectorBounds
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// virtual desctructor (to avoid warining?)
|
||||||
|
virtual ~eoRealVectorNoBounds(){}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Simple bounds = minimum and maximum (allowed)
|
||||||
|
*/
|
||||||
|
// Ctor: nothing to do!
|
||||||
|
eoRealVectorNoBounds(unsigned _dim=0) {}
|
||||||
|
|
||||||
|
|
||||||
|
virtual bool isBounded(unsigned) {return false;}
|
||||||
|
virtual bool isBounded(void) {return false;}
|
||||||
|
|
||||||
|
virtual bool hasNoBoundAtAll(unsigned) {return true;}
|
||||||
|
virtual bool hasNoBoundAtAll(void) {return true;}
|
||||||
|
|
||||||
|
virtual bool isMinBounded(unsigned) {return false;}
|
||||||
|
virtual bool isMaxBounded(unsigned) {return false;}
|
||||||
|
|
||||||
|
virtual void foldsInBounds(unsigned, double &) {return;}
|
||||||
|
virtual void foldsInBounds(vector<double> &) {return;}
|
||||||
|
|
||||||
|
virtual void truncate(unsigned, double &) {return;}
|
||||||
|
virtual void truncate(vector<double> &) {return;}
|
||||||
|
|
||||||
|
virtual bool isInBounds(unsigned, double) {return true;}
|
||||||
|
virtual bool isInBounds(vector<double>) {return true;}
|
||||||
|
|
||||||
|
// accessors
|
||||||
|
virtual double minimum(unsigned)
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to get minimum of eoRealVectorNoBounds");
|
||||||
|
}
|
||||||
|
virtual double maximum(unsigned)
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to get maximum of eoRealVectorNoBounds");
|
||||||
|
}
|
||||||
|
virtual double range(unsigned)
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to get range of eoRealVectorNoBounds");
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual double averageRange()
|
||||||
|
{
|
||||||
|
throw logic_error("Trying to get average range of eoRealVectorNoBounds");
|
||||||
|
}
|
||||||
|
|
||||||
|
// random generators
|
||||||
|
virtual double uniform(unsigned, eoRng & _rng = eo::rng)
|
||||||
|
{
|
||||||
|
throw logic_error("No uniform distribution on eoRealVectorNoBounds");
|
||||||
|
}
|
||||||
|
|
||||||
|
// fills a vector with uniformly chosen variables in bounds
|
||||||
|
void uniform(vector<double> &, eoRng & _rng = eo::rng)
|
||||||
|
{
|
||||||
|
throw logic_error("No uniform distribution on eoRealVectorNoBounds");
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
// one object for all
|
||||||
|
extern eoRealNoBounds eoDummyRealNoBounds;
|
||||||
|
extern eoRealVectorNoBounds eoDummyVectorNoBounds;
|
||||||
|
#endif
|
||||||
|
|
@ -14,10 +14,10 @@ CXXFLAGS = -g -Wall
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
check_PROGRAMS = t-eoParetoFitness t-eoPareto t-eofitness t-eoRandom t-eobin t-eoStateAndParser t-eoCheckpointing t-eoSSGA \
|
check_PROGRAMS = t-eoParetoFitness t-eoPareto t-eofitness t-eoRandom t-eobin t-eoStateAndParser t-eoCheckpointing t-eoSSGA \
|
||||||
t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoVector
|
t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoReal t-eoVector
|
||||||
TESTS=run_tests t-eoVector t-eoRandom t-eoSSGA t-eoPareto t-eoParetoFitness
|
TESTS=run_tests t-eoVector t-eoRandom t-eoSSGA t-eoPareto t-eoParetoFitness
|
||||||
# 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 t-eoGenOp t-eoGA
|
# 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-eoReal
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
|
|
@ -115,6 +115,13 @@ t_eoGA_LDADD = $(top_builddir)/src/ga/libga.a $(LDADDS)
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
|
t_eoReal_SOURCES = t-eoReal.cpp real_value.h
|
||||||
|
t_eoReal_DEPENDENCIES = $(DEPS) $(top_builddir)/src/es/libes.a
|
||||||
|
t_eoReal_LDFLAGS = -lm
|
||||||
|
t_eoReal_LDADD = $(top_builddir)/src/es/libes.a $(LDADDS)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
t_eoSSGA_SOURCES = t-eoSSGA.cpp binary_value.h
|
t_eoSSGA_SOURCES = t-eoSSGA.cpp binary_value.h
|
||||||
t_eoSSGA_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a
|
t_eoSSGA_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a
|
||||||
t_eoSSGA_LDFLAGS = -lm
|
t_eoSSGA_LDFLAGS = -lm
|
||||||
|
|
|
||||||
70
eo/test/t-eoReal.cpp
Normal file
70
eo/test/t-eoReal.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <es/es.h>
|
||||||
|
#include "real_value.h"
|
||||||
|
#include <apply.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
typedef eoReal<eoMinimizingFitness> EOT;
|
||||||
|
|
||||||
|
eoParser parser(argc, argv); // for user-parameter reading
|
||||||
|
|
||||||
|
eoState state; // keeps all things allocated
|
||||||
|
|
||||||
|
///// FIRST, problem or representation dependent stuff
|
||||||
|
//////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// The evaluation fn - encapsulated into an eval counter for output
|
||||||
|
eoEvalFuncPtr<EOT, double, const std::vector<double>&>
|
||||||
|
mainEval( real_value );
|
||||||
|
eoEvalFuncCounter<EOT> eval(mainEval);
|
||||||
|
|
||||||
|
// the genotype - through a genotype initializer
|
||||||
|
eoInit<EOT>& init = make_genotype(parser, state, eoMinimizingFitness());
|
||||||
|
|
||||||
|
// Build the variation operator (any seq/prop construct)
|
||||||
|
eoGenOp<EOT>& op = make_op(parser, state, init);
|
||||||
|
|
||||||
|
//// Now the representation-independent things
|
||||||
|
//////////////////////////////////////////////
|
||||||
|
|
||||||
|
// initialize the population - and evaluate
|
||||||
|
// yes, this is representation indepedent once you have an eoInit
|
||||||
|
eoPop<EOT>& pop = make_pop(parser, state, init);
|
||||||
|
apply(eval, pop);
|
||||||
|
|
||||||
|
// stopping criteria
|
||||||
|
eoContinue<EOT> & term = make_continue(parser, state, eval);
|
||||||
|
// output
|
||||||
|
eoCheckPoint<EOT> & checkpoint = make_checkpoint(parser, state, eval, term);
|
||||||
|
// algorithm (need the operator!)
|
||||||
|
eoAlgo<EOT>& ga = make_algo_scalar(parser, state, eval, checkpoint, op);
|
||||||
|
|
||||||
|
///// End of construction of the algorith
|
||||||
|
/////////////////////////////////////////
|
||||||
|
// to be called AFTER all parameters have been read!!!
|
||||||
|
make_help(parser);
|
||||||
|
|
||||||
|
//// GO
|
||||||
|
///////
|
||||||
|
cout << "Initial Population\n";
|
||||||
|
pop.sortedPrintOn(cout);
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
run_ea(ga, pop); // run the ga
|
||||||
|
|
||||||
|
cout << "Final Population\n";
|
||||||
|
pop.sortedPrintOn(cout);
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
catch(exception& e)
|
||||||
|
{
|
||||||
|
cout << e.what() << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -315,7 +315,7 @@ void main_function(int argc, char **argv)
|
||||||
fileMonitor.add(generationCounter);
|
fileMonitor.add(generationCounter);
|
||||||
fileMonitor.add(bestStat);
|
fileMonitor.add(bestStat);
|
||||||
fileMonitor.add(SecondStat);
|
fileMonitor.add(SecondStat);
|
||||||
// the fileMonitor can monitor parameters, too, but you must tell it!
|
// the gnuMonitor can monitor parameters, too, but you must tell it!
|
||||||
gnuMonitor.add(eval);
|
gnuMonitor.add(eval);
|
||||||
gnuMonitor.add(bestStat);
|
gnuMonitor.add(bestStat);
|
||||||
gnuMonitor.add(averageStat);
|
gnuMonitor.add(averageStat);
|
||||||
|
|
|
||||||
Reference in a new issue