included new fitness class eoScalarFitnessAssembled, that stores different fitness terms in a std::vector, but still acts as a scalar fitness. A new checkpoint uses these values for statistics.

This commit is contained in:
okoenig 2003-04-02 21:10:53 +00:00
commit b3e57bedad
8 changed files with 837 additions and 23 deletions

View file

@ -4,6 +4,8 @@
##
###############################################################################
CLEANFILES = *~ *.sav *.status
DEPS = $(top_builddir)/src/utils/libeoutils.a $(top_builddir)/src/libeo.a
###############################################################################
@ -15,7 +17,7 @@ CXXFLAGS = -g -Wall
# PLEASE don't break the line (see create_batch.sh)
check_PROGRAMS = t-eoParetoFitness t-eoPareto 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
check_PROGRAMS = t-eoParetoFitness t-eoPareto 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
#The run_tests script can be used to check various arguments
TESTS=$(check_PROGRAMS) run_tests
@ -175,3 +177,17 @@ t_eoPBIL_LDFLAGS = -lm
t_eoPBIL_LDADD = $(top_builddir)/src/ga/libga.a $(LDADDS)
###############################################################################
t_eoFitnessAssembled_SOURCES = t-eoFitnessAssembled.cpp
t_eoFitnessAssembled_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a
t_eoFitnessAssembled_LDFLAGS = -lm
t_eoFitnessAssembled_LDADD = $(top_builddir)/src/ga/libga.a $(LDADDS)
###############################################################################
t_eoFitnessAssembledEA_SOURCES = t-eoFitnessAssembledEA.cpp
t_eoFitnessAssembledEA_DEPENDENCIES = $(DEPS) $(top_builddir)/src/es/libes.a
t_eoFitnessAssembledEA_LDFLAGS = -lm
t_eoFitnessAssembledEA_LDADD = $(top_builddir)/src/es/libes.a $(LDADDS)
###############################################################################

View file

@ -0,0 +1,105 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// t-eoFitnessAssembled.cpp
// Marc Wintermantel & Oliver Koenig
// IMES-ST@ETHZ.CH
// March 2003
/*
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@inria.fr
mak@dhi.dk
*/
//-----------------------------------------------------------------------------
#include <iostream>
#include <stdexcept>
#include "eoScalarFitnessAssembled.h"
void test_eoScalarFitnessAssembledClass(){
// Create instances
eoAssembledMinimizingFitness A,B,C(5, 1.3, "C value");
// Add some values to them
A.push_back( 5.6, "first value" );
A.push_back( 3.2, "second value" );
A.push_back( 2.6, "third value" );
B.push_back( 1.2 );
B.push_back( 3.2 );
B.push_back( 5.2 );
B.setDescription( 1, "B descr" );
std::cout << "Created instances A,B and C, added some vals; testing << operator " << std::endl;
std::cout << "A= " << A << std::endl;
std::cout << "B= " << B << std::endl;
std::cout << "C= " << C << std::endl;
std::cout << "Printing values and descriptions: " << std::endl;
std::cout << "A: "; A.printAll( std::cout ); std::cout << std::endl;
std::cout << "B: "; B.printAll( std::cout ); std::cout << std::endl;
std::cout << "C: "; C.printAll( std::cout ); std::cout << std::endl;
A.resize(8, 100.3, "A resized");
std::cout << "Resized A: "; A.printAll( std::cout ); std::cout << std::endl;
std::cout << "Access fitness values of A and B: " << "f(A)= " << (double) A << " f(B)= " << (double) B << std::endl;
// Testing constructors and assignments
eoAssembledMinimizingFitness D(A) ,E(3.2);
std::cout << "D(A) = " << D << "\t" << "E(3.2)= " << E << std::endl;
eoAssembledMinimizingFitness F,G;
F=A;
G= 7.5;
std::cout << "F = A : " << F << "\t G = 7.5 : " << G << std::endl;
// Comparing...
std::cout << "A<B: " << (A<B) << std::endl;
std::cout << "A>B: " << (A>B) << std::endl;
std::cout << "A<=B: " << (A<=B) << std::endl;
std::cout << "A>=B: " << (A>=B) << std::endl;
}
int main(){
std::cout << "-----------------------------------" << std::endl;
std::cout << "START t-eoFitnessAssembled" << std::endl;
try{
// Test the fitness class itself
test_eoScalarFitnessAssembledClass();
}
catch(std::exception& e){
std::cout << e.what() << std::endl;
return 1;
}
std::cout << "END t-eoFitnessAssembled" << std::endl;
std::cout << "----------------------------------" << std::endl;
return 0;
}

View file

@ -0,0 +1,170 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// t-eoFitnessAssembledEA.cpp
// Marc Wintermantel & Oliver Koenig
// IMES-ST@ETHZ.CH
// March 2003
/*
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@inria.fr
mak@dhi.dk
*/
//-----------------------------------------------------------------------------
#include <iostream>
#include <cmath>
// General eo includes
#include <eo>
#include <utils/eoRealVectorBounds.h> // The real bounds (not yet in general eo include)
// Representation dependent includes and typedefs
#include <es/eoReal.h> // Definition of representation
#include <es/eoRealInitBounded.h> // Uniformly initializes real vector in bounds
#include <es/make_genotype_real.h> // Initialization of a genotype
#include <eoEvalFunc.h> // Base class for fitness evaluation
#include <es/make_op_real.h> // Variation operators using standard Real operators
#include <eoScalarFitnessAssembled.h> // The fitness class
typedef eoReal<eoAssembledMinimizingFitness> Indi;
// Representation independent modules
#include <do/make_pop.h> // Initialization of population
#include <do/make_continue.h> // The stopping criterion
#include <do/make_checkpoint_assembled.h> // Outputs (stats, population dumps, ...)
#include <do/make_algo_scalar.h> // Evolution engine (selection and replacement)
#include <do/make_run.h> // simple call to the algo.stays there for consistency reasons
// Define a fitness class
template <class EOT>
class eoAssembledEvalFunc : public eoEvalFunc<EOT>{
public:
// Constructor defining number and descriptions of fitness terms
eoAssembledEvalFunc() {
// Define a temporary fitness object to have access to its static traits
typename EOT::Fitness tmpfit(3, 0.0);
tmpfit.setDescription(0,"Fitness");
tmpfit.setDescription(1,"Some Value");
tmpfit.setDescription(2,"Other Value");
}
void operator()(EOT& _eo){
// Define temporary fitness object
// (automatically gets initialized with size given in constructor)
typename EOT::Fitness tmpfit;
// Eval some dummy fitness
double sum1=0.0, sum2=0.0;
for (unsigned i=0; i < _eo.size(); ++i){
sum1 += _eo[i]*_eo[i];
sum2 += fabs(_eo[i]) + fabs(_eo[i]);
}
// Store some fitness terms
tmpfit[1]= sum1;
tmpfit[2]= sum2;
// Store the fitness
tmpfit = (sum1 + sum2)/_eo.size();
// Pass it
_eo.fitness( tmpfit );
}
};
// checks for help demand, and writes the status file and make_help; in libutils
void make_help(eoParser & _parser);
// now use all of the above, + representation dependent things
int main(int argc, char* argv[]){
std::cout << "-----------------------------------" << std::endl;
std::cout << "START t-eoFitnessAssembledEA" << std::endl;
try{
// Parser & State
eoParser parser(argc, argv); // for user-parameter reading
eoState state; // keeps all things allocated
////
// A) Representation dependent stuff
////
// The fitness
eoAssembledEvalFunc<Indi> plainEval;
// turn that object into an evaluation counter
eoEvalFuncCounter<Indi> eval(plainEval);
// The genotype
eoRealInitBounded<Indi>& init = do_make_genotype(parser, state, Indi() );
// The variation operators
eoGenOp<Indi>& op = do_make_op(parser, state, init);
////
// B) Create representation independent stuff
////
// initialize the population
// yes, this is representation indepedent once you have an eoInit
eoPop<Indi>& pop = do_make_pop(parser, state, init);
// stopping criteria
eoContinue<Indi> & term = do_make_continue(parser, state, eval);
// output
eoCheckPoint<Indi> & checkpoint = do_make_checkpoint_assembled(parser, state, eval, term);
// algorithm (need the operator!)
eoAlgo<Indi>& ga = do_make_algo_scalar(parser, state, eval, checkpoint, op);
make_help(parser); // To be called after all parameters have been read !
////
// C) Run the algorithm
////
// evaluate intial population AFTER help and status in case it takes time
apply<Indi>(eval, pop);
// if you want to print it out
std::cout << "Initial Population\n";
pop.sortedPrintOn(std::cout);
std::cout << std::endl;
do_run(ga, pop); // run the ga
std::cout << "Final Population\n";
pop.sortedPrintOn(std::cout);
std::cout << std::endl;
}
catch(std::exception& e)
{
std::cout << e.what() << std::endl;
return 1;
}
std::cout << "-----------------------------------" << std::endl;
std::cout << "END t-eoFitnessAssembledEA" << std::endl;
return 0;
}