From 0f370ac9e17c9d3edbb2512088afbf561f548de4 Mon Sep 17 00:00:00 2001 From: jhumeau Date: Mon, 18 Jan 2010 17:29:10 +0000 Subject: [PATCH] git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1650 331e1502-861f-0410-8da2-ba01fb791d7f --- branches/newMo/src/algo/moLocalSearch.h | 3 + branches/newMo/src/comparator/moComparator.h | 34 ++++++++ .../src/comparator/moNeighborComparator.h | 25 ++++-- branches/newMo/src/eval/moEval.h | 14 ++++ branches/newMo/src/eval/moFullEvalByCopy.h | 45 +++++++++++ branches/newMo/src/eval/moFullEvalByModif.h | 56 +++++++++++++ branches/newMo/src/eval/moIncrEvalWrapper.h | 34 ++++++++ .../src/explorer/moNeighborhoodExplorer.h | 10 +++ .../newMo/src/explorer/moSimpleHCexplorer.h | 18 ++--- .../src/neighborhood/moBackableNeighbor.h | 20 +++++ .../src/neighborhood/moFullEvalNeighbor.h | 46 ----------- .../newMo/src/neighborhood/moMoveNeighbor.h | 81 +++++-------------- branches/newMo/src/neighborhood/moNeighbor.h | 54 +------------ .../newMo/src/neighborhood/moNeighborhood.h | 10 --- 14 files changed, 267 insertions(+), 183 deletions(-) create mode 100644 branches/newMo/src/comparator/moComparator.h create mode 100644 branches/newMo/src/eval/moEval.h create mode 100644 branches/newMo/src/eval/moFullEvalByCopy.h create mode 100644 branches/newMo/src/eval/moFullEvalByModif.h create mode 100644 branches/newMo/src/eval/moIncrEvalWrapper.h create mode 100644 branches/newMo/src/neighborhood/moBackableNeighbor.h delete mode 100644 branches/newMo/src/neighborhood/moFullEvalNeighbor.h diff --git a/branches/newMo/src/algo/moLocalSearch.h b/branches/newMo/src/algo/moLocalSearch.h index 18a201207..abee45996 100644 --- a/branches/newMo/src/algo/moLocalSearch.h +++ b/branches/newMo/src/algo/moLocalSearch.h @@ -44,6 +44,9 @@ public: searchExplorer.terminate(solution); + //A CHANGER + return true; + }; private: diff --git a/branches/newMo/src/comparator/moComparator.h b/branches/newMo/src/comparator/moComparator.h new file mode 100644 index 000000000..c65c6400c --- /dev/null +++ b/branches/newMo/src/comparator/moComparator.h @@ -0,0 +1,34 @@ +#ifndef _moNeighborComparator_h +#define _moNeighborComparator_h + +#include + +template< class Neigh > +class moNeighborComparator : public eoBF +{ +public: + + /* + * true if the neighbor1 is better than neighbor2 + */ + virtual bool operator()(const Neigh & neighbor1, const Neigh & neighbor2) { + return (neighbor1.fitness() > neighbor2.fitness()); + } + + /** Return the class id. + * @return the class name as a std::string + */ + virtual std::string className() const { return "moNeighborComparator"; } +}; + + +#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/comparator/moNeighborComparator.h b/branches/newMo/src/comparator/moNeighborComparator.h index c65c6400c..85a4d2f25 100644 --- a/branches/newMo/src/comparator/moNeighborComparator.h +++ b/branches/newMo/src/comparator/moNeighborComparator.h @@ -3,22 +3,35 @@ #include -template< class Neigh > -class moNeighborComparator : public eoBF + + +// moComparator => comparer deux solutions +// idée : +// - eoComparator +// - moComparator qui hérite de eoComparator ? +// - moeoComparator qui hérite de eoComparator +// idée J : +// - eoComparator : eoBF +// - eoSolComparator : eoComparator ? +// - moNeighborCompartor : : eoComparator +// +// une instantiation possible !! +template< class EOT > +class moComparator : public eoBF { public: /* - * true if the neighbor1 is better than neighbor2 + * true if the _sol1 is better than _sol2 */ - virtual bool operator()(const Neigh & neighbor1, const Neigh & neighbor2) { - return (neighbor1.fitness() > neighbor2.fitness()); + virtual bool operator()(const EOT & _sol1, const EOT & _sol2) { + return (_sol1.fitness() > _sol2.fitness()); } /** Return the class id. * @return the class name as a std::string */ - virtual std::string className() const { return "moNeighborComparator"; } + virtual std::string className() const { return "moComparator"; } }; diff --git a/branches/newMo/src/eval/moEval.h b/branches/newMo/src/eval/moEval.h new file mode 100644 index 000000000..e37bd84ef --- /dev/null +++ b/branches/newMo/src/eval/moEval.h @@ -0,0 +1,14 @@ +#ifndef moEval_H +#define moEval_H + +#include + +template +class moEval : public eoBF +{ +public: + typedef typename Neighbor::EOType EOT; + typedef typename EOT::Fitness Fitness; +}; + +#endif diff --git a/branches/newMo/src/eval/moFullEvalByCopy.h b/branches/newMo/src/eval/moFullEvalByCopy.h new file mode 100644 index 000000000..018fd9987 --- /dev/null +++ b/branches/newMo/src/eval/moFullEvalByCopy.h @@ -0,0 +1,45 @@ +#ifndef moFullEvalByCopy_H +#define moFullEvalByCopy_H + +#include +#include + +template +class moFullEvalByCopy : public moEval +{ +public: + using moEval::EOT EOT; + using moEval::Fitness Fitness; + + /** + * Ctor + * @param _eval the full evaluation object + */ + moFullEvalByCopy(eoEvalFunc & _eval) : eval(_eval) {} + + /** + * Full evaluation of the neighbor by copy + * @param _sol current solution + * @param _neighbor the neighbor to be evaluated + */ + void operator()(EOT & _sol, Neighbor & _neighbor) + { + // tmp solution + EOT tmp(_sol); + // move tmp solution wrt _neighbor + _neighbor.(tmp); + // eval copy + tmp.invalidate(); + eval(tmp); + // set the fitness value to the neighbor + _neighbor.fitness(tmp.fitness()); + } + + +private: + /** the full evaluation object */ + eoEvalFunc & eval; + +}; + +#endif diff --git a/branches/newMo/src/eval/moFullEvalByModif.h b/branches/newMo/src/eval/moFullEvalByModif.h new file mode 100644 index 000000000..f3ebb8559 --- /dev/null +++ b/branches/newMo/src/eval/moFullEvalByModif.h @@ -0,0 +1,56 @@ +#ifndef moFullEvalByModif_H +#define moFullEvalByModif_H + +#include +#include + +/* + * Full evaluation to use with a moBackableNeighbor + */ +template +class moFullEvalByModif : public moEval +{ +public: + using moEval::EOT EOT; + using moEval::Fitness Fitness; + + /** + * Ctor + * @param _eval the full evaluation object + */ + moFullEvalByCopy(eoEvalFunc & _eval) : eval(_eval) {} + + /** + * Full evaluation of the neighbor by copy + * @param _sol current solution + * @param _neighbor the neighbor to be evaluated + */ + void operator()(EOT & _sol, BackableNeighbor & _neighbor) + { + // tmp fitness value of the current solution + Fitness tmpFit; + + // save current fitness value + tmpFit = _sol.fitness(); + + // move the current solution wrt _neighbor + _neighbor.move(_sol); + // eval the modified solution + _sol.invalidate(); + eval(_sol); + // set the fitness value to the neighbor + _neighbor.fitness(_sol.fitness()); + // move the current solution back + _neighbor.moveBack(_sol); + // set the fitness back + _sol.fitness(tmpFit); + } + + +private: + /** the full evaluation object */ + eoEvalFunc & eval; + +}; + +#endif diff --git a/branches/newMo/src/eval/moIncrEvalWrapper.h b/branches/newMo/src/eval/moIncrEvalWrapper.h new file mode 100644 index 000000000..be1835c0d --- /dev/null +++ b/branches/newMo/src/eval/moIncrEvalWrapper.h @@ -0,0 +1,34 @@ +#ifndef moIncrEvalWrapper_H +#define moIncrEvalWrapper_H + +#include +#include + +/* + * (Old fashioned) Incremental evaluation to use with a moMoveNeighbor + * WARNING: Don't use this class unless you are an moMove user. + */ +template +class moIncrEvalWrapper : public moEval +{ +public: + using moEval::EOT EOT; + using moEval::Fitness Fitness; + + moIncrEvalWrapper(moIncrEval& _incr):incr(_incr){} + + /* + * make the evaluation of the current neighbor and update the information on this neighbor + * the evaluation could be increamental + */ + virtual void eval(MoveNeighbor& _neighbor,EOT & _solution){ + _neighbor.fitness(incrEval(*(_neighbor.getMove()), _solution)); + } + +private: + /** the full evaluation object */ + moIncrEval & incrEval; + +}; + +#endif diff --git a/branches/newMo/src/explorer/moNeighborhoodExplorer.h b/branches/newMo/src/explorer/moNeighborhoodExplorer.h index 8046edd8d..c8c6df60c 100644 --- a/branches/newMo/src/explorer/moNeighborhoodExplorer.h +++ b/branches/newMo/src/explorer/moNeighborhoodExplorer.h @@ -11,10 +11,14 @@ class moNeighborhoodExplorer : public eoUF public: typedef NH Neighborhood ; typedef typename Neighborhood::EOT EOT ; + typedef typename Neighborhood::Neighbor Neighbor ; // empty constructor moNeighborhoodExplorer() { } ; + // empty constructor + moNeighborhoodExplorer(Neighborhood& _neighborhood, moEval& _eval):neighborhood(_neighborhood), eval(_eval) { } ; + virtual void initParam (EOT & solution) = 0 ; virtual void updateParam (EOT & solution) = 0 ; @@ -31,8 +35,14 @@ public: * @return the class name as a std::string */ virtual std::string className() const { return "moNeighborhoodExplorer"; } + +protected: + Neighborhood & neighborhood; + moEval& eval; + }; + #endif diff --git a/branches/newMo/src/explorer/moSimpleHCexplorer.h b/branches/newMo/src/explorer/moSimpleHCexplorer.h index 162b11d51..de4052713 100644 --- a/branches/newMo/src/explorer/moSimpleHCexplorer.h +++ b/branches/newMo/src/explorer/moSimpleHCexplorer.h @@ -12,7 +12,7 @@ public: typedef typename Neighborhood::Neighbor Neighbor ; // empty constructor - moSimpleHCexplorer(Neighborhood & __neighborhood) : neighborhood(__neighborhood) { + moSimpleHCexplorer(Neighborhood & __neighborhood) : neighborhood(__neighborhood){ isAccept = false; } @@ -27,20 +27,20 @@ public: //est qu'on peut initializer if(neighborhood.hasNeighbor(solution)){ - neighborhood.init(solution, current); + neighborhood.init(solution, *current); - current.eval(solution); + (*current).eval(solution); - best = current; + best = ¤t; while (neighborhood.cont(solution)) { - neighborhood.next(solution, current); + neighborhood.next(solution, *current); - current.eval(solution); + (*current).eval(solution); if (current.betterThan(best)) { - best = current; + best = ¤t; } } @@ -72,9 +72,9 @@ private: Neighborhood & neighborhood; // attention il faut que le constructeur vide existe - Neighbor best ; + Neighbor* best; - Neighbor current ; + Neighbor* current; // true if the move is accepted bool isAccept ; diff --git a/branches/newMo/src/neighborhood/moBackableNeighbor.h b/branches/newMo/src/neighborhood/moBackableNeighbor.h new file mode 100644 index 000000000..397a88525 --- /dev/null +++ b/branches/newMo/src/neighborhood/moBackableNeighbor.h @@ -0,0 +1,20 @@ +#ifndef _BackableNeighbor_h +#define _BackableNeighbor_h + +/* + neighbor with a move back function to use in a moFullEvalByModif +*/ +template< class EOT , class Fitness > +class moBackableNeighbor : moNeighbor +{ +public: + +/* + * the move back function + * @param _solution the solution to moveBack + */ + virtual moveBack(EOT & _solution) = 0; + +}; + +#endif diff --git a/branches/newMo/src/neighborhood/moFullEvalNeighbor.h b/branches/newMo/src/neighborhood/moFullEvalNeighbor.h deleted file mode 100644 index 40d605525..000000000 --- a/branches/newMo/src/neighborhood/moFullEvalNeighbor.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef _fullEvalNeighbor_h -#define _fullEvalNeighbor_h - -/* - neighbor with full evaluation -*/ -template< class EOT , class Fitness > -class moFullEvalNeighbor : moNeighbor -{ -public: - // empty constructor - moFullEvalNeighbor(eoEvalFunc & _eval) : fulleval(_eval) { } ; - - /* - make the evaluation of the current neighbor and update the information on this neighbor - */ - virtual void eval(EOT & solution) { - Fitness fit = solution.fitness(); - - move(solution); - - fulleval(solution); - - moveBack(solution); - - fitness = solution.fitness(); - - solution.fitness(fit); - }; - - virtual moveBack(EOT & solution) ; - -private: - eoEvalFunc & fulleval ; -}; - -#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/moMoveNeighbor.h b/branches/newMo/src/neighborhood/moMoveNeighbor.h index 57b6fb744..ce655e609 100644 --- a/branches/newMo/src/neighborhood/moMoveNeighbor.h +++ b/branches/newMo/src/neighborhood/moMoveNeighbor.h @@ -16,41 +16,35 @@ template< class M , class Fitness > { public: - typedef typename M::EOType EOT; + typedef typename M::EOType EOT; // empty constructor - moMoveNeighbor() {_move=new M();}; + moMoveNeighbor() { + move=new M(); + }; - ~moMoveNeighbor() {delete _move;}; + ~moMoveNeighbor() { + delete move; + }; - // copy constructeur - moMoveNeighbor(const moMoveNeighbor & _n) { - moNeighbor::operator=(_n); - (*_move) = *(_n._move); - } + // copy constructeur + moMoveNeighbor(const moMoveNeighbor & _n) { + moNeighbor::operator=(_n); + (*move) = *(_n.getMove()); + } // assignment operator virtual moMoveNeighbor & operator=(const moMoveNeighbor & _n) { moNeighbor ::operator=(_n); - (*_move) = *(_n._move); - - std::cout << moNeighbor::fitness() << " , " << _n.fitness() << std::endl; + (*move) = *(_n.getMove()); return *this ; } - /* - * make the evaluation of the current neighbor and update the information on this neighbor - * the evaluation could be increamental - */ - virtual void eval(EOT & solution){ - fitness((*_incrEval)(*_move, solution)); - } - /* * move the solution */ - virtual void move(EOT & solution){ - (*_move)(solution); + virtual void move(EOT & _solution){ + (*move)(_solution); } /** Return the class id. @@ -58,51 +52,18 @@ public: */ virtual std::string className() const { return "moMoveNeighbor"; } - static void setIncrEval(moMoveIncrEval& increm) { - _incrEval = & increm ; + void setMove(M* _move){ + move=_move; } - /** - * Read object.\ \ - * Calls base class, just in case that one had something to do. - * The read and print methods should be compatible and have the same format. - * In principle, format is "plain": they just print a number - * @param _is a std::istream. - * @throw runtime_std::exception If a valid object can't be read. - */ - /* virtual void readFrom(std::istream& _is) { - std::string fitness_str; - int pos = _is.tellg(); - _is >> fitness_str; + M* getMove(){ + return move; + } - if (fitness_str == "INVALID") - { - throw std::runtime_error("invalid fitness"); - } - else - { - _is.seekg(pos); // rewind - _is >> repFitness; - } - } -*/ - - /** - * Write object. Called printOn since it prints the object _on_ a stream. - * @param _os A std::ostream. - */ - /*virtual void printOn(std::ostream& _os) const { - _os << repFitness << ' ' ; - }*/ - - M* _move; private: - - static moMoveIncrEval* _incrEval; + M* move; }; -template< class M , class Fitness > - moMoveIncrEval * moMoveNeighbor::_incrEval = NULL; #endif diff --git a/branches/newMo/src/neighborhood/moNeighbor.h b/branches/newMo/src/neighborhood/moNeighbor.h index 5a21613a3..55001431e 100644 --- a/branches/newMo/src/neighborhood/moNeighbor.h +++ b/branches/newMo/src/neighborhood/moNeighbor.h @@ -28,31 +28,19 @@ public: return *this ; } - /* - * make the evaluation of the current neighbor and update the information on this neighbor - * the evaluation could be increamental - */ - virtual void eval(EOT & solution) = 0 ; - /* * move the solution */ virtual void move(EOT & solution) = 0 ; - // true if the this is better than the neighbor __neighbor -// virtual bool betterThan(const moNeighbor & __neighbor) = 0 ; - virtual bool betterThan(const moNeighbor & __neighbor) { - return (*neighborComparator)(*this, __neighbor) ; - } ; - /// Return fitness value. const Fitness& fitness() const { - return repFitness; + return repFitness; } /// Get fitness as reference, useful when fitness is set in a multi-stage way, e.g., MOFitness gets performance information, is subsequently ranked Fitness& fitnessReference() { - return repFitness; + return repFitness; } /** Set fitness. At the same time, validates it. @@ -74,22 +62,9 @@ public: * The read and print methods should be compatible and have the same format. * In principle, format is "plain": they just print a number * @param _is a std::istream. - * @throw runtime_std::exception If a valid object can't be read. */ virtual void readFrom(std::istream& _is) { - std::string fitness_str; - int pos = _is.tellg(); - _is >> fitness_str; - - if (fitness_str == "INVALID") - { - throw std::runtime_error("invalid fitness"); - } - else - { - _is.seekg(pos); // rewind _is >> repFitness; - } } /** @@ -100,35 +75,10 @@ public: _os << repFitness << ' ' ; } - static void setNeighborComparator(const moNeighborComparator< moNeighbor > & comparator) { - neighborComparator = & comparator ; - } - - static const moNeighborComparator< moNeighbor > & getNeighborComparator() { - return *neighborComparator ; - } - private: // minimal information on the neighbor : fitness Fitness repFitness ; - // the comparator of neighbors - static moNeighborComparator > * neighborComparator ; - }; -// static default comparor -template -moNeighborComparator > * moNeighbor::neighborComparator = new moNeighborComparator >(); - - #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/moNeighborhood.h b/branches/newMo/src/neighborhood/moNeighborhood.h index d42f1fd67..37278331a 100644 --- a/branches/newMo/src/neighborhood/moNeighborhood.h +++ b/branches/newMo/src/neighborhood/moNeighborhood.h @@ -33,14 +33,4 @@ public: virtual std::string className() const { return "moNeighborhood"; } }; - #endif - - -// Local Variables: -// coding: iso-8859-1 -// mode: C++ -// c-file-offsets: ((c . 0)) -// c-file-style: "Stroustrup" -// fill-column: 80 -// End: