diff --git a/branches/newMo/CMakeLists.txt b/branches/newMo/CMakeLists.txt index 99a7232bd..9d16f1fa8 100644 --- a/branches/newMo/CMakeLists.txt +++ b/branches/newMo/CMakeLists.txt @@ -1,41 +1,108 @@ +###################################################################################### +### 0) Set your application properties +###################################################################################### +# check cmake version compatibility CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -########################################################################################################################################## -### 0) If you want to set your own variables in mo-conf.cmake and avoid the cmd line -########################################################################################################################################## +# Here define your project name +PROJECT(NEWMO) -INCLUDE(mo-conf.cmake OPTIONAL) - -########################################################################################################################################## +# Here define the name and the version of your package +SET(PACKAGE_NAME "VRP" CACHE STRING "Package name" FORCE) +SET(PACKAGE_VERSION "0" CACHE STRING "Package version" FORCE) -########################################################################################################################################## -### 1) Project properties -########################################################################################################################################## +# regular expression checking +INCLUDE_REGULAR_EXPRESSION("^.*$" "^$") -# set the project name -PROJECT(ParadisEO-MO) +# set a language for the entire project. +ENABLE_LANGUAGE(CXX) +ENABLE_LANGUAGE(C) -SET(PACKAGE_BUGREPORT "paradiseo-help@lists.gforge.inria.fr" CACHE STRING "Package bug report" FORCE) -SET(PACKAGE_NAME "ParadisEO-MO - Moving Objects" CACHE STRING "Package name" FORCE) -SET(PACKAGE_STRING "ParadisEO-MO 1.2" CACHE STRING "MO Package string full name" FORCE) -SET(PACKAGE_VERSION "1.2" CACHE STRING "Package version" FORCE) -SET(GLOBAL_VERSION "1.2" CACHE STRING "Global version" FORCE) -SET(VERSION "1.2" CACHE STRING "Version" FORCE) -########################################################################################################################################## +###################################################################################### +###################################################################################### +### 1) Include the install configuration file where are defined the main variables +###################################################################################### -########################################################################################################################################## -### 2) Include the common CMake configuration -########################################################################################################################################## +INCLUDE(${CMAKE_SOURCE_DIR}/install.cmake) -# The "config" variable must be provided on the command line -IF(NOT DEFINED config OR NOT config) - MESSAGE(FATAL_ERROR "The \"config\" variable must be set on the command line to - give the path of the install configuration file. ") -ENDIF(NOT DEFINED config OR NOT config) +###################################################################################### -# Need the config file whose full path is given thanks to the "config" variable -INCLUDE(${config}) -########################################################################################################################################## +###################################################################################### +### 2) Include the sources +###################################################################################### +###################################### +### Include required modules & utilities +##################################################################################### +INCLUDE(CMakeBackwardCompatibilityCXX) + +INCLUDE(FindDoxygen) + +INCLUDE(CheckLibraryExists) + +INCLUDE(Dart OPTIONAL) + +# Set a special flag if the environment is windows (should do the same in a config.g file) +IF (WIN32) + ADD_DEFINITIONS(-D_WINDOWS=1) +ENDIF (WIN32) + +###################################################################################### + +##################################################################################### +### Manage the build type +##################################################################################### + +# the user should choose the build type on windows environments,excepted under cygwin (default=none) +SET(CMAKE_DEFAULT_BUILD_TYPE "Release" CACHE STRING "Variable that stores the default CMake build type" FORCE) + +FIND_PROGRAM(MEMORYCHECK_COMMAND + NAMES purify valgrind + PATHS + "/usr/local/bin /usr/bin [HKEY_LOCAL_MACHINE\\SOFTWARE\\Rational Software\\Purify\\Setup;InstallFolder]" + DOC "Path to the memory checking command, used for memory error detection.") + +IF(NOT CMAKE_BUILD_TYPE) + SET( CMAKE_BUILD_TYPE + ${CMAKE_DEFAULT_BUILD_TYPE} CACHE STRING + "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." + FORCE) +ENDIF(NOT CMAKE_BUILD_TYPE) + +IF(WIN32 AND NOT CYGWIN) + IF(CMAKE_CXX_COMPILER MATCHES cl) + IF(NOT WITH_SHARED_LIBS) + IF(CMAKE_GENERATOR STREQUAL "Visual Studio 8 2005" OR CMAKE_GENERATOR STREQUAL "Visual Studio 9 2008") + SET(CMAKE_CXX_FLAGS "/nologo /Gy") + SET(CMAKE_CXX_FLAGS_DEBUG "/W3 /MTd /Z7 /Od") + SET(CMAKE_CXX_FLAGS_RELEASE "/w /MT /O2 /wd4530") + SET(CMAKE_CXX_FLAGS_MINSIZEREL "/MT /O2") + SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MTd /Z7 /Od") + SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:CONSOLE") + ENDIF(CMAKE_GENERATOR STREQUAL "Visual Studio 8 2005" OR CMAKE_GENERATOR STREQUAL "Visual Studio 9 2008") + ENDIF(NOT WITH_SHARED_LIBS) + ENDIF(CMAKE_CXX_COMPILER MATCHES cl) +ELSE(WIN32 AND NOT CYGWIN) + IF(CMAKE_COMPILER_IS_GNUCXX) + SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g -fprofile-arcs -ftest-coverage -Wall -Wextra -Wno-unused-parameter") + SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2") + SET(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -O6") + ENDIF(CMAKE_COMPILER_IS_GNUCXX) +ENDIF(WIN32 AND NOT CYGWIN) + +IF(CMAKE_BUILD_TYPE MATCHES Debug) + ADD_DEFINITIONS(-DCMAKE_VERBOSE_MAKEFILE=ON) +ENDIF(CMAKE_BUILD_TYPE MATCHES Debug) + +###################################################################################### +### 3) Link the librairies for your executable +###################################################################################### + +ADD_SUBDIRECTORY(doc) +ADD_SUBDIRECTORY(src) +ADD_SUBDIRECTORY(test) +ADD_SUBDIRECTORY(tutorial) + +###################################################################################### diff --git a/branches/newMo/install.cmake b/branches/newMo/install.cmake new file mode 100644 index 000000000..fbc8f81d6 --- /dev/null +++ b/branches/newMo/install.cmake @@ -0,0 +1,19 @@ +######################################################################################################### +# 1) ParadisEO install: SIMPLE Configuration +######################################################################################################### + +# Here, just specify PARADISEO_DIR : the directory where ParadisEO has been installed +SET(PARADISEO_DIR "/home/humeau/paradiseo-1.2.1" CACHE PATH "ParadisEO directory" FORCE) + +######################################################################################################### + + + +######################################################################################################### +# 2) ParadisEO install: ADVANCED Configuration +######################################################################################################### + +SET(PARADISEO_EO_SRC_DIR "${PARADISEO_DIR}/paradiseo-eo" CACHE PATH "ParadisEO-EO source directory" FORCE) +SET(PARADISEO_EO_BIN_DIR "${PARADISEO_DIR}/paradiseo-eo/build" CACHE PATH "ParadisEO-EO binary directory" FORCE) + + diff --git a/branches/newMo/src/algo/moLocalSearch.h b/branches/newMo/src/algo/moLocalSearch.h new file mode 100644 index 000000000..df34747c3 --- /dev/null +++ b/branches/newMo/src/algo/moLocalSearch.h @@ -0,0 +1,64 @@ +#ifndef _moLocalSearch_h +#define _moLocalSearch_h + +#include +#include + +/* + the main algorithm of the local search +*/ +template< class NHE , class C > +class moLocalSearch: public eoMonOp +{ +public: + typedef NHE NeighborhoodExplorer ; + typedef C Continuator ; + typedef typename NeighborhoodExplorer::EOT EOT ; + + moLocalSearch(NeighborhoodExplorer & __searchExpl, Continuator & __continuator) : searchExplorer(__searchExpl), continuator(__continuator) { } ; + + virtual bool operator() (EOT & solution) { + + // initialization of the external continuator (for example the time, or the number of generations) + continuator.init(solution); + + // initialization of the parameter of the search (for example fill empty the tabu list) + searchExplorer.initParam(solution); + + unsigned num = 0; + + do { + // explore the neighborhood of the solution + searchExplorer(solution); + + // if a solution in the neighborhood can be accepted + if (searchExplorer.accept(solution)) + searchExplorer.move(solution); + + // update the parameter of the search (for ex. Temperature of the SA) + searchExplorer.updateParam(solution); + + std::cout << num << " : " << solution << std::endl ; + num++; + } while (continuator(solution) && searchExplorer.isContinue(solution)); + + }; + +private: + // make the exploration of the neighborhood according to a local search heuristic + NeighborhoodExplorer & searchExplorer ; + + // external continuator + Continuator & continuator ; +}; + +#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 new file mode 100644 index 000000000..a7ae4f69b --- /dev/null +++ b/branches/newMo/src/comparator/moNeighborComparator.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/continuator/moContinuator.h b/branches/newMo/src/continuator/moContinuator.h new file mode 100644 index 000000000..923219095 --- /dev/null +++ b/branches/newMo/src/continuator/moContinuator.h @@ -0,0 +1,29 @@ +#ifndef _moContinuator_h +#define _moContinuator_h + +/* + to make specific continuator from a solution +*/ +template< class NH > +class moContinuator : public eoUF +{ +public: + typedef NH Neighborhood ; + typedef typename Neighborhood::EOT EOT ; + + // empty constructor + moContinuator() { } ; + + virtual void init(EOT & solution) = 0 ; +}; + +#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/continuator/moTrueContinuator.h b/branches/newMo/src/continuator/moTrueContinuator.h new file mode 100644 index 000000000..6c48435a1 --- /dev/null +++ b/branches/newMo/src/continuator/moTrueContinuator.h @@ -0,0 +1,42 @@ +#ifndef _moTrueContinuator_h +#define _moTrueContinuator_h + +#include + +/* + to make specific continuator from a solution +*/ +template< class NH > +class moTrueContinuator : public moContinuator +{ +public: + typedef NH Neighborhood ; + typedef typename Neighborhood::EOT EOT ; + + // empty constructor + moTrueContinuator() { i=0;} ; + + /* + always true + */ + virtual bool operator()(EOT & solution) { + i++; + return i<10; + }; + + virtual void init(EOT & solution) { + } + + int i; +}; + +#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/explorer/moNeighborhoodExplorer.h b/branches/newMo/src/explorer/moNeighborhoodExplorer.h new file mode 100644 index 000000000..a6ea12700 --- /dev/null +++ b/branches/newMo/src/explorer/moNeighborhoodExplorer.h @@ -0,0 +1,43 @@ +#ifndef _neighborhoodExplorer_h +#define _neighborhoodExplorer_h + +#include +/* + explore the neighborhood +*/ +template< class NH > +class moNeighborhoodExplorer : public eoUF +{ +public: + typedef NH Neighborhood ; + typedef typename Neighborhood::EOT EOT ; + + // empty constructor + moNeighborhoodExplorer() { } ; + + virtual void initParam (EOT & solution) = 0 ; + + virtual void updateParam (EOT & solution) = 0 ; + + virtual bool isContinue(EOT & solution) = 0 ; + + virtual void move(EOT & solution) = 0 ; + + virtual bool accept(EOT & solution) = 0 ; + + /** Return the class id. + * @return the class name as a std::string + */ + virtual std::string className() const { return "moNeighborhoodExplorer"; } +}; + +#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/explorer/moSimpleHCexplorer.h b/branches/newMo/src/explorer/moSimpleHCexplorer.h new file mode 100644 index 000000000..c570df181 --- /dev/null +++ b/branches/newMo/src/explorer/moSimpleHCexplorer.h @@ -0,0 +1,91 @@ +#ifndef _moSimpleHCexplorer_h +#define _moSimpleHCexplorer_h + +#include + +template< class NH > +class moSimpleHCexplorer : public moNeighborhoodExplorer +{ +public: + typedef NH Neighborhood ; + typedef typename Neighborhood::EOT EOT ; + typedef typename Neighborhood::Neighbor Neighbor ; + + // empty constructor + moSimpleHCexplorer(Neighborhood & __neighborhood) : neighborhood(__neighborhood) { + isAccept = false; + } + + virtual void initParam (EOT & solution) { } ; + + virtual void updateParam (EOT & solution) { } ; + + virtual void operator() (EOT & solution) { + +//est qu'on peut initializer + + if(neighborhood.hasNeighbor(solution)){ + neighborhood.init(solution, current); + + current.eval(solution); + + best = current; + + while (neighborhood.cont(solution)) { + neighborhood.next(solution, current); + + current.eval(solution); + + if (current.betterThan(best)) { + + best = current; + + } + } + } + else{ + isAccept=false; + } + }; + + virtual bool isContinue(EOT & solution) { + return isAccept ; + }; + + virtual void move(EOT & solution) { + + best.move(solution); + + solution.fitness(best.fitness()); + }; + + virtual bool accept(EOT & solution) { + if(neighborhood.hasNeighbor(solution)){ + isAccept = (solution.fitness() < best.fitness()) ; + } + return isAccept; + }; + +private: + Neighborhood & neighborhood; + +// attention il faut que le constructeur vide existe + Neighbor best ; + + Neighbor current ; + + // true if the move is accepted + bool isAccept ; +}; + + +#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/move/moMove.h b/branches/newMo/src/move/moMove.h new file mode 100644 index 000000000..d24832db3 --- /dev/null +++ b/branches/newMo/src/move/moMove.h @@ -0,0 +1,57 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008 + (C) OPAC Team, LIFL, 2002-2008 + + Sébastien Cahon, Jean-Charles Boisson (Jean-Charles.Boisson@lifl.fr) + + 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 + +*/ + +#ifndef _moMove_h +#define _moMove_h + +#include + +//! Definition of a move. + +/*! + A move transforms a solution to another close solution. + It describes how a solution can be modified to another one. +*/ +template < class EOT > +class moMove:public eoUF < EOT &, void > +{ + public: + + //! Alias for the type + typedef EOT EOType; +}; + +#endif diff --git a/branches/newMo/src/move/moMoveIncrEval.h b/branches/newMo/src/move/moMoveIncrEval.h new file mode 100644 index 000000000..af1032dcd --- /dev/null +++ b/branches/newMo/src/move/moMoveIncrEval.h @@ -0,0 +1,52 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008 + (C) OPAC Team, LIFL, 2002-2008 + + Sébastien Cahon, Jean-Charles Boisson (Jean-Charles.Boisson@lifl.fr) + + 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 +*/ + +#ifndef _moMoveIncrEval_h +#define _moMoveIncrEval_h + +#include + +//! (generally) Efficient evaluation function based a move and a solution. + +/*! + From a move and a solution, it computes + a new fitness that could be associated to + the solution if this one is updated. +*/ +template < class M, class Objective = typename M::EOType::Fitness> +class moMoveIncrEval:public eoBF < const M &, const typename M::EOType &, Objective > +{}; + +#endif diff --git a/branches/newMo/src/mo.h b/branches/newMo/src/move/moMoveInit.h old mode 100755 new mode 100644 similarity index 78% rename from branches/newMo/src/mo.h rename to branches/newMo/src/move/moMoveInit.h index 5d34d22c7..bd521d90a --- a/branches/newMo/src/mo.h +++ b/branches/newMo/src/move/moMoveInit.h @@ -1,21 +1,21 @@ /* - - Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007 - (C) OPAC Team, LIFL, 2002-2007 - + + Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008 + (C) OPAC Team, LIFL, 2002-2008 + Sébastien Cahon, Jean-Charles Boisson (Jean-Charles.Boisson@lifl.fr) - + 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, @@ -28,14 +28,23 @@ 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 _mo_h -#define _mo_h +#ifndef _moMoveInit_h +#define _moMoveInit_h -#include +#include + +//! Move (moMove) initializer +/*! + Class which allows to initiase a move. + Only a description... An object that herits from this class needs to be designed to be used. +*/ +template < class M > +class moMoveInit:public eoBF < M &, const typename M::EOType &, void > +{}; #endif diff --git a/branches/newMo/src/move/moNextMove.h b/branches/newMo/src/move/moNextMove.h new file mode 100644 index 000000000..5a067039c --- /dev/null +++ b/branches/newMo/src/move/moNextMove.h @@ -0,0 +1,50 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008 + (C) OPAC Team, LIFL, 2002-2008 + + Sébastien Cahon, Jean-Charles Boisson (Jean-Charles.Boisson@lifl.fr) + + 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 +*/ + +#ifndef _moNextMove_h +#define _moNextMove_h + +#include + +//! Class which allows to generate a new move (moMove). +/*! + Useful for the explorer (for moTS or moHC). + Does nothing... An object that herits from this class needs to be designed for being used. +*/ +template < class M > +class moNextMove:public eoBF < M &, const typename M::EOType &, bool > +{}; + +#endif diff --git a/branches/newMo/src/neighborhood/moBitNeighbor.h b/branches/newMo/src/neighborhood/moBitNeighbor.h new file mode 100644 index 000000000..263b81bff --- /dev/null +++ b/branches/newMo/src/neighborhood/moBitNeighbor.h @@ -0,0 +1,107 @@ +#ifndef _bitNeighbor_h +#define _bitNeighbor_h + +#include +#include + +/* + contener of the neighbor information +*/ +template< class Fitness > +class moBitNeighbor : public moNeighbor< eoBit , Fitness> +{ +public: + typedef eoBit EOType ; + + using moNeighbor< eoBit , Fitness>::fitness; + + // describe the neighbor + unsigned bit ; + + // empty constructor needed + moBitNeighbor() : moNeighbor , Fitness>() { } ; + + // copy constructor + moBitNeighbor(const moBitNeighbor & n) : moNeighbor , Fitness>(n) { + this->bit = n.bit ; + } ; + + moBitNeighbor(unsigned b) : moNeighbor , Fitness>() , bit(b) { } ; + + /* + * operator of assignment + */ + virtual moBitNeighbor & operator=(const moBitNeighbor & source) { + moNeighbor::operator=(source); + + this->bit = source.bit ; + + return *this ; + } + + /* + move the solution + */ + virtual void move(EOType & solution) { + solution[bit] = solution[bit]?false:true ; + } + + // by default: if the fitness of the current solution is stricly higher than the other neighbor + virtual bool betterThan(const moNeighbor & __neighbor) { + return (this->fitness() > __neighbor.fitness()) ; + }; + + /** Return the class id. + * @return the class name as a std::string + */ + virtual std::string className() const { return "moBitNeighbor"; } + + /** + * 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; + + if (fitness_str == "INVALID") + { + throw std::runtime_error("invalid fitness"); + } + else + { + Fitness repFit ; + _is.seekg(pos); // rewind + _is >> repFit; + _is >> bit; + + fitness(repFit); + } + } + + /** + * 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 << fitness() << ' ' << bit << ' ' ; + } + + +}; + +#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/moBitNeighborhood.h b/branches/newMo/src/neighborhood/moBitNeighborhood.h new file mode 100644 index 000000000..d0c026fa3 --- /dev/null +++ b/branches/newMo/src/neighborhood/moBitNeighborhood.h @@ -0,0 +1,60 @@ +#ifndef _bitNeighborhood_h +#define _bitNeighborhood_h + +#include + +template< class N > +class moBitNeighborhood : public moNeighborhood +{ +public: + typedef N Neighbor ; + typedef typename Neighbor::EOType EOT ; + + moBitNeighborhood() : moNeighborhood() { } + + virtual bool hasNeighbor(EOT & solution) { + + return true; + + } + + /* + initialisation of the neighborhood + */ + virtual void init(EOT & solution, Neighbor & _neighbor) { + currentBit = 0 ; + + _neighbor.bit = currentBit ; + } + + /* + Give the next neighbor + */ + virtual void next(EOT & solution, Neighbor & neighbor) { + currentBit++ ; + + neighbor.bit = currentBit ; + } + + /* + if false, there is no neighbor left to explore + */ + virtual bool cont(EOT & solution) { + return (currentBit < solution.size()) ; + } + +private: + unsigned currentBit; +}; + + +#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/moEmptyNeighbor.h b/branches/newMo/src/neighborhood/moEmptyNeighbor.h new file mode 100644 index 000000000..19b553db6 --- /dev/null +++ b/branches/newMo/src/neighborhood/moEmptyNeighbor.h @@ -0,0 +1,41 @@ +#ifndef _emptyNeighbor_h +#define _emptyNeighbor_h + +#include + +/* + contener of the neighbor information +*/ +template< class EOT , class Fitness > +class moEmptyNeighbor : public moNeighbor +{ +public: + typedef EOT EOType ; + + // empty constructor + moEmptyNeighbor() : moNeighbor() { } ; + + /* + make the evaluation of the current neighbor and update the information on this neighbor + */ + virtual void eval(EOT & solution) { } + + /* + move the solution + */ + virtual void move(EOT & solution) { } + + // true if the this is better than the neighbor __neighbor + virtual bool betterThan(moNeighbor & __neighbor) { return true; } +}; + +#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/moFullEvalBitNeighbor.h b/branches/newMo/src/neighborhood/moFullEvalBitNeighbor.h new file mode 100644 index 000000000..96a260f59 --- /dev/null +++ b/branches/newMo/src/neighborhood/moFullEvalBitNeighbor.h @@ -0,0 +1,64 @@ +#ifndef _fullEvalBitNeighbor_h +#define _fullEvalBitNeighbor_h + +#include +#include + +/* + contener of the neighbor information +*/ +template< class Fitness > +class moFullEvalBitNeighbor : public moBitNeighbor +{ +public: + typedef eoBit EOType ; + + using moBitNeighbor::bit ; + + // empty constructor needed + moFullEvalBitNeighbor() : moBitNeighbor() { } ; + + moFullEvalBitNeighbor(unsigned b) : moBitNeighbor(bit) { } ; + + /* + make the evaluation of the current neighbor and update the information on this neighbor + */ + virtual void eval(EOType & solution) { + Fitness fit = solution.fitness(); + + solution[bit] = solution[bit]?false:true ; + + (*fullEval)(solution); + + fitness(solution.fitness()); + + solution[bit] = solution[bit]?false:true ; + + solution.fitness(fit); + }; + + static void setFullEvalFunc(eoEvalFunc & eval) { + fullEval = & eval ; + } + + static eoEvalFunc * fullEval ; + + /** Return the class id. + * @return the class name as a std::string + */ + virtual std::string className() const { return "moFullEvalBitNeighbor"; } +}; + +template +eoEvalFunc< eoBit > * moFullEvalBitNeighbor::fullEval = NULL ; + +#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/moFullEvalNeighbor.h b/branches/newMo/src/neighborhood/moFullEvalNeighbor.h new file mode 100644 index 000000000..40d605525 --- /dev/null +++ b/branches/newMo/src/neighborhood/moFullEvalNeighbor.h @@ -0,0 +1,46 @@ +#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 new file mode 100644 index 000000000..57b6fb744 --- /dev/null +++ b/branches/newMo/src/neighborhood/moMoveNeighbor.h @@ -0,0 +1,108 @@ +#ifndef _moMoveNeighbor_h +#define _moMoveNeighbor_h + +#include + +#include +#include +#include + + +/* + contener of the neighbor informations +*/ +template< class M , class Fitness > + class moMoveNeighbor : public moNeighbor +{ +public: + + typedef typename M::EOType EOT; + + // empty constructor + moMoveNeighbor() {_move=new M();}; + + ~moMoveNeighbor() {delete _move;}; + + // copy constructeur + moMoveNeighbor(const moMoveNeighbor & _n) { + moNeighbor::operator=(_n); + (*_move) = *(_n._move); + } + + // assignment operator + virtual moMoveNeighbor & operator=(const moMoveNeighbor & _n) { + moNeighbor ::operator=(_n); + (*_move) = *(_n._move); + + std::cout << moNeighbor::fitness() << " , " << _n.fitness() << std::endl; + 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); + } + + /** Return the class id. + * @return the class name as a std::string + */ + virtual std::string className() const { return "moMoveNeighbor"; } + + static void setIncrEval(moMoveIncrEval& increm) { + _incrEval = & increm ; + } + + /** + * 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; + + 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; + +}; + +template< class M , class Fitness > + moMoveIncrEval * moMoveNeighbor::_incrEval = NULL; +#endif diff --git a/branches/newMo/src/neighborhood/moMoveNeighborhood.h b/branches/newMo/src/neighborhood/moMoveNeighborhood.h new file mode 100644 index 000000000..b66b925a2 --- /dev/null +++ b/branches/newMo/src/neighborhood/moMoveNeighborhood.h @@ -0,0 +1,59 @@ +#ifndef _moMoveNeighborhood_h +#define _moMoveNeighborhood_h + +#include +#include + +#include +#include + +template< class M, class Fitness > + class moMoveNeighborhood : public moNeighborhood > +{ +public: + + typedef moMoveNeighbor Neighbor; + typedef typename M::EOType EOT; + + moMoveNeighborhood(moMoveInit& i, moNextMove& n):_init(i), _next(n), isContinue(true) {} + + virtual bool hasNeighbor(EOT & solution){ + return true; + } + + /* + initialisation of the neighborhood + */ + virtual void init(EOT & solution, Neighbor & current){ + _init(*(current._move), solution); + isContinue=true; + } + + /* + Give the next neighbor + */ + virtual void next(EOT & solution, Neighbor & current){ + isContinue=_next(*(current._move), solution); + } + + /* + if false, there is no neighbor left to explore + */ + virtual bool cont(EOT & solution){ + return isContinue; + } + + /** Return the class id. + * @return the class name as a std::string + */ + virtual std::string className() const { return "moMoveNeighborhood"; } + + private: + moMoveInit& _init; + moNextMove& _next; + bool isContinue; + +}; + + +#endif diff --git a/branches/newMo/src/neighborhood/moNeighbor.h b/branches/newMo/src/neighborhood/moNeighbor.h new file mode 100644 index 000000000..99a813e3e --- /dev/null +++ b/branches/newMo/src/neighborhood/moNeighbor.h @@ -0,0 +1,135 @@ +#ifndef _moNeighbor_h +#define _moNeighbor_h + +#include + +#include + +/* + contener of the neighbor informations +*/ +template< class EOT , class Fitness > +class moNeighbor : public eoObject, public eoPersistent +{ +public: + typedef EOT EOType ; + + // empty constructor + moNeighbor() { } ; + + // copy constructeur + moNeighbor(const moNeighbor & _n) { + repFitness = _n.fitness(); + } + + // assignment operator + virtual moNeighbor & operator=(const moNeighbor & _n) { + repFitness = _n.fitness(); + + 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; + } + + /// 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; + } + + /** Set fitness. At the same time, validates it. + * @param _fitness New fitness value. + */ + void fitness(const Fitness& _fitness) + { + repFitness = _fitness; + } + + /** Return the class id. + * @return the class name as a std::string + */ + virtual std::string className() const { return "moNeighbor"; } + + /** + * 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; + + 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 << ' ' ; + } + + 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 new file mode 100644 index 000000000..d42f1fd67 --- /dev/null +++ b/branches/newMo/src/neighborhood/moNeighborhood.h @@ -0,0 +1,46 @@ +#ifndef _moNeighborhood_h +#define _moNeighborhood_h + +template< class Neigh > +class moNeighborhood : public eoObject +{ +public: + typedef Neigh Neighbor; + typedef typename Neighbor::EOType EOT; + + moNeighborhood() { } + + virtual bool hasNeighbor(EOT & solution) = 0 ; + + /* + initialisation of the neighborhood + */ + virtual void init(EOT & solution, Neighbor & current) = 0 ; + + /* + Give the next neighbor + */ + virtual void next(EOT & solution, Neighbor & current) = 0 ; + + /* + if false, there is no neighbor left to explore + */ + virtual bool cont(EOT & solution) = 0 ; + + /** Return the class id. + * @return the class name as a std::string + */ + 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: diff --git a/branches/newMo/src/mo b/branches/newMo/src/newmo similarity index 87% rename from branches/newMo/src/mo rename to branches/newMo/src/newmo index d0319b60a..99b3af237 100755 --- a/branches/newMo/src/mo +++ b/branches/newMo/src/newmo @@ -1,10 +1,7 @@ -/* +/* - Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2008 - (C) OPAC Team, LIFL, 2002-2008 - - Sébastien Cahon, Jean-Charles Boisson (Jean-Charles.Boisson@lifl.fr) - + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + 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 @@ -33,9 +30,10 @@ Contact: paradiseo-help@lists.gforge.inria.fr */ -#ifndef __mo -#define __mo -#include "mo.h" +#ifndef __newmo +#define __newmo + +#include "newmo.h" #endif diff --git a/branches/newMo/src/newmo.h b/branches/newMo/src/newmo.h new file mode 100755 index 000000000..2aa9b165a --- /dev/null +++ b/branches/newMo/src/newmo.h @@ -0,0 +1,61 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + 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 +*/ + +#ifndef _newmo_h +#define _newmo_h + +#include + +#include + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#endif diff --git a/branches/newMo/tutorial/CMakeLists.txt b/branches/newMo/tutorial/CMakeLists.txt new file mode 100644 index 000000000..2fc13942e --- /dev/null +++ b/branches/newMo/tutorial/CMakeLists.txt @@ -0,0 +1 @@ +ADD_SUBDIRECTORY(oneMax) \ No newline at end of file diff --git a/branches/newMo/tutorial/oneMax/CMakeLists.txt b/branches/newMo/tutorial/oneMax/CMakeLists.txt new file mode 100644 index 000000000..8bf58768d --- /dev/null +++ b/branches/newMo/tutorial/oneMax/CMakeLists.txt @@ -0,0 +1 @@ +ADD_SUBDIRECTORY(application) \ No newline at end of file diff --git a/branches/newMo/tutorial/oneMax/application/CMakeLists.txt b/branches/newMo/tutorial/oneMax/application/CMakeLists.txt new file mode 100644 index 000000000..5d1511768 --- /dev/null +++ b/branches/newMo/tutorial/oneMax/application/CMakeLists.txt @@ -0,0 +1,11 @@ +INCLUDE_DIRECTORIES(${PARADISEO_EO_SRC_DIR}/src + ${NEWMO_SRC_DIR}/src + ${CMAKE_CURRENT_SOURCE_DIR}/../src) + +LINK_DIRECTORIES(${PARADISEO_EO_BIN_DIR}/lib) + +ADD_EXECUTABLE(testSimpleHC testSimpleHC.cpp) +ADD_EXECUTABLE(testWithMove testWithMove.cpp) + +TARGET_LINK_LIBRARIES(testSimpleHC eoutils ga eo) +TARGET_LINK_LIBRARIES(testWithMove eoutils ga eo) \ No newline at end of file diff --git a/branches/newMo/tutorial/oneMax/application/testSimpleHC.cpp b/branches/newMo/tutorial/oneMax/application/testSimpleHC.cpp new file mode 100644 index 000000000..f87114db0 --- /dev/null +++ b/branches/newMo/tutorial/oneMax/application/testSimpleHC.cpp @@ -0,0 +1,203 @@ +//----------------------------------------------------------------------------- +/** testSimpleHC.cpp + * + * SV - 12/01/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; + +//----------------------------------------------------------------------------- +// fitness function +#include + +#include +#include + +#include + +// local search algorithm +#include + +// the simple HC explorer +#include + +// explore the neighborhood of a bit string in order +#include + +#include + +#include + +// REPRESENTATION +//----------------------------------------------------------------------------- +// define your individuals +typedef eoBit Indi; +//typedef OneMaxBitNeighbor Neighbor ; // incremental evaluation +typedef moFullEvalBitNeighbor Neighbor ; // full evaluation +typedef moBitNeighborhood Neighborhood ; + +// GENERAL +//----------------------------------------------------------------------------- + +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(); + + string fileOut("out.dat"); + eoValueParam fileStatParam(fileOut.c_str(), "out", "A file to export results", 'o'); + parser.processParam( fileStatParam, "Persistence" ); + fileOut = fileStatParam.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 + * + * ========================================================= */ + + FuncOneMax eval(vecSize); + + /* ========================================================= + * + * Initilisation of the solution + * + * ========================================================= */ + + // a Indi random initializer + eoUniformGenerator uGen; + eoInitFixedLength random(vecSize, uGen); + + /* ========================================================= + * + * evaluation of a neighbor solution + * + * ========================================================= */ + + // no need if incremental evaluation with OneMaxBitNeighbor + Neighbor::setFullEvalFunc(eval); + + /* ========================================================= + * + * the neighborhood of a solution + * + * ========================================================= */ + + Neighborhood neighborhood; + + /* ========================================================= + * + * a neighborhood explorator solution + * + * ========================================================= */ + + moSimpleHCexplorer explorer(neighborhood); + + /* ========================================================= + * + * the local search algorithm + * + * ========================================================= */ + + moTrueContinuator continuator; + + moLocalSearch< moSimpleHCexplorer, moTrueContinuator > localSearch(explorer, continuator); + + /* ========================================================= + * + * execute the local search from random sollution + * + * ========================================================= */ + + Indi solution; + + random(solution); + + eval(solution); + + std::cout << "initial: " << solution << std::endl ; + + localSearch(solution); + + std::cout << "final: " << solution << std::endl ; + +} + +// 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; +} diff --git a/branches/newMo/tutorial/oneMax/application/testWithMove.cpp b/branches/newMo/tutorial/oneMax/application/testWithMove.cpp new file mode 100644 index 000000000..441666d98 --- /dev/null +++ b/branches/newMo/tutorial/oneMax/application/testWithMove.cpp @@ -0,0 +1,214 @@ +//----------------------------------------------------------------------------- +/** testSimpleHC.cpp + * + * SV - 12/01/10 + * + */ +//----------------------------------------------------------------------------- + +// standard includes +#define HAVE_SSTREAM + +#include // runtime_error +#include // cout +#include // ostrstream, istrstream +#include +#include + +// the general include for eo +#include +#include "ga.h" + +using namespace std; + +//----------------------------------------------------------------------------- +// fitness function +#include + +//#include +//#include + +#include + +// local search algorithm +#include + +// the simple HC explorer +#include + +// explore the neighborhood of a bit string in order +#include + +//#include + +#include + +#include +#include +#include +#include + +// REPRESENTATION +//----------------------------------------------------------------------------- +// define your individuals +typedef eoBit Indi; +//typedef OneMaxBitNeighbor Neighbor ; // incremental evaluation +typedef moMoveNeighbor, unsigned> Neighbor ; // full evaluation +typedef moMoveNeighborhood, unsigned> Neighborhood ; + +// GENERAL +//----------------------------------------------------------------------------- + +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(); + + string fileOut("out.dat"); + eoValueParam fileStatParam(fileOut.c_str(), "out", "A file to export results", 'o'); + parser.processParam( fileStatParam, "Persistence" ); + fileOut = fileStatParam.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 + * + * ========================================================= */ + + FuncOneMax eval(vecSize); + + OneMaxIncrEval incrEval; + Neighbor::setIncrEval(incrEval); + + /* ========================================================= + * + * Initilisation of the solution + * + * ========================================================= */ + + // a Indi random initializer + eoUniformGenerator uGen; + eoInitFixedLength random(vecSize, uGen); + + /* ========================================================= + * + * evaluation of a neighbor solution + * + * ========================================================= */ + + // no need if incremental evaluation with OneMaxBitNeighbor + // Neighbor::setFullEvalFunc(eval); + + /* ========================================================= + * + * the neighborhood of a solution + * + * ========================================================= */ + BitMove_init init; + BitMove_next next; + + + Neighborhood neighborhood(init, next); + + /* ========================================================= + * + * a neighborhood explorator solution + * + * ========================================================= */ + + moSimpleHCexplorer explorer(neighborhood); + + /* ========================================================= + * + * the local search algorithm + * + * ========================================================= */ + + moTrueContinuator continuator; + + moLocalSearch< moSimpleHCexplorer, moTrueContinuator > localSearch(explorer, continuator); + + /* ========================================================= + * + * execute the local search from random sollution + * + * ========================================================= */ + + Indi solution; + + random(solution); + + eval(solution); + + std::cout << "initial: " << solution << std::endl ; + + localSearch(solution); + + std::cout << "final: " << solution << std::endl ; + +} + +// 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; +} diff --git a/branches/newMo/tutorial/oneMax/src/bitMove.h b/branches/newMo/tutorial/oneMax/src/bitMove.h new file mode 100644 index 000000000..17415ebda --- /dev/null +++ b/branches/newMo/tutorial/oneMax/src/bitMove.h @@ -0,0 +1,34 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +// "bitMove.h" + +#ifndef __bitMove +#define __bitMove + +#include +#include + +template +class BitMove : public moMove { + +public : + +typedef EOT EOType; + + unsigned bit; + + BitMove() { + bit = 0; + } + + BitMove(unsigned _bit) : bit(_bit) { } + + + void operator () (EOT & chrom) + { + chrom[bit] = !chrom[bit]; + }; + +} ; + +#endif diff --git a/branches/newMo/tutorial/oneMax/src/bitMoveIncrEval.h b/branches/newMo/tutorial/oneMax/src/bitMoveIncrEval.h new file mode 100644 index 000000000..fce128de5 --- /dev/null +++ b/branches/newMo/tutorial/oneMax/src/bitMoveIncrEval.h @@ -0,0 +1,27 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +// "incrEval_funcNK.h" + +#ifndef __incr_eval_funcNK_h +#define __incr_eval_funcNK_h + +#include +#include "bitMove.h" + +template +class OneMaxIncrEval : public moMoveIncrEval < BitMove > { + +public : + OneMaxIncrEval(){ }; + + typename EOT::Fitness operator () (const BitMove & move, const EOT & chrom) { + if(chrom[move.bit]==0){ + return chrom.fitness()+1; +} + else{ + return chrom.fitness()-1; +} +}; +}; + +#endif diff --git a/branches/newMo/tutorial/oneMax/src/bitMove_init.h b/branches/newMo/tutorial/oneMax/src/bitMove_init.h new file mode 100644 index 000000000..a6254dca7 --- /dev/null +++ b/branches/newMo/tutorial/oneMax/src/bitMove_init.h @@ -0,0 +1,21 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +// "bitMove_init.h" +#ifndef __bitMove_init +#define __bitMove_init + +#include +#include "bitMove.h" + +template +class BitMove_init : public moMoveInit < BitMove > { + +public : + + void operator () (BitMove & __move, const EOT & genome) { + __move.bit = 0 ; + }; + +} ; + +#endif diff --git a/branches/newMo/tutorial/oneMax/src/bitMove_next.h b/branches/newMo/tutorial/oneMax/src/bitMove_next.h new file mode 100644 index 000000000..1950ad9f0 --- /dev/null +++ b/branches/newMo/tutorial/oneMax/src/bitMove_next.h @@ -0,0 +1,33 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +// "bitMove_next.h" + +#ifndef __bitMove_next_h +#define __bitMove_next_h + +#include +#include "bitMove.h" + +template +class BitMove_next : public moNextMove < BitMove > { + +public: + BitMove_next() + { + }; + + bool operator () (BitMove & __move, const EOT & genome) { + + if (__move.bit >= (genome.size() - 1)){ + return false ; +} + else { + __move.bit++; + return true ; + } + }; + + +} ; + +#endif diff --git a/branches/newMo/tutorial/oneMax/src/funcOneMax.h b/branches/newMo/tutorial/oneMax/src/funcOneMax.h new file mode 100644 index 000000000..b35057203 --- /dev/null +++ b/branches/newMo/tutorial/oneMax/src/funcOneMax.h @@ -0,0 +1,33 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// funcOneMax.h +// 25/11/2009 : copy from FuncU.h +//----------------------------------------------------------------------------- + +#ifndef __FuncOneMax +#define __FuncOneMax + +template< class EOT > +class FuncOneMax : public eoEvalFunc +{ +private: + unsigned N; + +public: + FuncOneMax(unsigned n) : N(n) {}; + + ~FuncOneMax(void) {} ; + + void operator() (EOT & genome) { + unsigned sum = 0; + + for (int i = 0; i < N; i++) + sum += genome[i]; + + genome.fitness(sum); + } + +}; + +#endif diff --git a/branches/newMo/tutorial/oneMax/src/oneMaxBitNeighbor.h b/branches/newMo/tutorial/oneMax/src/oneMaxBitNeighbor.h new file mode 100644 index 000000000..bfe93f767 --- /dev/null +++ b/branches/newMo/tutorial/oneMax/src/oneMaxBitNeighbor.h @@ -0,0 +1,38 @@ +#ifndef _oneMaxBitNeighbor_h +#define _oneMaxBitNeighbor_h + +#include +#include + +/* + contener of the neighbor information +*/ +template< class Fitness > +class OneMaxBitNeighbor : public moBitNeighbor +{ +public: + typedef eoBit EOType ; + + using moBitNeighbor::bit ; + + /* + * incremental evaluation of the solution for the oneMax problem + */ + virtual void eval(EOType & solution) { + if (solution[bit] == 0) + fitness(solution.fitness() + 1); + else + fitness(solution.fitness() - 1); + }; +}; + +#endif + + +// Local Variables: +// coding: iso-8859-1 +// mode: C++ +// c-file-offsets: ((c . 0)) +// c-file-style: "Stroustrup" +// fill-column: 80 +// End: