Essai du nouveau "modele"

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1655 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
jhumeau 2010-01-20 09:43:06 +00:00
commit c3085595bf
13 changed files with 85 additions and 98 deletions

View file

@ -22,7 +22,7 @@ public:
* @return true if the neighbor1 is better than neighbor2
*/
virtual bool operator()(const Neighbor& _neighbor1, const Neighbor& _neighbor2) {
return (neighbor1.fitness() > neighbor2.fitness());
return (_neighbor1.fitness() > _neighbor2.fitness());
}
/**

View file

@ -4,11 +4,8 @@
#include <neighborhood/moBitNeighbor.h>
#include <ga.h>
/**
* contener of the neighbor information
*/
template< class Fitness >
class moFullEvalBitNeighbor : public moBitNeighbor<Fitness>
class moFullEvalBitNeighbor : public moFullEvalByModif<moBitNeighbor<Fitness> >
{
public:
typedef eoBit<Fitness> EOType ;

View file

@ -2,7 +2,7 @@
#define moFullEvalByModif_H
#include <eoEvalFunc.h>
#include <moEval.h>
#include <eval/moEval.h>
/**
* Full evaluation to use with a moBackableNeighbor
@ -11,14 +11,14 @@ template<class BackableNeighbor>
class moFullEvalByModif : public moEval<BackableNeighbor>
{
public:
using moEval<BackableNeighbor>::EOT EOT;
using moEval<BackableNeighbor>::Fitness Fitness;
typedef typename moEval<BackableNeighbor>::EOT EOT;
typedef typename moEval<BackableNeighbor>::Fitness Fitness;
/**
* Ctor
* @param _eval the full evaluation object
*/
moFullEvalByCopy(eoEvalFunc<EOT> & _eval) : eval(_eval) {}
moFullEvalByModif(eoEvalFunc<EOT>& _eval) : eval(_eval) {}
/**
* Full evaluation of the neighbor by copy
@ -44,6 +44,7 @@ public:
_neighbor.moveBack(_sol);
// set the fitness back
_sol.fitness(tmpFit);
}

View file

@ -5,6 +5,7 @@
#include <eoFunctor.h>
#include <neighborhood/moNeighborhood.h>
#include <eval/moEval.h>
/**
* Explore the neighborhood
@ -22,7 +23,7 @@ public:
* @param _neighborhood the neighborhood
* @param _eval the evaluation function
*/
moNeighborhoodExplorer(Neighborhood& _neighborhood, moEval<Neighbor>& _eval):neighborhood(_neighborhood), eval(_eval) {}
moNeighborhoodExplorer(Neighborhood& _neighborhood, moEval<Neighbor>& _eval, moNeighborComparator<Neighbor>& _comparator):neighborhood(_neighborhood), eval(_eval), comparator(_comparator) {}
/**
* Init Search parameters
@ -73,7 +74,7 @@ public:
protected:
Neighborhood & neighborhood;
moEval<Neighbor>& eval;
moNeighborComparator<Neighbor>& comparator;
};
#endif

View file

@ -3,17 +3,22 @@
#include <explorer/moNeighborhoodExplorer.h>
template< class NH >
class moSimpleHCexplorer : public moNeighborhoodExplorer<NH>
template< class Neighborhood >
class moSimpleHCexplorer : public moNeighborhoodExplorer<Neighborhood>
{
public:
typedef NH Neighborhood ;
typedef typename Neighborhood::EOT EOT ;
typedef typename Neighborhood::Neighbor Neighbor ;
using moNeighborhoodExplorer<Neighborhood>::neighborhood;
using moNeighborhoodExplorer<Neighborhood>::eval;
using moNeighborhoodExplorer<Neighborhood>::comparator;
// empty constructor
moSimpleHCexplorer(Neighborhood & __neighborhood) : neighborhood(__neighborhood){
isAccept = false;
moSimpleHCexplorer(Neighborhood& _neighborhood, moEval<Neighbor>& _eval, moNeighborComparator<Neighbor>& _comparator) : moNeighborhoodExplorer<Neighborhood>(_neighborhood, _eval, _comparator){
isAccept = false;
current=new Neighbor();
best=new Neighbor();
}
virtual void initParam (EOT & solution) { } ;
@ -24,52 +29,51 @@ public:
virtual void operator() (EOT & solution) {
//est qu'on peut initializer
if(neighborhood.hasNeighbor(solution)){
neighborhood.init(solution, *current);
(*current).eval(solution);
best = &current;
//est qu'on peut initializer
while (neighborhood.cont(solution)) {
neighborhood.next(solution, *current);
if(neighborhood.hasNeighbor(solution)){
neighborhood.init(solution, (*current));
(*current).eval(solution);
if (current.betterThan(best)) {
best = &current;
eval(solution, (*current));
std::cout <<"sol et neighbor:"<< solution << ", "<< (*current) << std::endl;
(*best) = (*current);
while (neighborhood.cont(solution)) {
neighborhood.next(solution, (*current));
eval(solution, (*current));
std::cout <<"sol et neighbor:"<< solution << ", "<< (*current) << std::endl;
if (comparator((*current), (*best))) {
(*best) = (*current);
}
}
}
else{
isAccept=false;
}
}
}
else{
isAccept=false;
}
};
virtual bool isContinue(EOT & solution) {
return isAccept ;
return isAccept ;
};
virtual void move(EOT & solution) {
best.move(solution);
(*best).move(solution);
solution.fitness(best.fitness());
solution.fitness((*best).fitness());
};
virtual bool accept(EOT & solution) {
if(neighborhood.hasNeighbor(solution)){
isAccept = (solution.fitness() < best.fitness()) ;
}
return isAccept;
if(neighborhood.hasNeighbor(solution)){
isAccept = (solution.fitness() < (*best).fitness()) ;
}
return isAccept;
};
private:
Neighborhood & neighborhood;
// attention il faut que le constructeur vide existe
Neighbor* best;
@ -82,12 +86,3 @@ private:
#endif
// Local Variables:
// coding: iso-8859-1
// mode: C++
// c-file-offsets: ((c . 0))
// c-file-style: "Stroustrup"
// fill-column: 80
// End:

View file

@ -5,7 +5,7 @@
* Neighbor with a move back function to use in a moFullEvalByModif
*/
template< class EOT , class Fitness >
class moBackableNeighbor : moNeighbor<EOT, Fitness>
class moBackableNeighbor : public moNeighbor<EOT, Fitness>
{
public:
@ -13,7 +13,7 @@ public:
* the move back function
* @param _solution the solution to moveBack
*/
virtual moveBack(EOT & _solution) = 0;
virtual void moveBack(EOT & _solution){}
};

View file

@ -2,24 +2,25 @@
#define _bitNeighbor_h
#include <ga/eoBit.h>
#include <neighborhood/moNeighbor.h>
#include <neighborhood/moBackableNeighbor.h>
/*
contener of the neighbor information
*/
template< class Fitness >
class moBitNeighbor : public moNeighbor< eoBit<Fitness> , Fitness>
class moBitNeighbor : public moBackableNeighbor<eoBit<Fitness>, Fitness>
{
public:
typedef eoBit<Fitness> EOType ;
using moNeighbor< eoBit<Fitness> , Fitness>::fitness;
using moNeighbor<eoBit<Fitness>, Fitness>::fitness;
// describe the neighbor
unsigned bit ;
// empty constructor needed
moBitNeighbor() : moNeighbor<eoBit<Fitness> , Fitness>() { } ;
moBitNeighbor() : moBackableNeighbor<eoBit<Fitness> , Fitness>() { } ;
// copy constructor
moBitNeighbor(const moBitNeighbor & n) : moNeighbor<eoBit<Fitness> , Fitness>(n) {
@ -43,7 +44,11 @@ public:
move the solution
*/
virtual void move(EOType & solution) {
solution[bit] = solution[bit]?false:true ;
solution[bit] = !solution[bit];
}
virtual void moveBack(EOType & solution) {
solution[bit] = !solution[bit];
}
// by default: if the fitness of the current solution is stricly higher than the other neighbor

View file

@ -22,18 +22,18 @@ public:
initialisation of the neighborhood
*/
virtual void init(EOT & solution, Neighbor & _neighbor) {
currentBit = 0 ;
currentBit = 0 ;
_neighbor.bit = currentBit ;
_neighbor.bit = currentBit ;
}
/*
Give the next neighbor
*/
virtual void next(EOT & solution, Neighbor & neighbor) {
currentBit++ ;
currentBit++ ;
neighbor.bit = currentBit ;
neighbor.bit = currentBit ;
}
/*
@ -50,11 +50,3 @@ private:
#endif
// Local Variables:
// coding: iso-8859-1
// mode: C++
// c-file-offsets: ((c . 0))
// c-file-style: "Stroustrup"
// fill-column: 80
// End:

View file

@ -61,7 +61,7 @@ public:
* Copy Constructor
* @param _neighbor to copy
*/
moNeighbor(const moNeighbor<EOType, Fitness>& _neighbor) {
moNeighbor(const moNeighbor<EOT, Fitness>& _neighbor) {
repFitness = _neighbor.fitness();
}
@ -131,7 +131,7 @@ public:
_os << repFitness << ' ' ;
}
private:
protected:
// minimal information on the neighbor : fitness
Fitness repFitness ;

View file

@ -8,9 +8,6 @@ typedef EO<int> Solution;
class moDummyNeighbor : public moNeighbor<Solution,int>{
virtual void eval(Solution & solution){}
virtual void move(Solution & solution){}
};
#endif

View file

@ -1,18 +1,10 @@
#include "moTestClass.h"
#include <cstdlib>
#include <cassert>
int main(){
moDummyNeighbor n1, n3;
n1.fitness(12);
moDummyNeighbor n2(n1);
assert(n1.fitness() == n2.fitness());
n3=n1;
assert(n1.fitness() == n3.fitness());
return EXIT_SUCCESS;
}

View file

@ -5,7 +5,7 @@ INCLUDE_DIRECTORIES(${PARADISEO_EO_SRC_DIR}/src
LINK_DIRECTORIES(${PARADISEO_EO_BIN_DIR}/lib)
ADD_EXECUTABLE(testSimpleHC testSimpleHC.cpp)
ADD_EXECUTABLE(testWithMove testWithMove.cpp)
#ADD_EXECUTABLE(testWithMove testWithMove.cpp)
TARGET_LINK_LIBRARIES(testSimpleHC eoutils ga eo)
TARGET_LINK_LIBRARIES(testWithMove eoutils ga eo)
#TARGET_LINK_LIBRARIES(testWithMove eoutils ga eo)

View file

@ -40,17 +40,20 @@ using namespace std;
// explore the neighborhood of a bit string in order
#include <neighborhood/moBitNeighborhood.h>
#include <eval/moFullEvalBitNeighbor.h>
#include <eval/moFullEvalByModif.h>
#include <oneMaxBitNeighbor.h>
#include <comparator/moNeighborComparator.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 ;
//typedef moFullEvalBitNeighbor<unsigned> Neighbor ; // full evaluation
//typedef moBitNeighborhood<Neighbor> Neighborhood ;
// GENERAL
//-----------------------------------------------------------------------------
@ -121,6 +124,8 @@ void main_function(int argc, char **argv)
FuncOneMax<Indi> eval(vecSize);
moFullEvalByModif<moBitNeighbor<unsigned int> > fulleval(eval);
/* =========================================================
*
* Initilisation of the solution
@ -138,7 +143,7 @@ void main_function(int argc, char **argv)
* ========================================================= */
// no need if incremental evaluation with OneMaxBitNeighbor
Neighbor::setFullEvalFunc(eval);
// Neighbor::setFullEvalFunc(eval);
/* =========================================================
*
@ -146,7 +151,9 @@ void main_function(int argc, char **argv)
*
* ========================================================= */
Neighborhood neighborhood;
moNeighborComparator<moBitNeighbor<unsigned int> > comparator;
moBitNeighborhood<moBitNeighbor<unsigned int> > neighborhood ;
/* =========================================================
*
@ -154,7 +161,7 @@ void main_function(int argc, char **argv)
*
* ========================================================= */
moSimpleHCexplorer<Neighborhood> explorer(neighborhood);
moSimpleHCexplorer<moBitNeighborhood<moBitNeighbor<unsigned int> > > explorer(neighborhood, fulleval, comparator);
/* =========================================================
*
@ -162,9 +169,9 @@ void main_function(int argc, char **argv)
*
* ========================================================= */
moTrueContinuator<Neighborhood> continuator;
moTrueContinuator<moBitNeighborhood<moBitNeighbor<unsigned int> > > continuator;
moLocalSearch< moSimpleHCexplorer<Neighborhood>, moTrueContinuator<Neighborhood> > localSearch(explorer, continuator);
moLocalSearch< moSimpleHCexplorer<moBitNeighborhood<moBitNeighbor<unsigned int> > >, moTrueContinuator<moBitNeighborhood<moBitNeighbor<unsigned int> > > > localSearch(explorer, continuator);
/* =========================================================
*
@ -180,7 +187,7 @@ void main_function(int argc, char **argv)
std::cout << "initial: " << solution << std::endl ;
localSearch(solution);
localSearch(solution);
std::cout << "final: " << solution << std::endl ;