From 1f09aa656f3db2433b329563829dafc6f5172e78 Mon Sep 17 00:00:00 2001 From: manu Date: Thu, 1 May 2014 19:21:43 +0200 Subject: [PATCH 1/9] Typo in moeo comments. --- moeo/src/algo/moeoSPEA2.h | 4 ++-- .../moeoFrontByFrontCrowdingDiversityAssignment.h | 4 ++-- .../diversity/moeoNearestNeighborDiversityAssignment.h | 9 ++++++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/moeo/src/algo/moeoSPEA2.h b/moeo/src/algo/moeoSPEA2.h index a6416ecc2..8b419b5f5 100644 --- a/moeo/src/algo/moeoSPEA2.h +++ b/moeo/src/algo/moeoSPEA2.h @@ -255,7 +255,7 @@ protected: eoQuadCloneOp < MOEOT > defaultQuadOp; /** an object for genetic operators (used as default) */ eoSGAGenOp < MOEOT > defaultSGAGenOp; - /** fitness assignment used in NSGA-II */ + /** fitness assignment used in SPEA2 */ moeoDominanceCountRankingFitnessAssignment < MOEOT > fitnessAssignment; /** general breeder */ eoGeneralBreeder < MOEOT > genBreed; @@ -265,7 +265,7 @@ protected: eoSelectTransform selectTransform; /** breeder */ eoBreed < MOEOT > & breed; - /** diversity assignment used in NSGA-II */ + /** diversity assignment used in SPEA2 */ moeoNearestNeighborDiversityAssignment < MOEOT > diversityAssignment; /** elitist replacement */ moeoGenerationalReplacement < MOEOT > replace; diff --git a/moeo/src/diversity/moeoFrontByFrontCrowdingDiversityAssignment.h b/moeo/src/diversity/moeoFrontByFrontCrowdingDiversityAssignment.h index 7ab4e0067..c4909d504 100644 --- a/moeo/src/diversity/moeoFrontByFrontCrowdingDiversityAssignment.h +++ b/moeo/src/diversity/moeoFrontByFrontCrowdingDiversityAssignment.h @@ -44,9 +44,9 @@ /** - * Diversity assignment sheme based on crowding proposed in: + * Diversity assignment scheme based on crowding proposed in: * K. Deb, A. Pratap, S. Agarwal, T. Meyarivan, "A Fast and Elitist Multi-Objective Genetic Algorithm: NSGA-II", IEEE Transactions on Evolutionary Computation, vol. 6, no. 2 (2002). - * Tis strategy assigns diversity values FRONT BY FRONT. It is, for instance, used in NSGA-II. + * This strategy assigns diversity values FRONT BY FRONT. It is, for instance, used in NSGA-II. */ template < class MOEOT > class moeoFrontByFrontCrowdingDiversityAssignment : public moeoCrowdingDiversityAssignment < MOEOT > diff --git a/moeo/src/diversity/moeoNearestNeighborDiversityAssignment.h b/moeo/src/diversity/moeoNearestNeighborDiversityAssignment.h index 8571ed475..5b5680eef 100644 --- a/moeo/src/diversity/moeoNearestNeighborDiversityAssignment.h +++ b/moeo/src/diversity/moeoNearestNeighborDiversityAssignment.h @@ -46,7 +46,14 @@ #include /** - * moeoNearestNeighborDiversityAssignment is a moeoDiversityAssignment using distance between individuals to assign diversity. + * moeoNearestNeighborDiversityAssignment is a moeoDiversityAssignment + * using distance between individuals to assign diversity. Proposed in: + * E. Zitzler, M. Laumanns, and L. Thiele. SPEA2: Improving the + * Strength Pareto Evolutionary Algorithm. Technical Report 103, + * Computer Engineering and Networks Laboratory (TIK), ETH Zurich, + * Zurich, Switzerland, 2001. + + * It is used in moeoSPEA2. */ template < class MOEOT > class moeoNearestNeighborDiversityAssignment : public moeoDiversityAssignment < MOEOT > From e71dea0c5a4d0aab7aebccca6bd841e476e76d43 Mon Sep 17 00:00:00 2001 From: quemy Date: Sat, 3 May 2014 18:58:55 +0200 Subject: [PATCH 2/9] Fix some warnings in eo and moeo. --- eo/src/eoNDSorting.h | 2 +- eo/src/eoTimeContinue.h | 1 + moeo/src/utils/moeoBinaryMetricSavingUpdater.h | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/eo/src/eoNDSorting.h b/eo/src/eoNDSorting.h index 6ab86c177..bb2e94fcc 100644 --- a/eo/src/eoNDSorting.h +++ b/eo/src/eoNDSorting.h @@ -273,7 +273,7 @@ private : { unsigned i; - typedef typename EOT::Fitness::fitness_traits traits; + //typedef typename EOT::Fitness::fitness_traits traits; std::vector > S(_pop.size()); // which individuals does guy i dominate std::vector n(_pop.size(), 0); // how many individuals dominate guy i diff --git a/eo/src/eoTimeContinue.h b/eo/src/eoTimeContinue.h index d49f5ea56..c0fcc3fe5 100644 --- a/eo/src/eoTimeContinue.h +++ b/eo/src/eoTimeContinue.h @@ -56,6 +56,7 @@ public: */ virtual bool operator() (const eoPop < EOT > & _pop) { + (void)_pop; time_t elapsed = (time_t) difftime(time(NULL), start); if (elapsed >= max) { diff --git a/moeo/src/utils/moeoBinaryMetricSavingUpdater.h b/moeo/src/utils/moeoBinaryMetricSavingUpdater.h index 0d8df0759..0848ef8ae 100644 --- a/moeo/src/utils/moeoBinaryMetricSavingUpdater.h +++ b/moeo/src/utils/moeoBinaryMetricSavingUpdater.h @@ -65,7 +65,7 @@ class moeoBinaryMetricSavingUpdater : public eoUpdater * @param _filename the target filename */ moeoBinaryMetricSavingUpdater (moeoVectorVsVectorBinaryMetric < ObjectiveVector, double > & _metric, const eoPop < MOEOT > & _pop, std::string _filename) : - metric(_metric), pop(_pop), filename(_filename), counter(1), firstGen(true) + metric(_metric), pop(_pop), filename(_filename), firstGen(true), counter(1) {} From b0479a15e939be205af6075b79f0b28c3682f8f2 Mon Sep 17 00:00:00 2001 From: quemy Date: Sun, 25 May 2014 21:52:22 +0200 Subject: [PATCH 3/9] Fix a bug in SMP Master / Worker model when the population size is lower than the number of workers. --- smp/src/scheduler.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/smp/src/scheduler.cpp b/smp/src/scheduler.cpp index 68b366c08..9a5210e0a 100644 --- a/smp/src/scheduler.cpp +++ b/smp/src/scheduler.cpp @@ -67,17 +67,21 @@ void paradiseo::smp::Scheduler::operator()(eoUF& func, e indice = i*nbIndi+j; } } - - for(unsigned i = 0; i < remaining; i++) - popPackages[i].push_back(&pop[indice+i+1]); + + if(nbIndi != 0) // Handle the offset if there is less individuals than workers + indice++; + for(unsigned i = 0; i < remaining; i++) + popPackages[i].push_back(&pop[indice+i]); // Starting threads for(unsigned i = 0; i < workers.size(); i++) - workers[i] = std::thread(&Scheduler::applyLinearPolicy, this, std::ref(func), std::ref(popPackages[i])); - + if(!popPackages[i].empty()) + workers[i] = std::thread(&Scheduler::applyLinearPolicy, this, std::ref(func), std::ref(popPackages[i])); + // Wait the end of tasks for(unsigned i = 0; i < workers.size(); i++) - workers[i].join(); + if(!popPackages[i].empty() && workers[i].joinable()) + workers[i].join(); } template @@ -117,7 +121,7 @@ void paradiseo::smp::Scheduler::operator()(eoUF& func, e } done = true; - + for(unsigned i = 0; i < workers.size(); i++) workers[i].join(); } From af1b621a105a1cfec13b8a9ee9c87bf2a72332b0 Mon Sep 17 00:00:00 2001 From: verel Date: Sun, 22 Jun 2014 15:45:33 +0200 Subject: [PATCH 4/9] add incremental eval for NK landscapes with corresponding test. Update INSTAL section test with the correct option --- mo/src/problems/eval/moNKlandscapesIncrEval.h | 131 ++++++++++++++++++ mo/test/t-moNKlandscapesIncrEval.cpp | 89 ++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 mo/src/problems/eval/moNKlandscapesIncrEval.h create mode 100644 mo/test/t-moNKlandscapesIncrEval.cpp diff --git a/mo/src/problems/eval/moNKlandscapesIncrEval.h b/mo/src/problems/eval/moNKlandscapesIncrEval.h new file mode 100644 index 000000000..6635c0de0 --- /dev/null +++ b/mo/src/problems/eval/moNKlandscapesIncrEval.h @@ -0,0 +1,131 @@ +/* + +Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + +Sebastien Verel, Arnaud Liefooghe, Jeremie 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 _moNKlandscapesIncrEval_H +#define _moNKlandscapesIncrEval_H + +#include +#include +#include + +/** + * + * Incremental evaluation function (1 bit flip, Hamming distance 1) + * for the NK-landscapes problem + * + * + */ +template< class Neighbor > +class moNKlandscapesIncrEval : public moEval +{ +public: + typedef typename Neighbor::EOT EOT; + + /* + * Constructor + * + * @param _nk fitness function of the NK landscapes + */ + moNKlandscapesIncrEval(nkLandscapesEval & _nk) : nk(_nk) { + inverseLinks = new std::vector[ nk.N ]; + + // compute the contributions which are modified by flipping one bit + for(unsigned int i = 0; i < nk.N; i++) + for(unsigned int j = 0; j < nk.K + 1; j++) { + inverseLinks[ nk.links[i][j] ].push_back(i); + } + } + + /* + * Destructor + * + */ + ~moNKlandscapesIncrEval() { + delete [] inverseLinks; + } + + /* + * incremental evaluation of the neighbor for the oneMax problem + * @param _solution the solution to move (bit string) + * @param _neighbor the neighbor to consider (of type moBitNeigbor) + */ + virtual void operator()(EOT & _solution, Neighbor & _neighbor) { + unsigned bit = _neighbor.index() ; + unsigned sig, nonSig; + unsigned i; + + double delta = 0 ; + + for(unsigned int j = 0; j < inverseLinks[bit].size(); j++) { + i = inverseLinks[bit][j]; + sigma(_solution, i, bit, sig, nonSig); + delta += nk.tables[i][nonSig] - nk.tables[i][sig]; + } + + _neighbor.fitness(_solution.fitness() + delta / (double) nk.N); + } + +private: + // Original nk fitness function + nkLandscapesEval & nk; + + // give the list of contributions which are modified when the corresponding bit is flipped + std::vector * inverseLinks; + + /** + * Compute the mask of the linked bits, and the mask when the bit is flipped + * + * @param _solution the solution to evaluate + * @param i the bit of the contribution + * @param _bit the bit to flip + * @param sig value of the mask of contribution i + * @param nonSig value of the mask of contribution i when the bit _bit is flipped + */ + void sigma(EOT & _solution, int i, unsigned _bit, unsigned & sig, unsigned & nonSig) { + sig = 0; + nonSig = 0; + + unsigned int n = 1; + for(int j = 0; j < nk.K + 1; j++) { + if (_solution[ nk.links[i][j] ] == 1) + sig = sig | n; + + if (nk.links[i][j] == _bit) + nonSig = n; + + n = n << 1; + } + + nonSig = sig ^ nonSig; + } + +}; + +#endif + diff --git a/mo/test/t-moNKlandscapesIncrEval.cpp b/mo/test/t-moNKlandscapesIncrEval.cpp new file mode 100644 index 000000000..cf9df9de1 --- /dev/null +++ b/mo/test/t-moNKlandscapesIncrEval.cpp @@ -0,0 +1,89 @@ +/* + + 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 use, + 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". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + 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 +*/ + +#include +#include +#include +#include +#include + +#include +#include + +typedef eoBit Solution; +typedef moBitNeighbor Neighbor ; + +int main() { + + std::cout << "[t-moNKlandscapesIncrEval] => START" << std::endl; + + // nk fitness function + int N = 18; + int K = 4; + rng.reseed(0); // random seed = 0 + + nkLandscapesEval eval(N, K); + + // init + eoUniformGenerator uGen; + eoInitFixedLength init(N, uGen); + + // verif constructor + moNKlandscapesIncrEval neighborEval(eval); + + Solution solution; + + // random initialization + rng.reseed(1); // random seed = 1 + + init(solution); + + // evaluation + eval(solution); + + Neighbor n ; + n.index(0); + + neighborEval(solution, n); + + n.move(solution); + eval(solution); + + // verif incremental eval + assert(solution.fitness() == n.fitness()); + + std::cout << "[t-moNKlandscapesIncrEval] => OK" << std::endl; + + return EXIT_SUCCESS; +} From d3d88d4cc5fa0d4fa23fedb3c7dbd5c44038bebe Mon Sep 17 00:00:00 2001 From: verel Date: Mon, 23 Jun 2014 08:11:47 +0200 Subject: [PATCH 5/9] Add MPX crossover --- eo/src/eoPartiallyMappedXover.h | 126 +++++++++++++++++++++++++++ eo/test/t-eoPartiallyMappedXover.cpp | 71 +++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 eo/src/eoPartiallyMappedXover.h create mode 100644 eo/test/t-eoPartiallyMappedXover.cpp diff --git a/eo/src/eoPartiallyMappedXover.h b/eo/src/eoPartiallyMappedXover.h new file mode 100644 index 000000000..16b23f6c9 --- /dev/null +++ b/eo/src/eoPartiallyMappedXover.h @@ -0,0 +1,126 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Sébastien Verel + + 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 eoPartiallyMappedXover__h +#define eoPartiallyMappedXover__h + +//----------------------------------------------------------------------------- + +#include +#include + +/** + * + * Partially Mapped CrossOver (PMX) + * for permutation representation + * + */ +template +class eoPartiallyMappedXover : public eoQuadOp +{ +public: + /** + * + * @param _solution1 The first solution + * @param _solution2 The second solution + * @return true if the solution has changed + */ + bool operator()(EOT & _solution1, EOT & _solution2) { + if (_solution1.size() > 1) { + // random indexes such that i1 < i2 + int i1 = rng.random(_solution1.size()); + int i2 = rng.random(_solution1.size()); + + while (i1 == i2) + i2 = rng.random(_solution1.size()); + + if (i1 > i2) { + int tmp = i1; + i1 = i2; + i2 = tmp; + } + + // the permutations between s1 and s2 + int * p1 = new int[_solution1.size()]; + int * p2 = new int[_solution1.size()]; + + int i; + for(i = 0; i < _solution1.size(); i++) { + p1[i] = -1; + p2[i] = -1; + } + + for(i = i1; i <= i2; i++) { + p1[ _solution2[i] ] = _solution1[i] ; + p2[ _solution1[i] ] = _solution2[i] ; + } + + // replace if necessary + for(i = 0; i < i1; i++) { + while (p1[ _solution1[i] ] != -1) + _solution1[i] = p1[_solution1[i]]; + while (p2[ _solution2[i] ] != -1) + _solution2[i] = p2[_solution2[i]]; + } + + // swap between solution1 and solution2 for [i1..i2] + for(i = i1; i <= i2; i++) { + _solution1[i] = p2[ _solution1[i] ]; + _solution2[i] = p1[ _solution2[i] ]; + } + + // replace if necessary + for(i = i2 + 1; i < _solution1.size(); i++) { + while (p1[ _solution1[i] ] != -1) + _solution1[i] = p1[_solution1[i]]; + while (p2[ _solution2[i] ] != -1) + _solution2[i] = p2[_solution2[i]]; + } + + // invalidate the solutions because they have been modified + _solution1.invalidate(); + _solution2.invalidate(); + + delete [] p1; + delete [] p2; + + return true; + } else + return false; + } + + /** + * The class name. + */ + virtual std::string className() const { + return "eoPartiallyMappedXover"; + } + +}; + +#endif diff --git a/eo/test/t-eoPartiallyMappedXover.cpp b/eo/test/t-eoPartiallyMappedXover.cpp new file mode 100644 index 000000000..371c45a18 --- /dev/null +++ b/eo/test/t-eoPartiallyMappedXover.cpp @@ -0,0 +1,71 @@ +/* + +*/ + +#include +#include + +#include + +#include +#include + +//----------------------------------------------------------------------------- + +typedef eoInt Solution; + +int main() { + + std::cout << "[t-eoPartiallyMappedXover] => START" << std::endl; + + Solution sol1, sol2; + sol1.resize(9); + sol2.resize(9); + + for(int i = 0; i < sol1.size(); i++) + sol1[i] = i; + + sol2[0] = 3; + sol2[1] = 4; + sol2[2] = 1; + sol2[3] = 0; + sol2[4] = 7; + sol2[5] = 6; + sol2[6] = 5; + sol2[7] = 8; + sol2[8] = 2; + + std::cout << sol1 << std::endl; + std::cout << sol2 << std::endl; + + eoPartiallyMappedXover xover; + xover(sol1, sol2); + + std::cout << "apres" << std::endl; + std::cout << sol1 << std::endl; + std::cout << sol2 << std::endl; + + int verif[9]; + for(int i = 0; i < sol1.size(); i++) + verif[i] = -1; + + for(int i = 0; i < sol1.size(); i++) + verif[ sol1[i] ] = 1; + + for(int i = 0; i < sol1.size(); i++) + assert(verif[i] != -1); + + + for(int i = 0; i < sol2.size(); i++) + verif[i] = -1; + + for(int i = 0; i < sol2.size(); i++) + verif[ sol2[i] ] = 1; + + for(int i = 0; i < sol2.size(); i++) + assert(verif[i] != -1); + + std::cout << "[t-eoPartiallyMappedXover] => OK" << std::endl; + + return EXIT_SUCCESS; +} From edefae4b281e6a93097bcbd5646b017922825831 Mon Sep 17 00:00:00 2001 From: verel Date: Wed, 25 Jun 2014 17:02:38 +0200 Subject: [PATCH 6/9] Small modif in moIndexedSwapNeighbor --- INSTALL | 4 +- eo/src/CMakeLists.txt | 2 +- eo/src/utils/eoDistance.h | 25 ++++++- eo/test/CMakeLists.txt | 1 + mo/src/algo/moRandomWalk.h | 2 +- mo/src/continuator/moVectorMonitor.h | 28 ++++++-- mo/src/mo.h | 1 + mo/src/neighborhood/moIndexNeighbor.h | 22 +++++- .../moRndWithoutReplNeighborhood.h | 2 +- .../permutation/moIndexedSwapNeighbor.h | 33 ++++++++- mo/test/CMakeLists.txt | 1 + mo/tutorial/Lesson6/adaptiveWalks.cpp | 1 + mo/tutorial/Lesson6/autocorrelation.cpp | 1 + mo/tutorial/Lesson6/densityOfStates.cpp | 1 + moeo/src/metric/moeoHyperVolumeMetric.h | 2 +- moeo/src/selection/moeoRouletteSelect.h | 2 +- problems/eval/nkLandscapesEval.h | 67 ++++++++++--------- 17 files changed, 141 insertions(+), 54 deletions(-) diff --git a/INSTALL b/INSTALL index 9734fc95a..6f1d121a7 100644 --- a/INSTALL +++ b/INSTALL @@ -183,9 +183,9 @@ Easy, isn't it ? By performing tests, you can check your installation. Testing is disable by default, except if you build with the full install type. -To enable testing, define -DENABLE_TESTING when you launch cmake. +To enable testing, define -DENABLE_CMAKE_TESTING=true when you launch cmake. -To perform tests simply type ctest ou make test. +To perform tests simply type ctest or make test. ------------------------------------------------------------------------------------------ 5.2 REPORTING diff --git a/eo/src/CMakeLists.txt b/eo/src/CMakeLists.txt index d38543098..b2b445a93 100644 --- a/eo/src/CMakeLists.txt +++ b/eo/src/CMakeLists.txt @@ -47,7 +47,7 @@ install(DIRECTORY do es ga gp other utils add_subdirectory(es) add_subdirectory(ga) add_subdirectory(utils) -add_subdirectory(serial) +#add_subdirectory(serial) if(ENABLE_PYEO) add_subdirectory(pyeo) diff --git a/eo/src/utils/eoDistance.h b/eo/src/utils/eoDistance.h index b26f249d6..35ce9a109 100644 --- a/eo/src/utils/eoDistance.h +++ b/eo/src/utils/eoDistance.h @@ -62,13 +62,15 @@ public: }; /** + SV: from eoHammingDistance, it is in fact the L1 distance + This is a generic class for L1 distance computation: assumes the 2 things are std::vectors of something that is double-castable For bitstrings, this is the Hamming distance */ template< class EOT > -class eoHammingDistance : public eoDistance +class eoL1Distance : public eoDistance { public: double operator()(const EOT & _v1, const EOT & _v2) @@ -83,6 +85,27 @@ public: } }; +/** + SV: change to have the Hamming (number of differences) + + For bitstrings, this is the Hamming distance +*/ +template< class EOT > +class eoHammingDistance : public eoDistance +{ +public: + double operator()(const EOT & _v1, const EOT & _v2) + { + double sum=0.0; + for (unsigned i=0; i<_v1.size(); i++) + { + if (_v1[i] != _v2[i]) + sum++; + } + return sum; + } +}; + /* this distance measures the difference in fitness * I am not sure it can be of any use, though ... * except for some testing diff --git a/eo/test/CMakeLists.txt b/eo/test/CMakeLists.txt index 15fd22464..4a0c74e43 100644 --- a/eo/test/CMakeLists.txt +++ b/eo/test/CMakeLists.txt @@ -69,6 +69,7 @@ set (TEST_LIST #t-openmp # does not work anymore since functions used in this test were removed from EO #t-eoDualFitness t-eoParser + t-eoPartiallyMappedXover ) diff --git a/mo/src/algo/moRandomWalk.h b/mo/src/algo/moRandomWalk.h index dafa3f277..6ccdbc107 100644 --- a/mo/src/algo/moRandomWalk.h +++ b/mo/src/algo/moRandomWalk.h @@ -60,7 +60,7 @@ public: */ moRandomWalk(Neighborhood& _neighborhood, eoEvalFunc& _fullEval, moEval& _eval, unsigned _nbStepMax): moLocalSearch(explorer, iterCont, _fullEval), - iterCont(_nbStepMax), + iterCont(_nbStepMax, false), explorer(_neighborhood, _eval) {} diff --git a/mo/src/continuator/moVectorMonitor.h b/mo/src/continuator/moVectorMonitor.h index b04eb8adf..d6870bb80 100644 --- a/mo/src/continuator/moVectorMonitor.h +++ b/mo/src/continuator/moVectorMonitor.h @@ -53,7 +53,7 @@ public: * Constructor * @param _param the parameter of type double to save in the vector */ - moVectorMonitor(eoValueParam & _param) : doubleParam(&_param), intParam(NULL), eotParam(NULL) + moVectorMonitor(eoValueParam & _param) : doubleParam(&_param), intParam(NULL), intLongParam(NULL), eotParam(NULL) { // precision of the output by default precisionOutput = std::cout.precision(); @@ -63,7 +63,17 @@ public: * Default Constructor * @param _param the parameter of type unsigned int to save in the vector */ - moVectorMonitor(eoValueParam & _param) : doubleParam(NULL), intParam(&_param), eotParam(NULL) + moVectorMonitor(eoValueParam & _param) : doubleParam(NULL), intParam(&_param), intLongParam(NULL), eotParam(NULL) + { + // precision of the output by default + precisionOutput = std::cout.precision(); + } + + /** + * Default Constructor + * @param _param the parameter of type unsigned int to save in the vector + */ + moVectorMonitor(eoValueParam & _param) : doubleParam(NULL), intParam(NULL), intLongParam(&_param), eotParam(NULL) { // precision of the output by default precisionOutput = std::cout.precision(); @@ -73,7 +83,7 @@ public: * Default Constructor * @param _param the parameter of type EOT to save in the vector */ - moVectorMonitor(eoValueParam & _param) : doubleParam(NULL), intParam(NULL), eotParam(&_param) + moVectorMonitor(eoValueParam & _param) : doubleParam(NULL), intParam(NULL), intLongParam(NULL), eotParam(&_param) { // precision of the output by default precisionOutput = std::cout.precision(); @@ -84,7 +94,7 @@ public: * @param _param the parameter of type eoScalarFitness to save in the vector */ template - moVectorMonitor(eoValueParam > & _param) : doubleParam( & (eoValueParam&)_param), intParam(NULL), eotParam(NULL) + moVectorMonitor(eoValueParam > & _param) : doubleParam( & (eoValueParam&)_param), intParam(NULL), intLongParam(NULL), eotParam(NULL) { // precision of the output by default precisionOutput = std::cout.precision(); @@ -95,7 +105,7 @@ public: * @param _param unvalid Parameter */ template - moVectorMonitor(eoValueParam & _param) : doubleParam(NULL), intParam(NULL), eotParam(NULL) + moVectorMonitor(eoValueParam & _param) : doubleParam(NULL), intParam(NULL), intLongParam(NULL), eotParam(NULL) { std::cerr << "Sorry the type can not be in a vector of moVectorMonitor" << std::endl; } @@ -120,8 +130,11 @@ public: else if (intParam != NULL) valueVec.push_back((double) intParam->value()); - else - eotVec.push_back(eotParam->value()); + else + if (intLongParam != NULL) + valueVec.push_back((double) intLongParam->value()); + else + eotVec.push_back(eotParam->value()); return *this ; } @@ -227,6 +240,7 @@ public: protected: eoValueParam * doubleParam ; eoValueParam * intParam ; + eoValueParam * intLongParam ; eoValueParam * eotParam ; std::vector valueVec; diff --git a/mo/src/mo.h b/mo/src/mo.h index 2a56a86a5..2ba411388 100755 --- a/mo/src/mo.h +++ b/mo/src/mo.h @@ -190,6 +190,7 @@ //#include //#include //#include +#include #include diff --git a/mo/src/neighborhood/moIndexNeighbor.h b/mo/src/neighborhood/moIndexNeighbor.h index 5ce17cdda..eaf302966 100644 --- a/mo/src/neighborhood/moIndexNeighbor.h +++ b/mo/src/neighborhood/moIndexNeighbor.h @@ -109,9 +109,9 @@ public: * @param _solution solution from which the neighborhood is visited * @param _key index of the IndexNeighbor */ - virtual void index(EOT & _solution, unsigned int _key) { - key = _key; - } + virtual void index(EOT & _solution, unsigned int _key) { + key = _key; + } /** * @param _neighbor a neighbor @@ -121,6 +121,22 @@ public: return (key == _neighbor.index()); } + /** + * Write object with its index + * @param _os A std::ostream. + */ + virtual void printOn(std::ostream& _os) const { + if (this->invalid()) { + _os << "INVALID "; + } + else + { + _os << this->fitness() << ' '; + } + + _os << key ; + } + protected: // key allowing to describe the neighbor unsigned int key; diff --git a/mo/src/neighborhood/moRndWithoutReplNeighborhood.h b/mo/src/neighborhood/moRndWithoutReplNeighborhood.h index 52113cc07..e51c6d6e7 100644 --- a/mo/src/neighborhood/moRndWithoutReplNeighborhood.h +++ b/mo/src/neighborhood/moRndWithoutReplNeighborhood.h @@ -130,7 +130,7 @@ public: return "moRndWithoutReplNeighborhood"; } -private: +protected: unsigned int maxIndex; std::vector indexVector; }; diff --git a/mo/src/problems/permutation/moIndexedSwapNeighbor.h b/mo/src/problems/permutation/moIndexedSwapNeighbor.h index d30305100..d3ddababf 100644 --- a/mo/src/problems/permutation/moIndexedSwapNeighbor.h +++ b/mo/src/problems/permutation/moIndexedSwapNeighbor.h @@ -46,6 +46,33 @@ public: using moIndexNeighbor::index; /** + * Default Constructor + */ + moIndexedSwapNeighbor() : moIndexNeighbor() { + } + + /** + * Copy Constructor + * @param _n the neighbor to copy + */ + moIndexedSwapNeighbor(const moIndexedSwapNeighbor & _n) : moIndexNeighbor(_n) + { + indices.first = _n.first(); + indices.second = _n.second(); + } + + /** + * Assignment operator + * @param _source the source neighbor + */ + moIndexedSwapNeighbor & operator=(const moIndexedSwapNeighbor & _source) { + moIndexNeighbor::operator=(_source); + indices.first = _source.first(); + indices.second = _source.second(); + return *this; + } + + /** * Apply the swap * @param _solution the solution to move */ @@ -111,7 +138,7 @@ public: * Getter of the firt location * @return first indice */ - unsigned int first() { + unsigned int first() const { return indices.first; } @@ -119,11 +146,11 @@ public: * Getter of the second location * @return second indice */ - unsigned int second() { + unsigned int second() const { return indices.second; } -private: +protected: std::pair indices; }; diff --git a/mo/test/CMakeLists.txt b/mo/test/CMakeLists.txt index 652b66be3..a27f97db9 100644 --- a/mo/test/CMakeLists.txt +++ b/mo/test/CMakeLists.txt @@ -18,6 +18,7 @@ set (TEST_LIST t-moOrderNeighborhood t-moFullEvalByCopy t-moFullEvalByModif + t-moNKlandscapesIncrEval t-moNeighborComparator t-moSolNeighborComparator t-moTrueContinuator diff --git a/mo/tutorial/Lesson6/adaptiveWalks.cpp b/mo/tutorial/Lesson6/adaptiveWalks.cpp index 36ab47ca0..1976f577b 100644 --- a/mo/tutorial/Lesson6/adaptiveWalks.cpp +++ b/mo/tutorial/Lesson6/adaptiveWalks.cpp @@ -85,6 +85,7 @@ void main_function(int argc, char **argv) string str_out = "out.dat"; // default value eoValueParam outParam(str_out.c_str(), "out", "Output file of the sampling", 'o'); parser.processParam(outParam, "Persistence" ); + str_out = outParam.value(); // the name of the "status" file where all actual parameter values will be saved string str_status = parser.ProgramName() + ".status"; // default value diff --git a/mo/tutorial/Lesson6/autocorrelation.cpp b/mo/tutorial/Lesson6/autocorrelation.cpp index 6f07d8ae7..f034efc43 100644 --- a/mo/tutorial/Lesson6/autocorrelation.cpp +++ b/mo/tutorial/Lesson6/autocorrelation.cpp @@ -89,6 +89,7 @@ void main_function(int argc, char **argv) string str_out = "out.dat"; // default value eoValueParam outParam(str_out.c_str(), "out", "Output file of the sampling", 'o'); parser.processParam(outParam, "Persistence" ); + str_out = outParam.value(); // the name of the "status" file where all actual parameter values will be saved string str_status = parser.ProgramName() + ".status"; // default value diff --git a/mo/tutorial/Lesson6/densityOfStates.cpp b/mo/tutorial/Lesson6/densityOfStates.cpp index a47323423..9e2eeddca 100644 --- a/mo/tutorial/Lesson6/densityOfStates.cpp +++ b/mo/tutorial/Lesson6/densityOfStates.cpp @@ -83,6 +83,7 @@ void main_function(int argc, char **argv) string str_out = "out.dat"; // default value eoValueParam outParam(str_out.c_str(), "out", "Output file of the sampling", 'o'); parser.processParam(outParam, "Persistence" ); + str_out = outParam.value(); // the name of the "status" file where all actual parameter values will be saved string str_status = parser.ProgramName() + ".status"; // default value diff --git a/moeo/src/metric/moeoHyperVolumeMetric.h b/moeo/src/metric/moeoHyperVolumeMetric.h index f59ba19a6..99f132131 100644 --- a/moeo/src/metric/moeoHyperVolumeMetric.h +++ b/moeo/src/metric/moeoHyperVolumeMetric.h @@ -83,7 +83,7 @@ class moeoHyperVolumeMetric : public moeoVectorUnaryMetric < ObjectiveVector , d * @param _ref_point the reference point * @param _bounds bounds value */ - moeoHyperVolumeMetric(ObjectiveVector& _ref_point=NULL, std::vector < eoRealInterval >& _bounds=NULL): normalize(false), rho(0.0), ref_point(_ref_point), bounds(_bounds){} + moeoHyperVolumeMetric(ObjectiveVector& _ref_point, std::vector < eoRealInterval >& _bounds): normalize(false), rho(0.0), ref_point(_ref_point), bounds(_bounds){} /** * calculates and returns the HyperVolume value of a pareto front diff --git a/moeo/src/selection/moeoRouletteSelect.h b/moeo/src/selection/moeoRouletteSelect.h index b5adcb0c2..fb34259e7 100644 --- a/moeo/src/selection/moeoRouletteSelect.h +++ b/moeo/src/selection/moeoRouletteSelect.h @@ -80,7 +80,7 @@ class moeoRouletteSelect:public moeoSelectOne < MOEOT > protected: /** size */ - double & tSize; + unsigned int & tSize; }; diff --git a/problems/eval/nkLandscapesEval.h b/problems/eval/nkLandscapesEval.h index 29fd20d5f..070fb1b43 100644 --- a/problems/eval/nkLandscapesEval.h +++ b/problems/eval/nkLandscapesEval.h @@ -31,6 +31,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr #define __nkLandscapesEval_H #include +#include template< class EOT > class nkLandscapesEval : public eoEvalFunc { @@ -79,7 +80,7 @@ public: */ nkLandscapesEval(const char * _fileName) { - string fname(_fileName); + std::string fname(_fileName); load(fname); }; @@ -132,16 +133,16 @@ public: * * @param _fileName file name of the instance */ - virtual void load(const string _fileName) + virtual void load(const std::string _fileName) { - fstream file; - file.open(_fileName.c_str(), ios::in); + std::fstream file; + file.open(_fileName.c_str(), std::fstream::in); if (file.is_open()) { - string s; + std::string s; // Read the commentairies - string line; + std::string line; file >> s; while (s[0] == 'c') { getline(file,line,'\n'); @@ -150,14 +151,14 @@ public: // Read the parameters if (s[0] != 'p') { - string str = "nkLandscapesEval.load: -- p -- expected in [" + _fileName + "] at the begining." ; - throw runtime_error(str); + std::string str = "nkLandscapesEval.load: -- p -- expected in [" + _fileName + "] at the begining." ; + throw std::runtime_error(str); } file >> s; if (s != "NK") { - string str = "nkLandscapesEval.load: -- NK -- expected in [" + _fileName + "] at the begining." ; - throw runtime_error(str); + std::string str = "nkLandscapesEval.load: -- NK -- expected in [" + _fileName + "] at the begining." ; + throw std::runtime_error(str); } // read parameters N and K @@ -166,22 +167,22 @@ public: // read the links if (s[0] != 'p') { - string str = "nkLandscapesEval.load: -- p -- expected in [" + _fileName + "] after the parameters N and K." ; - throw runtime_error(str); + std::string str = "nkLandscapesEval.load: -- p -- expected in [" + _fileName + "] after the parameters N and K." ; + throw std::runtime_error(str); } file >> s; if (s == "links") { loadLinks(file); } else { - string str = "nkLandscapesEval.load: -- links -- expected in [" + _fileName + "] after the parameters N and K." ; - throw runtime_error(str); + std::string str = "nkLandscapesEval.load: -- links -- expected in [" + _fileName + "] after the parameters N and K." ; + throw std::runtime_error(str); } // lecture des tables if (s[0] != 'p') { - string str = "nkLandscapesEval.load: -- p -- expected in [" + _fileName + "] after the links." ; - throw runtime_error(str); + std::string str = "nkLandscapesEval.load: -- p -- expected in [" + _fileName + "] after the links." ; + throw std::runtime_error(str); } file >> s; @@ -189,14 +190,14 @@ public: if (s == "tables") { loadTables(file); } else { - string str = "nkLandscapesEval.load: -- tables -- expected in [" + _fileName + "] after the links." ; - throw runtime_error(str); + std::string str = "nkLandscapesEval.load: -- tables -- expected in [" + _fileName + "] after the links." ; + throw std::runtime_error(str); } file.close(); } else { - string str = "nkLandscapesEval.load: Could not open file [" + _fileName + "]." ; - throw runtime_error(str); + std::string str = "nkLandscapesEval.load: Could not open file [" + _fileName + "]." ; + throw std::runtime_error(str); } }; @@ -206,7 +207,7 @@ public: * * @param file the file to read */ - void loadLinks(fstream & file) { + void loadLinks(std::fstream & file) { for(int j = 0; j < K+1; j++) for(int i = 0; i < N; i++) { file >> links[i][j]; @@ -218,7 +219,7 @@ public: * * @param file the file to read */ - void loadTables(fstream & file) { + void loadTables(std::fstream & file) { for(int j = 0; j < (1<<(K+1)); j++) for(int i = 0; i < N; i++) file >> tables[i][j]; @@ -230,29 +231,29 @@ public: * @param _fileName the file name of instance */ virtual void save(const char * _fileName) { - fstream file; - file.open(_fileName, ios::out); + std::fstream file; + file.open(_fileName, std::fstream::out); if (file.is_open()) { - file << "c name of the file : " << _fileName << endl; - file << "p NK " << N << " " << K < Date: Wed, 25 Jun 2014 22:54:15 +0200 Subject: [PATCH 7/9] Update moMonOpPerturb --- mo/src/perturb/moMonOpPerturb.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/mo/src/perturb/moMonOpPerturb.h b/mo/src/perturb/moMonOpPerturb.h index e0dc138da..87b409069 100644 --- a/mo/src/perturb/moMonOpPerturb.h +++ b/mo/src/perturb/moMonOpPerturb.h @@ -50,7 +50,16 @@ public: * @param _fullEval a full evaluation function * @param _nbPerturbation number of operator executions for perturbation */ - moMonOpPerturb(eoMonOp& _monOp, eoEvalFunc& _fullEval, unsigned int _nbPerturbation = 1):monOp(_monOp), fullEval(_fullEval), nbPerturbation(_nbPerturbation) {} + moMonOpPerturb(eoMonOp& _monOp, eoEvalFunc& _fullEval, unsigned int _nbPerturbation = 1):monOp(_monOp), fullEval(_fullEval), nbPerturbation(_nbPerturbation), rndPerturb(false) {} + + /** + * Constructor with random value at each iteration of the number of perturbation moves + * @param _monOp an eoMonOp (pertubation operator) + * @param _fullEval a full evaluation function + * @param _nbPerturbationMin minimum number of operator executions for perturbation + * @param _nbPerturbationMax maximum number of operator executions for perturbation + */ + moMonOpPerturb(eoMonOp& _monOp, eoEvalFunc& _fullEval, unsigned int _nbPerturbationMin, unsigned int _nbPerturbationMax):monOp(_monOp), fullEval(_fullEval), nbPerturbationMin(_nbPerturbationMin), nbPerturbationMax(_nbPerturbationMax), rndPerturb(true) { } /** * Apply monOp on the solution @@ -60,6 +69,9 @@ public: bool operator()(EOT& _solution) { bool res = false; + if (rndPerturb) + nbPerturbation = nbPerturbationMin + rng.random(nbPerturbationMax); + for(unsigned int i = 0; i < nbPerturbation; i++) res = monOp(_solution) || res; @@ -74,6 +86,9 @@ private: eoMonOp& monOp; eoEvalFunc& fullEval; unsigned int nbPerturbation; + unsigned int nbPerturbationMin; + unsigned int nbPerturbationMax; + bool rndPerturb; }; #endif From fa03cd7efe0f3016ad9f42f811753ad3d3a97e4b Mon Sep 17 00:00:00 2001 From: verel Date: Fri, 27 Jun 2014 21:25:08 +0200 Subject: [PATCH 8/9] update moIndexedVectorTabuList --- mo/src/memory/moIndexedVectorTabuList.h | 31 ++++++++++++++++++++++--- mo/src/perturb/moMonOpPerturb.h | 2 +- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/mo/src/memory/moIndexedVectorTabuList.h b/mo/src/memory/moIndexedVectorTabuList.h index f01e17f1b..cbb2c9c1e 100644 --- a/mo/src/memory/moIndexedVectorTabuList.h +++ b/mo/src/memory/moIndexedVectorTabuList.h @@ -35,8 +35,13 @@ Contact: paradiseo-help@lists.gforge.inria.fr #include /** + * * Tabu List of indexed neighbors save in a vector * each neighbor can not used during howlong iterations + * + * The tabu tenure could be random between two bounds + * such as in robust tabu search + * */ template class moIndexedVectorTabuList : public moTabuList @@ -49,7 +54,17 @@ public: * @param _maxSize maximum size of the tabu list * @param _howlong how many iteration a move is tabu */ - moIndexedVectorTabuList(unsigned int _maxSize, unsigned int _howlong) : maxSize(_maxSize), howlong(_howlong) { + moIndexedVectorTabuList(unsigned int _maxSize, unsigned int _howlong) : maxSize(_maxSize), howlong(_howlong), robust(false) { + tabuList.resize(_maxSize); + } + + /** + * Constructor + * @param _maxSize maximum size of the tabu list + * @param _howlongMin minimal number of iterations during a move is tabu + * @param _howlongMax maximal number of iterations during a move is tabu + */ + moIndexedVectorTabuList(unsigned int _maxSize, unsigned int _howlongMin, unsigned int _howlongMax) : maxSize(_maxSize), howlongMin(_howlongMin), howlongMax(_howlongMax), robust(true) { tabuList.resize(_maxSize); } @@ -68,8 +83,13 @@ public: * @param _neighbor the current neighbor */ virtual void add(EOT & _sol, Neighbor & _neighbor) { - if (_neighbor.index() < maxSize) + if (_neighbor.index() < maxSize) { + if (robust) + // random value between min and max + howlong = howlongMin + rng.random(howlongMax - howlongMin); + tabuList[_neighbor.index()] = howlong; + } } /** @@ -115,7 +135,12 @@ protected: unsigned int maxSize; //how many iteration a move is tabu unsigned int howlong; - + // Minimum number of iterations during a move is tabu + unsigned int howlongMin; + // Maximum number of iterations during a move is tabu + unsigned int howlongMax; + // true: robust tabu search way + bool robust; }; #endif diff --git a/mo/src/perturb/moMonOpPerturb.h b/mo/src/perturb/moMonOpPerturb.h index 87b409069..b043a99e9 100644 --- a/mo/src/perturb/moMonOpPerturb.h +++ b/mo/src/perturb/moMonOpPerturb.h @@ -70,7 +70,7 @@ public: bool res = false; if (rndPerturb) - nbPerturbation = nbPerturbationMin + rng.random(nbPerturbationMax); + nbPerturbation = nbPerturbationMin + rng.random(nbPerturbationMax - nbPerturbationMin); for(unsigned int i = 0; i < nbPerturbation; i++) res = monOp(_solution) || res; From 7a1b38aa02b22a958024af8dc2d2ce0c5625c56e Mon Sep 17 00:00:00 2001 From: verel Date: Wed, 23 Jul 2014 12:02:18 +0200 Subject: [PATCH 9/9] add include in indextabulist --- mo/src/memory/moIndexedVectorTabuList.h | 1 + 1 file changed, 1 insertion(+) diff --git a/mo/src/memory/moIndexedVectorTabuList.h b/mo/src/memory/moIndexedVectorTabuList.h index cbb2c9c1e..e1ea94492 100644 --- a/mo/src/memory/moIndexedVectorTabuList.h +++ b/mo/src/memory/moIndexedVectorTabuList.h @@ -30,6 +30,7 @@ Contact: paradiseo-help@lists.gforge.inria.fr #ifndef _moIndexedVectorTabuList_h #define _moIndexedVectorTabuList_h +#include #include #include #include