move paradiseo/eo to deprecated/ before merge with eodev
This commit is contained in:
parent
948da627ea
commit
0c5120f675
61 changed files with 0 additions and 0 deletions
116
deprecated/eo/test/CMakeLists.txt
Normal file
116
deprecated/eo/test/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
###############################################################################
|
||||
##
|
||||
## CMakeLists file for eo/test
|
||||
##
|
||||
###############################################################################
|
||||
|
||||
|
||||
######################################################################################
|
||||
### 1) Include the sources
|
||||
######################################################################################
|
||||
|
||||
INCLUDE_DIRECTORIES(${EO_SOURCE_DIR}/src)
|
||||
INCLUDE_DIRECTORIES(${EO_SOURCE_DIR}/contrib)
|
||||
INCLUDE_DIRECTORIES(${EO_SOURCE_DIR}/contrib/MGE)
|
||||
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
######################################################################################
|
||||
### 2) Specify where CMake can find the libraries
|
||||
######################################################################################
|
||||
|
||||
LINK_DIRECTORIES(${EO_BINARY_DIR}/lib)
|
||||
|
||||
######################################################################################
|
||||
### 3) Define your targets and link the librairies
|
||||
######################################################################################
|
||||
|
||||
SET (TEST_LIST
|
||||
t-eofitness
|
||||
t-eoRandom
|
||||
t-eobin
|
||||
t-eoVirus
|
||||
t-MGE
|
||||
t-MGE1bit
|
||||
t-MGE-control
|
||||
t-eoStateAndParser
|
||||
t-eoCheckpointing
|
||||
t-eoSSGA
|
||||
t-eoExternalEO
|
||||
t-eoSymreg
|
||||
t-eo
|
||||
t-eoReplacement
|
||||
t-eoSelect
|
||||
t-eoGenOp
|
||||
t-eoGA
|
||||
t-eoReal
|
||||
t-eoVector
|
||||
t-eoESAll
|
||||
t-eoPBIL
|
||||
t-eoFitnessAssembled
|
||||
t-eoFitnessAssembledEA
|
||||
t-eoRoulette
|
||||
t-eoSharing
|
||||
t-eoCMAES
|
||||
t-eoSecondsElapsedContinue
|
||||
t-eoRNG
|
||||
t-eoEasyPSO
|
||||
t-eoInt
|
||||
t-eoInitPermutation
|
||||
t-eoSwapMutation
|
||||
t-eoShiftMutation
|
||||
t-eoTwoOptMutation
|
||||
t-eoRingTopology
|
||||
t-eoSyncEasyPSO
|
||||
t-eoOrderXover
|
||||
t-eoExtendedVelocity
|
||||
t-eoLogger
|
||||
#t-eoIQRStat # Temporary by-passed in order to test coverage
|
||||
t-eoParallel
|
||||
#t-openmp # does not work anymore since functions used in this test were removed from EO
|
||||
#t-eoDualFitness
|
||||
t-eoParser
|
||||
)
|
||||
|
||||
|
||||
FOREACH (test ${TEST_LIST})
|
||||
SET ("T_${test}_SOURCES" "${test}.cpp")
|
||||
ENDFOREACH (test)
|
||||
|
||||
|
||||
IF(ENABLE_MINIMAL_CMAKE_TESTING)
|
||||
|
||||
SET (MIN_TEST_LIST t-eoEasyPSO)
|
||||
FOREACH (mintest ${MIN_TEST_LIST})
|
||||
SET ("T_${mintest}_SOURCES" "${mintest}.cpp")
|
||||
ADD_EXECUTABLE(${mintest} ${T_${mintest}_SOURCES})
|
||||
ADD_TEST(${mintest} ${mintest})
|
||||
TARGET_LINK_LIBRARIES(${mintest} ga es cma eoutils eo)
|
||||
ENDFOREACH (mintest)
|
||||
|
||||
ELSEIF(ENABLE_CMAKE_TESTING)
|
||||
|
||||
FOREACH (test ${TEST_LIST})
|
||||
ADD_EXECUTABLE(${test} ${T_${test}_SOURCES})
|
||||
ADD_TEST(${test} ${test})
|
||||
TARGET_LINK_LIBRARIES(${test} ga es cma eoutils eo)
|
||||
INSTALL(TARGETS ${test} RUNTIME DESTINATION local/share${INSTALL_SUB_DIR}/eo/test COMPONENT test)
|
||||
ENDFOREACH (test)
|
||||
|
||||
SET(RESOURCES
|
||||
boxplot.py
|
||||
boxplot_to_png.py
|
||||
boxplot_to_pdf.py
|
||||
t-openmp.py
|
||||
)
|
||||
|
||||
FOREACH(file ${RESOURCES})
|
||||
EXECUTE_PROCESS(
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/${file}
|
||||
${CMAKE_CURRENT_BINARY_DIR}/${file}
|
||||
)
|
||||
ENDFOREACH(file)
|
||||
|
||||
ENDIF(ENABLE_MINIMAL_CMAKE_TESTING)
|
||||
|
||||
######################################################################################
|
||||
36
deprecated/eo/test/ChangeLog
Normal file
36
deprecated/eo/test/ChangeLog
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
2006-12-04 Jochen Kpper <jochen@fhi-berlin.mpg.de>
|
||||
|
||||
* Makefile.am: Add t-eoRNG
|
||||
|
||||
* t-eoRNG.cpp: Start test for random number generator.
|
||||
|
||||
2006-12-02 Jochen Kpper <jochen@fhi-berlin.mpg.de>
|
||||
|
||||
* t-MGE1bit.cpp: Change float to double.
|
||||
|
||||
* t-eoGenOp.cpp (init): Do not add std::ends to end of string, as this
|
||||
results in escape-codes (^@) to be printed at runtime and is not
|
||||
necessary anyway.
|
||||
|
||||
* test/t-eoSymreg.cpp (SymregNode::operator()): Initialize r1 and r2 to
|
||||
avoid compiler warnings.
|
||||
|
||||
|
||||
2006-07-02 Thomas Legrand <thomas.legrand@inria.fr>
|
||||
|
||||
* test/t-eoEasyPSO.cpp: added PSO test
|
||||
|
||||
* test/Makefile.am: added PSO test
|
||||
|
||||
|
||||
2006-02-27 Thomas Legrand <thomas.legrand@inria.fr>
|
||||
|
||||
* test/t-eoSyncEasyPSO.cpp: added synchronous PSO test
|
||||
* test/t-eoEasyPSO.cpp: customized PSO test (initialization)
|
||||
* test/Makefile.am: added synchronous PSO test
|
||||
|
||||
* Local Variables:
|
||||
* coding: iso-8859-1
|
||||
* mode: flyspell
|
||||
* fill-column: 80
|
||||
* End:
|
||||
60
deprecated/eo/test/RoyalRoad.h
Normal file
60
deprecated/eo/test/RoyalRoad.h
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
RoyalRoad.h
|
||||
-- Implementation of the Royal Road function for any length and block size
|
||||
(c) GeNeura Team 2001, Marc Schoenauer 2000
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
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
|
||||
CVS Info: $Date: 2001-06-21 12:03:17 $ $Header: /home/nojhan/dev/eodev/eodev_cvs/eo/test/RoyalRoad.h,v 1.3 2001-06-21 12:03:17 jmerelo Exp $ $Author: jmerelo $
|
||||
*/
|
||||
|
||||
#ifndef RoyalRoad_h
|
||||
#define RoyalRoad_h
|
||||
|
||||
template<class EOT>
|
||||
class RoyalRoad: public eoEvalFunc<EOT> {
|
||||
|
||||
public:
|
||||
|
||||
typedef typename EOT::Fitness FitT;
|
||||
|
||||
/// Ctor: takes a length, and divides that length in equal parts
|
||||
RoyalRoad( unsigned _div ): eoEvalFunc<EOT >(), div( _div ) {};
|
||||
|
||||
// Applies the function
|
||||
virtual void operator() ( EOT & _eo ) {
|
||||
FitT fitness = 0;
|
||||
if (_eo.invalid()) {
|
||||
for ( unsigned i = 0; i < _eo.size()/div; i ++ ) {
|
||||
bool block = true;
|
||||
for ( unsigned j = 0; j < div; j ++ ) {
|
||||
block &= _eo[i*div+j];
|
||||
}
|
||||
if (block) {
|
||||
fitness += div;
|
||||
}
|
||||
}
|
||||
_eo.fitness( fitness );
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
unsigned div;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
16
deprecated/eo/test/binary_value.h
Normal file
16
deprecated/eo/test/binary_value.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#include <algorithm>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** Just the simple function that takes binary value of a chromosome and sets
|
||||
the fitnes.
|
||||
@param _chrom A binary chromosome
|
||||
*/
|
||||
|
||||
template <class Chrom> double binary_value(const Chrom& _chrom)
|
||||
{
|
||||
double sum = 0.0;
|
||||
for (unsigned i=0; i<_chrom.size(); i++)
|
||||
sum += _chrom[i];
|
||||
return sum;
|
||||
}
|
||||
15
deprecated/eo/test/boxplot.py
Executable file
15
deprecated/eo/test/boxplot.py
Executable file
|
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import pylab
|
||||
import sys
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) < 2:
|
||||
print 'Usage: boxplot.py [Results files, ...]'
|
||||
sys.exit()
|
||||
|
||||
for i in range(1, len(sys.argv)):
|
||||
pylab.boxplot( [ [ float(value) for value in line.split() ] for line in open( sys.argv[i] ).readlines() ] )
|
||||
|
||||
pylab.xlabel('iterations')
|
||||
pylab.show()
|
||||
15
deprecated/eo/test/boxplot_to_pdf.py
Executable file
15
deprecated/eo/test/boxplot_to_pdf.py
Executable file
|
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import pylab
|
||||
import sys
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) < 3:
|
||||
print 'Usage: boxplot_to_pdf.py [Results files, ...] [output file in .pdf]'
|
||||
sys.exit()
|
||||
|
||||
for i in range(1, len(sys.argv) - 1):
|
||||
pylab.boxplot( [ [ float(value) for value in line.split() ] for line in open( sys.argv[i] ).readlines() ] )
|
||||
|
||||
pylab.xlabel('iterations')
|
||||
pylab.savefig( sys.argv[ len(sys.argv) - 1 ], format='pdf', transparent=True )
|
||||
15
deprecated/eo/test/boxplot_to_png.py
Executable file
15
deprecated/eo/test/boxplot_to_png.py
Executable file
|
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import pylab
|
||||
import sys
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) < 3:
|
||||
print 'Usage: boxplot_to_png.py [Results files, ...] [output file in .png]'
|
||||
sys.exit()
|
||||
|
||||
for i in range(1, len(sys.argv) - 1):
|
||||
pylab.boxplot( [ [ float(value) for value in line.split() ] for line in open( sys.argv[i] ).readlines() ] )
|
||||
|
||||
pylab.xlabel('iterations')
|
||||
pylab.savefig( sys.argv[ len(sys.argv) - 1 ], format='png', transparent=True, papertype='a0' )
|
||||
431
deprecated/eo/test/fitness_traits.cpp
Normal file
431
deprecated/eo/test/fitness_traits.cpp
Normal file
|
|
@ -0,0 +1,431 @@
|
|||
#include <map> // for pair
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <math.h> // for exp
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* fitness_traits.h */
|
||||
|
||||
// default traits: defaults to a double that needs to be maximized
|
||||
template <class T = double>
|
||||
struct fitness_traits
|
||||
{
|
||||
// Needs mapping can be used to figure out whether you need to do fitness scaling (or not)
|
||||
const static bool needs_mapping = false;
|
||||
|
||||
// storage_type: what to store next to the genotype
|
||||
typedef T storage_type;
|
||||
|
||||
// performance_type: what the eoEvalFunc calculates
|
||||
typedef T performance_type;
|
||||
|
||||
// worth_type: what the scaling function does
|
||||
typedef T worth_type;
|
||||
|
||||
// access_performance: how to get from what is stored to a mutable performance
|
||||
static performance_type& access_performance(storage_type& a) { return a; }
|
||||
|
||||
// access_worth: how to get from what is stored to a mutable worth
|
||||
static worth_type& access_worth(storage_type& a) { return a; }
|
||||
|
||||
// get_performance: from storage_type to a performance figure
|
||||
static performance_type get_performance(storage_type a) { return a; }
|
||||
|
||||
// get_worth: from storage_type to a worth figure
|
||||
static worth_type get_worth(storage_type a) { return a; }
|
||||
|
||||
// get the fitness out of the individual
|
||||
template <class EOT>
|
||||
static worth_type get_fitness(const EOT& _eo) { return _eo.performance(); }
|
||||
|
||||
// compare the two individuals
|
||||
template <class EOT>
|
||||
static bool is_better(const EOT& _eo1, const EOT& _eo2)
|
||||
{
|
||||
return _eo1.performance() > _eo2.performance();
|
||||
}
|
||||
};
|
||||
|
||||
struct minimization {};
|
||||
struct maximization {};
|
||||
|
||||
struct fitness_traits<minimization> : public fitness_traits<double>
|
||||
{
|
||||
// for minimization, invert the is_better
|
||||
template <class EOT>
|
||||
static bool is_better(const EOT& _eo1, const EOT& _eo2)
|
||||
{
|
||||
return _eo1.performance() < _eo2.performance();
|
||||
}
|
||||
};
|
||||
|
||||
// for maximization, just take the default behaviour
|
||||
struct fitness_traits<maximization> : public fitness_traits<double> {};
|
||||
|
||||
// forward declaration
|
||||
//template <class EOT> class eoPop;
|
||||
//template <class Fitness, class Traits> class EO;
|
||||
|
||||
// unfortunately, partial template specialization is not approved by Microsoft (though ANSI says it's ok)
|
||||
// Probably need some macro-magic to make this work (MicroSoft == MacroHard)
|
||||
// A pair class: first == performance, second == worth, redefine all types, data and functions
|
||||
template <class Performance, class Worth>
|
||||
struct fitness_traits< pair<Performance, Worth> >
|
||||
{
|
||||
typedef pair<Performance, Worth> storage_type;
|
||||
typedef Performance performance_type;
|
||||
typedef Worth worth_type;
|
||||
|
||||
const static bool needs_mapping = true;
|
||||
|
||||
static performance_type& access_performance(storage_type& a) { return a.first; }
|
||||
static worth_type& access_worth(storage_type& a) { return a.second; }
|
||||
|
||||
static performance_type get_performance(const storage_type& a) { return a.first; }
|
||||
static worth_type get_worth(const storage_type& a) { return a.second; }
|
||||
|
||||
// This function calls _eo.worth() which in turn checks the fitness flag and calls get_worth above
|
||||
// The compiler should be able to inline all these calls and come up with a very compact solution
|
||||
template <class EOT>
|
||||
static worth_type get_fitness(const EOT& _eo) { return _eo.worth(); }
|
||||
|
||||
template <class EOT>
|
||||
static bool is_better(const EOT& _eo1, const EOT& _eo2)
|
||||
{
|
||||
return _eo1.worth() > _eo2.worth();
|
||||
}
|
||||
};
|
||||
|
||||
/* end fitness_traits.h */
|
||||
|
||||
/* EO.h
|
||||
|
||||
The Fitness template argument is there for backward compatibility reasons
|
||||
|
||||
*/
|
||||
|
||||
template <class Fitness, class Traits = fitness_traits<Fitness> >
|
||||
class EO
|
||||
{
|
||||
public :
|
||||
|
||||
typedef Traits fitness_traits;
|
||||
typedef typename Traits::storage_type storage_type;
|
||||
typedef typename Traits::performance_type performance_type;
|
||||
typedef typename Traits::worth_type worth_type;
|
||||
|
||||
EO() : valid_performance(false), valid_worth(false), rep_fitness() {}
|
||||
|
||||
// for backwards compatibility
|
||||
void fitness(performance_type perf)
|
||||
{
|
||||
performance(perf);
|
||||
}
|
||||
|
||||
void performance(performance_type perf)
|
||||
{
|
||||
valid_performance = true;
|
||||
Traits::access_performance(rep_fitness) = perf;
|
||||
}
|
||||
|
||||
performance_type performance(void) const
|
||||
{
|
||||
if(!valid_performance) throw runtime_error("no performance");
|
||||
return Traits::get_performance(rep_fitness);
|
||||
}
|
||||
|
||||
void worth(worth_type worth)
|
||||
{
|
||||
valid_worth = true;
|
||||
Traits::access_worth(rep_fitness) = worth;
|
||||
}
|
||||
|
||||
worth_type worth(void) const
|
||||
{
|
||||
if(!valid_worth) throw runtime_error("no worth");
|
||||
if(!Traits::needs_mapping) throw runtime_error("no mapping");
|
||||
return Traits::get_worth(rep_fitness);
|
||||
}
|
||||
|
||||
worth_type fitness(void) const
|
||||
{
|
||||
return Traits::get_fitness(*this);
|
||||
}
|
||||
|
||||
void invalidate(void)
|
||||
{
|
||||
valid_performance = false;
|
||||
valid_worth = false;
|
||||
}
|
||||
|
||||
void invalidate_worth(void)
|
||||
{
|
||||
valid_worth = false;
|
||||
}
|
||||
|
||||
bool operator<(const EO<Fitness, Traits>& other) const
|
||||
{
|
||||
return !Traits::is_better(other, *this);
|
||||
}
|
||||
|
||||
bool operator>(const EO<Fitness, Traits>& other) const
|
||||
{
|
||||
return Traits::is_better(other, *this);
|
||||
}
|
||||
|
||||
private :
|
||||
|
||||
bool valid_performance;
|
||||
bool valid_worth;
|
||||
storage_type rep_fitness;
|
||||
};
|
||||
|
||||
/* end EO.h */
|
||||
|
||||
/* eoPerf2Worth.h */
|
||||
|
||||
// get the name known
|
||||
template <class EOT> class eoPop;
|
||||
|
||||
template <class EOT>
|
||||
void exponential_scaling(eoPop<EOT>& _pop)
|
||||
{
|
||||
for (unsigned i = 0; i < _pop.size(); ++i)
|
||||
{ // change minimimization into maximization
|
||||
_pop[i].worth(exp(-_pop[i].performance()));
|
||||
}
|
||||
}
|
||||
|
||||
template <class EOT>
|
||||
class eoPerf2Worth /* : public eoUF<eoPop<EOT>&, void> */
|
||||
{
|
||||
public :
|
||||
virtual void operator()(eoPop<EOT>& _pop)
|
||||
{
|
||||
return exponential_scaling(_pop);
|
||||
}
|
||||
};
|
||||
|
||||
/* end eoPerf2Worth.h */
|
||||
|
||||
|
||||
/* eoPop.h */
|
||||
|
||||
template <class EOT>
|
||||
class eoPop : public vector<EOT>
|
||||
{
|
||||
public :
|
||||
|
||||
typedef typename EOT::fitness_traits fitness_traits;
|
||||
|
||||
eoPop(void) : p2w(0) {}
|
||||
|
||||
void sort()
|
||||
{
|
||||
scale(); // get the worths up to date
|
||||
|
||||
std::sort(begin(), end(), greater<EOT>());
|
||||
}
|
||||
|
||||
void scale()
|
||||
{
|
||||
if (p2w)
|
||||
{
|
||||
if (!fitness_traits::needs_mapping)
|
||||
{
|
||||
throw runtime_error("eoPop: no scaling needed, yet a scaling function is defined");
|
||||
}
|
||||
|
||||
(*p2w)(*this);
|
||||
}
|
||||
else if (fitness_traits::needs_mapping)
|
||||
{
|
||||
throw runtime_error("eoPop: no scaling function attached to the population, while one was certainly called for");
|
||||
}
|
||||
}
|
||||
|
||||
void setPerf2Worth(eoPerf2Worth<EOT>& _p2w)
|
||||
{
|
||||
p2w = &_p2w;
|
||||
}
|
||||
|
||||
void setPerf2Worth(eoPerf2Worth<EOT>* _p2w)
|
||||
{
|
||||
p2w = _p2w;
|
||||
}
|
||||
|
||||
eoPerf2Worth<EOT>* getPerf2Worth() { return p2w; }
|
||||
|
||||
void swap(eoPop<EOT>& other)
|
||||
{
|
||||
vector<EOT>::swap(other);
|
||||
eoPerf2Worth<EOT>* tmp = p2w;
|
||||
p2w = other.p2w;
|
||||
other.p2w = tmp;
|
||||
}
|
||||
|
||||
private :
|
||||
|
||||
// a pointer as it can be emtpy
|
||||
eoPerf2Worth<EOT>* p2w;
|
||||
};
|
||||
|
||||
// need this one to be able to swap the members as well...
|
||||
template <class EOT>
|
||||
void swap(eoPop<EOT>& _p1, eoPop<EOT>& _p2)
|
||||
{
|
||||
_p1.swap(_p2);
|
||||
}
|
||||
|
||||
/* end eoPop.h */
|
||||
|
||||
/* main and test */
|
||||
|
||||
template <class EOT>
|
||||
void algo(eoPop<EOT>& _pop)
|
||||
{
|
||||
eoPop<EOT> offspring; // how to get the scaling info into this guy??
|
||||
offspring.setPerf2Worth(_pop.getPerf2Worth()); // like this!
|
||||
|
||||
std::copy(_pop.begin(), _pop.end(), back_inserter(offspring));
|
||||
|
||||
offspring.sort(); // should call scale
|
||||
|
||||
swap(_pop, offspring);
|
||||
}
|
||||
|
||||
void minimization_test()
|
||||
{
|
||||
typedef EO<minimization> eo_type;
|
||||
|
||||
eo_type eo1;
|
||||
eo_type eo2;
|
||||
|
||||
eo1.performance(1.0);
|
||||
eo2.performance(2.0);
|
||||
|
||||
std::cout << "With minimizing fitness" << std::endl;
|
||||
std::cout << eo1.fitness() << " < " << eo2.fitness() << " returns " << (eo1 < eo2) << std::endl;
|
||||
std::cout << eo2.fitness() << " < " << eo1.fitness() << " returns " << (eo2 < eo1) << std::endl;
|
||||
}
|
||||
|
||||
void the_main()
|
||||
{
|
||||
typedef EO<double> simple_eo;
|
||||
typedef EO<pair<double, double> > scaled_eo;
|
||||
|
||||
simple_eo eo1;
|
||||
simple_eo eo3;
|
||||
|
||||
/* First test some simple comparisons */
|
||||
|
||||
eo1.fitness(10); // could also use performance()
|
||||
eo3.fitness(5);
|
||||
|
||||
std::cout << eo1.fitness() << std::endl;
|
||||
std::cout << eo3.fitness() << std::endl;
|
||||
|
||||
std::cout << "eo1 < eo3 = " << (eo1 < eo3) << std::endl;
|
||||
|
||||
|
||||
scaled_eo eo2;
|
||||
scaled_eo eo4;
|
||||
eo2.performance(10);
|
||||
eo4.performance(8);
|
||||
|
||||
/* Now test if the worth gets accessed and if the flag protects it */
|
||||
|
||||
try
|
||||
{
|
||||
std::cout << eo2.fitness() << std::endl;
|
||||
std::cout << "did not throw" << std::endl;
|
||||
assert(false); // should throw
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cout << "Fitness threw exception, as it should" << std::endl;
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
|
||||
/* Set the worth and all is well (this is normally done by some perf2worth functor */
|
||||
|
||||
eo2.worth(3);
|
||||
eo4.worth(5);
|
||||
|
||||
std::cout << "with maximization " << std::endl;
|
||||
std::cout << eo2.fitness() << std::endl;
|
||||
std::cout << eo4.fitness() << std::endl;
|
||||
std::cout << eo2.fitness() << " < " << eo4.fitness() << " returns " << (eo2 < eo4) << std::endl;
|
||||
|
||||
/* Test the minimization of fitness */
|
||||
minimization_test();
|
||||
|
||||
|
||||
/* Populations */
|
||||
|
||||
// test pop without scaling, should have no overhead save for a single empty pointer in pop
|
||||
eoPop<simple_eo> pop0;
|
||||
pop0.resize(1);
|
||||
pop0[0].fitness(1);
|
||||
|
||||
algo(pop0);
|
||||
|
||||
std::cout << pop0[0].fitness() << std::endl;
|
||||
|
||||
assert(pop0[0].fitness() == 1);
|
||||
|
||||
/* test pop with scaling */
|
||||
|
||||
eoPerf2Worth<scaled_eo> perf2worth;
|
||||
eoPop<scaled_eo> pop1;
|
||||
|
||||
pop1.resize(1);
|
||||
|
||||
pop1[0].fitness(1.0); // emulate evaluation
|
||||
|
||||
// at this point getting the fitness should throw
|
||||
try
|
||||
{
|
||||
std::cout << pop1[0].fitness() << std::endl;
|
||||
std::cout << "did not throw" << std::endl;
|
||||
assert(false); // should throw
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cout << "Fitness threw exception, as it should" << std::endl;
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
|
||||
// at this point trying to scale should throw
|
||||
try
|
||||
{
|
||||
algo(pop1); // should complain that it cannot scale
|
||||
assert(false); // so it would never get here
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{ // but rather ends here
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
|
||||
// ok, now set the scaling
|
||||
pop1.setPerf2Worth(perf2worth);
|
||||
|
||||
algo(pop1);
|
||||
|
||||
std::cout << "the fitness has been transformed from " << pop1[0].performance() << " to exp(-1) = " << pop1[0].fitness() << std::endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
the_main();
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
16
deprecated/eo/test/real_value.h
Normal file
16
deprecated/eo/test/real_value.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#include <vector>
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
/** Just a simple function that takes an eoEsBase<double> and sets the fitnes
|
||||
to sphere
|
||||
@param _ind vector<double>
|
||||
*/
|
||||
|
||||
double real_value(const std::vector<double>& _ind)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _ind.size(); i++)
|
||||
sum += _ind[i] * _ind[i];
|
||||
return sum/_ind.size();
|
||||
}
|
||||
9
deprecated/eo/test/run_tests
Executable file
9
deprecated/eo/test/run_tests
Executable file
|
|
@ -0,0 +1,9 @@
|
|||
#!/bin/sh
|
||||
|
||||
echo "Testing -h"
|
||||
./t-eoCheckpointing -h
|
||||
|
||||
echo "Finished"
|
||||
|
||||
#TODO test if an error occured
|
||||
echo "Ok"
|
||||
104
deprecated/eo/test/t-MGE-control.cpp
Normal file
104
deprecated/eo/test/t-MGE-control.cpp
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// t-eoMGE.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#ifndef __GNUG__
|
||||
// to avoid long name warnings
|
||||
#pragma warning(disable:4786)
|
||||
#endif // __GNUG__
|
||||
|
||||
#include "eo"
|
||||
#include "ga/eoBitOp.h"
|
||||
|
||||
#include "RoyalRoad.h"
|
||||
|
||||
// Viri
|
||||
#include "VirusOp.h"
|
||||
#include "eoVirus.h"
|
||||
#include "eoInitVirus.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
typedef eoVirus<float> Chrom;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
int main()
|
||||
{
|
||||
const unsigned POP_SIZE = 10, CHROM_SIZE = 12;
|
||||
unsigned i;
|
||||
eoBooleanGenerator gen;
|
||||
|
||||
// the populations:
|
||||
eoPop<Chrom> pop;
|
||||
|
||||
// Evaluation
|
||||
RoyalRoad<Chrom> rr( 8 );
|
||||
eoEvalFuncCounter<Chrom> eval( rr );
|
||||
|
||||
eoInitVirus<float> random(CHROM_SIZE, gen);
|
||||
for (i = 0; i < POP_SIZE; ++i) {
|
||||
Chrom chrom;
|
||||
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
|
||||
eoStochTournamentSelect<Chrom> lottery(0.9 );
|
||||
|
||||
// breeder
|
||||
eoOneBitFlip<Chrom> vm;
|
||||
eoUBitXover<Chrom> xover;
|
||||
eoProportionalOp<Chrom> propSel;
|
||||
eoGeneralBreeder<Chrom> breeder( lottery, propSel );
|
||||
propSel.add(vm, 0.2);
|
||||
propSel.add(xover, 0.8);
|
||||
|
||||
// Replace a single one
|
||||
eoCommaReplacement<Chrom> replace;
|
||||
|
||||
// Terminators
|
||||
eoGenContinue<Chrom> continuator1(10);
|
||||
eoFitContinue<Chrom> continuator2(CHROM_SIZE);
|
||||
eoCombinedContinue<Chrom> continuator(continuator1, continuator2);
|
||||
eoCheckPoint<Chrom> checkpoint(continuator);
|
||||
eoStdoutMonitor monitor;
|
||||
checkpoint.add(monitor);
|
||||
eoSecondMomentStats<Chrom> stats;
|
||||
eoPopStat<Chrom> dumper( 10 );
|
||||
monitor.add(stats);
|
||||
checkpoint.add(dumper);
|
||||
checkpoint.add(stats);
|
||||
|
||||
// GA generation
|
||||
eoEasyEA<Chrom> ea(checkpoint, eval, breeder, replace );
|
||||
|
||||
// evolution
|
||||
try
|
||||
{
|
||||
ea(pop);
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cout << "exception: " << e.what() << std::endl;;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
std::cout << "pop" << std::endl;
|
||||
for (i = 0; i < pop.size(); ++i)
|
||||
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
|
||||
|
||||
std::cout << "\n --> Number of Evaluations = " << eval.getValue() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
108
deprecated/eo/test/t-MGE.cpp
Normal file
108
deprecated/eo/test/t-MGE.cpp
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// t-eoMGE.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#ifndef __GNUG__
|
||||
// to avoid long name warnings
|
||||
#pragma warning(disable:4786)
|
||||
#endif // __GNUG__
|
||||
|
||||
#include <eo>
|
||||
#include <ga/eoBitOp.h>
|
||||
|
||||
#include "RoyalRoad.h"
|
||||
|
||||
// Viri
|
||||
#include "VirusOp.h"
|
||||
#include "eoVirus.h"
|
||||
#include "eoInitVirus.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
typedef eoVirus<float> Chrom;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
int main()
|
||||
{
|
||||
const unsigned POP_SIZE = 10, CHROM_SIZE = 12;
|
||||
unsigned i;
|
||||
eoBooleanGenerator gen;
|
||||
|
||||
// the populations:
|
||||
eoPop<Chrom> pop;
|
||||
|
||||
// Evaluation
|
||||
RoyalRoad<Chrom> rr( 8 );
|
||||
eoEvalFuncCounter<Chrom> eval( rr );
|
||||
|
||||
eoInitVirus<float> random(CHROM_SIZE, gen);
|
||||
for (i = 0; i < POP_SIZE; ++i) {
|
||||
Chrom chrom;
|
||||
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
|
||||
eoStochTournamentSelect<Chrom> lottery(0.9 );
|
||||
|
||||
// breeder
|
||||
VirusMutation<float> vm;
|
||||
VirusTransmission<float> vt;
|
||||
VirusBitFlip<float> vf;
|
||||
eoUBitXover<Chrom> xover;
|
||||
eoProportionalOp<Chrom> propSel;
|
||||
eoGeneralBreeder<Chrom> breeder( lottery, propSel );
|
||||
propSel.add(vm, 0.1);
|
||||
propSel.add(vf, 0.05);
|
||||
propSel.add(vt, 0.05);
|
||||
propSel.add(xover, 0.8);
|
||||
|
||||
// Replace a single one
|
||||
eoCommaReplacement<Chrom> replace;
|
||||
|
||||
// Terminators
|
||||
eoGenContinue<Chrom> continuator1(10);
|
||||
eoFitContinue<Chrom> continuator2(CHROM_SIZE);
|
||||
eoCombinedContinue<Chrom> continuator(continuator1, continuator2);
|
||||
eoCheckPoint<Chrom> checkpoint(continuator);
|
||||
eoStdoutMonitor monitor;
|
||||
checkpoint.add(monitor);
|
||||
eoSecondMomentStats<Chrom> stats;
|
||||
eoPopStat<Chrom> dumper( 10 );
|
||||
monitor.add(stats);
|
||||
checkpoint.add(dumper);
|
||||
checkpoint.add(stats);
|
||||
|
||||
// GA generation
|
||||
eoEasyEA<Chrom> ea(checkpoint, eval, breeder, replace );
|
||||
|
||||
// evolution
|
||||
try
|
||||
{
|
||||
ea(pop);
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::cout << "exception: " << e.what() << std::endl;;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
std::cout << "pop" << std::endl;
|
||||
for (i = 0; i < pop.size(); ++i)
|
||||
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
|
||||
|
||||
std::cout << "\n --> Number of Evaluations = " << eval.getValue() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
111
deprecated/eo/test/t-MGE1bit.cpp
Normal file
111
deprecated/eo/test/t-MGE1bit.cpp
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// t-eoMGE.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#ifndef __GNUG__
|
||||
// to avoid long name warnings
|
||||
#pragma warning(disable:4786)
|
||||
#endif // __GNUG__
|
||||
|
||||
#include "eo"
|
||||
#include "ga/eoBitOp.h"
|
||||
#include "RoyalRoad.h"
|
||||
|
||||
// Viri
|
||||
#include "VirusOp.h"
|
||||
#include "eoVirus.h"
|
||||
#include "eoInitVirus.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
typedef eoVirus<double> Chrom;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
int main()
|
||||
{
|
||||
const unsigned POP_SIZE = 10, CHROM_SIZE = 12;
|
||||
unsigned i;
|
||||
eoBooleanGenerator gen;
|
||||
|
||||
// the populations:
|
||||
eoPop<Chrom> pop;
|
||||
|
||||
// Evaluation
|
||||
RoyalRoad<Chrom> rr( 8 );
|
||||
eoEvalFuncCounter<Chrom> eval( rr );
|
||||
|
||||
eoInitVirus1bit<double> random(CHROM_SIZE, gen);
|
||||
for (i = 0; i < POP_SIZE; ++i) {
|
||||
Chrom chrom;
|
||||
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
|
||||
eoStochTournamentSelect<Chrom> lottery(0.9 );
|
||||
|
||||
// breeder
|
||||
VirusShiftMutation<double> vm;
|
||||
VirusTransmission<double> vt;
|
||||
VirusBitFlip<double> vf;
|
||||
eoUBitXover<Chrom> xover;
|
||||
eoProportionalOp<Chrom> propSel;
|
||||
eoGeneralBreeder<Chrom> breeder( lottery, propSel );
|
||||
propSel.add(vm, 0.8);
|
||||
propSel.add(vf, 0.05);
|
||||
propSel.add(vt, 0.05);
|
||||
propSel.add(xover, 0.1);
|
||||
|
||||
// Replace a single one
|
||||
eoCommaReplacement<Chrom> replace;
|
||||
|
||||
// Terminators
|
||||
eoGenContinue<Chrom> continuator1(10);
|
||||
eoFitContinue<Chrom> continuator2(CHROM_SIZE);
|
||||
eoCombinedContinue<Chrom> continuator(continuator1, continuator2);
|
||||
eoCheckPoint<Chrom> checkpoint(continuator);
|
||||
eoStdoutMonitor monitor;
|
||||
checkpoint.add(monitor);
|
||||
eoSecondMomentStats<Chrom> stats;
|
||||
eoPopStat<Chrom> dumper( 10 );
|
||||
monitor.add(stats);
|
||||
checkpoint.add(dumper);
|
||||
checkpoint.add(stats);
|
||||
|
||||
// GA generation
|
||||
eoEasyEA<Chrom> ea(checkpoint, eval, breeder, replace);
|
||||
|
||||
// evolution
|
||||
try {
|
||||
ea(pop);
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "exception: " << e.what() << std::endl;;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
std::cout << "pop" << std::endl;
|
||||
for (i = 0; i < pop.size(); ++i)
|
||||
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
|
||||
|
||||
std::cout << "\n --> Number of Evaluations = " << eval.getValue() << std::endl;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// Local Variables:
|
||||
// mode: C++
|
||||
// c-file-style: "Stroustrup"
|
||||
// End:
|
||||
14
deprecated/eo/test/t-eo.cpp
Normal file
14
deprecated/eo/test/t-eo.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <eo>
|
||||
|
||||
typedef EO<float> Chrom;
|
||||
|
||||
int main()
|
||||
{
|
||||
Chrom chrom1, chrom2;
|
||||
|
||||
// EO objects can be printed with stream operators
|
||||
std::cout << "chrom1 = " << chrom1 << std::endl
|
||||
<< "chrom2 = " << chrom2 << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
220
deprecated/eo/test/t-eo2dVector.cc
Normal file
220
deprecated/eo/test/t-eo2dVector.cc
Normal file
|
|
@ -0,0 +1,220 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
/*
|
||||
-----------------------------------------------------------------------------
|
||||
File............: t-eo2dVector.cc
|
||||
Author..........: Geneura Team (this file: Victor Rivas, vrivas@ujaen.es)
|
||||
Date............: 01-Oct-1999, at Fac. of Sciences, Univ. of Granada (Spain)
|
||||
Description.....: Test for 2 dimensional eoVector.
|
||||
|
||||
================ Modif. 1 ================
|
||||
Author........:
|
||||
Date..........:
|
||||
Description...:
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <stdexcept>
|
||||
#include <eo2dVector.h> // eo2dVector
|
||||
#include <eoUniform.h> // Random generator
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
typedef unsigned T;
|
||||
typedef double fitnessT;
|
||||
typedef eo2dVector<T,fitnessT> C;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
main()
|
||||
{
|
||||
{
|
||||
C c1;
|
||||
cout << "Default constructor: " << endl
|
||||
<< c1 << endl;
|
||||
}
|
||||
{
|
||||
C c1( 5,6,1 );
|
||||
cout << "Default constructor with values: " << endl
|
||||
<< c1 << endl;
|
||||
|
||||
}
|
||||
{
|
||||
eoUniform<T> aleat( 1,10 );
|
||||
C c1( 5,6, aleat );
|
||||
cout << "Random constructor: " << endl
|
||||
<< c1 << endl;
|
||||
|
||||
}
|
||||
{
|
||||
C c1( 3,4,1 ), c2( c1 );
|
||||
cout << "Copy constructor: " << endl
|
||||
<< "Original chromosome: " << endl
|
||||
<< c1 << endl
|
||||
<< "Copy chromosome: " << endl
|
||||
<< c2 << endl;
|
||||
}
|
||||
|
||||
eoUniform<T> aleat( 1,10 );
|
||||
C c1( 3,4,aleat );
|
||||
|
||||
cout << "-----------------------------------------------------" << endl
|
||||
<< "Since now on all the operations are applied to " << endl
|
||||
<< c1
|
||||
<< "-----------------------------------------------------" << endl;
|
||||
{
|
||||
cout << "getGene(2,2): "
|
||||
<< c1.getGene(2,2) << endl;
|
||||
}
|
||||
{
|
||||
c1.setGene( 2,2,300 );
|
||||
cout << "setGene(2,2,300): " << endl
|
||||
<< c1 << endl;
|
||||
}
|
||||
{
|
||||
unsigned u1=0, u3=333, u5=555;
|
||||
vector<T> v1( 4,u1 ), v2( 4,u3 ), v3( 4,u5 );
|
||||
c1.insertRow( 0,v1 );
|
||||
c1.insertRow( 3,v2 );
|
||||
c1.insertRow( 5,v3 );
|
||||
cout << "Insert rows at positions 0, 3 and 5: " << endl
|
||||
<< c1 << endl;
|
||||
}
|
||||
{
|
||||
c1.deleteRow( 5 );
|
||||
c1.deleteRow( 3 );
|
||||
c1.deleteRow( 0 );
|
||||
cout << "Delete rows at positions 5, 3 and 0: " << endl
|
||||
<< c1 << endl;
|
||||
}
|
||||
{
|
||||
unsigned u1=0, u3=333, u6=666;
|
||||
vector<T> v1( 3,u1 ), v2( 3,u3 ), v3( 3,u6 );
|
||||
c1.insertCol( 0,v1 );
|
||||
c1.insertCol( 3,v2 );
|
||||
c1.insertCol( 6,v3 );
|
||||
cout << "Insert columns at positions 0, 3 and 6: " << endl
|
||||
<< c1 << endl;
|
||||
}
|
||||
{
|
||||
c1.deleteCol( 6 );
|
||||
c1.deleteCol( 3 );
|
||||
c1.deleteCol( 0 );
|
||||
cout << "Delete columns at positions 6, 3 and 0: " << endl
|
||||
<< c1 << endl;
|
||||
}
|
||||
{
|
||||
cout << "Number of Rows: " << endl
|
||||
<< c1.numOfRows() << endl;
|
||||
}
|
||||
{
|
||||
cout << "Number of Columns: " << endl
|
||||
<< c1.numOfCols() << endl;
|
||||
}
|
||||
|
||||
{
|
||||
cout << "Class Name: " << endl
|
||||
<< c1.className() << endl;
|
||||
}
|
||||
|
||||
|
||||
cout << "-----------------------------------------------------" << endl
|
||||
<< "Catching exceptions: " << endl
|
||||
<< c1
|
||||
<< "-----------------------------------------------------" << endl;
|
||||
{
|
||||
cout << "* Trying getGene(10,1): " << endl;
|
||||
try {
|
||||
c1.getGene( 10,1 );
|
||||
} catch (exception& e ) {
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
}
|
||||
{
|
||||
cout << "* Trying getGene(1,10): " << endl;
|
||||
try {
|
||||
c1.getGene( 1,10) ;
|
||||
} catch (exception& e ) {
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
}
|
||||
{
|
||||
cout << "* Trying setGene( 10,1,999 ): " << endl;
|
||||
try {
|
||||
c1.setGene( 10,1,999 );
|
||||
} catch (exception& e ) {
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
}
|
||||
{
|
||||
cout << "* Trying setGene( 1,10,999 ): " << endl;
|
||||
try {
|
||||
c1.setGene( 1,10,999 );
|
||||
} catch (exception& e ) {
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
}
|
||||
{
|
||||
unsigned u1=111;
|
||||
vector<T> v1( 4, u1 );
|
||||
cout << "* Trying insertRow( 10, v1 ): " << endl;
|
||||
try {
|
||||
c1.insertRow( 10,v1 );
|
||||
} catch (exception& e ) {
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
}
|
||||
{
|
||||
unsigned u1=111;
|
||||
vector<T> v1( 5, u1 );
|
||||
cout << "* Trying insertRow( 1, v1 ) with v1.size()=5: " << endl;
|
||||
try {
|
||||
c1.insertRow( 1,v1 );
|
||||
} catch (exception& e ) {
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
cout << "* Trying deleteRow( 10 ): " << endl;
|
||||
try {
|
||||
c1.deleteRow( 10 );
|
||||
} catch (exception& e ) {
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
unsigned u1=111;
|
||||
vector<T> v1( 3, u1 );
|
||||
cout << "* Trying insertCol( 10,v1 ): " << endl;
|
||||
try {
|
||||
c1.insertCol( 10,v1 );
|
||||
} catch (exception& e ) {
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
unsigned u1=111;
|
||||
vector<T> v1( 5, u1 );
|
||||
cout << "* Trying insertCol( 1,v1 ) with v1.size()=5: " << endl;
|
||||
try {
|
||||
c1.insertCol( 1,v1 );
|
||||
} catch (exception& e ) {
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
cout << "* Trying deleteCol( 10 ): " << endl;
|
||||
try {
|
||||
c1.deleteCol( 10 );
|
||||
} catch (exception& e ) {
|
||||
cerr << e.what() << endl;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
124
deprecated/eo/test/t-eoCMAES.cpp
Normal file
124
deprecated/eo/test/t-eoCMAES.cpp
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
#include <iostream>
|
||||
|
||||
#include <eoScalarFitness.h>
|
||||
#include <eoVector.h>
|
||||
#include <eoPop.h>
|
||||
#include <utils/eoParser.h>
|
||||
#include <utils/eoCheckPoint.h>
|
||||
#include <eoEvalFuncPtr.h>
|
||||
|
||||
#include <eoGenContinue.h>
|
||||
#include <eoFitContinue.h>
|
||||
#include <utils/eoStdoutMonitor.h>
|
||||
#include <utils/eoStat.h>
|
||||
#include <utils/eoTimedMonitor.h>
|
||||
|
||||
#include <eoMergeReduce.h>
|
||||
#include <eoEasyEA.h>
|
||||
|
||||
#include <es/CMAState.h>
|
||||
#include <es/CMAParams.h>
|
||||
#include <es/eoCMAInit.h>
|
||||
#include <es/eoCMABreed.h>
|
||||
|
||||
using namespace eo;
|
||||
using namespace std;
|
||||
|
||||
typedef eoMinimizingFitness FitT;
|
||||
typedef eoVector<FitT, double> EoType;
|
||||
|
||||
double sqr(double x) { return x*x; }
|
||||
|
||||
eoValueParam<int> evals(0,"Function Evals","Number of Evaluations");
|
||||
|
||||
double f_sphere(const vector<double>& values) {
|
||||
double sum = 0.0;
|
||||
for (unsigned i = 0; i < values.size(); ++i) {
|
||||
sum += values[i] * values[i];
|
||||
}
|
||||
++evals.value();
|
||||
return sum;
|
||||
}
|
||||
|
||||
double f_rosen(const vector<double>& x) {
|
||||
double sum =0.0;
|
||||
|
||||
for (unsigned i = 0; i < x.size()-1; ++i) {
|
||||
sum += 100 * sqr(sqr(x[i])-x[i+1]) + sqr(1.-x[i]);
|
||||
}
|
||||
++evals.value();
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
// make sure we have a dimensionality parameter (for testing)
|
||||
char** rargv = new char*[argc+1];
|
||||
rargv[0] = argv[0];
|
||||
rargv[1] = (char*)"-N10";
|
||||
for (int i = 2; i < argc; ++i) {
|
||||
rargv[i] = argv[i-1];
|
||||
}
|
||||
|
||||
eoParser parser(argc+1, rargv);
|
||||
|
||||
CMAParams params(parser);
|
||||
|
||||
vector<double> initial_point(params.n, 0.0);
|
||||
|
||||
CMAState state(params, initial_point);
|
||||
|
||||
if (parser.userNeedsHelp())
|
||||
{
|
||||
parser.printHelp(std::cout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
eoCMAInit<FitT> init(state);
|
||||
|
||||
eoPop<EoType> pop(params.mu, init);
|
||||
|
||||
eoEvalFuncPtr<EoType, double, const vector<double>&> eval( f_rosen );
|
||||
|
||||
eoCMABreed<FitT> breed(state, params.lambda);
|
||||
|
||||
for (unsigned i = 0; i < pop.size(); ++i) {
|
||||
eval(pop[i]);
|
||||
}
|
||||
|
||||
eoCommaReplacement<EoType> comma;
|
||||
|
||||
eoGenContinue<EoType> gen(params.maxgen);
|
||||
eoFitContinue<EoType> fit(1e-10);
|
||||
|
||||
eoCheckPoint<EoType> checkpoint(gen);
|
||||
checkpoint.add(fit);
|
||||
|
||||
eoBestFitnessStat<EoType> stat;
|
||||
|
||||
eoStdoutMonitor mon;
|
||||
mon.add(stat);
|
||||
mon.add(evals);
|
||||
|
||||
eoTimedMonitor timed(1);// 1 seconds
|
||||
timed.add(mon); // wrap it
|
||||
|
||||
checkpoint.add(timed);
|
||||
checkpoint.add(stat);
|
||||
|
||||
eoEasyEA<EoType> algo(
|
||||
checkpoint,
|
||||
eval,
|
||||
breed,
|
||||
comma);
|
||||
|
||||
|
||||
algo(pop);
|
||||
pop.sort();
|
||||
|
||||
cout << pop[0] << endl;
|
||||
cout << "Fitness achieved = " << pop[0].fitness() << endl;
|
||||
cout << "Function evaluations = " << evals.value() << endl;
|
||||
}
|
||||
173
deprecated/eo/test/t-eoCheckpointing.cpp
Normal file
173
deprecated/eo/test/t-eoCheckpointing.cpp
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
// to avoid long name warnings
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4786)
|
||||
#endif
|
||||
|
||||
#include <stdexcept> // runtime_error
|
||||
|
||||
// general
|
||||
#include <utils/eoRNG.h> // Random number generators
|
||||
#include <ga.h>
|
||||
#include <utils/eoParser.h>
|
||||
#include <utils/eoState.h>
|
||||
#include <eoGenContinue.h>
|
||||
|
||||
// include package checkpointing
|
||||
#include <utils/checkpointing>
|
||||
|
||||
struct Dummy : public EO<double>
|
||||
{
|
||||
typedef double Type;
|
||||
};
|
||||
|
||||
|
||||
struct eoDummyPop : public eoPop<Dummy>
|
||||
{
|
||||
public :
|
||||
eoDummyPop(int s = 2) { resize(s); }
|
||||
};
|
||||
|
||||
|
||||
int the_main(int argc, char **argv)
|
||||
{ // ok, we have a command line parser and a state
|
||||
|
||||
typedef eoBit<float> Chrom;
|
||||
|
||||
eoParser parser(argc, argv);
|
||||
|
||||
// Define Parameters
|
||||
eoValueParam<double> rate(0.01, "mutationRatePerBit", "Initial value for mutation rate per bit");
|
||||
eoValueParam<double> factor(0.99, "mutationFactor", "Decrease factor for mutation rate");
|
||||
eoValueParam<uint32_t> seed(time(0), "seed", "Random number seed");
|
||||
eoValueParam<std::string> load_name("", "Load","Load",'L');
|
||||
eoValueParam<std::string> save_name("", "Save","Save",'S');
|
||||
|
||||
// Register them
|
||||
parser.processParam(rate, "Genetic Operators");
|
||||
parser.processParam(factor, "Genetic Operators");
|
||||
parser.processParam(load_name, "Persistence");
|
||||
parser.processParam(save_name, "Persistence");
|
||||
parser.processParam(seed, "Rng seeding");
|
||||
|
||||
eoState state;
|
||||
state.registerObject(parser);
|
||||
|
||||
if (load_name.value() != "")
|
||||
{ // load the parser. This is only neccessary when the user wants to
|
||||
// be able to change the parameters in the state file by hand.
|
||||
state.load(load_name.value()); // load the parser
|
||||
}
|
||||
|
||||
// Create the algorithm here
|
||||
typedef Dummy EoType;
|
||||
|
||||
eoDummyPop pop;
|
||||
|
||||
eoGenContinue<EoType> genTerm(5); // run for 5 generations
|
||||
|
||||
eoCheckPoint<EoType> checkpoint(genTerm);
|
||||
// The algorithm will now quit after five generations
|
||||
|
||||
// Create a counter parameter
|
||||
eoValueParam<unsigned> generationCounter(0, "Generation");
|
||||
|
||||
// Create an incrementor (wich is an eoUpdater). Note that the
|
||||
// Parameter's value is passed by reference, so every time the incrementer increments,
|
||||
// the data in generationCounter will change.
|
||||
eoIncrementor<unsigned> increment(generationCounter.value());
|
||||
|
||||
// Add it to the checkpoint, this will result in the counter being incremented every generation
|
||||
checkpoint.add(increment);
|
||||
|
||||
// The file monitor will print parameters to a comma seperated file
|
||||
eoFileMonitor monitor("monitor.csv");
|
||||
|
||||
// the checkpoint mechanism can handle multiple monitors
|
||||
checkpoint.add(monitor);
|
||||
|
||||
// the monitor can monitor parameters such as the generationCounter
|
||||
monitor.add(generationCounter);
|
||||
|
||||
// Second moment stats: average and stdev
|
||||
eoSecondMomentStats<EoType> stats;
|
||||
|
||||
// Add it to the checkpoint to get it called at the appropriate time
|
||||
checkpoint.add(stats);
|
||||
|
||||
// Add it to the monitor to get it written to the file
|
||||
monitor.add(stats);
|
||||
|
||||
// save state every third generation
|
||||
eoCountedStateSaver stateSaver1(3, state, "generation");
|
||||
// save state every 2 seconds
|
||||
eoTimedStateSaver stateSaver2(2, state, "time");
|
||||
|
||||
// And add the two savers to the checkpoint
|
||||
checkpoint.add(stateSaver1);
|
||||
checkpoint.add(stateSaver2);
|
||||
|
||||
// Register the algorithm
|
||||
state.registerObject(rng);
|
||||
state.registerObject(pop);
|
||||
|
||||
if (parser.userNeedsHelp())
|
||||
{
|
||||
parser.printHelp(std::cout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Either load or initialize
|
||||
if (load_name.value() != "")
|
||||
{
|
||||
state.load(load_name.value()); // load the rest
|
||||
}
|
||||
else
|
||||
{
|
||||
// else
|
||||
|
||||
// initialize rng and population
|
||||
|
||||
rng.reseed(seed.value());
|
||||
|
||||
pop.resize(2);
|
||||
|
||||
pop[0].fitness(1);
|
||||
pop[1].fitness(2);
|
||||
}
|
||||
|
||||
while(checkpoint(pop))
|
||||
{
|
||||
pop[0].fitness(pop[0].fitness() + 1);
|
||||
|
||||
time_t now = time(0);
|
||||
|
||||
while (time(0) == now) {} // wait a second to test timed saver
|
||||
|
||||
std::cout << "gen " << generationCounter.value() << std::endl;
|
||||
}
|
||||
|
||||
// run the algorithm
|
||||
|
||||
// Save when needed
|
||||
if (save_name.value() != "")
|
||||
{
|
||||
std::string file_name = save_name.value();
|
||||
save_name.value() = ""; // so that it does not appear in the parser section of the state file
|
||||
state.save(file_name);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
the_main(argc, argv);
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cout << "Exception: " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
88
deprecated/eo/test/t-eoDualFitness.cpp
Normal file
88
deprecated/eo/test/t-eoDualFitness.cpp
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
#include <utility>
|
||||
|
||||
#include <eo>
|
||||
#include <es.h>
|
||||
#include <utils/eoStat.h>
|
||||
|
||||
typedef eoVector<eoDualFitness<double,eoMinimizingFitness>,double> DualVector;
|
||||
|
||||
template<class EOT>
|
||||
class DualSphere : public eoEvalFunc<EOT>
|
||||
{
|
||||
public:
|
||||
virtual void operator()( EOT & x )
|
||||
{
|
||||
if( x.invalid() ) { return; }
|
||||
|
||||
double sum = 0;
|
||||
int sign = 1;
|
||||
for( unsigned int i=0, s=x.size(); i<s; ++i ) {
|
||||
sum += x[i] * x[i];
|
||||
sign *= x[i]<0 ? -1 : 1;
|
||||
}
|
||||
|
||||
x.fitness( std::make_pair( sum, sign>0 ? true : false ) );
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
double test( eoPop<DualVector>& pop, double target_value )
|
||||
{
|
||||
DualSphere<DualVector> eval;
|
||||
|
||||
eoPopLoopEval<DualVector> pop_eval(eval);
|
||||
|
||||
pop_eval(pop,pop);
|
||||
|
||||
eoInterquartileRangeStat<DualVector> iqr_stat( std::make_pair(0.0,false), "IQR" );
|
||||
|
||||
iqr_stat( pop );
|
||||
|
||||
std::cout << iqr_stat.longName() << "=" << iqr_stat.value() << " should be " << target_value << std::endl;
|
||||
|
||||
return iqr_stat.value().value();
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
eoPop<DualVector> pop;
|
||||
|
||||
// fixed test
|
||||
DualVector sol1(2,-1);
|
||||
DualVector sol2(2,-1);
|
||||
DualVector sol3(2,1);
|
||||
DualVector sol4(2,1);
|
||||
pop.push_back( sol1 );
|
||||
pop.push_back( sol2 );
|
||||
pop.push_back( sol3 );
|
||||
pop.push_back( sol4 );
|
||||
// on the sphere function everyone has the same fitness of 1
|
||||
if( test(pop, 0) != 0 ) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
pop.erase(pop.begin(),pop.end());
|
||||
|
||||
// fixed test
|
||||
sol1 = DualVector(2,0);
|
||||
sol2 = DualVector(2,0);
|
||||
sol3 = DualVector(2,1);
|
||||
sol4 = DualVector(2,1);
|
||||
pop.push_back( sol1 );
|
||||
pop.push_back( sol2 );
|
||||
pop.push_back( sol3 );
|
||||
pop.push_back( sol4 );
|
||||
if( test(pop, 1) != 1 ) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// test on a random normal distribution
|
||||
eoNormalGenerator<double> normal(1,rng);
|
||||
eoInitFixedLength<DualVector> init_N(2, normal);
|
||||
pop = eoPop<DualVector>( 1000000, init_N );
|
||||
double iqr = test(pop, 1.09);
|
||||
if( iqr < 1.08 || iqr > 1.11 ) {
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
150
deprecated/eo/test/t-eoESAll.cpp
Normal file
150
deprecated/eo/test/t-eoESAll.cpp
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
// Program to test several EO-ES features
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4786)
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <stdexcept>
|
||||
#include <ctime>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <crtdbg.h>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <eo>
|
||||
|
||||
// representation specific
|
||||
#include <es/make_es.h>
|
||||
|
||||
#include "real_value.h" // the sphere fitness
|
||||
|
||||
// Now the main
|
||||
///////////////
|
||||
typedef eoMinimizingFitness FitT;
|
||||
|
||||
template <class EOT>
|
||||
void runAlgorithm(EOT, eoParser& _parser, eoState& _state);
|
||||
|
||||
int main_function(int argc, char *argv[])
|
||||
{
|
||||
// Create the command-line parser
|
||||
eoParser parser(argc, argv); // for user-parameter reading
|
||||
eoState state; // keeps all things allocated
|
||||
eoValueParam<bool>& simpleParam = parser.getORcreateParam(true, "Isotropic",
|
||||
"Isotropic self-adaptive mutation",
|
||||
'i', "ES mutation");
|
||||
eoValueParam<bool>& stdevsParam = parser.getORcreateParam(false, "Stdev",
|
||||
"One self-adaptive stDev per variable",
|
||||
's', "ES mutation");
|
||||
eoValueParam<bool>& corrParam = parser.getORcreateParam(false, "Correl",
|
||||
"Use correlated mutations",
|
||||
'c', "ES mutation");
|
||||
// Run the appropriate algorithm
|
||||
if (simpleParam.value() == false)
|
||||
{
|
||||
std::cout << "Using eoReal" << std::endl;
|
||||
runAlgorithm(eoReal<FitT>(), parser, state);
|
||||
}
|
||||
else if (stdevsParam.value() == false)
|
||||
{
|
||||
std::cout << "Using eoEsSimple" << std::endl;
|
||||
runAlgorithm(eoEsSimple<FitT>(), parser, state);
|
||||
}
|
||||
else if (corrParam.value() == false)
|
||||
{
|
||||
std::cout << "Using eoEsStdev" << std::endl;
|
||||
runAlgorithm(eoEsStdev<FitT>(), parser, state);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Using eoEsFull" << std::endl;
|
||||
runAlgorithm(eoEsFull<FitT>(), parser, state);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// A main that catches the exceptions
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
// rng.reseed(42);
|
||||
int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
|
||||
flag |= _CRTDBG_LEAK_CHECK_DF;
|
||||
_CrtSetDbgFlag(flag);
|
||||
// _CrtSetBreakAlloc(100);
|
||||
#endif
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** The templatized main (sort of)
|
||||
|
||||
quite similar to the main of other genotypes (e.g. t-eoReal and t-eoGA
|
||||
in test dir)
|
||||
*/
|
||||
template <class EOT>
|
||||
void runAlgorithm(EOT, eoParser& _parser, eoState& _state)
|
||||
{
|
||||
typedef typename EOT::Fitness FitT;
|
||||
|
||||
///// 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
|
||||
eoRealInitBounded<EOT>& init = make_genotype(_parser, _state, EOT());
|
||||
|
||||
// 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<EOT>(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
|
||||
///////
|
||||
std::cout << "Initial Population\n";
|
||||
pop.sortedPrintOn(std::cout);
|
||||
std::cout << std::endl;
|
||||
|
||||
run_ea(ga, pop); // run the ga
|
||||
|
||||
std::cout << "Final Population\n";
|
||||
pop.sortedPrintOn(std::cout);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
180
deprecated/eo/test/t-eoESFull.cpp
Normal file
180
deprecated/eo/test/t-eoESFull.cpp
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
// Program to test several EO-ES features
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4786)
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <stdexcept>
|
||||
#include <time.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <eo>
|
||||
|
||||
// representation specific
|
||||
#include <es.h>
|
||||
|
||||
#include "real_value.h" // the sphere fitness
|
||||
|
||||
// Now the main
|
||||
///////////////
|
||||
typedef eoMinimizingFitness FitT;
|
||||
|
||||
template <class EOT>
|
||||
void runAlgorithm(EOT, eoParser& _parser, eoState& _state, eoRealVectorBounds& _bounds, eoValueParam<string> _load_name);
|
||||
|
||||
int main_function(int argc, char *argv[])
|
||||
{
|
||||
// Create the command-line parser
|
||||
eoParser parser( argc, argv, "Basic EA for vector<float> with adaptive mutations");
|
||||
|
||||
// Define Parameters and load them
|
||||
eoValueParam<uint32_t>& seed = parser.createParam(static_cast<uint32_t>(time(0)),
|
||||
"seed", "Random number seed");
|
||||
eoValueParam<string>& load_name = parser.createParam(string(), "Load","Load a state file",'L');
|
||||
eoValueParam<string>& save_name = parser.createParam(string(), "Save","Saves a state file",'S');
|
||||
eoValueParam<bool>& stdevs = parser.createParam(false, "Stdev", "Use adaptive mutation rates", 's');
|
||||
eoValueParam<bool>& corr = parser.createParam(false, "Correl", "Use correlated mutations", 'c');
|
||||
eoValueParam<unsigned>& chromSize = parser.createParam(unsigned(50), "ChromSize", "Number of chromosomes", 'n');
|
||||
eoValueParam<double>& minimum = parser.createParam(-1.0, "Min", "Minimum for Objective Variables", 'l');
|
||||
eoValueParam<double>& maximum = parser.createParam(1.0, "Max", "Maximum for Objective Variables", 'h');
|
||||
|
||||
eoState state;
|
||||
state.registerObject(parser);
|
||||
rng.reseed(seed.value());
|
||||
|
||||
if (!load_name.value().empty())
|
||||
{ // load the parser. This is only neccessary when the user wants to
|
||||
// be able to change the parameters in the state file by hand
|
||||
// Note that only parameters inserted in the parser at this point
|
||||
// will be loaded!.
|
||||
state.load(load_name.value()); // load the parser
|
||||
}
|
||||
|
||||
state.registerObject(rng);
|
||||
|
||||
eoRealVectorBounds bounds(chromSize.value(), minimum.value(), maximum.value());
|
||||
|
||||
// Run the appropriate algorithm
|
||||
if (stdevs.value() == false && corr.value() == false)
|
||||
{
|
||||
runAlgorithm(eoEsSimple<FitT>() ,parser, state, bounds, load_name);
|
||||
}
|
||||
else if (corr.value() == true)
|
||||
{
|
||||
runAlgorithm(eoEsFull<FitT>(),parser, state, bounds, load_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
runAlgorithm(eoEsStdev<FitT>(), parser, state, bounds, load_name);
|
||||
}
|
||||
|
||||
// and save
|
||||
if (!save_name.value().empty())
|
||||
{
|
||||
string file_name = save_name.value();
|
||||
save_name.value() = ""; // so that it does not appear in the parser section of the state file
|
||||
state.save(file_name);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
// rng.reseed(42);
|
||||
int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
|
||||
flag |= _CRTDBG_LEAK_CHECK_DF;
|
||||
_CrtSetDbgFlag(flag);
|
||||
// _CrtSetBreakAlloc(100);
|
||||
#endif
|
||||
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
std::cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
template <class EOT>
|
||||
void runAlgorithm(EOT, eoParser& _parser, eoState& _state, eoRealVectorBounds& _bounds, eoValueParam<string> _load_name)
|
||||
{
|
||||
// evaluation
|
||||
eoEvalFuncPtr<EOT, double, const vector<double>&> eval( real_value );
|
||||
|
||||
// population parameters, unfortunately these can not be altered in the state file
|
||||
eoValueParam<unsigned> mu = _parser.createParam(unsigned(7), "mu","Size of the population");
|
||||
eoValueParam<double>lambda_rate = _parser.createParam(double(7.0), "lambda_rate", "Factor of children to produce");
|
||||
|
||||
if (lambda_rate.value() < 1.0f)
|
||||
{
|
||||
throw logic_error("lambda_rate must be larger than 1 in a comma strategy");
|
||||
}
|
||||
|
||||
// Initialization
|
||||
eoEsChromInit<EOT> init(_bounds);
|
||||
|
||||
// State takes ownership of pop because it needs to save it in caller
|
||||
eoPop<EOT>& pop = _state.takeOwnership(eoPop<EOT>(mu.value(), init));
|
||||
|
||||
_state.registerObject(pop);
|
||||
|
||||
if (!_load_name.value().empty())
|
||||
{ // The real loading happens here when all objects are registered
|
||||
_state.load(_load_name.value()); // load all and everything
|
||||
}
|
||||
else
|
||||
{
|
||||
// evaluate initial population
|
||||
apply<EOT>(eval, pop);
|
||||
}
|
||||
|
||||
// Ok, time to set up the algorithm
|
||||
// Proxy for the mutation parameters
|
||||
eoEsMutationInit mutateInit(_parser);
|
||||
|
||||
eoEsMutate<EOT> mutate(mutateInit, _bounds);
|
||||
|
||||
// monitoring, statistics etc.
|
||||
eoAverageStat<EOT> average;
|
||||
eoStdoutMonitor monitor;
|
||||
|
||||
monitor.add(average);
|
||||
|
||||
eoGenContinue<EOT> cnt(100);
|
||||
eoCheckPoint<EOT> checkpoint(cnt);
|
||||
checkpoint.add(monitor);
|
||||
checkpoint.add(average);
|
||||
|
||||
// only mutation (== with rate 1.0)
|
||||
eoMonGenOp<EOT> op(mutate);
|
||||
|
||||
// the selection: sequential selection
|
||||
eoSequentialSelect<EOT> select;
|
||||
// the general breeder (lambda is a rate -> true)
|
||||
eoGeneralBreeder<EOT> breed(select, op, lambda_rate.value(), true);
|
||||
|
||||
// the replacement - hard-coded Comma replacement
|
||||
eoCommaReplacement<EOT> replace;
|
||||
|
||||
// now the eoEasyEA
|
||||
eoEasyEA<EOT> es(checkpoint, eval, breed, replace);
|
||||
|
||||
es(pop);
|
||||
|
||||
pop.sort();
|
||||
std::cout << "Final population\n" << pop << std::endl;
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue