diff --git a/branches/newMo/src/comparator/moNeighborComparator.h b/branches/newMo/src/comparator/moNeighborComparator.h index c5c52ef19..a26f602dc 100644 --- a/branches/newMo/src/comparator/moNeighborComparator.h +++ b/branches/newMo/src/comparator/moNeighborComparator.h @@ -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()); } /** diff --git a/branches/newMo/src/eval/moFullEvalBitNeighbor.h b/branches/newMo/src/eval/moFullEvalBitNeighbor.h index b7cb87749..bde2d7e56 100644 --- a/branches/newMo/src/eval/moFullEvalBitNeighbor.h +++ b/branches/newMo/src/eval/moFullEvalBitNeighbor.h @@ -4,11 +4,8 @@ #include #include -/** - * contener of the neighbor information - */ template< class Fitness > -class moFullEvalBitNeighbor : public moBitNeighbor +class moFullEvalBitNeighbor : public moFullEvalByModif > { public: typedef eoBit EOType ; diff --git a/branches/newMo/src/eval/moFullEvalByModif.h b/branches/newMo/src/eval/moFullEvalByModif.h index 9d7dd48e1..7d2d586ac 100644 --- a/branches/newMo/src/eval/moFullEvalByModif.h +++ b/branches/newMo/src/eval/moFullEvalByModif.h @@ -2,7 +2,7 @@ #define moFullEvalByModif_H #include -#include +#include /** * Full evaluation to use with a moBackableNeighbor @@ -11,14 +11,14 @@ template class moFullEvalByModif : public moEval { public: - using moEval::EOT EOT; - using moEval::Fitness Fitness; + typedef typename moEval::EOT EOT; + typedef typename moEval::Fitness Fitness; /** * Ctor * @param _eval the full evaluation object */ - moFullEvalByCopy(eoEvalFunc & _eval) : eval(_eval) {} + moFullEvalByModif(eoEvalFunc& _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); + } diff --git a/branches/newMo/src/explorer/moNeighborhoodExplorer.h b/branches/newMo/src/explorer/moNeighborhoodExplorer.h index 1b747e2e6..ef26b676e 100644 --- a/branches/newMo/src/explorer/moNeighborhoodExplorer.h +++ b/branches/newMo/src/explorer/moNeighborhoodExplorer.h @@ -5,6 +5,7 @@ #include #include +#include /** * Explore the neighborhood @@ -22,7 +23,7 @@ public: * @param _neighborhood the neighborhood * @param _eval the evaluation function */ - moNeighborhoodExplorer(Neighborhood& _neighborhood, moEval& _eval):neighborhood(_neighborhood), eval(_eval) {} + moNeighborhoodExplorer(Neighborhood& _neighborhood, moEval& _eval, moNeighborComparator& _comparator):neighborhood(_neighborhood), eval(_eval), comparator(_comparator) {} /** * Init Search parameters @@ -73,7 +74,7 @@ public: protected: Neighborhood & neighborhood; moEval& eval; - + moNeighborComparator& comparator; }; #endif diff --git a/branches/newMo/src/explorer/moSimpleHCexplorer.h b/branches/newMo/src/explorer/moSimpleHCexplorer.h index de4052713..f35826027 100644 --- a/branches/newMo/src/explorer/moSimpleHCexplorer.h +++ b/branches/newMo/src/explorer/moSimpleHCexplorer.h @@ -3,17 +3,22 @@ #include -template< class NH > -class moSimpleHCexplorer : public moNeighborhoodExplorer +template< class Neighborhood > +class moSimpleHCexplorer : public moNeighborhoodExplorer { public: - typedef NH Neighborhood ; typedef typename Neighborhood::EOT EOT ; typedef typename Neighborhood::Neighbor Neighbor ; + using moNeighborhoodExplorer::neighborhood; + using moNeighborhoodExplorer::eval; + using moNeighborhoodExplorer::comparator; + // empty constructor - moSimpleHCexplorer(Neighborhood & __neighborhood) : neighborhood(__neighborhood){ - isAccept = false; + moSimpleHCexplorer(Neighborhood& _neighborhood, moEval& _eval, moNeighborComparator& _comparator) : moNeighborhoodExplorer(_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 = ¤t; + //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 = ¤t; - + 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: diff --git a/branches/newMo/src/neighborhood/moBackableNeighbor.h b/branches/newMo/src/neighborhood/moBackableNeighbor.h index d9123e2e3..4175fdac4 100644 --- a/branches/newMo/src/neighborhood/moBackableNeighbor.h +++ b/branches/newMo/src/neighborhood/moBackableNeighbor.h @@ -5,7 +5,7 @@ * Neighbor with a move back function to use in a moFullEvalByModif */ template< class EOT , class Fitness > -class moBackableNeighbor : moNeighbor +class moBackableNeighbor : public moNeighbor { 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){} }; diff --git a/branches/newMo/src/neighborhood/moBitNeighbor.h b/branches/newMo/src/neighborhood/moBitNeighbor.h index 263b81bff..bd0ee8f3a 100644 --- a/branches/newMo/src/neighborhood/moBitNeighbor.h +++ b/branches/newMo/src/neighborhood/moBitNeighbor.h @@ -2,24 +2,25 @@ #define _bitNeighbor_h #include -#include +#include + /* contener of the neighbor information */ template< class Fitness > -class moBitNeighbor : public moNeighbor< eoBit , Fitness> +class moBitNeighbor : public moBackableNeighbor, Fitness> { public: typedef eoBit EOType ; - using moNeighbor< eoBit , Fitness>::fitness; + using moNeighbor, Fitness>::fitness; // describe the neighbor unsigned bit ; // empty constructor needed - moBitNeighbor() : moNeighbor , Fitness>() { } ; + moBitNeighbor() : moBackableNeighbor , Fitness>() { } ; // copy constructor moBitNeighbor(const moBitNeighbor & n) : moNeighbor , 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 diff --git a/branches/newMo/src/neighborhood/moBitNeighborhood.h b/branches/newMo/src/neighborhood/moBitNeighborhood.h index d0c026fa3..e225f2058 100644 --- a/branches/newMo/src/neighborhood/moBitNeighborhood.h +++ b/branches/newMo/src/neighborhood/moBitNeighborhood.h @@ -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: diff --git a/branches/newMo/src/neighborhood/moNeighbor.h b/branches/newMo/src/neighborhood/moNeighbor.h index bc94890cc..ff430d107 100644 --- a/branches/newMo/src/neighborhood/moNeighbor.h +++ b/branches/newMo/src/neighborhood/moNeighbor.h @@ -61,7 +61,7 @@ public: * Copy Constructor * @param _neighbor to copy */ - moNeighbor(const moNeighbor& _neighbor) { + moNeighbor(const moNeighbor& _neighbor) { repFitness = _neighbor.fitness(); } @@ -131,7 +131,7 @@ public: _os << repFitness << ' ' ; } -private: +protected: // minimal information on the neighbor : fitness Fitness repFitness ; diff --git a/branches/newMo/test/moTestClass.h b/branches/newMo/test/moTestClass.h index 58d17e6bb..7bcbfc9c3 100644 --- a/branches/newMo/test/moTestClass.h +++ b/branches/newMo/test/moTestClass.h @@ -8,9 +8,6 @@ typedef EO Solution; class moDummyNeighbor : public moNeighbor{ - virtual void eval(Solution & solution){} - - virtual void move(Solution & solution){} }; #endif diff --git a/branches/newMo/test/t-moNeighbor.cpp b/branches/newMo/test/t-moNeighbor.cpp index 2bbcdc0e9..c504ffa66 100644 --- a/branches/newMo/test/t-moNeighbor.cpp +++ b/branches/newMo/test/t-moNeighbor.cpp @@ -1,18 +1,10 @@ #include "moTestClass.h" +#include #include 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; } diff --git a/branches/newMo/tutorial/oneMax/application/CMakeLists.txt b/branches/newMo/tutorial/oneMax/application/CMakeLists.txt index 5d1511768..0900d2414 100644 --- a/branches/newMo/tutorial/oneMax/application/CMakeLists.txt +++ b/branches/newMo/tutorial/oneMax/application/CMakeLists.txt @@ -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) \ No newline at end of file +#TARGET_LINK_LIBRARIES(testWithMove eoutils ga eo) \ No newline at end of file diff --git a/branches/newMo/tutorial/oneMax/application/testSimpleHC.cpp b/branches/newMo/tutorial/oneMax/application/testSimpleHC.cpp index 59861980e..56c957f90 100644 --- a/branches/newMo/tutorial/oneMax/application/testSimpleHC.cpp +++ b/branches/newMo/tutorial/oneMax/application/testSimpleHC.cpp @@ -40,17 +40,20 @@ using namespace std; // explore the neighborhood of a bit string in order #include -#include +#include + #include +#include + // REPRESENTATION //----------------------------------------------------------------------------- // define your individuals typedef eoBit Indi; //typedef OneMaxBitNeighbor Neighbor ; // incremental evaluation -typedef moFullEvalBitNeighbor Neighbor ; // full evaluation -typedef moBitNeighborhood Neighborhood ; +//typedef moFullEvalBitNeighbor Neighbor ; // full evaluation +//typedef moBitNeighborhood Neighborhood ; // GENERAL //----------------------------------------------------------------------------- @@ -121,6 +124,8 @@ void main_function(int argc, char **argv) FuncOneMax eval(vecSize); + moFullEvalByModif > 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 > comparator; + + moBitNeighborhood > neighborhood ; /* ========================================================= * @@ -154,7 +161,7 @@ void main_function(int argc, char **argv) * * ========================================================= */ - moSimpleHCexplorer explorer(neighborhood); + moSimpleHCexplorer > > explorer(neighborhood, fulleval, comparator); /* ========================================================= * @@ -162,9 +169,9 @@ void main_function(int argc, char **argv) * * ========================================================= */ - moTrueContinuator continuator; + moTrueContinuator > > continuator; - moLocalSearch< moSimpleHCexplorer, moTrueContinuator > localSearch(explorer, continuator); + moLocalSearch< moSimpleHCexplorer > >, moTrueContinuator > > > 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 ;