add tests interfaces as examples in the doc ; remove unused test binaries ; some code formating

This commit is contained in:
Johann Dreo 2010-11-07 23:12:28 +01:00
commit bd236ee67f
54 changed files with 129 additions and 1410 deletions

View file

@ -628,7 +628,7 @@ EXCLUDE_SYMBOLS =
# directories that contain example code fragments that are included (see
# the \include command).
EXAMPLE_PATH =
EXAMPLE_PATH = @CMAKE_SOURCE_DIR@/test
# If the value of the EXAMPLE_PATH tag contains directories, you can use the
# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp

View file

@ -60,4 +60,3 @@ article in Lecture Notes In Computer Science, 2310, Selected Papers from the 5th
- http://eodev.sourceforge.net/eo/doc/LeCreusot.pdf
- http://eodev.sourceforge.net/eo/doc/EO_EA2001.pdf
*/

View file

@ -55,6 +55,8 @@
@ref Operators that effectively modify EO objects must invalidate them.
The fitness object must have, besides an void ctor, a copy ctor.
@example t-eo.cpp
*/
template<class F = double> class EO: public eoObject, public eoPersistent
{

View file

@ -307,5 +307,7 @@ eoAlgo<EOT> & do_make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc
// that's it!
return *algo;
}
/** @example t-eoGA.cpp
*/
#endif

View file

@ -273,9 +273,11 @@ template<class EOT> class eoEasyEA: public eoAlgo<EOT>
// Friend classes
friend class eoIslandsEasyEA <EOT> ;
friend class eoDistEvalEasyEA <EOT> ;
};
//-----------------------------------------------------------------------------
};
/**
@example t-eoEasyEA.cpp
Example of a test program building an EA algorithm.
*/
#endif

View file

@ -188,6 +188,9 @@ protected:
}dummyInit;
};
/**
* @example t-eoEasyPSO.cpp
* Example of a test program building a PSO algorithm.
*/
#endif /*_EOEASYPSO_H*/

View file

@ -35,7 +35,9 @@
//-----------------------------------------------------------------------------
/** Extended velocity performer for particle swarm optimization. Derivated from abstract eoVelocity,
/** Extended velocity performer for particle swarm optimization.
*
* Derivated from abstract eoVelocity,
* At step t: v(t+1)= w * v(t) + c1 * r1 * ( xbest(t)-x(t) ) + c2 * r2 * ( lbest(t) - x(t) ) + c3 * r3 * ( gbest(t) - x(t) )
* It includes both a "topology" best and a global best in the social knowledge. Each topology
* provides a method to retrieve the global best <=> the best of all the neighborhood the topology contains.
@ -60,7 +62,7 @@ public:
* @param _c3 - Learning factor used for the global best
* @param _bounds - An eoRealBaseVectorBounds: real bounds for real velocities.
* If the velocities are not real, they won't be bounded by default. Should have a eoBounds ?
* @param _boundsModifier - An eoRealBoundModifier used to modify the bounds (for real bounds only).
* @param _bndsModifier - An eoRealBoundModifier used to modify the bounds (for real bounds only).
* @param _gen - The eo random generator, default=rng
*/
eoExtendedVelocity (eoTopology < POT > & _topology,
@ -204,7 +206,9 @@ protected:
// If the bound modifier doesn't need to be used, use the dummy instance
eoDummyRealBoundModifier dummyModifier;
};
/** @todo this example does not appear in the doc for an unknown reason
* @example t-eoExtendedVelocity.cpp
* Example of a test program using this class:
*/
#endif /*eoExtendedVelocity_H */

View file

@ -60,6 +60,8 @@ public :
/// tag to identify a binary function in compile time function selection @see functor_category
struct binary_function_tag {};
};
/** @example t-eoFunctop.cpp
*/
/**
Basic Function. Derive from this class when defining

View file

@ -81,6 +81,8 @@ class eoGenOp : public eoOp<EOT>, public eoUF<eoPopulator<EOT> &, void>
*/
virtual void apply(eoPopulator<EOT>& _pop) = 0;
};
/** @example t-eoGenOp.cpp
*/
/** Wrapper for eoMonOp */

View file

@ -183,6 +183,8 @@ class eoInitPermutation: public eoInit<EOT>
unsigned startFrom;
UF_random_generator<unsigned int> gen;
};
/** @example t-eoInitPermutation.cpp
*/
/**

View file

@ -54,6 +54,8 @@ template <class FitT> class eoInt: public eoVector<FitT, int>
}
};
/** @example t-eoInt.cpp
*/
//-----------------------------------------------------------------------------

View file

@ -63,10 +63,16 @@ Variators are operators that modify individuals.
Selectors are operators that select a subset of a population.
Example:
@include t-eoSelect.cpp
@defgroup Replacors Replacement operators
Replacors are operators that replace a subset of a population by another set of individuals.
Here is an example with several replacement operators:
@include t-eoReplacement.cpp
*/
/** Abstract data types for EO operators.

View file

@ -84,5 +84,7 @@ template<class Chrom> class eoOrderXover: public eoQuadOp<Chrom>
}
};
/** @example t-eoOrderXover.cpp
*/
#endif

View file

@ -81,5 +81,7 @@ private :
typedef std::vector<typename EOT::Fitness> FitVec;
FitVec cumulative;
};
/** @example t-eoRoulette.cpp
*/
#endif

View file

@ -188,5 +188,7 @@ protected:
unsigned neighborhoodSize;
bool isSetup;
};
/** @example t-eoRingTopology.cpp
*/
#endif /*EORINGTOPOLOGY_H_*/

View file

@ -79,6 +79,9 @@ class eoScalarFitness
private :
ScalarType value;
};
/** @example t-eofitness.cpp
*
*/
/**
Typedefs for fitness comparison, Maximizing Fitness compares with less,

View file

@ -77,6 +77,8 @@ public:
private:
static std::vector<std::string> TermDescriptions;
};
/** @example t-eoFitnessAssembled.cpp
*/
//! Implements fitness as std::vector, storing all values that might occur during fitness assembly
/*! Properties:
@ -237,6 +239,10 @@ public:
bool operator>=(const eoScalarFitnessAssembled<ScalarType, Compare, FitnessTraits>& y ) const { return !(*this < y); }
};
/**
* @example t-eoFitnessAssembledEA.cpp
*/
/**
Typedefs for fitness comparison, Maximizing Fitness compares with less,

View file

@ -64,6 +64,8 @@ public:
}
};
/** @example t-eoSecondsElapsedContinue.cpp
*/
#endif

View file

@ -53,6 +53,8 @@ class eoSelectOne : public eoUF<const eoPop<EOT>&, const EOT&>
(void)_pop;
}
};
/** @example t-eoSelectOne.cpp
*/
#endif

View file

@ -53,5 +53,7 @@ public:
private :
eoSharing<EOT> sharing; // derived from eoPerf2Worth
};
/** @example t-eoSharing.cpp
*/
#endif

View file

@ -83,6 +83,8 @@ template<class EOT> class eoShiftMutation: public eoMonOp<EOT>
}
};
/** @example t-eoShiftMutation.cpp
*/
//-----------------------------------------------------------------------------

View file

@ -104,5 +104,7 @@ private :
typedef std::vector<unsigned> IndexVec;
IndexVec indices;
};
/** @example t-eoRoulette.cpp
*/
#endif

View file

@ -75,6 +75,8 @@ template<class Chrom> class eoSwapMutation: public eoMonOp<Chrom>
private:
unsigned int howManySwaps;
};
/** @example t-eoSwapMutation.cpp
*/
//-----------------------------------------------------------------------------
#endif

View file

@ -253,6 +253,8 @@ private:
}dummyInit;
};
/** @example t-eoSyncEasyPSO.cpp
*/
#endif /*_EOSYNCEASYPSO_H*/

View file

@ -69,6 +69,8 @@ public:
}
};
/** @example t-eoTwoOptMutation.cpp
*/
//-----------------------------------------------------------------------------

View file

@ -60,7 +60,7 @@ public:
* @param _c2 - The second learning factor used for the local/global best(s). Type must be POT::ParticleVelocityType
* @param _bounds - An eoRealBaseVectorBounds: real bounds for real velocities.
* If the velocities are not real, they won't be bounded by default. Should have a eoBounds ?
* @param _boundsModifier - An eoRealBoundModifier used to modify the bounds (for real bounds only).
* @param _bndsModifier - An eoRealBoundModifier used to modify the bounds (for real bounds only).
* @param _gen - The eo random generator, default=rng
*/
eoVariableInertiaWeightedVelocity (eoTopology < POT > & _topology,

View file

@ -56,7 +56,7 @@ public :
/** default ctor
* @param nMax max number of atoms
* @param _nMax max number of atoms
* @param _atomInit an Atom initializer
*/
eoVlAddMutation(unsigned _nMax, eoInit<AtomType> & _atomInit) :
@ -118,15 +118,15 @@ public :
/** ctor with an external gene chooser
* @param nMin min number of atoms to leave in the individual
* @param _geneChooser an eoGeneCHooser to choose which one to delete
* @param _nMin min number of atoms to leave in the individual
* @param _chooser an eoGeneCHooser to choose which one to delete
*/
eoVlDelMutation(unsigned _nMin, eoGeneDelChooser<EOT> & _chooser) :
nMin(_nMin), uChooser(), chooser(_chooser) {}
/** ctor with uniform gene chooser - the default
* @param nMin min number of atoms to leave in the individual
* @param _nMin min number of atoms to leave in the individual
*/
eoVlDelMutation(unsigned _nMin) :
nMin(_nMin), uChooser(), chooser(uChooser) {}

View file

@ -66,8 +66,8 @@ public:
/** default constructor
@param size Length of vector (default is 0)
@param value Initial value of all elements (default is default value of type GeneType)
@param _size Length of vector (default is 0)
@param _value Initial value of all elements (default is default value of type GeneType)
*/
eoVector(unsigned _size = 0, GeneType _value = GeneType())
: EO<FitT>(), std::vector<GeneType>(_size, _value)
@ -129,6 +129,8 @@ public:
}
}
};
/** @example t-eoVector.cpp
*/
/** Less than

View file

@ -66,9 +66,9 @@ public:
/** Default constructor.
* @param _size Length of the tree vectors (we expect the same size), default is 0
* @param position
* @param velocity
* @param bestPositions
* @param _position
* @param _velocity
* @param _bestPositions
*/
eoVectorParticle (unsigned _size = 0,PositionType _position = PositionType (), VelocityType _velocity = VelocityType (), PositionType _bestPositions = PositionType ()):PO < FitT > (),std::vector < PositionType > (_size, _position), bestPositions (_size, _bestPositions), velocities (_size,
_velocity)

View file

@ -54,6 +54,8 @@ template <class FitT> class eoReal: public eoVector<FitT, double>
}
};
/** @example t-eoReal.cpp
*/
//-----------------------------------------------------------------------------

View file

@ -44,7 +44,10 @@
/** @defgroup bitstring Bit strings
Various functions for a bitstring representation
Various functions for a bitstring representation.
Example of a complete test program that use various bitstrings operators:
@include t-eobin.cpp
@ingroup Representations
*/

View file

@ -41,6 +41,9 @@ using namespace gp_parse_tree;
Various functions for tree-based Genetic Programming
Example:
@include t-eoSymreg.cpp
@ingroup Representations
*/
@ -166,6 +169,8 @@ public:
*/
}
};
/** @example t-eoSymreg.cpp
*/
// friend function to print eoParseTree
template <class FType, class Node>

View file

@ -22,8 +22,9 @@
class Node (your node in the tree) must have the following implemented:
****** Arity ******
\code
int arity(void) const
\endcode
Note: the default constructor of a Node should provide a
Node with arity 0!
@ -36,7 +37,9 @@
is the simplest evaluation, it will call
\code
RetVal Node::operator()(RetVal, subtree<Node, RetVal>::const_iterator)
\endcode
(Unfortunately the first RetVal argument is mandatory (although you
might not need it. This is because MSVC does not support member template
@ -50,7 +53,9 @@
will call:
\code
RetVal Node::operator()(RetVal, subtree<... , It values)
\endcode
where It is whatever type you desire (most of the time
this will be a std::vector containing the values of your
@ -60,7 +65,9 @@
will call:
\code
RetVal Node::operator()(RetVal, subtree<... , It values, It2 moreValues)
\endcode
although I do not see the immediate use of this, however...
@ -68,7 +75,9 @@
that calls:
\code
RetVal Node::operator()(subtree<... , It values, It2 args, It3 adfs)
\endcode
can be useful for implementing adfs.
@ -77,8 +86,10 @@
arguments open so that different ways of evaluation remain
possible. Implement the simplest eval as:
\code
template <class It>
RetVal operator()(RetVal dummy, It begin) const
\endcode
****** Internal Structure ******
@ -144,7 +155,9 @@
or from an std::istream:
copy(std::istream_iterator<T>(my_stream), std::istream_iterator<T>(), back_inserter(tree));
\code
copy(std::istream_iterator<T>(my_stream), std::istream_iterator<T>(), back_inserter(tree));
\endcode
Note that the back_inserter must be used as there is no
resize member in the parse_tree. back_inserter will use

View file

@ -74,6 +74,8 @@ public :
}
};
/** @example t-eoExternalEO.cpp
*/
/** To remove ambiguities between EO<F> and External, streaming operators are defined yet again
* @ingroup Utilities

View file

@ -39,10 +39,13 @@
*
* Use them with eoStats, eoUpdater and eoMonitor to get statistics at each generation.
*
* @see eoStats
* @see eoStat
* @see eoMonitor
* @see eoUpdater
*
* Example of a test program using checkpointing:
* @include t-eoCheckpointing.cpp
*
* @ingroup Utilities
*
* @{

View file

@ -247,6 +247,8 @@ private:
*/
std::map< std::ostream*, int > _standard_io_streams;
};
/** @example t-eoLogger.cpp
*/
namespace eo
{

View file

@ -438,6 +438,8 @@ private:
*/
eoRng& operator=(const eoRng&);
};
/** @example t-eoRNG.cpp
*/

View file

@ -33,6 +33,12 @@
/**
\defgroup Real Vector of reals
Set of classes related to continuous black-box optimization problems.
Here are several examples of test programs using eoReal, eoEsSimple, eoEsStdev or eoEsFull to build an Evoution Strategies algorithm:
@include t-eoESAll.cpp
@include t-eoESFull.cpp
@ingroup Representations
*/

View file

@ -88,12 +88,16 @@ template <class T = double> class eoUniformGenerator : public eoRndGenerator<T>
for ints and unsigneds
*/
T operator()(void) { return minim+static_cast<T>(uniform.uniform(range)); }
/** @example t-eoUniform.cpp
*/
private :
T minim;
T range;
eoRng& uniform;
};
/** @example t-eoRandom.cpp
*/
/// Specialization for bool, does an unbiased coin flip

View file

@ -407,6 +407,8 @@ private :
}
};
/** @example t-eoSSGA.cpp
*/
template <class EOT>
class eoDistanceStat : public eoStat<EOT, double>

View file

@ -132,5 +132,7 @@ private :
eoState& operator=(const eoState&);
};
/** @example t-eoStateAndParser.cpp
*/
#endif

View file

@ -1,66 +0,0 @@
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
t-eoAtomOps.cpp
Program that tests the atomic operator classes
(c) GeNeura Team, 1999
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; 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
*/
//-----------------------------------------------------------------------------//
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif
#include <string>
#include <iostream>
#include <iterator>
using namespace std;
// Operators we are going to test
#include <eoAtomCreep.h>
#include <eoAtomBitFlip.h>
#include <eoAtomRandom.h>
#include <eoAtomMutation.h>
// Several EOs
#include <other/eoString.h>
// RNGs
#include <eoNegExp.h>
main(int argc, char *argv[]) {
eoString<float> aString("123456");
eoAtomCreep<char> creeper;
eoAtomMutation< eoString<float> > mutator( creeper, 0.5 );
eoNegExp<char> charNE( 2 );
eoAtomRandom<char> randomer( charNE );
eoAtomMutation< eoString<float> > mutator2 ( randomer, 0.5 );
std::cout << "Before aString " << aString << std::endl;
mutator( aString);
std::cout << " after mutator " << aString << std::endl;
mutator2( aString);
std::cout << " after mutator2 " << aString << std::endl;;
return 0; // to avoid VC++ complaints
}

View file

@ -1,26 +0,0 @@
#include <eoBaseFunctions.h>
#include <iostream>
using namespace std;
struct eo1 : public eoF<void>
{
void operator()(void) {}
};
struct eo2 : public eoF<int>
{
int operator()(void) { return 1; }
};
int main()
{
eo1 _1; _1();
eo2 _2;
int i = _2();
std::cout << i << '\n';
return i;
}

View file

@ -1,116 +0,0 @@
// Program to test several EO-ES features
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif
#include <string>
#include <iostream>
#include <iterator>
using namespace std;
// general
#include <utils/eoParser.h> // though contained in all others!
// evolution specific
#include <eoEvalFuncPtr.h>
//#include <eoSequentialOpHolder.h>
//#include <eoFullEA.h>
// representation specific
#include <es/eoESFullChrom.h> // though contained in following
//#include <eoESReco.h>
#include <es/eoESFullMut.h>
//#include <eoESRandomize.h>
// this fitness
#include "real_value.h" // the sphere fitness
// Now the main
///////////////
typedef eoESFullChrom<float> Ind;
main(int argc, char *argv[]) {
unsigned mu, lambda;
bool comma;
// Create the command-line parser
Parser parser( argc, argv, "Basic EA for vector<float> with adaptive mutations");
//reproducible random seed - thanks, Maarten
InitRandom(parser);
// a first Ind, reading its parameters from the parser
// will be used later to inialize the whole population
Ind FirstEO(parser);
// Evaluation
// here we should call some parser-based constructor,
// as many evaluation function need parameters
// and also have some preliminary stuffs to do
eoEvalFuncPtr<Ind> eval( real_value );
// recombination and mutation operators, reading their parameters from the parser
eoESMutate<float> MyMut(parser,
FirstEO.StdDevLength(), FirstEO.size(),
FirstEO.CorCffLength() );
std::cout << "First EO " << FirstEO << std::endl;
MyMut(FirstEO);
std::cout << "First EO mutated" << FirstEO << std::endl;
/*
// Evolution and population parameters
eoScheme<Ind> the_scheme(parser);
// recombination and mutation operators, reading their parameters from the parser
eoESReco<float> MyReco(parser, FirstEO);
eoESMutate<float> MyMut(parser, FirstEO);
// termination conditions read by the parser
eoTermVector<Ind> the_terms(parser);
// Initialization of the population
// shoudl be called using the parser, in case you want to read from file(s)
eoESRandomize<float> randomize; // an eoESInd randomnizer
eoPop<Ind> pop(the_scheme.PopSize(), FirstEO, randomize);
// eval(pop); // shoudl we call it from inside the constructor???
// ALL parmeters have been read: write them out
// Writing the parameters on arv[0].status
// but of course this can be modified - see the example parser.cpp
parser.outputParam();
// except the help parameter???
if( parser.getBool("-h" , "--help" , "Shows this help")) {
parser.printHelp();
exit(1);
}
unsigned i, iind;
std::cout << "Initial population: \n" << std::endl;
for (i = 0; i < pop.size(); ++i) {
eval(pop[i]);
std::cout << pop[i].fitness() << "\t" << pop[i] << std::endl;
}
// the Operators
eoSequentialOpHolder <Ind> seqholder;
// seqholder.addOp(MyReco, 1.0);
seqholder.addOp(MyMut, 1.0);
// One generation
eoEvolStep<Ind> evol_scheme(the_scheme, seqholder, eval);
// the algorithm:
eoFullEA<Ind> ea(evol_scheme, the_terms);
ea(pop);
std::cout << "Final population: \n" << std::endl;
for (i = 0; i < pop.size(); ++i)
std::cout << pop[i].fitness() << "\t" << pop[i] << std::endl;
return 0;
*/
}

View file

@ -1,131 +0,0 @@
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
t-eoGOpSel.cpp
Testing proportional operator selectors
(c) Maarten Keijzer and GeNeura Team, 2000
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; 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
*/
//-----------------------------------------------------------------------------//
#ifndef __GNUG__
// to avoid long name warnings
#pragma warning(disable:4786)
#endif // __GNUG__
#include <iostream>
#include <ga/eoBin.h> // eoBin, eoPop, eoBreeder
#include <eoPop.h>
#include <ga/eoBitOp.h>
#include <eoProportionalGOpSel.h>
#include <eoSequentialGOpSelector.h>
#include <eoRandomIndiSelector.h>
#include <eoDetTournamentIndiSelector.h>
#include <eoDetTournamentInserter.h>
#include <eoStochTournamentInserter.h>
#include <eoGOpBreeder.h>
#include <utils/eoRNG.h>
#include <utils/eoState.h>
// Fitness evaluation
#include "binary_value.h"
//-----------------------------------------------------------------------------
typedef eoBin<float> Chrom;
//-----------------------------------------------------------------------------
main()
{
rng.reseed(42); // reproducible random seed
const unsigned POP_SIZE = 8, CHROM_SIZE = 4;
unsigned i;
eoBinRandom<Chrom> random;
eoPop<Chrom> pop;
for (i = 0; i < POP_SIZE; ++i)
{
Chrom chrom(CHROM_SIZE);
random(chrom);
chrom.fitness(binary_value(chrom));
pop.push_back(chrom);
}
std::cout << "population:" << std::endl;
for (i = 0; i < pop.size(); ++i)
std::cout << pop[i] << " " << pop[i].fitness() << std::endl;
eoBinBitFlip<Chrom> bitflip;
eoBinCrossover<Chrom> xover;
eoEvalFuncPtr<Chrom> eval(binary_value);
//Create the proportional operator selector and add the
// two operators creatd above to it.
eoProportionalGOpSel<Chrom > propSel;
eoSequentialGOpSel<Chrom> seqSel;
propSel.addOp(bitflip, 0.5);
propSel.addOp(xover, 0.5);
// seqSel selects operator in sequence, creating a combined operator
// add a bitflip, an xover and another bitflip
seqSel.addOp(bitflip, 0.25);
seqSel.addOp(xover, 0.5);
seqSel.addOp(bitflip, 0.25);
eoRandomIndiSelector<Chrom> selector1;
eoDetTournamentIndiSelector<Chrom> selector2(2);
eoBackInserter<Chrom> inserter1;
eoDetTournamentInserter<Chrom> inserter2(eval, 2);
eoStochTournamentInserter<Chrom> inserter3(eval, 0.9f);
eoGOpBreeder<Chrom> breeder1(propSel, selector1);
eoGOpBreeder<Chrom> breeder2(seqSel, selector1);
eoGOpBreeder<Chrom> breeder3(propSel, selector2);
eoGOpBreeder<Chrom> breeder4(seqSel, selector2);
// test the breeders
breeder1(pop);
breeder2(pop);
breeder3(pop);
breeder4(pop);
eoState state;
state.registerObject(pop);
state.save(std::std::cout);
return 0;
}
//-----------------------------------------------------------------------------

View file

@ -1,31 +0,0 @@
//-----------------------------------------------------------------------------
// t-eoNonUniform.cc
//-----------------------------------------------------------------------------
#include <iostream>
#include <eo>
//-----------------------------------------------------------------------------
main()
{
eoNonUniform nu(1000);
std::cout << "----------------------------------------------------------" << std::endl
<< "nu.step() = " << nu.step()
<< "\t nu.num_step() = " << nu.num_step() << std::endl
<< "----------------------------------------------------------" << std::endl;
eoLinear l1(0, 1, nu), l2(1, 0, nu);
eoNegExp2 n1(0.1, 8, nu), n2(0.75, 3, nu);
for (; nu; ++nu)
{
std::cout << nu.step()
<< "\t" << l1() << "\t" << l2()
<< "\t" << n1() << "\t" << n2()
<< std::endl;
}
}
//-----------------------------------------------------------------------------

View file

@ -1,274 +0,0 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <eo>
//#include <utils/eoMOFitnessStat.h>
#include <eoNDSorting.h>
#include <eoParetoFitness.h>
using namespace std;
// Look: overloading the maximization without overhead (thing can be inlined)
class MinimizingFitnessTraits : public eoParetoFitnessTraits
{
public :
static bool maximizing(int) { return false; }
};
typedef eoParetoFitness<MinimizingFitnessTraits> fitness_type;
const unsigned chromsize=5;
const double minval = -15;
const double maxval = 15;
struct eoDouble : public EO<fitness_type>
{
double value[chromsize];
};
class Mutate : public eoMonOp<eoDouble>
{
bool operator()(eoDouble& _eo)
{
for (unsigned i = 0; i < chromsize; ++i)
{
if (rng.flip(1./chromsize))
_eo.value[i] += rng.normal() * 0.1 * _eo.value[i];
if (_eo.value[i] < minval)
_eo.value[i] = minval;
else if (_eo.value[i] > maxval)
_eo.value[i] = maxval;
}
return true;
}
};
class Eval : public eoEvalFunc<eoDouble>
{
void operator()(eoDouble& _eo)
{
vector<double> x(_eo.value, _eo.value + chromsize);
fitness_type f;
for (unsigned i = 0; i < chromsize; ++i)
{
if (i < chromsize-1)
{
f[0] += -10.0 * exp(-0.2 * sqrt(x[i]*x[i] + x[i+1]*x[i+1]));
}
f[1] += pow(fabs(x[i]), 0.8) + 5 * pow(sin(x[i]),3.);
}
_eo.fitness(f);
}
};
class Eval2 : public eoEvalFunc<eoDouble>
{
void operator()(eoDouble& _eo)
{
vector<double> x(_eo.value, _eo.value + chromsize);
fitness_type f;
for (unsigned i = 0; i < chromsize; ++i)
{
f[0] += x[i] * x[i];
}
f[1] =
3 * x[0] + 2 * x[1]
- x[2]/3 + 0.01*pow(x[3] - x[4], 3);
_eo.fitness(f);
}
};
class Init : public eoInit<eoDouble>
{
void operator()(eoDouble& _eo)
{
_eo.value[0] = rng.uniform();
double range = maxval - minval;
for (unsigned i = 1; i < chromsize; ++i)
_eo.value[i] = rng.uniform() * range + minval;
_eo.invalidate();
}
};
/** @brief An elitist non-dominated sorted replacement scheme.
Trying out an elitist non-dominated sorted replacement scheme.
*/
template <class EOT, class WorthT = double>
class eoNDPlusReplacement : public eoReplacement<EOT>
{
public:
// using eoNDPlusReplacement< EOT, WorthT >::first;
eoNDPlusReplacement(eoPerf2Worth<EOT, WorthT>& _perf2worth)
: perf2worth(_perf2worth)
{}
struct WorthPair : public pair<WorthT, const EOT*>
{
bool operator<(const WorthPair& other) const
{ return other.first < this->first; }
};
void operator()(eoPop<EOT>& _parents, eoPop<EOT>& _offspring)
{
unsigned sz = _parents.size();
_parents.reserve(_parents.size() + _offspring.size());
std::copy(_offspring.begin(), _offspring.end(), back_inserter(_parents));
// calculate worths
perf2worth(_parents);
perf2worth.sort_pop(_parents);
perf2worth.resize(_parents, sz);
_offspring.clear();
}
private :
eoPerf2Worth<EOT, WorthT>& perf2worth;
};
template <class EOT>
eoPerf2Worth<EOT, double>& make_perf2worth(eoParser& parser, eoState& state)
{
unsigned what = parser.createParam(unsigned(1), "perf2worth", "worth mapping indicator : \n\t \
0: non_dominated sorting \n\t\
1: non_dominated sorting 2 \n\t\
2: simple ranking \n\t", 'w').value();
switch (what)
{
case 1 : return state.storeFunctor(new eoNDSorting_II<EOT>());
case 2 :
{
eoDominanceMap<eoDouble>& dominance = state.storeFunctor(new eoDominanceMap<EOT>);
return state.storeFunctor(new eoParetoRanking<EOT>(dominance));
}
}
//default
if (what > 2)
{
std::cout << "Warning, need an integer < 3 for perf2worth" << std::endl;
// should actually set parser flag, but I don't care
}
return state.storeFunctor(new eoNDSorting_I<EOT>(0.5));
}
template <class EOT>
eoSelectOne<EOT>& make_selector(eoParser& parser, eoState& state, eoPerf2Worth<EOT, double>& perf2worth)
{
unsigned tournamentsize = parser.createParam(unsigned(2), "tournament_size", "Tournament Size", 't').value();
double stochtour = parser.createParam(unsigned(0.95), "tournament_prob", "Probability in stochastic tournament").value();
switch (parser.createParam(unsigned(0), "selector", "Which selector (too lazy to explain: use the source)", 's').value())
{
case 1 : return state.storeFunctor(new eoStochTournamentWorthSelect<eoDouble>(perf2worth, stochtour));
case 2 : return state.storeFunctor(new eoRouletteWorthSelect<eoDouble>(perf2worth));
case 3 : return state.storeFunctor(new eoRandomSelect<EOT>);
}
// default
return state.storeFunctor(new eoDetTournamentWorthSelect<eoDouble>(perf2worth, tournamentsize));
}
// Test pareto dominance and perf2worth, and while you're at it, test the eoGnuPlot monitor as well
void the_main(int argc, char* argv[])
{
Init init;
Eval eval;
Mutate mutate;
eoParser parser(argc, argv);
eoState state;
unsigned num_gen = parser.createParam(unsigned(50), "num_gen", "number of generations to run", 'g').value();
unsigned pop_size = parser.createParam(unsigned(100), "pop_size", "population size", 'p').value();
eoPop<eoDouble> pop(pop_size, init);
// Look, a factory function
eoPerf2Worth<eoDouble, double>& perf2worth = make_perf2worth<eoDouble>(parser, state);
// Look: another factory function, now for selection
eoSelectOne<eoDouble>& select = make_selector<eoDouble>(parser, state, perf2worth);
// One general operator
eoProportionalOp<eoDouble> opsel;
opsel.add(mutate, 1.0);
// the breeder
eoGeneralBreeder<eoDouble> breeder(select, opsel);
// replacement
eoNDPlusReplacement<eoDouble> replace(perf2worth);
unsigned long generation = 0;
eoGenContinue<eoDouble> gen(num_gen, generation);
eoCheckPoint<eoDouble> cp(gen);
eoMOFitnessStat<eoDouble> fitness0(0, "FirstObjective");
eoMOFitnessStat<eoDouble> fitness1(1, "SecondObjective");
cp.add(fitness0);
cp.add(fitness1);
#ifdef HAVE_GNUPLOT
eoGnuplot1DSnapshot snapshot("pareto");
//snapshot.with(eoGnuplot::Points(3));
cp.add(snapshot);
snapshot.add(fitness0);
snapshot.add(fitness1);
#endif
// the algo
eoEasyEA<eoDouble> ea(cp, eval, breeder, replace);
if (parser.userNeedsHelp())
{
parser.printHelp(std::cout);
return;
}
apply<eoDouble>(eval, pop);
ea(pop);
}
int main(int argc, char* argv[])
{
try
{
the_main(argc, argv);
}
catch (std::exception& e)
{
std::cout << "Exception thrown: " << e.what() << std::endl;
throw e; // make sure it does not pass the test
}
}
// Local Variables:
// mode: C++
// c-file-style: "Stroustrup"
// End:

View file

@ -1,196 +0,0 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// t-eoParetoFitness.cpp
// (c) Maarten Keijzer
/*
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: mak@dhi.dk
30/01/02 - MS - Added the eoVariableParetoTraits - and the compare Fn
*/
//-----------------------------------------------------------------------------
#include <cassert>
#include <iostream>
#include "eoParetoFitness.h"
using namespace std;
/** test program for Pareto Fitness */
class MinimizingTraits : public eoParetoFitnessTraits
{
public :
static bool maximizing(int) { return false; }
};
template <class F>
void compare(F & _eo1, F & _eo2)
{
if (_eo1.dominates(_eo2))
std::cout << _eo1 << " dominates " << _eo2 << std::endl;
else if (_eo2.dominates(_eo1))
std::cout << _eo2 << " dominates " << _eo1 << std::endl;
else
std::cout << "None of " << _eo1 << " and " << _eo2 << "dominates the other" << std::endl;
return;
}
int main()
{
typedef eoParetoFitness<> MaxFitness;
typedef eoParetoFitness<MinimizingTraits> MinFitness;
typedef eoParetoFitness<eoVariableParetoTraits> VarFitness;
try{
MaxFitness f0;
f0[0] = 0.0;
f0[1] = 1.0;
MaxFitness f1;
f1[0] = 1.0;
f1[1] = 0.0;
MaxFitness f2;
f2[0] = 0.0;
f2[1] = 0.5;
// now f0 should dominate f2;
if (!f0.dominates(f2))
{
std::cout << f2 << " not dominated by " << f0;
throw;
}
// f0 and f1 should not dominate each other
if (f0.dominates(f1) || f1.dominates(f0))
{
std::cout << f0 << " and " << f1 << " dominate";
throw;
}
if (! (f0 == f0))
{
std::cout << "f0 == f0 failed" << std::endl;
throw;
}
// test ctors and such
MaxFitness f3 = f0;
f3[0] += 1e-9;
// test tolerance
assert(f3 == f0);
MinFitness m0;
MinFitness m1;
MinFitness m2;
MinFitness m3;
m0[0] = 0.0;
m0[1] = 1.0;
m1[0] = 1.0;
m1[1] = 0.0;
m2[0] = 0.0;
m2[1] = 0.5;
m3[0] = 0.5;
m3[1] = 0.5;
//m2 should dominate m0
assert(m2.dominates(m0));
assert(!m1.dominates(m0));
assert(!m0.dominates(m1));
assert(!m0.dominates(m2)); // (m2 < m0));
assert(m2.dominates(m3)); //m3 < m2);
assert(!m3.dominates(m2)); // (m2 < m3));
assert(m2.dominates(m3)); //m2 > m3);
//////////////////////////////////////////
// now the run-time set-able number of objectives
////////////////////////////////////////////
std::cout << "On y va" << std::endl;
// setup fitness WARNING do not try to allocate any EO before that (runtime error)
vector<bool> b(2, true);
b[0]=true;
b[1]=false;
VarFitness::setUp(2, b);
std::cout << "\nMAXimizing on Obj 0 and MINimizing on Obj 1\n";
VarFitness mv0;
VarFitness mv1;
VarFitness mv2;
VarFitness mv3;
mv0[0] = 0.0;
mv0[1] = 1.0;
mv1[0] = 1.0;
mv1[1] = 0.0;
mv2[0] = 0.0;
mv2[1] = 0.5;
mv3[0] = 0.5;
mv3[1] = 0.5;
compare <VarFitness>(mv0,mv1);
compare <VarFitness>(mv0,mv2);
compare <VarFitness>(mv0,mv3);
compare <VarFitness>(mv1,mv2);
compare <VarFitness>(mv1,mv3);
compare <VarFitness>(mv2,mv3);
std::cout << "\nChanging now the min <-> max\n";
b[0]=false;
b[1]=true;
VarFitness::setUp(2, b);
std::cout << "\nMINimizing on Obj 0 and MAXimizing on Obj 1\n";
compare <VarFitness>(mv0,mv1);
compare <VarFitness>(mv0,mv2);
compare <VarFitness>(mv0,mv3);
compare <VarFitness>(mv1,mv2);
compare <VarFitness>(mv1,mv3);
compare <VarFitness>(mv2,mv3);
std::cout << "\nTesting WARNING\n";
b.resize(3);
b[0]=false;
b[1]=true;
b[2]=true;
VarFitness::setUp(3, b);
}
catch(std::exception& e)
{
std::cout << e.what() << std::endl;
}
}

View file

@ -1,87 +0,0 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// t-eobreeder.cpp
// This program test the breeder object
// (c) GeNeura Team, 1998
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU 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
*/
//-----------------------------------------------------------------------------
#ifndef __GNUG__
// to avoid long name warnings
#pragma warning(disable:4786)
#endif // __GNUG__
#include <ga/eoBin.h> // eoBin, eoPop, eoBreeder
#include <eoPop.h>
#include <ga/eoBitOp.h>
#include <eoProportionalOpSel.h>
#include <eoBreeder.h>
//-----------------------------------------------------------------------------
typedef eoBin<float> Chrom;
#include "binary_value.h"
//-----------------------------------------------------------------------------
main()
{
const unsigned POP_SIZE = 8, CHROM_SIZE = 4;
unsigned i;
eoBinRandom<Chrom> random;
eoPop<Chrom> pop;
BinaryValue eval;
for (i = 0; i < POP_SIZE; ++i)
{
Chrom chrom(CHROM_SIZE);
random(chrom);
eval(chrom);
pop.push_back(chrom);
}
std::cout << "population:" << std::endl;
for (i = 0; i < pop.size(); ++i)
std::cout << pop[i] << " " << pop[i].fitness() << std::endl;
eoBinBitFlip<Chrom> bitflip;
eoBinCrossover<Chrom> xover;
eoProportionalOpSel<Chrom> propSel;
eoBreeder<Chrom> breeder( propSel );
propSel.addOp(bitflip, 0.25);
propSel.addOp(xover, 0.75);
breeder(pop);
// reevaluation of fitness
for_each(pop.begin(), pop.end(), BinaryValue());
std::cout << "new population:" << std::endl;
for (i = 0; i < pop.size(); ++i)
std::cout << pop[i] << " " << pop[i].fitness() << std::endl;
return 0;
}
//-----------------------------------------------------------------------------

View file

@ -1,145 +0,0 @@
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
t-eogeneration.cpp
Testing the eoGeneration classes, and classes related to it
(c) GeNeura Team, 1999, 2000
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; 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
*/
//-----------------------------------------------------------------------------//
// to avoid long name warnings
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif
#include <eoGeneration.h>
#include <eoEvalFuncPtrCnt.h>
#include "binary_value.h"
//-----------------------------------------------------------------------------
typedef eoBin<float> Chrom;
//-----------------------------------------------------------------------------
main()
{
const unsigned POP_SIZE = 8, CHROM_SIZE = 16;
unsigned i;
eoBinRandom<Chrom> random;
eoPop<Chrom> pop;
// Evaluation
eoEvalFuncPtr<Chrom> eval( binary_value );
for (i = 0; i < POP_SIZE; ++i)
{
Chrom chrom(CHROM_SIZE);
random(chrom);
eval(chrom);
pop.push_back(chrom);
}
std::cout << "population:" << std::endl;
for (i = 0; i < pop.size(); ++i)
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
// selection
eoLottery<Chrom> lottery;
// breeder
eoBinBitFlip<Chrom> bitflip;
eoBinCrossover<Chrom> xover;
eoProportionalOpSel<Chrom> propSel;
eoBreeder<Chrom> breeder( propSel );
propSel.addOp(bitflip, 0.25);
propSel.addOp(xover, 0.75);
// replacement
eoInclusion<Chrom> inclusion;
// GA generation
eoGeneration<Chrom> generation(lottery, breeder, inclusion, eval);
// evolution
unsigned g = 0;
do {
try
{
generation(pop);
}
catch (std::exception& e)
{
std::cout << "exception: " << e.what() << std::endl;;
exit(EXIT_FAILURE);
}
std::cout << "pop[" << ++g << "]" << std::endl;
for (i = 0; i < pop.size(); ++i)
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
} while (pop[0].fitness() < pow(2.0, CHROM_SIZE) - 1);
// Try again, with a "counted" evaluation function
// GA generation
// Evaluation
eoEvalFuncPtrCnt<Chrom> eval2( binary_value );
eoPop<Chrom> pop2;
for (i = 0; i < POP_SIZE; ++i)
{
Chrom chrom(CHROM_SIZE);
random(chrom);
binary_value(chrom);
eval2(chrom);
pop2.push_back(chrom);
}
eoGeneration<Chrom> generation2(lottery, breeder, inclusion, eval2);
// evolution
do {
try
{
generation2(pop2);
}
catch (std::exception& e)
{
std::cout << "exception: " << e.what() << std::endl;;
exit(EXIT_FAILURE);
}
std::cout << "pop[" << ++g << "]" << std::endl;
for (i = 0; i < pop2.size(); ++i)
std::cout << "\t" << pop2[i] << " " << pop[i].fitness() << std::endl;
} while (pop2[0].fitness() < pow(2.0, CHROM_SIZE) - 1);
std::cout << "Number of evaluations " << eval2.getNumOfEvaluations() << std::endl;
return 0;
}
//-----------------------------------------------------------------------------

View file

@ -1,97 +0,0 @@
//-----------------------------------------------------------------------------
// t-eoinclusion.cpp
//-----------------------------------------------------------------------------
#include <ga/eoBin.h>
#include <eoPop.h>
#include <eoInclusion.h>
//-----------------------------------------------------------------------------
typedef eoBin<float> Chrom;
#include "binary_value.h"
//-----------------------------------------------------------------------------
main()
{
const unsigned CHROM_SIZE = 4;
eoBinRandom<Chrom> random;
BinaryValue eval;
for (unsigned POP_SIZE = 4; POP_SIZE <=6; POP_SIZE++)
{
eoPop<Chrom> pop;
unsigned i;
for ( i = 0; i < POP_SIZE; i++)
{
Chrom chrom(CHROM_SIZE);
random(chrom);
eval(chrom);
pop.push_back(chrom);
}
for (unsigned POP2_SIZE = 4; POP2_SIZE <=6; POP2_SIZE++)
{
eoPop<Chrom> pop2, pop3, pop4, pop5;
for (i = 0; i < POP2_SIZE; i++)
{
Chrom chrom(CHROM_SIZE);
random(chrom);
eval(chrom);
pop2.push_back(chrom);
}
std::cout << "--------------------------------------------------" << std::endl
<< "breeders \tpop" << std::endl
<< "--------------------------------------------------" << std::endl;
for (i = 0; i < max(pop.size(), pop2.size()); i++)
{
if (pop.size() > i)
std::cout << pop[i] << " " << pop[i].fitness() << " \t";
else
std::cout << "\t\t";
if (pop2.size() > i)
std::cout << pop2[i] << " " << pop2[i].fitness();
std::cout << std::endl;
}
eoInclusion<Chrom> inclusion(0.75);
pop3 = pop2;
inclusion(pop, pop3);
eoInclusion<Chrom> inclusion2;
pop4 = pop2;
inclusion2(pop, pop4);
eoInclusion<Chrom> inclusion3(1.5);
pop5 = pop2;
inclusion3(pop, pop5);
std::cout << std::endl
<< "0.75 \t\t1.0 \t\t1.5" << std::endl
<< "---- \t\t--- \t\t---" << std::endl;
for (i = 0; i < pop5.size(); i++)
{
if (pop3.size() > i)
std::cout << pop3[i] << " " << pop3[i].fitness() << " \t";
else
std::cout << " \t\t";
if (pop4.size() > i)
std::cout << pop4[i] << " " << pop4[i].fitness() << " \t";
else
std::cout << " \t\t";
if (pop5.size() > i)
std::cout << pop5[i] << " " << pop5[i].fitness();
std::cout << std::endl;
}
}
}
return 0;
}
//-----------------------------------------------------------------------------

View file

@ -1,108 +0,0 @@
//-----------------------------------------------------------------------------
// t-eoinsertion.cpp
//-----------------------------------------------------------------------------
#include <eo> // eoBin, eoPop, eoInsertion
//-----------------------------------------------------------------------------
typedef eoBin<float> Chrom;
//-----------------------------------------------------------------------------
void binary_value(Chrom& chrom)
{
float sum = 0;
for (unsigned i = 0; i < chrom.size(); i++)
if (chrom[i])
sum += pow(2, chrom.size() - i - 1);
chrom.fitness(sum);
}
//-----------------------------------------------------------------------------
main()
{
const unsigned CHROM_SIZE = 4;
unsigned i;
eoBinRandom<Chrom> random;
for (unsigned POP_SIZE = 4; POP_SIZE <=6; POP_SIZE++)
{
eoPop<Chrom> pop;
for (i = 0; i < POP_SIZE; i++)
{
Chrom chrom(CHROM_SIZE);
random(chrom);
binary_value(chrom);
pop.push_back(chrom);
}
for (unsigned POP2_SIZE = 4; POP2_SIZE <=6; POP2_SIZE++)
{
eoPop<Chrom> pop2, pop3, pop4, pop5, popx;
for (i = 0; i < POP2_SIZE; i++)
{
Chrom chrom(CHROM_SIZE);
random(chrom);
binary_value(chrom);
pop2.push_back(chrom);
}
std::cout << "--------------------------------------------------" << std::endl
<< "breeders \tpop" << std::endl
<< "--------------------------------------------------" << std::endl;
for (i = 0; i < max(pop.size(), pop2.size()); i++)
{
if (pop.size() > i)
std::cout << pop[i] << " " << pop[i].fitness() << " \t";
else
std::cout << "\t\t";
if (pop2.size() > i)
std::cout << pop2[i] << " " << pop2[i].fitness();
std::cout << std::endl;
}
eoInsertion<Chrom> insertion(0.75);
popx = pop;
pop3 = pop2;
insertion(popx, pop3);
eoInsertion<Chrom> insertion2;
popx = pop;
pop4 = pop2;
insertion2(popx, pop4);
eoInsertion<Chrom> insertion3(1.5);
popx = pop;
pop5 = pop2;
insertion3(popx, pop5);
std::cout << std::endl
<< "0.75 \t\t1.0 \t\t1.5" << std::endl
<< "---- \t\t--- \t\t---" << std::endl;
for (i = 0; i < pop5.size(); i++)
{
if (pop3.size() > i)
std::cout << pop3[i] << " " << pop3[i].fitness() << " \t";
else
std::cout << " \t\t";
if (pop4.size() > i)
std::cout << pop4[i] << " " << pop4[i].fitness() << " \t";
else
std::cout << " \t\t";
if (pop5.size() > i)
std::cout << pop5[i] << " " << pop5[i].fitness();
std::cout << std::endl;
}
}
}
return 0;
}
//-----------------------------------------------------------------------------

View file

@ -1,54 +0,0 @@
//-----------------------------------------------------------------------------
// t-eolottery.cpp
//-----------------------------------------------------------------------------
#include <eo> // eoBin, eoPop, eoLottery
//-----------------------------------------------------------------------------
typedef eoBin<float> Chrom;
void binary_value(Chrom& chrom)
{
float sum = 0;
for (unsigned i = 0; i < chrom.size(); i++)
if (chrom[i])
sum += pow(2, i);
chrom.fitness(sum);
}
//-----------------------------------------------------------------------------
main()
{
const unsigned POP_SIZE = 8, CHROM_SIZE = 4;
eoPop<Chrom> pop, pop2;
eoBinRandom<Chrom> random;
unsigned i;
for (i = 0; i < POP_SIZE; i++)
{
Chrom chrom(CHROM_SIZE);
random(chrom);
binary_value(chrom);
pop.push_back(chrom);
}
std::cout << "original population:" << std::endl;
sort(pop.begin(), pop.end());
for (i = 0; i < pop.size(); i++)
std::cout << pop[i] << " " << pop[i].fitness() << std::endl;
eoLottery<Chrom> lottery;
lottery(pop, pop2);
std::cout << "selected by lottery population:" << std::endl;
sort(pop2.begin(), pop2.end());
for (i = 0; i < pop2.size(); i++)
std::cout << pop2[i] << " " << pop2[i].fitness() << std::endl;
return 0;
}
//-----------------------------------------------------------------------------

View file

@ -1,55 +0,0 @@
//-----------------------------------------------------------------------------
// t-eoproblem.cpp
// (c) GeNeura Team 1998
//-----------------------------------------------------------------------------
#include <time.h> // time
#include <math.h> // fabs
#include <iostream> // std::cout
#include <eo> // eoVector, eoProblem
//-----------------------------------------------------------------------------
typedef eoVector<float, float> Chrom;
//-----------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const Chrom& chrom)
{
copy(chrom.begin(), chrom.end(), std::ostream_iterator<int>(os));
return os;
}
//-----------------------------------------------------------------------------
class Easy//: public eoProblem<Chrom>
{
public:
static const unsigned size;
float operator()(const Chrom& chrom)
{
return 1.0 / (fabs(chrom[0]) + 1.0);
}
};
const unsigned Easy::size = 1;
//-----------------------------------------------------------------------------
int main()
{
Easy easy;
Chrom chrom(Easy::size);
srand(time(0));
chrom[0] = ((float)rand()) / ((float)RAND_MAX);
chrom.fitness(easy(chrom));
std::cout << "chrom = " << chrom << std::endl
<< "chrom.fitness() = " << chrom.fitness() << std::endl;
return 0;
}
//-----------------------------------------------------------------------------