From af1b621a105a1cfec13b8a9ee9c87bf2a72332b0 Mon Sep 17 00:00:00 2001 From: verel Date: Sun, 22 Jun 2014 15:45:33 +0200 Subject: [PATCH] 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; +}