Début du dev de newMo

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1639 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
jhumeau 2010-01-15 10:45:55 +00:00
commit e13fd250d1
34 changed files with 1946 additions and 48 deletions

View file

@ -0,0 +1 @@
ADD_SUBDIRECTORY(oneMax)

View file

@ -0,0 +1 @@
ADD_SUBDIRECTORY(application)

View file

@ -0,0 +1,11 @@
INCLUDE_DIRECTORIES(${PARADISEO_EO_SRC_DIR}/src
${NEWMO_SRC_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/../src)
LINK_DIRECTORIES(${PARADISEO_EO_BIN_DIR}/lib)
ADD_EXECUTABLE(testSimpleHC testSimpleHC.cpp)
ADD_EXECUTABLE(testWithMove testWithMove.cpp)
TARGET_LINK_LIBRARIES(testSimpleHC eoutils ga eo)
TARGET_LINK_LIBRARIES(testWithMove eoutils ga eo)

View file

@ -0,0 +1,203 @@
//-----------------------------------------------------------------------------
/** testSimpleHC.cpp
*
* SV - 12/01/10
*
*/
//-----------------------------------------------------------------------------
// standard includes
#define HAVE_SSTREAM
#include <stdexcept> // runtime_error
#include <iostream> // cout
#include <sstream> // ostrstream, istrstream
#include <fstream>
#include <string.h>
// the general include for eo
#include <eo>
#include <ga.h>
using namespace std;
//-----------------------------------------------------------------------------
// fitness function
#include <funcOneMax.h>
#include <eoInt.h>
#include <neighborhood/moEmptyNeighbor.h>
#include <continuator/moTrueContinuator.h>
// local search algorithm
#include <algo/moLocalSearch.h>
// the simple HC explorer
#include <explorer/moSimpleHCexplorer.h>
// explore the neighborhood of a bit string in order
#include <neighborhood/moBitNeighborhood.h>
#include <neighborhood/moFullEvalBitNeighbor.h>
#include <oneMaxBitNeighbor.h>
// REPRESENTATION
//-----------------------------------------------------------------------------
// define your individuals
typedef eoBit<unsigned> Indi;
//typedef OneMaxBitNeighbor<unsigned> Neighbor ; // incremental evaluation
typedef moFullEvalBitNeighbor<unsigned> Neighbor ; // full evaluation
typedef moBitNeighborhood<Neighbor> Neighborhood ;
// GENERAL
//-----------------------------------------------------------------------------
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters
*
* ========================================================= */
// First define a parser from the command-line arguments
eoParser parser(argc, argv);
// For each parameter, define Parameter, read it through the parser,
// and assign the value to the variable
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// description of genotype
eoValueParam<unsigned int> vecSizeParam(8, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
string fileOut("out.dat");
eoValueParam<string> fileStatParam(fileOut.c_str(), "out", "A file to export results", 'o');
parser.processParam( fileStatParam, "Persistence" );
fileOut = fileStatParam.value();
// the name of the "status" file where all actual parameter values will be saved
string str_status = parser.ProgramName() + ".status"; // default value
eoValueParam<string> statusParam(str_status.c_str(), "status", "Status file");
parser.processParam( statusParam, "Persistence" );
// do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED
// i.e. in case you need parameters somewhere else, postpone these
if (parser.userNeedsHelp())
{
parser.printHelp(cout);
exit(1);
}
if (statusParam.value() != "")
{
ofstream os(statusParam.value().c_str());
os << parser; // and you can use that file as parameter file
}
/* =========================================================
*
* Random seed
*
* ========================================================= */
//reproducible random seed: if you don't change SEED above,
// you'll aways get the same result, NOT a random run
rng.reseed(seed);
/* =========================================================
*
* Eval fitness function
*
* ========================================================= */
FuncOneMax<Indi> eval(vecSize);
/* =========================================================
*
* Initilisation of the solution
*
* ========================================================= */
// a Indi random initializer
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// no need if incremental evaluation with OneMaxBitNeighbor
Neighbor::setFullEvalFunc(eval);
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
Neighborhood neighborhood;
/* =========================================================
*
* a neighborhood explorator solution
*
* ========================================================= */
moSimpleHCexplorer<Neighborhood> explorer(neighborhood);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moTrueContinuator<Neighborhood> continuator;
moLocalSearch< moSimpleHCexplorer<Neighborhood>, moTrueContinuator<Neighborhood> > localSearch(explorer, continuator);
/* =========================================================
*
* execute the local search from random sollution
*
* ========================================================= */
Indi solution;
random(solution);
eval(solution);
std::cout << "initial: " << solution << std::endl ;
localSearch(solution);
std::cout << "final: " << solution << std::endl ;
}
// A main that catches the exceptions
int main(int argc, char **argv)
{
try
{
main_function(argc, argv);
}
catch(exception& e)
{
cout << "Exception: " << e.what() << '\n';
}
return 1;
}

View file

@ -0,0 +1,214 @@
//-----------------------------------------------------------------------------
/** testSimpleHC.cpp
*
* SV - 12/01/10
*
*/
//-----------------------------------------------------------------------------
// standard includes
#define HAVE_SSTREAM
#include <stdexcept> // runtime_error
#include <iostream> // cout
#include <sstream> // ostrstream, istrstream
#include <fstream>
#include <string.h>
// the general include for eo
#include <eo>
#include "ga.h"
using namespace std;
//-----------------------------------------------------------------------------
// fitness function
#include <funcOneMax.h>
//#include <eoInt.h>
//#include <moEmptyNeighbor.h>
#include <continuator/moTrueContinuator.h>
// local search algorithm
#include <algo/moLocalSearch.h>
// the simple HC explorer
#include <explorer/moSimpleHCexplorer.h>
// explore the neighborhood of a bit string in order
#include <neighborhood/moMoveNeighborhood.h>
//#include <moFullEvalBitNeighbor.h>
#include <neighborhood/moMoveNeighbor.h>
#include <bitMove.h>
#include <bitMove_init.h>
#include <bitMoveIncrEval.h>
#include <bitMove_next.h>
// REPRESENTATION
//-----------------------------------------------------------------------------
// define your individuals
typedef eoBit<unsigned> Indi;
//typedef OneMaxBitNeighbor<unsigned> Neighbor ; // incremental evaluation
typedef moMoveNeighbor<BitMove<Indi>, unsigned> Neighbor ; // full evaluation
typedef moMoveNeighborhood<BitMove<Indi>, unsigned> Neighborhood ;
// GENERAL
//-----------------------------------------------------------------------------
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters
*
* ========================================================= */
// First define a parser from the command-line arguments
eoParser parser(argc, argv);
// For each parameter, define Parameter, read it through the parser,
// and assign the value to the variable
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// description of genotype
eoValueParam<unsigned int> vecSizeParam(8, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
string fileOut("out.dat");
eoValueParam<string> fileStatParam(fileOut.c_str(), "out", "A file to export results", 'o');
parser.processParam( fileStatParam, "Persistence" );
fileOut = fileStatParam.value();
// the name of the "status" file where all actual parameter values will be saved
string str_status = parser.ProgramName() + ".status"; // default value
eoValueParam<string> statusParam(str_status.c_str(), "status", "Status file");
parser.processParam( statusParam, "Persistence" );
// do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED
// i.e. in case you need parameters somewhere else, postpone these
if (parser.userNeedsHelp())
{
parser.printHelp(cout);
exit(1);
}
if (statusParam.value() != "")
{
ofstream os(statusParam.value().c_str());
os << parser; // and you can use that file as parameter file
}
/* =========================================================
*
* Random seed
*
* ========================================================= */
//reproducible random seed: if you don't change SEED above,
// you'll aways get the same result, NOT a random run
rng.reseed(seed);
/* =========================================================
*
* Eval fitness function
*
* ========================================================= */
FuncOneMax<Indi> eval(vecSize);
OneMaxIncrEval<Indi> incrEval;
Neighbor::setIncrEval(incrEval);
/* =========================================================
*
* Initilisation of the solution
*
* ========================================================= */
// a Indi random initializer
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// no need if incremental evaluation with OneMaxBitNeighbor
// Neighbor::setFullEvalFunc(eval);
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
BitMove_init<Indi> init;
BitMove_next<Indi> next;
Neighborhood neighborhood(init, next);
/* =========================================================
*
* a neighborhood explorator solution
*
* ========================================================= */
moSimpleHCexplorer<Neighborhood> explorer(neighborhood);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moTrueContinuator<Neighborhood> continuator;
moLocalSearch< moSimpleHCexplorer<Neighborhood>, moTrueContinuator<Neighborhood> > localSearch(explorer, continuator);
/* =========================================================
*
* execute the local search from random sollution
*
* ========================================================= */
Indi solution;
random(solution);
eval(solution);
std::cout << "initial: " << solution << std::endl ;
localSearch(solution);
std::cout << "final: " << solution << std::endl ;
}
// A main that catches the exceptions
int main(int argc, char **argv)
{
try
{
main_function(argc, argv);
}
catch(exception& e)
{
cout << "Exception: " << e.what() << '\n';
}
return 1;
}

View file

@ -0,0 +1,34 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "bitMove.h"
#ifndef __bitMove
#define __bitMove
#include <utility>
#include <move/moMove.h>
template <class EOT>
class BitMove : public moMove <EOT> {
public :
typedef EOT EOType;
unsigned bit;
BitMove() {
bit = 0;
}
BitMove(unsigned _bit) : bit(_bit) { }
void operator () (EOT & chrom)
{
chrom[bit] = !chrom[bit];
};
} ;
#endif

View file

@ -0,0 +1,27 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "incrEval_funcNK.h"
#ifndef __incr_eval_funcNK_h
#define __incr_eval_funcNK_h
#include <move/moMoveIncrEval.h>
#include "bitMove.h"
template <class EOT>
class OneMaxIncrEval : public moMoveIncrEval < BitMove<EOT> > {
public :
OneMaxIncrEval(){ };
typename EOT::Fitness operator () (const BitMove<EOT> & move, const EOT & chrom) {
if(chrom[move.bit]==0){
return chrom.fitness()+1;
}
else{
return chrom.fitness()-1;
}
};
};
#endif

View file

@ -0,0 +1,21 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "bitMove_init.h"
#ifndef __bitMove_init
#define __bitMove_init
#include <move/moMoveInit.h>
#include "bitMove.h"
template <class EOT>
class BitMove_init : public moMoveInit < BitMove<EOT> > {
public :
void operator () (BitMove<EOT> & __move, const EOT & genome) {
__move.bit = 0 ;
};
} ;
#endif

View file

@ -0,0 +1,33 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "bitMove_next.h"
#ifndef __bitMove_next_h
#define __bitMove_next_h
#include <move/moNextMove.h>
#include "bitMove.h"
template <class EOT>
class BitMove_next : public moNextMove < BitMove<EOT> > {
public:
BitMove_next()
{
};
bool operator () (BitMove<EOT> & __move, const EOT & genome) {
if (__move.bit >= (genome.size() - 1)){
return false ;
}
else {
__move.bit++;
return true ;
}
};
} ;
#endif

View file

@ -0,0 +1,33 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// funcOneMax.h
// 25/11/2009 : copy from FuncU.h
//-----------------------------------------------------------------------------
#ifndef __FuncOneMax
#define __FuncOneMax
template< class EOT >
class FuncOneMax : public eoEvalFunc<EOT>
{
private:
unsigned N;
public:
FuncOneMax(unsigned n) : N(n) {};
~FuncOneMax(void) {} ;
void operator() (EOT & genome) {
unsigned sum = 0;
for (int i = 0; i < N; i++)
sum += genome[i];
genome.fitness(sum);
}
};
#endif

View file

@ -0,0 +1,38 @@
#ifndef _oneMaxBitNeighbor_h
#define _oneMaxBitNeighbor_h
#include <neighborhood/moBitNeighbor.h>
#include <ga.h>
/*
contener of the neighbor information
*/
template< class Fitness >
class OneMaxBitNeighbor : public moBitNeighbor<Fitness>
{
public:
typedef eoBit<Fitness> EOType ;
using moBitNeighbor<Fitness>::bit ;
/*
* incremental evaluation of the solution for the oneMax problem
*/
virtual void eval(EOType & solution) {
if (solution[bit] == 0)
fitness(solution.fitness() + 1);
else
fitness(solution.fitness() - 1);
};
};
#endif
// Local Variables:
// coding: iso-8859-1
// mode: C++
// c-file-offsets: ((c . 0))
// c-file-style: "Stroustrup"
// fill-column: 80
// End: