diff --git a/trunk/paradiseo-mo/src/problems/eval/oneMaxFullEval.h b/trunk/paradiseo-mo/src/problems/eval/oneMaxFullEval.h index b7f680885..22a9d4948 100644 --- a/trunk/paradiseo-mo/src/problems/eval/oneMaxFullEval.h +++ b/trunk/paradiseo-mo/src/problems/eval/oneMaxFullEval.h @@ -30,8 +30,10 @@ Contact: paradiseo-help@lists.gforge.inria.fr #ifndef _oneMaxFullEval_h #define _oneMaxFullEval_h +#include + /** - * Full evalution Function for OneMax problem + * Full evaluation Function for OneMax problem */ template< class EOT > class oneMaxFullEval : public eoEvalFunc diff --git a/trunk/paradiseo-mo/src/problems/eval/queenFullEval.h b/trunk/paradiseo-mo/src/problems/eval/queenFullEval.h new file mode 100644 index 000000000..29eaf77f1 --- /dev/null +++ b/trunk/paradiseo-mo/src/problems/eval/queenFullEval.h @@ -0,0 +1,57 @@ +/* + +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 _queenFullEval_h +#define _queenFullEval_h + +#include + +/** + * Full evaluation function for QUEEN problem + */ +template< class EOT > +class queenFullEval : public eoEvalFunc +{ +public: + + /** + * Count number of threat + * @param _queen a solution + */ + void operator()(EOT& _queen){ + unsigned int fit=0; + for(unsigned int i=0; i<_queen.size()-1; i++) + for(unsigned int j=i+1; j< _queen.size(); j++) + if((_queen[i]+j-i == _queen[j]) || (_queen[i]+i-j == _queen[j])) + fit++; + _queen.fitness(fit); + } +}; + +#endif diff --git a/trunk/paradiseo-mo/src/problems/permutation/moShiftNeighbor.h b/trunk/paradiseo-mo/src/problems/permutation/moShiftNeighbor.h new file mode 100644 index 000000000..c67b55d0f --- /dev/null +++ b/trunk/paradiseo-mo/src/problems/permutation/moShiftNeighbor.h @@ -0,0 +1,115 @@ +/* + +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 _moShiftNeighbor_h +#define _moShiftNeighbor_h + +#include + +/** + * Indexed Shift Neighbor + */ +template +class moShiftNeighbor: public moIndexNeighbor +{ +public: + + using moIndexNeighbor::key; + + /** + * Apply move on a solution regarding a key + * @param _sol the solution to move + */ + virtual void move(EOT & _sol){ + unsigned int tmp ; + size=_sol.size(); + translate(key+1); + // keep the first component to change + tmp = _sol[first]; + // shift + if (first < second){ + for (unsigned int i=first; i second*/ + for (unsigned int i=first; i>second; i--) + _sol[i] = _sol[i-1]; + // shift the first component + _sol[second] = tmp; + } + _sol.invalidate(); + } + + /** + * fix two indexes regarding a key + * @param _key the key allowing to compute the two indexes for the shift + */ + void translate(unsigned int _key){ + int step; + int val = _key; + int tmpSize = size * (size-1) / 2; + // moves from left to right + if (val <= tmpSize){ + step = size - 1; + first = 0; + while ((val - step) > 0){ + val = val - step; + step--; + first++; + } + second = first + val + 1; + } + // moves from right to left (equivalent moves are avoided) + else{ /* val > tmpSize */ + val = val - tmpSize; + step = size - 2; + second = 0; + while ((val - step) > 0){ + val = val - step; + step--; + second++; + } + first = second + val + 1; + } + } + + void print(){ + std::cout << key << ": [" << first << ", " << second << "] -> " << (*this).fitness() << std::endl; + } + +private: + unsigned int first; + unsigned int second; + unsigned int size; + +}; + +#endif diff --git a/trunk/paradiseo-mo/src/problems/permutation/moSwapNeighbor.h b/trunk/paradiseo-mo/src/problems/permutation/moSwapNeighbor.h new file mode 100644 index 000000000..7416294d1 --- /dev/null +++ b/trunk/paradiseo-mo/src/problems/permutation/moSwapNeighbor.h @@ -0,0 +1,90 @@ +/* + +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 _moSwapNeighbor_h +#define _moSwapNeighbor_h + +#include + + +/** + * Swap Neighbor + */ +template +class moSwapNeighbor: public moBackableNeighbor +{ +public: + + /** + * Apply the swap + * @param _solution the solution to move + */ + virtual void move(EOT& _solution){ + unsigned int tmp; + tmp=_solution[indices.first]; + _solution[indices.first]=_solution[indices.second]; + _solution[indices.second]=tmp; + _solution.invalidate(); + } + + /** + * apply the swap to restore the solution (use by moFullEvalByModif) + * @param _solution the solution to move + */ + virtual void moveBack(EOT& _solution){ + unsigned int tmp; + tmp=_solution[indices.first]; + _solution[indices.first]=_solution[indices.second]; + _solution[indices.second]=tmp; + _solution.invalidate(); + } + + /** + * Setter to fix the two indexes to swap + * @param _first first index + * @param _second second index + */ + void setIndices(unsigned int _first, unsigned int _second){ + indices.first = _first; + indices.second = _second; + } + + /** + * Print the Neighbor + */ + void print(){ + std::cout << "[" << indices.first << ", " << indices.second << "] -> " << (*this).fitness() << std::endl; + } + +private: + std::pair indices; + +}; + +#endif diff --git a/trunk/paradiseo-mo/src/problems/permutation/moSwapNeighborhood.h b/trunk/paradiseo-mo/src/problems/permutation/moSwapNeighborhood.h new file mode 100644 index 000000000..79410c50b --- /dev/null +++ b/trunk/paradiseo-mo/src/problems/permutation/moSwapNeighborhood.h @@ -0,0 +1,101 @@ +/* + +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 _moSwapNeighborhood_h +#define _moSwapNeighborhood_h + +#include +#include + +/** + * Swap Neighborhood + */ +template +class moSwapNeighborhood : public moNeighborhood > +{ +public: + typedef moSwapNeighbor Neighbor; + + /** + * @return if there are available Neighbor + */ + virtual bool hasNeighbor(EOT& _solution){ + return (_solution.size() > 1); + }; + + /** + * Initialization of the neighborhood + * @param _solution the solution to explore + * @param _current the first neighbor + */ + virtual void init(EOT& _solution, Neighbor& _current){ + indices.first=0; + indices.second=1; + size=_solution.size(); + _current.setIndices(0,1); + } + + /** + * Give the next neighbor + * @param _solution the solution to explore + * @param _current the next neighbor + */ + virtual void next(EOT& _solution, Neighbor& _current){ + if(indices.second==size-1){ + indices.first++; + indices.second=indices.first+1; + } + else + indices.second++; + _current.setIndices(indices.first, indices.second); + } + + /** + * Test if there is again a neighbor + * @param _solution the solution to explore + * @return if there is again a neighbor not explored + */ + virtual bool cont(EOT& _solution){ + return !((indices.first == (size-2)) && (indices.second == (size-1))); + } + + /** + * Return the class id. + * @return the class name as a std::string + */ + virtual std::string className() const { + return "moSwapNeighborhood"; + } + +private: + std::pair indices; + unsigned int size; +}; + +#endif diff --git a/trunk/paradiseo-mo/tutorial/Lesson2/CMakeLists.txt b/trunk/paradiseo-mo/tutorial/Lesson2/CMakeLists.txt index e69de29bb..593460073 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson2/CMakeLists.txt +++ b/trunk/paradiseo-mo/tutorial/Lesson2/CMakeLists.txt @@ -0,0 +1,9 @@ +INCLUDE_DIRECTORIES(${EO_SRC_DIR}/src + ${MO_SRC_DIR}/src + ${CMAKE_CURRENT_SOURCE_DIR}/../src) + +LINK_DIRECTORIES(${EO_BIN_DIR}/lib) + +ADD_EXECUTABLE(testNeighborhood testNeighborhood.cpp) + +TARGET_LINK_LIBRARIES(testNeighborhood eoutils ga eo) diff --git a/trunk/paradiseo-mo/tutorial/Lesson2/testNeighborhood.cpp b/trunk/paradiseo-mo/tutorial/Lesson2/testNeighborhood.cpp new file mode 100644 index 000000000..d5983c166 --- /dev/null +++ b/trunk/paradiseo-mo/tutorial/Lesson2/testNeighborhood.cpp @@ -0,0 +1,239 @@ +//----------------------------------------------------------------------------- +/** testNeighborhood.cpp + * + * JH - 09/04/10 + * + */ +//----------------------------------------------------------------------------- + +// standard includes +#define HAVE_SSTREAM + +#include // runtime_error +#include // cout +#include // ostrstream, istrstream +#include +#include + +// the general include for eo +#include +#include + +using namespace std; + +//---------------------------------------------------------------------------- + +//Representation and initializer +#include +#include + +// fitness function +#include +#include +#include + +//Neighbors and Neighborhoods +#include +#include +#include +#include +#include +#include + + +// Define types of the representation solution, different neighbors and neighborhoods +//----------------------------------------------------------------------------- +typedef eoInt Queen; //Permutation (Queen's problem representation) +typedef moSwapNeighbor swapNeighbor ; //swap Neighbor +typedef moShiftNeighbor shiftNeighbor; //shift Neighbor +typedef moSwapNeighborhood swapNeighborhood; //classical swap Neighborhood +typedef moOrderNeighborhood orderShiftNeighborhood; //order shift Neighborhood (Indexed) +typedef moRndWithoutReplNeighborhood rndWithoutReplShiftNeighborhood; //random without replacement shift Neighborhood (Indexed) +typedef moRndWithReplNeighborhood rndWithReplShiftNeighborhood; //random with replacement shift Neighborhood (Indexed) + +void main_function(int argc, char **argv) +{ + /* ========================================================= + * + * Parameters + * + * ========================================================= */ + + // First define a parser from the command-line arguments + eoParser parser(argc, argv); + + // For each parameter, define Parameter, read it through the parser, + // and assign the value to the variable + + eoValueParam seedParam(time(0), "seed", "Random number seed", 'S'); + parser.processParam( seedParam ); + unsigned seed = seedParam.value(); + + // description of genotype + eoValueParam vecSizeParam(8, "vecSize", "Genotype size", 'V'); + parser.processParam( vecSizeParam, "Representation" ); + unsigned vecSize = vecSizeParam.value(); + + // the name of the "status" file where all actual parameter values will be saved + string str_status = parser.ProgramName() + ".status"; // default value + eoValueParam statusParam(str_status.c_str(), "status", "Status file"); + parser.processParam( statusParam, "Persistence" ); + + // do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED + // i.e. in case you need parameters somewhere else, postpone these + if (parser.userNeedsHelp()) { + parser.printHelp(cout); + exit(1); + } + if (statusParam.value() != "") { + ofstream os(statusParam.value().c_str()); + os << parser;// and you can use that file as parameter file + } + + /* ========================================================= + * + * Random seed + * + * ========================================================= */ + + //reproducible random seed: if you don't change SEED above, + // you'll aways get the same result, NOT a random run + rng.reseed(seed); + + + /* ========================================================= + * + * Eval fitness function + * + * ========================================================= */ + + queenFullEval fullEval; + + + /* ========================================================= + * + * Initializer of the solution + * + * ========================================================= */ + + eoInitPermutation init(vecSize); + + + /* ========================================================= + * + * evaluation operators of a neighbor solution + * + * ========================================================= */ + + moFullEvalByModif swapEval(fullEval); + moFullEvalByCopy shiftEval(fullEval); + + + /* ========================================================= + * + * Neighbors and Neighborhoods + * + * ========================================================= */ + + swapNeighborhood swapNH; + orderShiftNeighborhood orderShiftNH(pow(vecSize-1, 2)); + rndWithoutReplShiftNeighborhood rndNoReplShiftNH(pow(vecSize-1, 2)); + rndWithReplShiftNeighborhood rndReplShiftNH(pow(vecSize-1, 2)); + + + /* ========================================================= + * + * Init and eval a Queen + * + * ========================================================= */ + + Queen solution; + + init(solution); + + fullEval(solution); + + std::cout << "Initial Solution:" << std::endl; + std::cout << solution << std::endl << std::endl; + + /* ========================================================= + * + * Use classical Neighbor and Neighborhood (swap) + * + * ========================================================= */ + + std::cout << "SWAP NEIGHBORHOOD" << std::endl; + std::cout << "-----------------" << std::endl; + std::cout << "Neighbors List: (Neighbor -> fitness)" << std::endl; + + swapNeighbor n1; + swapNH.init(solution, n1); + swapEval(solution,n1); + n1.print(); + while(swapNH.cont(solution)){ + swapNH.next(solution, n1); + swapEval(solution,n1); + n1.print(); + } + + /* ========================================================= + * + * Use indexed Neighborhood with shift operator + * + * ========================================================= */ + + std::cout << "\nSHIFT ORDER NEIGHBORHOOD" << std::endl; + std::cout << "------------------------" << std::endl; + std::cout << "Neighbors List: (key: Neighbor -> fitness)" << std::endl; + + shiftNeighbor n2; + + orderShiftNH.init(solution, n2); + shiftEval(solution,n2); + n2.print(); + while(orderShiftNH.cont(solution)){ + orderShiftNH.next(solution, n2); + shiftEval(solution,n2); + n2.print(); + } + + std::cout << "\nSHIFT RANDOM WITHOUT REPLACEMENT NEIGHBORHOOD" << std::endl; + std::cout << "---------------------------------------------" << std::endl; + std::cout << "Neighbors List: (key: Neighbor -> fitness)" << std::endl; + + rndNoReplShiftNH.init(solution, n2); + shiftEval(solution,n2); + n2.print(); + while(rndNoReplShiftNH.cont(solution)){ + rndNoReplShiftNH.next(solution, n2); + shiftEval(solution,n2); + n2.print(); + } + + std::cout << "\nSHIFT RANDOM WITH REPLACEMENT NEIGHBORHOOD" << std::endl; + std::cout << "---------------------------------------------" << std::endl; + std::cout << "Neighbors List: (key: Neighbor -> fitness)" << std::endl; + + rndReplShiftNH.init(solution, n2); + shiftEval(solution,n2); + n2.print(); + for(unsigned int i=0; i<100; i++){ + rndReplShiftNH.next(solution, n2); + shiftEval(solution,n2); + n2.print(); + } + +} + +// A main that catches the exceptions + +int main(int argc, char **argv) +{ + try { + main_function(argc, argv); + } + catch (exception& e) { + cout << "Exception: " << e.what() << '\n'; + } + return 1; +}