Tester for CMA
This commit is contained in:
parent
8b03a5232a
commit
0eee2ff13d
2 changed files with 125 additions and 2 deletions
|
|
@ -8,6 +8,7 @@ LIBEO = $(top_builddir)/src/libeo.a
|
|||
LIBEOUTILS = $(top_builddir)/src/utils/libeoutils.a
|
||||
LIBES = $(top_builddir)/src/es/libes.a
|
||||
LIBGA = $(top_builddir)/src/ga/libga.a
|
||||
LIBCMA = $(top_builddir)/src/es/libcma.a
|
||||
|
||||
# PLEASE don't break the line (see create_batch.sh)
|
||||
check_PROGRAMS = t-eoParetoFitness \
|
||||
|
|
@ -36,7 +37,8 @@ check_PROGRAMS = t-eoParetoFitness \
|
|||
t-eoFitnessAssembled \
|
||||
t-eoFitnessAssembledEA \
|
||||
t-eoRoulette \
|
||||
t-eoSharing
|
||||
t-eoSharing \
|
||||
t-eoCMAES
|
||||
|
||||
|
||||
TESTS = $(check_PROGRAMS) \
|
||||
|
|
@ -46,7 +48,7 @@ noinst_HEADERS = binary_value.h real_value.h RoyalRoad.h
|
|||
|
||||
|
||||
AM_CXXFLAGS = -I$(top_srcdir)/src
|
||||
DEPS = $(LIBGA) $(LIBES) $(LIBEOUTILS) $(LIBEO)
|
||||
DEPS = $(LIBGA) $(LIBES) $(LIBCMA) $(LIBEOUTILS) $(LIBEO)
|
||||
LIBS = $(DEPS)
|
||||
|
||||
CLEANFILES = monitor.csv t-eoRandom.out
|
||||
|
|
@ -88,3 +90,4 @@ t_eoPBIL_SOURCES = t-eoPBIL.cpp
|
|||
t_eoFitnessAssembled_SOURCES = t-eoFitnessAssembled.cpp
|
||||
t_eoRoulette_SOURCES = t-eoRoulette.cpp
|
||||
t_eoSharing_SOURCES = t-eoSharing.cpp
|
||||
t_eoCMAES_SOURCES = t-eoCMAES.cpp
|
||||
|
|
|
|||
120
eo/test/t-eoCMAES.cpp
Normal file
120
eo/test/t-eoCMAES.cpp
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
|
||||
#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 (int 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[]) {
|
||||
|
||||
eoParser parser(argc, argv);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in a new issue