diff --git a/trunk/paradiseo-mo/src/acceptCrit/moBetterAcceptCrit.h b/trunk/paradiseo-mo/src/acceptCrit/moBetterAcceptCrit.h index def0a398e..6248c03e8 100644 --- a/trunk/paradiseo-mo/src/acceptCrit/moBetterAcceptCrit.h +++ b/trunk/paradiseo-mo/src/acceptCrit/moBetterAcceptCrit.h @@ -30,5 +30,34 @@ Contact: paradiseo-help@lists.gforge.inria.fr #ifndef _moBetterAcceptCrit_h #define _moBetterAcceptCrit_h +#include +#include +#include + +/** + * Acceptance Criterion for extreme intensification : accept if the new solution is better than previous one + */ +template< class Neighbor > +class moBetterAcceptCrit : public moAcceptanceCriterion, public moDummyMemory{ + +public: + typedef typename Neighbor::EOT EOT; + + moBetterAcceptCrit(moSolComparator& _comparator):comparator(_comparator){} + + /** + * Accept if the new solution is better than previous one + * @param _sol1 the previous solution + * @param _sol2 the new solution after local search + * @return true if the new solution is better than previous one + */ + bool operator()(EOT& _sol1, EOT& _sol2){ + return comparator(_sol1, _sol2); + } + +private: + moSolComparator& comparator; + +}; #endif diff --git a/trunk/paradiseo-mo/src/comparator/moNeighborComparator.h b/trunk/paradiseo-mo/src/comparator/moNeighborComparator.h index 362443f2d..19fbcd49b 100644 --- a/trunk/paradiseo-mo/src/comparator/moNeighborComparator.h +++ b/trunk/paradiseo-mo/src/comparator/moNeighborComparator.h @@ -35,13 +35,9 @@ #ifndef _moNeighborComparator_h #define _moNeighborComparator_h -#include -#include - #include #include - /** * Comparator of two neighbors */ diff --git a/trunk/paradiseo-mo/src/comparator/moSolComparator.h b/trunk/paradiseo-mo/src/comparator/moSolComparator.h new file mode 100644 index 000000000..c9f9eff5d --- /dev/null +++ b/trunk/paradiseo-mo/src/comparator/moSolComparator.h @@ -0,0 +1,72 @@ +/* + +Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + +Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can ue, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. +The fact that you are presently reading this means that you have had +knowledge of the CeCILL license and that you accept its terms. + +ParadisEO WebSite : http://paradiseo.gforge.inria.fr +Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef _moSolComparator_h +#define _moSolComparator_h + +#include + +/** + * Comparator of two solutions + */ +template< class EOT > +class moSolComparator : public moComparator +{ +public: + + /** + * Compare two solutions + * @param _sol1 the first solution + * @param _sol2 the second solution + * @return true if the solution2 is better than solution1 + */ + virtual bool operator()(const EOT& _sol1, const EOT& _sol2) { + return (_sol1.fitness() < _sol2.fitness()); + } + + /** + * Compare two solutions + * @param _sol1 the first solution + * @param _sol2 the second solution + * @return true if the solution2 is equal to solution1 + */ + virtual bool equals(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 "moSolComparator"; + } +}; + +#endif diff --git a/trunk/paradiseo-mo/src/memory/moCountMoveMemory.h b/trunk/paradiseo-mo/src/memory/moCountMoveMemory.h new file mode 100644 index 000000000..90e7bb871 --- /dev/null +++ b/trunk/paradiseo-mo/src/memory/moCountMoveMemory.h @@ -0,0 +1,119 @@ +/* + +Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + +Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can ue, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. +The fact that you are presently reading this means that you have had +knowledge of the CeCILL license and that you accept its terms. + +ParadisEO WebSite : http://paradiseo.gforge.inria.fr +Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef _moCountMoveMemory_h +#define _moCountMoveMemory_h + +#include + +/** + * Count the number of move, noMove and the number of successive stagnation since the last Move + */ +template< class Neighbor > +class moCountMoveMemory : virtual public moMemory{ + +public: + typedef typename Neighbor::EOT EOT; + + /** + * Init all the counters + * @param _sol unused solution + */ + void init(EOT & _sol) { + nbMove=0; + nbNoMove=0; + counter=0; + } + + /** + * @param _sol unused solution + * @param _neighbor unused neighbor + */ + void add(EOT & _sol, Neighbor & _neighbor) { + nbMove++; + counter=0; + } + + /** + * @param _sol unused solution + * @param _neighbor unused neighbor + */ + void update(EOT & _sol, Neighbor & _neighbor) { + nbNoMove++; + counter++; + } + + /** + * ClearMemory : Reinit all the counters + */ + void clearMemory() { + nbMove=0; + nbNoMove=0; + counter=0; + } + + /** + * Getter of the number of move + * @return the counter + */ + unsigned int getNbMove(){ + return nbMove; + } + + /** + * Getter of the number of no move + * @return the counter + */ + unsigned int getNbNoMove(){ + return nbNoMove; + } + + /** + * Getter of the number of successive stagnation since the last Move + * @return the counter + */ + unsigned int getCounter(){ + return counter; + } + + /** + * Init counter + */ + void initCounter(){ + counter=0; + } + +private: + unsigned int nbMove; + unsigned int nbNoMove; + unsigned int counter; + +}; + +#endif diff --git a/trunk/paradiseo-mo/src/mo.h b/trunk/paradiseo-mo/src/mo.h index 6fad1f33b..3e394fbee 100755 --- a/trunk/paradiseo-mo/src/mo.h +++ b/trunk/paradiseo-mo/src/mo.h @@ -40,6 +40,7 @@ #include #include #include +#include #include #include @@ -86,6 +87,7 @@ #include #include #include +#include #include #include @@ -100,9 +102,12 @@ #include #include +#include +#include #include #include +#include #include #include diff --git a/trunk/paradiseo-mo/src/perturb/moMonOpPerturb.h b/trunk/paradiseo-mo/src/perturb/moMonOpPerturb.h index fa7b67a54..8afdd7735 100644 --- a/trunk/paradiseo-mo/src/perturb/moMonOpPerturb.h +++ b/trunk/paradiseo-mo/src/perturb/moMonOpPerturb.h @@ -47,6 +47,7 @@ public: /** * Default Constructor * @param _monOp an eoMonOp (pertubation operator) + * @param _fullEval a full evaluation function */ moMonOpPerturb(eoMonOp& _monOp, eoEvalFunc& _fullEval):monOp(_monOp), fullEval(_fullEval){} diff --git a/trunk/paradiseo-mo/src/perturb/moNeighborhoodPerturb.h b/trunk/paradiseo-mo/src/perturb/moNeighborhoodPerturb.h index a3ba1b73d..4e02c22ab 100644 --- a/trunk/paradiseo-mo/src/perturb/moNeighborhoodPerturb.h +++ b/trunk/paradiseo-mo/src/perturb/moNeighborhoodPerturb.h @@ -30,5 +30,79 @@ Contact: paradiseo-help@lists.gforge.inria.fr #ifndef _moNeighborhoodPerturb_h #define _moNeighborhoodPerturb_h +#include +#include + +/** + * Neighborhood Perturbation: explore the neighborhood to perturb the solution (the neighborhood could be different as the one used in the Local Search) + */ +template< class Neighbor, class OtherNH > +class moNeighborhoodPerturb : public moPerturbation{ + +public: + typedef typename Neighbor::EOT EOT; + typedef typename OtherNH::Neighbor OtherN; + + /** + * Default Constructor + * @param _otherNeighborhood a neighborhood + * @param _eval an Evaluation Function + */ + moNeighborhoodPerturb(OtherNH& _otherNeighborhood, moEval& _eval): otherNeighborhood(_otherNeighborhood), eval(_eval){} + + /** + * Apply move on the solution + * @param _solution the current solution + * @return true + */ + virtual bool operator()(EOT& _solution){ + if(otherNeighborhood.hasNeighbor(_solution)){ + eval(_solution, current); + current.move(_solution); + _solution.fitness(current.fitness()); + } + return true; + } + + /** + * Init the neighborhood + * @param _sol the current solution + */ + virtual void init(EOT & _sol){ + if(otherNeighborhood.hasNeighbor(_sol)) + otherNeighborhood.init(_sol, current); + } + + /** + * ReInit the neighborhood because a move was done + * @param _sol the current solution + * @param _neighbor unused neighbor (always empty) + */ + virtual void add(EOT & _sol, Neighbor & _neighbor){ + (*this).init(_sol); + } + + /** + * Explore another neighbor because no move was done + * @param _sol the current solution + * @param _neighbor unused neighbor (always empty) + */ + virtual void update(EOT & _sol, Neighbor & _neighbor){ + if(otherNeighborhood.cont(_sol)) + otherNeighborhood.next(_sol, current); + else + (*this).init(_sol); + } + + /** + * NOTHING TO DO + */ + virtual void clearMemory(){} + +private: + OtherNH& otherNeighborhood; + moEval& eval; + OtherN current; +}; #endif diff --git a/trunk/paradiseo-mo/src/perturb/moRestartPerturb.h b/trunk/paradiseo-mo/src/perturb/moRestartPerturb.h index 955e244c0..d86bad67c 100644 --- a/trunk/paradiseo-mo/src/perturb/moRestartPerturb.h +++ b/trunk/paradiseo-mo/src/perturb/moRestartPerturb.h @@ -31,4 +31,47 @@ Contact: paradiseo-help@lists.gforge.inria.fr #define _moRestartPerturb_h +#include +#include +#include +#include + +/** + * Restart Perturbation : restart when maximum number of iteration with no improvement is reached + */ +template< class Neighbor > +class moRestartPerturb : public moPerturbation, public moCountMoveMemory { + +public: + typedef typename Neighbor::EOT EOT; + + /** + * Default Constructor + * @param _init an initializer of solution + * @param _fullEval a full evaluation function + * @param _threshold maximum number of iteration with no improvement + */ + moRestartPerturb(eoInit& _init, eoEvalFunc& _fullEval, unsigned int _threshold):init(_init), fullEval(_fullEval), threshold(_threshold) {} + + /** + * Apply restart when necessary + * @param _solution to restart + * @return true + */ + bool operator()(EOT& _solution){ + if((*this).getCounter()>= threshold){ + init(_solution); + fullEval(_solution); + (*this).initCounter(); + } + return true; + } + +private: + eoInit& init; + eoEvalFunc& fullEval; + unsigned int threshold; +}; + + #endif diff --git a/trunk/paradiseo-mo/tutorial/oneMax/application/testILS.cpp b/trunk/paradiseo-mo/tutorial/oneMax/application/testILS.cpp index faf025aed..523d0eaf1 100644 --- a/trunk/paradiseo-mo/tutorial/oneMax/application/testILS.cpp +++ b/trunk/paradiseo-mo/tutorial/oneMax/application/testILS.cpp @@ -25,8 +25,10 @@ using namespace std; //----------------------------------------------------------------------------- // fitness function #include +#include #include #include +#include #include #include @@ -39,14 +41,18 @@ using namespace std; #include #include +#include +#include #include +#include #include // REPRESENTATION //----------------------------------------------------------------------------- -typedef eoBit Indi; +typedef eoBit Indi; typedef moBitNeighbor Neighbor ; // incremental evaluation typedef moOrderNeighborhood Neighborhood ; +typedef moRndWithoutReplNeighborhood Neighborhood2 ; typedef moSimpleHCexplorer NHE; void main_function(int argc, char **argv) @@ -107,6 +113,7 @@ void main_function(int argc, char **argv) FuncOneMax eval(vecSize); + //FuncNK eval(vecSize, 2); /* ========================================================= * @@ -148,6 +155,7 @@ void main_function(int argc, char **argv) * ========================================================= */ Neighborhood neighborhood(vecSize); + Neighborhood2 neighborhood2(vecSize); /* ========================================================= @@ -171,13 +179,20 @@ void main_function(int argc, char **argv) eoBitMutation monOp(1.0/vecSize); - moMonOpPerturb perturb(monOp, eval); + //moMonOpPerturb perturb(monOp, eval); - moAlwaysAcceptCrit accept; + //moRestartPerturb perturb(random, eval, 5); + + moNeighborhoodPerturb perturb(neighborhood2, fulleval); + + moSolComparator comp; + + //moAlwaysAcceptCrit accept; + moBetterAcceptCrit accept(comp); moILSexplorer< NHE > explorerILS(hc, perturb, accept); - moIterContinuator continuatorILS(10); + moIterContinuator continuatorILS(100); moLocalSearch< moILSexplorer< moSimpleHCexplorer > >localSearch(explorerILS, continuatorILS, eval); diff --git a/trunk/paradiseo-mo/tutorial/oneMax/src/funcNK.h b/trunk/paradiseo-mo/tutorial/oneMax/src/funcNK.h new file mode 100644 index 000000000..4a3beb3e5 --- /dev/null +++ b/trunk/paradiseo-mo/tutorial/oneMax/src/funcNK.h @@ -0,0 +1,328 @@ +#ifndef __funcNK +#define __funcNK + +#include +#include +#include + +using namespace std; + +template< class EOT > +class FuncNK : public eoEvalFunc { +public: + // tables des contributions + double ** tables; + + // liste des liens epistatiques en fonction du bit i + // pour chaque contribution, donne la liste des variables consernés + unsigned ** links; + + // liste inverse + // pour chaque variable, donne la liste indices des contributions + vector * knils; + + unsigned N; + unsigned K; + + // constructeur vide + FuncNK() : N(0), K(0) + { + tables = NULL; + links = NULL; + }; + + FuncNK(unsigned _n) : N(_n), K(0) + { + tables = NULL; + links = NULL; + }; + + // construction de tables aléatoirement + FuncNK(int n, int k, bool consecutive = false) : N(n), K(k) + { + if (consecutive) + consecutiveTables(); + else + randomTables(); + }; + + // construction à partir d'un fichier des tables et des liens + FuncNK(const char * fichier = "") + { + load(fichier); + }; + + ~FuncNK() + { + deleteTables(); + }; + + void buildTables() + { + links = new unsigned*[N]; + knils = new vector[N]; + tables = new double*[N]; + for(unsigned i = 0; i < N; i++) { + tables[i] = new double[1<<(K+1)]; + links[i] = new unsigned[K+1]; + knils[i].clear(); + } + }; + + void deleteTables() + { + if (links != NULL) { + for(int i = 0; i < N; i++) { + delete [] (links[i]); + } + delete [] links; + links = NULL; + } + + if (knils != NULL) { + /* + for(int i = 0; i < N; i++) { + knils[i].clear(); + } + */ + delete [] knils; + knils = NULL; + } + + if (tables != NULL) { + for(int i = 0; i < N; i++) { + delete [] (tables[i]); + } + delete [] tables; + tables = NULL; + } + }; + + virtual void load(const char * nomTables) + { + fstream file; + file.open(nomTables, ios::in); + + if (file.is_open()) { + //cout <<"loadTables: chargement des tables " <> s; + while (s[0] == 'c') { + getline(file,line,'\n'); + file >> s; + } + + // lecture des parametres + if (s[0] != 'p') { + cerr <<"loadTables: erreur de lecture de paramètre pour " << nomTables <> s; + if (s != "NK") { + cerr <<"erreur " <> N >> K; + buildTables(); + + // lecture des liens + if (s[0] != 'p') { + cerr <<"loadTables: erreur de lecture de paramètre 'links' pour " << nomTables <> s; + if (s == "links") { + loadLinks(file); + } else { + cerr <<"loadTables: erreur de lecture de paramètre 'links' pour " << nomTables <> s; + + if (s == "tables") { + loadTables(file); + } else { + cerr << "loadTables: erreur de lecture de paramètre 'tables' pour " << nomTables <> links[i][j]; + knils[links[i][j]].push_back(i); + } + } + + void loadTables(fstream & file) { + for(int j = 0; j < (1<<(K+1)); j++) + for(int i = 0; i < N; i++) + file >> tables[i][j]; + } + + virtual void save(const char * nomTables) + { + // cout <<"saveTables: sauvegarde de la table " <=0; j--) + perm(tabTirage, t[j], N-1-j); + }; + + void consecutiveLinks(int i) { + for(int j=0; j