New lessons
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@772 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
parent
88d17c9aff
commit
4880c449c8
42 changed files with 947 additions and 2589 deletions
|
|
@ -5,14 +5,14 @@
|
|||
|
||||
SET (CMAKE_CXX_COMPILER mpicxx)
|
||||
|
||||
ADD_CUSTOM_TARGET(install DEPENDS ${ParadisEO-PEO_SOURCE_DIR}/tutorial/Lesson1/lesson.param ${ParadisEO-PEO_SOURCE_DIR}/tutorial/Lesson1/schema.xml)
|
||||
ADD_CUSTOM_TARGET(install DEPENDS ${ParadisEO-PEO_SOURCE_DIR}/tutorial/Lesson1/param ${ParadisEO-PEO_SOURCE_DIR}/tutorial/Lesson1/schema.xml)
|
||||
|
||||
ADD_CUSTOM_COMMAND(
|
||||
TARGET install
|
||||
POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND}
|
||||
ARGS -E copy_if_different
|
||||
${ParadisEO-PEO_SOURCE_DIR}/tutorial/Lesson1/lesson.param
|
||||
${ParadisEO-PEO_SOURCE_DIR}/tutorial/Lesson1/param
|
||||
${ParadisEO-PEO_BINARY_DIR}/tutorial/Lesson1)
|
||||
ADD_CUSTOM_COMMAND(
|
||||
TARGET install
|
||||
|
|
@ -21,13 +21,7 @@ ADD_CUSTOM_COMMAND(
|
|||
ARGS -E copy_if_different
|
||||
${ParadisEO-PEO_SOURCE_DIR}/tutorial/Lesson1/schema.xml
|
||||
${ParadisEO-PEO_BINARY_DIR}/tutorial/Lesson1)
|
||||
ADD_CUSTOM_COMMAND(
|
||||
TARGET install
|
||||
POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND}
|
||||
ARGS -E copy_if_different
|
||||
${ParadisEO-PEO_SOURCE_DIR}/tutorial/Lesson1/ParadisEO-PEO_Lesson1.pdf
|
||||
${ParadisEO-PEO_BINARY_DIR}/tutorial/Lesson1)
|
||||
|
||||
######################################################################################
|
||||
|
||||
|
||||
|
|
@ -35,7 +29,7 @@ ADD_CUSTOM_COMMAND(
|
|||
### 1) Include the sources
|
||||
######################################################################################
|
||||
|
||||
INCLUDE_DIRECTORIES(${EO_SRC_DIR}/src ${MO_SRC_DIR}/src ${ParadisEO-PEO_SOURCE_DIR}/src ${TSP_SRC_DIR})
|
||||
INCLUDE_DIRECTORIES(${EO_SRC_DIR}/src ${MO_SRC_DIR}/src ${ParadisEO-PEO_SOURCE_DIR}/src)
|
||||
|
||||
######################################################################################
|
||||
|
||||
|
|
@ -45,12 +39,12 @@ INCLUDE_DIRECTORIES(${EO_SRC_DIR}/src ${MO_SRC_DIR}/src ${ParadisEO-PEO_SOURCE_D
|
|||
######################################################################################
|
||||
|
||||
IF(NOT WIN32 OR CYGWIN)
|
||||
LINK_DIRECTORIES(${EO_BIN_DIR}/lib ${ParadisEO-PEO_BINARY_DIR}/lib ${TSP_BINARY_DIR}/lib)
|
||||
LINK_DIRECTORIES(${EO_BIN_DIR}/lib ${ParadisEO-PEO_BINARY_DIR}/lib)
|
||||
ENDIF(NOT WIN32 OR CYGWIN)
|
||||
|
||||
# especially for Visual Studio
|
||||
IF(WIN32 AND NOT CYGWIN)
|
||||
LINK_DIRECTORIES(${EO_BIN_DIR}\\lib\\${CMAKE_BUILD_TYPE} ${ParadisEO-PEO_BINARY_DIR}\\lib\\${CMAKE_BUILD_TYPE} ${TSP_BINARY_DIR}\\lib\\${CMAKE_BUILD_TYPE})
|
||||
LINK_DIRECTORIES(${EO_BIN_DIR}\\lib\\${CMAKE_BUILD_TYPE} ${ParadisEO-PEO_BINARY_DIR}\\lib\\${CMAKE_BUILD_TYPE})
|
||||
ENDIF(WIN32 AND NOT CYGWIN)
|
||||
######################################################################################
|
||||
|
||||
|
|
@ -59,8 +53,10 @@ ENDIF(WIN32 AND NOT CYGWIN)
|
|||
### 3) Define your target(s): just an executable here
|
||||
######################################################################################
|
||||
|
||||
ADD_EXECUTABLE(tspExample main.cpp)
|
||||
ADD_DEPENDENCIES(tspExample tsp peo rmc_mpi)
|
||||
ADD_EXECUTABLE(pso mainPSO.cpp)
|
||||
ADD_DEPENDENCIES(pso peo rmc_mpi)
|
||||
ADD_EXECUTABLE(ea mainEA.cpp)
|
||||
ADD_DEPENDENCIES(ea peo rmc_mpi)
|
||||
######################################################################################
|
||||
|
||||
|
||||
|
|
@ -69,7 +65,8 @@ ADD_DEPENDENCIES(tspExample tsp peo rmc_mpi)
|
|||
######################################################################################
|
||||
|
||||
SET(LESSON1_VERSION ${GLOBAL_VERSION})
|
||||
SET_TARGET_PROPERTIES(tspExample PROPERTIES VERSION "${LESSON1_VERSION}")
|
||||
SET_TARGET_PROPERTIES(pso PROPERTIES VERSION "${LESSON1_VERSION}")
|
||||
SET_TARGET_PROPERTIES(ea PROPERTIES VERSION "${LESSON1_VERSION}")
|
||||
######################################################################################
|
||||
|
||||
|
||||
|
|
@ -77,7 +74,8 @@ SET_TARGET_PROPERTIES(tspExample PROPERTIES VERSION "${LESSON1_VERSION}")
|
|||
### 5) Link the librairies
|
||||
######################################################################################
|
||||
|
||||
TARGET_LINK_LIBRARIES(tspExample ${XML2_LIBS} tsp peo rmc_mpi eo eoutils)
|
||||
TARGET_LINK_LIBRARIES(pso ${XML2_LIBS} peo rmc_mpi eo eoutils)
|
||||
TARGET_LINK_LIBRARIES(ea ${XML2_LIBS} peo rmc_mpi eo eoutils)
|
||||
|
||||
######################################################################################
|
||||
|
||||
|
|
|
|||
|
|
@ -1,498 +0,0 @@
|
|||
//! \mainpage Creating a simple ParadisEO-PEO Evolutionary Algorithm
|
||||
//!
|
||||
//! \section structure Introduction
|
||||
//!
|
||||
//! One of the first steps in designing an evolutionary algorithm using the ParadisEO-PEO framework
|
||||
//! consists in having a clear overview of the implemented algorithm. A brief pseudo-code description is offered
|
||||
//! bellow - the entire source code for the ParadisEO-PEO evolutionary algorithm is defined in the <b>peoEA.h</b>
|
||||
//! header file. The main elements to be considered when building an evolutionary algorithm are the transformation
|
||||
//! operators, i.e. crossover and mutation, the evaluation function, the continuation criterion and the selection
|
||||
//! and replacement strategy.
|
||||
//!
|
||||
//! <table style="border:none; border-spacing:0px;text-align:left; vertical-align:top; font-size:8pt;" border="0">
|
||||
//! <tr><td><b>do</b> { </td> <td> </td></tr>
|
||||
//! <tr><td> select( population, offsprings ); </td> <td>// select the offsprings from the current population</td></tr>
|
||||
//! <tr><td> transform( offsprings ); </td> <td>// crossover and mutation operators are applied on the selected offsprings</td></tr>
|
||||
//! <tr><td> evaluate( offsprings ); </td> <td>// evaluation step of the resulting offsprings</td></tr>
|
||||
//! <tr><td> replace( population, offsprings ); </td> <td>// replace the individuals in the current population whith individuals from the offspring population, according to a specified replacement strategy</td></tr>
|
||||
//! <tr><td>} <b>while</b> ( eaCheckpointContinue( population ) ); </td> <td>// checkpoint operators are applied on the current population</td></tr>
|
||||
//! </table>
|
||||
//!
|
||||
//! The peoEA class offers an elementary evolutionary algorithm implementation. The peoEA class has the underlying structure
|
||||
//! for including parallel evaluation and parallel transformation operators, migration operators etc. Although there is
|
||||
//! no restriction on using the algorithms provided by the EO framework, no parallelism is provided - the EO implementation is exclusively sequential.
|
||||
//! <br/>
|
||||
//!
|
||||
//! \section requirements Requirements
|
||||
//!
|
||||
//! You should have already installed the ParadisEO-PEO package - this requires several additional packages which should be already
|
||||
//! included in the provided archive. The installation script has to be launched in order to configure and compile all the required
|
||||
//! components. At the end of the installation phase you should end up having a directory tree resembling the following:
|
||||
//! <b>
|
||||
//! <br/> ...
|
||||
//! <br/> paradiseo-mo
|
||||
//! <br/> paradiseo-moeo
|
||||
//! <br/> paradiseo-peo
|
||||
//! <br/> docs
|
||||
//! <br/> examples
|
||||
//! <br/> lesson1
|
||||
//! <br/> lesson2
|
||||
//! <br/> ...
|
||||
//! <br/> shared
|
||||
//! <br/> ...
|
||||
//! <br/> src
|
||||
//! <br/> ...
|
||||
//! <br/> ...
|
||||
//! </b>
|
||||
//! <br/>
|
||||
//!
|
||||
//! The source-code for this tutorial may be found in the <b>paradiseo-peo/examples/lesson1</b> directory, in the <b>main.cpp</b> file.
|
||||
//! We strongly encourage creating a backup copy of the file if you consider modifying the source code. For a complete reference on the
|
||||
//! TSP-related classes and definitions please refer to the files under the <b>paradiseo-peo/examples/shared</b>. After the installation
|
||||
//! phase you should end up having an <b>tspExample</b> executable file in the <b>paradiseo-peo/examples/lesson1</b> directory.
|
||||
//! We will discuss testing and launching aspects later in the tutorial.
|
||||
//!
|
||||
//! You are supposed to be familiar with working in C/C++ (with an extensive use of templates) and you should have at least an introductory
|
||||
//! background in working with the EO framework.
|
||||
//!
|
||||
//! <hr/>
|
||||
//! <b>NOTE</b>: All the presented examples have as case study the <i>Traveling Salesman Problem (TSP)</i>. All the presented tutorials rely
|
||||
//! on a <a href="../../lsnshared/html/index.html" target="new">common shared source code</a> defining transformation operators,
|
||||
//! evaluation functions, etc. for the TSP problem. For a complete understanding of the presented tutorials please take your time for
|
||||
//! consulting and for studying the additional underlying defined classes.
|
||||
//! <hr/><br/>
|
||||
//!
|
||||
//! \section problemDef Problem Definition and Representation
|
||||
//!
|
||||
//! As we are not directly concerned with the <i>Traveling Salesman Problem</i>, and to some extent out of scope, no in depth details are offered
|
||||
//! for the TSP. The problem requires finding the shortest path connecting a given set of cities, while visiting each of
|
||||
//! the specified cities only once and returning to the startpoint city. The problem is known to be NP-complete, i.e. no polynomial
|
||||
//! time algorithm exists for solving the problem in exact manner.
|
||||
//!
|
||||
//! The construction of a ParadisEO-PEO evolutionary algorithm requires following a few simple steps - please take your time to study the signature
|
||||
//! of the peoEA constructor:
|
||||
//!
|
||||
//! <table border="0" width="100%">
|
||||
//! <tr><td style="vertical-align:top;">
|
||||
//! peoEA(
|
||||
//! <br/> eoContinue< EOT >& __cont,
|
||||
//! <br/> peoPopEval< EOT >& __pop_eval,
|
||||
//! <br/> eoSelect< EOT >& __select,
|
||||
//! <br/> peoTransform< EOT >& __trans,
|
||||
//! <br/> eoReplacement< EOT >& __replace
|
||||
//! <br/> );
|
||||
//! </td>
|
||||
//! <td style="vertical-align:top; text-align:center;">
|
||||
//! \image html peoEA.png
|
||||
//! </td></tr>
|
||||
//! </table>
|
||||
//!
|
||||
//! A few remarks have to be made: while most of the parameters are passed as EO-specific types, the evaluation and the transformation objects have to be
|
||||
//! derived from the ParadisEO-PEO peoPopEval and peoTransform classes. Derived classes like the peoParaPopEval and peoParaSGATransform classes allow
|
||||
//! for parallel evaluation of the population and parallel transformation operators, respectively. Wrappers are provided thus allowing to make use
|
||||
//! of the EO classes.
|
||||
//!
|
||||
//! In the followings, the main required elements for building an evolutionary algorithm are enumerated. For complete details regarding the
|
||||
//! implementation aspects of each of the components, please refer to the <a href="../../lsnshared/html/index.html" target="new">common shared source code</a>.
|
||||
//! Each of the bellow referred header files may be found in the <b>pardiseo-peo/examples/shared</b> directory.
|
||||
//!
|
||||
//! <ol>
|
||||
//! <li><i><b>representation</b></i> - the first decision to be taken concerns the representation of the individuals. You may create your
|
||||
//! own representation or you may use/derive one of the predefined classes of the EO framework. <br/>
|
||||
//!
|
||||
//! For our case study, the TSP, each city is defined as a Node in the <b>node.h</b> header file - in fact an unsigned value defined
|
||||
//! as <b>typedef unsigned Node</b>. Moreover, each individual (of the evolutionary algorithm) is represented as a Route object, a vector of Node objects, in
|
||||
//! the <b>route.h</b> header file - <b>typedef eoVector< int, Node > Route</b>. The definition of the Route object implies two
|
||||
//! elements: (1) a route is a vector of nodes, and (2) the fitness is an integer value (please refer to the eoVector
|
||||
//! definition in the EO framework).
|
||||
//!
|
||||
//! In addition you should also take a look in the <b>route_init.h</b> header file which includes the RouteInit class, defined for
|
||||
//! initializing in random manner Route objects.
|
||||
//! </li>
|
||||
//! <li><i><b>evaluation function</b></i> - having a representation model, an evaluation object has to be defined, implementing a specific
|
||||
//! fitness function. The designed class has to be derived (directly or indirectly) from the peoPopEval class - you have the choice of
|
||||
//! using peoSeqPopEval or peoParaPopEval for sequential and parallel evaluation, respectively. These classes act as wrappers requiring
|
||||
//! the specification of an EO evaluation object derived from the eoEvalFunc class - please refer to their respective documentation. <br/>
|
||||
//!
|
||||
//! The fitness function for our TSP case study is implemented in the <b>route_eval.h</b> header file. The class is derived from the eoEvalFunc
|
||||
//! EO class, being defined as <b>class RouteEval : public eoEvalFunc< Route ></b>.
|
||||
//! </li>
|
||||
//! <li><i><b>transformation operators</b></i> - in order to assure the evolution of the initial population, transformation operators have to be defined.
|
||||
//! Depending on your problem, you may specify quadruple operators (two input individuals, two output resulting individuals), i.e. crossover operators,
|
||||
//! binary operators (one input individual and one output resulting individual), i.e. mutation operators, or combination of both types. As for the
|
||||
//! evaluation function, the signature of the peoEA constructor requires specifying a peoTransform derived object as transformation operator.
|
||||
//!
|
||||
//! The transform operators, crossover and mutation, for the herein presented example are defined in the <b>order_xover.h</b> and the <b>city_swap.h</b>
|
||||
//! header files, respectively.
|
||||
//! </li>
|
||||
//! <li><i><b>continuation criterion</b></i> - the evolutionary algorithm evolves in an iterative manner; a continuation criterion has to be specified.
|
||||
//! One of the most common and simplest options considers a maximum number of generations. It is your choice whether to use
|
||||
//! a predefined EO class for specifying the continuation criterion or using a custom defined class. In the later case you have to
|
||||
//! make sure that your class derives the eoContinue class.<br/>
|
||||
//! </li>
|
||||
//! <li><i><b>selection strategy</b></i> - at each iteration a set of individuals are selected for applying the transform operators, in order
|
||||
//! to obtain the offspring population. As the specified parameter has to be derived from the eoSelect it is your option of whether using
|
||||
//! the EO provided selection strategies or implementing your own, as long as it inherits the eoSelect class.
|
||||
//!
|
||||
//! For our example we chose to use the eoRankingSelect strategy, provided in the EO framework.
|
||||
//! </li>
|
||||
//! <li><i><b>replacement strategy</b></i> - once the offspring population is obtained, the offsprings have to be integrated back into the initial
|
||||
//! population, according to a given strategy. For custom defined strategies you have to inherit the eoReplacement EO class. We chose to
|
||||
//! use an eoPlusReplacement as strategy (please review the EO documentation for details on the different strategies available).
|
||||
//! </li>
|
||||
//! </ol>
|
||||
//! <br/>
|
||||
//!
|
||||
//! \section example A simple example for constructing a peoEA object
|
||||
//!
|
||||
//! The source code for this example may be found in the <b>main.cpp</b> file, under the <b>paradiseo-peo/examples/lesson1</b> directory. Please make sure you
|
||||
//! At this point you have two options: (a) you can just follow the example without touching the <b>main.cpp</b> or, (b) you can start from scratch,
|
||||
//! following the presented steps, in which case you are required make a backup copy of the <b>main.cpp</b> file and replace the original file with an
|
||||
//! empty one.
|
||||
//!
|
||||
//! <ol>
|
||||
//! <li> <b>include the necessary header files</b> - as we will be using Route objects, we have to include the files
|
||||
//! which define the Route type, the initializing functor and the evaluation functions. Furthermore, in order to make use of
|
||||
//! transform operators, we require having the headers which define the crossover and the mutation operators.
|
||||
//! All these files may be found in the shared directory that we mentioned in the beginning. At this point you
|
||||
//! should have something like the following:<br/>
|
||||
//!
|
||||
//! <pre>
|
||||
//! ##include "route.h"
|
||||
//! ##include "route_init.h"
|
||||
//! ##include "route_eval.h"
|
||||
//!
|
||||
//! ##include "order_xover.h"
|
||||
//! ##include "city_swap.h"
|
||||
//! </pre>
|
||||
//! In addition we require having the <i>paradiseo</i> header file, in order to use the ParadisEO-PEO features, and a header specific
|
||||
//! for our problem, dealing with processing command-line parameters - the <b>param.h</b> header file. The complete picture at this point
|
||||
//! with all the required header files is as follows:<br/>
|
||||
//!
|
||||
//! <pre>
|
||||
//! ##include "route.h"
|
||||
//! ##include "route_init.h"
|
||||
//! ##include "route_eval.h"
|
||||
//!
|
||||
//! ##include "order_xover.h"
|
||||
//! ##include "city_swap.h"
|
||||
//!
|
||||
//! ##include "param.h"
|
||||
//!
|
||||
//! ##include <paradiseo>
|
||||
//! </pre>
|
||||
//! <b>NOTE</b>: the <b><i>paradiseo</i></b> header file is in fact a "super-header" - it includes all the esential ParadisEO-PEO header files.
|
||||
//! It is at at your choice if you want use the <b><i>paradiseo</i></b> header file or to explicitly include different header files,
|
||||
//! like the <b>peoEA.h</b> header file, for example.
|
||||
//!
|
||||
//! </li>
|
||||
//! <li> <b>define problem specific parameters</b> - in our case we have to specify how many individuals we want to have in our population, the number
|
||||
//! of generations for the evolutionary algorithm to iterate and the probabilities associated with the crossover and mutation operators.<br/>
|
||||
//!
|
||||
//! <pre>
|
||||
//! ##include "route.h"
|
||||
//! ##include "route_init.h"
|
||||
//! ##include "route_eval.h"
|
||||
//!
|
||||
//! ##include "order_xover.h"
|
||||
//! ##include "city_swap.h"
|
||||
//!
|
||||
//! ##include "param.h"
|
||||
//!
|
||||
//! ##include <paradiseo>
|
||||
//!
|
||||
//!
|
||||
//! ##define POP_SIZE 10
|
||||
//! ##define NUM_GEN 100
|
||||
//! ##define CROSS_RATE 1.0
|
||||
//! ##define MUT_RATE 0.01
|
||||
//! </pre>
|
||||
//! </li>
|
||||
//! <li> <b>construct the skeleton of a simple ParadisEO-PEO program</b> - the main function including the code for initializing the ParadisEO-PEO
|
||||
//! environment, for loading problem data and for shutting down the ParadisEO-PEO environment. From this point on we will make
|
||||
//! abstraction of the previous part referring only to the main function.<br/>
|
||||
//!
|
||||
//! <pre>
|
||||
//! ...
|
||||
//!
|
||||
//! int main( int __argc, char** __argv ) {
|
||||
//!
|
||||
//! <i>//</i> initializing the ParadisEO-PEO environment
|
||||
//! peo :: init( __argc, __argv );
|
||||
//!
|
||||
//! <i>//</i> processing the command line specified parameters
|
||||
//! loadParameters( __argc, __argv );
|
||||
//!
|
||||
//!
|
||||
//! <i>//</i> EVOLUTIONARY ALGORITHM TO BE DEFINED
|
||||
//!
|
||||
//!
|
||||
//! peo :: run( );
|
||||
//! peo :: finalize( );
|
||||
//! <i>//</i> shutting down the ParadisEO-PEO environment
|
||||
//!
|
||||
//! return 0;
|
||||
//! }
|
||||
//! </pre>
|
||||
//! </li>
|
||||
//! <li> <b>initialization functors, evaluation function and transform operators</b> - basically we only need to create instances for each of the
|
||||
//! enumerated objects, to be passed later as parameters for higher-level components of the evolutionary algorithm.<br/>
|
||||
//!
|
||||
//! <pre>
|
||||
//! RouteInit route_init; <i>//</i> random init object - creates random Route objects
|
||||
//! RouteEval full_eval; <i>//</i> evaluator object - offers a fitness value for a specified Route object
|
||||
//!
|
||||
//! OrderXover crossover; <i>//</i> crossover operator - creates two offsprings out of two specified parents
|
||||
//! CitySwap mutation; <i>//</i> mutation operator - randomly mutates one gene for a specified individual
|
||||
//! </pre>
|
||||
//! </li>
|
||||
//! <li> <b>construct the components of the evolutionary algorithm</b> - each of the components that has to be passed as parameter to the
|
||||
//! <b>peoEA</b> constructor has to be defined along with the associated parameters. Except for the requirement to provide the
|
||||
//! appropriate objects (for example, a peoPopEval derived object must be specified for the evaluation functor), there is no strict
|
||||
//! path to follow. It is your option what elements to mix, depending on your problem - we aimed for simplicity in our example.
|
||||
//!
|
||||
//! <ul>
|
||||
//! <li> an initial population has to be specified; the constructor accepts the specification of an initializing object. Further,
|
||||
//! an evaluation object is required - the <b>peoEA</b> constructor requires a <b>peoPopEval</b> derived class.
|
||||
//! </li>
|
||||
//! </ul>
|
||||
//! <pre>
|
||||
//! eoPop< Route > population( POP_SIZE, route_init ); <i>//</i> initial population for the algorithm having POP_SIZE individuals
|
||||
//! peoSeqPopEval< Route > eaPopEval( full_eval ); // evaluator object - to be applied at each iteration on the entire population
|
||||
//! </pre>
|
||||
//! <ul>
|
||||
//! <li> the evolutionary algorithm continues to iterate till a continuation criterion is not met. For our case we consider
|
||||
//! a fixed number of generations. The continuation criterion has to be specified as a checkpoint object, thus requiring
|
||||
//! the creation of an <i>eoCheckPoint</i> object in addition.
|
||||
//! </li>
|
||||
//! </ul>
|
||||
//! <pre>
|
||||
//! eoGenContinue< Route > eaCont( NUM_GEN ); <i>//</i> continuation criterion - the algorithm will iterate for NUM_GEN generations
|
||||
//! eoCheckPoint< Route > eaCheckpointContinue( eaCont ); <i>//</i> checkpoint object - verify at each iteration if the continuation criterion is met
|
||||
//! </pre>
|
||||
//! <ul>
|
||||
//! <li> selection strategy - we are required to specify a selection strategy for extracting individuals out of the parent
|
||||
//! population; in addition the number of individuals to be selected has to be specified.
|
||||
//! </li>
|
||||
//! </ul>
|
||||
//! <pre>
|
||||
//! eoRankingSelect< Route > selectionStrategy; <i>//</i> selection strategy - applied at each iteration for selecting parent individuals
|
||||
//! eoSelectNumber< Route > eaSelect( selectionStrategy, POP_SIZE ); <i>//</i> selection object - POP_SIZE individuals are selected at each iteration
|
||||
//! </pre>
|
||||
//! <ul>
|
||||
//! <li> transformation operators - we have to integrate the crossover and the mutation functors into an object which may be passed
|
||||
//! as a parameter when creating the <b>peoEA</b> object. The constructor of <b>peoEA</b> requires a <b>peoTransform</b> derived
|
||||
//! object. Associated probabilities have to be specified also.
|
||||
//! </li>
|
||||
//! </ul>
|
||||
//! <pre>
|
||||
//! <i>//</i> transform operator - includes the crossover and the mutation operators with a specified associated rate
|
||||
//! eoSGATransform< Route > transform( crossover, CROSS_RATE, mutation, MUT_RATE );
|
||||
//! peoSeqTransform< Route > eaTransform( transform ); <i>//</i> ParadisEO transform operator (please remark the peo prefix) - wraps an e EO transform object
|
||||
//! </pre>
|
||||
//! <ul>
|
||||
//! <li> replacement strategy - required for defining the way for integrating the resulting offsprings into the initial population.
|
||||
//! At your option whether you would like to chose one of the predefined replacement strategies that come with the EO framework
|
||||
//! or if you want to define your own.
|
||||
//! </li>
|
||||
//! </ul>
|
||||
//! <pre>
|
||||
//! eoPlusReplacement< Route > eaReplace; <i>//</i> replacement strategy - for replacing the initial population with offspring individuals
|
||||
//! </pre>
|
||||
//! </li>
|
||||
//! <li> <b>evolutionary algorithm</b> - having defined all the previous components, we are ready for instanciating an evolutionary algorithm.
|
||||
//! In the end we have to associate a population with the algorithm, which will serve as the initial population, to be iteratively evolved.
|
||||
//!
|
||||
//! <pre>
|
||||
//! peoEA< Route > eaAlg( eaCheckpointContinue, eaPopEval, eaSelect, eaTransform, eaReplace );
|
||||
//!
|
||||
//! eaAlg( population ); // specifying the initial population for the algorithm, to be iteratively evolved
|
||||
//! </pre>
|
||||
//! </li>
|
||||
//! </ol>
|
||||
//!
|
||||
//! If you have not missed any of the enumerated points, your program should be like the following:
|
||||
//!
|
||||
//! <pre>
|
||||
//! ##include "route.h"
|
||||
//! ##include "route_init.h"
|
||||
//! ##include "route_eval.h"
|
||||
//!
|
||||
//! ##include "order_xover.h"
|
||||
//! ##include "city_swap.h"
|
||||
//!
|
||||
//! ##include "param.h"
|
||||
//!
|
||||
//! ##include <paradiseo>
|
||||
//!
|
||||
//!
|
||||
//! ##define POP_SIZE 10
|
||||
//! ##define NUM_GEN 100
|
||||
//! ##define CROSS_RATE 1.0
|
||||
//! ##define MUT_RATE 0.01
|
||||
//!
|
||||
//!
|
||||
//! int main( int __argc, char** __argv ) {
|
||||
//!
|
||||
//! <i>//</i> initializing the ParadisEO-PEO environment
|
||||
//! peo :: init( __argc, __argv );
|
||||
//!
|
||||
//!
|
||||
//! <i>//</i> processing the command line specified parameters
|
||||
//! loadParameters( __argc, __argv );
|
||||
//!
|
||||
//!
|
||||
//! <i>//</i> init, eval operators, EA operators -------------------------------------------------------------------------------------------------------------
|
||||
//!
|
||||
//! RouteInit route_init; <i>//</i> random init object - creates random Route objects
|
||||
//! RouteEval full_eval; <i>//</i> evaluator object - offers a fitness value for a specified Route object
|
||||
//!
|
||||
//! OrderXover crossover; <i>//</i> crossover operator - creates two offsprings out of two specified parents
|
||||
//! CitySwap mutation; <i>//</i> mutation operator - randomly mutates one gene for a specified individual
|
||||
//! <i>//</i> ------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
//!
|
||||
//!
|
||||
//! <i>//</i> evolutionary algorithm components --------------------------------------------------------------------------------------------------------------
|
||||
//!
|
||||
//! eoPop< Route > population( POP_SIZE, route_init ); <i>//</i> initial population for the algorithm having POP_SIZE individuals
|
||||
//! peoSeqPopEval< Route > eaPopEval( full_eval ); <i>//</i> evaluator object - to be applied at each iteration on the entire population
|
||||
//!
|
||||
//! eoGenContinue< Route > eaCont( NUM_GEN ); <i>//</i> continuation criterion - the algorithm will iterate for NUM_GEN generations
|
||||
//! eoCheckPoint< Route > eaCheckpointContinue( eaCont ); <i>//</i> checkpoint object - verify at each iteration if the continuation criterion is met
|
||||
//!
|
||||
//! eoRankingSelect< Route > selectionStrategy; <i>//</i> selection strategy - applied at each iteration for selecting parent individuals
|
||||
//! eoSelectNumber< Route > eaSelect( selectionStrategy, POP_SIZE ); <i>//</i> selection object - POP_SIZE individuals are selected at each iteration
|
||||
//!
|
||||
//! <i>//</i> transform operator - includes the crossover and the mutation operators with a specified associated rate
|
||||
//! eoSGATransform< Route > transform( crossover, CROSS_RATE, mutation, MUT_RATE );
|
||||
//! peoSeqTransform< Route > eaTransform( transform ); <i>//</i> ParadisEO transform operator (please remark the peo prefix) - wraps an e EO transform object
|
||||
//!
|
||||
//! eoPlusReplacement< Route > eaReplace; <i>//</i> replacement strategy - for replacing the initial population with offspring individuals
|
||||
//! <i>//</i> ------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
//!
|
||||
//!
|
||||
//! <i>//</i> ParadisEO-PEO evolutionary algorithm -----------------------------------------------------------------------------------------------------------
|
||||
//!
|
||||
//! peoEA< Route > eaAlg( eaCheckpointContinue, eaPopEval, eaSelect, eaTransform, eaReplace );
|
||||
//!
|
||||
//! eaAlg( population ); <i>//</i> specifying the initial population for the algorithm, to be iteratively evolved
|
||||
//! <i>//</i> ------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
//!
|
||||
//!
|
||||
//! peo :: run( );
|
||||
//! peo :: finalize( );
|
||||
//! <i>//</i> shutting down the ParadisEO-PEO environment
|
||||
//!
|
||||
//! return 0;
|
||||
//! }
|
||||
//! </pre>
|
||||
//!
|
||||
//!
|
||||
//! \section testing Compilation and Execution
|
||||
//!
|
||||
//! First, please make sure that you followed all the previous steps in defining the evolutionary algorithm. Your file should be called <b>main.cpp</b> - please
|
||||
//! make sure you do not rename the file (we will be using a pre-built makefile, thus you are required not to change the file names). Please make sure you
|
||||
//! are in the <b>paradiseo-peo/examples/lesson1</b> directory - you should open a console and you should change your current directory to the one of Lesson1.
|
||||
//!
|
||||
//! <b>Compilation</b>: being in the <b>paradiseo-peo/examples/lesson1</b> directory, you have to type <i>make</i>. As a result the <b>main.cpp</b> file
|
||||
//! will be compiled and you should obtain an executable file called <b>tspExample</b>. If you have errors, please verify any of the followings:
|
||||
//!
|
||||
//! <ul>
|
||||
//! <li>you are under the right directory - you can verify by typing the <i>pwd</i> command - you should have something like <b>.../paradiseo-peo/examples/lesson1</b></li>
|
||||
//! <li>you saved your modifications in a file called <b>main.cpp</b>, in the <b>paradiseo-peo/examples/lesson1</b> directory</li>
|
||||
//! <li>there are no differences between the example presented above and your file</li>
|
||||
//! </ul>
|
||||
//!
|
||||
//! <b>NOTE</b>: in order to successfully compile your program you should already have installed an MPI distribution in your system.
|
||||
//!
|
||||
//! <b>Execution</b>: the execution of a ParadisEO-PEO program requires having already created an environment for launching MPI programs. For <i>MPICH-2</i>,
|
||||
//! for example, this requires starting a ring of daemons. The implementation that we provided as an example is sequential and includes no parallelism - we
|
||||
//! will see in the end how to include also parallelism. Executing a parallel program requires specifying a mapping of resources, in order to assing different
|
||||
//! algorithms to different machines, define worker machines etc. This mapping is defined by an XML file called <b>schema.xml</b>, which, for our case, has
|
||||
//! the following structure:
|
||||
//!
|
||||
//! <pre>
|
||||
//! <?xml version="1.0"?>
|
||||
//!
|
||||
//! <schema>
|
||||
//! <group scheduler="0">
|
||||
//! <node name="0" num_workers="0">
|
||||
//! </node>
|
||||
//!
|
||||
//! <node name="1" num_workers="0">
|
||||
//! <runner>1</runner>
|
||||
//! </node>
|
||||
//!
|
||||
//! <node name="2" num_workers="1">
|
||||
//! </node>
|
||||
//! <node name="3" num_workers="1">
|
||||
//! </node>
|
||||
//! </group>
|
||||
//! </schema>
|
||||
//! </pre>
|
||||
//!
|
||||
//! Not going into details, the XML file presented above describes a mapping which includes four nodes, the first one having the role of scheduler,
|
||||
//! the second one being the node on which the evolutionary algorithm is actually executed and the third and the fourth ones being slave nodes. Overall
|
||||
//! the mapping says that we will be launching four processes, out of which only one will be executing the evolutionary algorithm. The other node entries
|
||||
//! in the XML file have no real functionality as we have no parallelism in our program - the entries were created for you convenience, in order to provide
|
||||
//! a smooth transition to creating a parallel program.
|
||||
//!
|
||||
//! Launching the program may be different, depending on your MPI distribution - for MPICH-2, in a console, in the <b>paradiseo-peo/examples/lesson1</b>
|
||||
//! directory you have to type the following command:
|
||||
//!
|
||||
//! <b>mpiexec -n 4 ./tspExample @lesson.param</b>
|
||||
//!
|
||||
//! <b>NOTE</b>: the "-n 4" indicates the number of processes to be launched. The last argument, "@lesson.param", indicates a file which specifies different
|
||||
//! application specific parameters (the mapping file to be used, for example, whether to use logging or not, etc).
|
||||
//!
|
||||
//! The result of your execution should be similar to the following:
|
||||
//! <pre>
|
||||
//! Loading '../data/eil101.tsp'.
|
||||
//! NAME: eil101.
|
||||
//! COMMENT: 101-city problem (Christofides/Eilon).
|
||||
//! TYPE: TSP.
|
||||
//! DIMENSION: 101.
|
||||
//! EDGE_WEIGHT_TYPE: EUC_2D.
|
||||
//! Loading '../data/eil101.tsp'.
|
||||
//! NAME: eil101.
|
||||
//! COMMENT: 101-city problem (Christofides/Eilon).
|
||||
//! EOF.
|
||||
//! TYPE: TSP.
|
||||
//! DIMENSION: 101.
|
||||
//! EDGE_WEIGHT_TYPE: EUC_2D.
|
||||
//! EOF.
|
||||
//! Loading '../data/eil101.tsp'.
|
||||
//! NAME: eil101.
|
||||
//! COMMENT: 101-city problem (Christofides/Eilon).
|
||||
//! TYPE: TSP.
|
||||
//! DIMENSION: 101.
|
||||
//! EDGE_WEIGHT_TYPE: EUC_2D.
|
||||
//! EOF.
|
||||
//! Loading '../data/eil101.tsp'.
|
||||
//! NAME: eil101.
|
||||
//! COMMENT: 101-city problem (Christofides/Eilon).
|
||||
//! TYPE: TSP.
|
||||
//! DIMENSION: 101.
|
||||
//! EDGE_WEIGHT_TYPE: EUC_2D.
|
||||
//! EOF.
|
||||
//! STOP in eoGenContinue: Reached maximum number of generations [100/100]
|
||||
//! </pre>
|
||||
//!
|
||||
//!
|
||||
//! \section paraIntro Introducing parallelism
|
||||
//!
|
||||
//! Creating parallel programs with ParadisEO-PEO represents an easy task once you have the basic structure for your program. For experimentation,
|
||||
//! in the <b>main.cpp</b> file, replace the line
|
||||
//! <pre>
|
||||
//! peo<b>Seq</b>PopEval< Route > eaPopEval( full_eval );
|
||||
//! </pre>
|
||||
//! with
|
||||
//! <pre>
|
||||
//! peo<b>Para</b>PopEval< Route > eaPopEval( full_eval );
|
||||
//! </pre>
|
||||
//! The second line only tells that we would like to evaluate individuals in parallel - this is very interesting if you have a time consuming fitness
|
||||
//! evaluation function. If you take another look on the <b>schema.xml</b> XML file you will see the last two nodes being marked as slaves (the "num_workers"
|
||||
//! attribute - these nodes will be used for computing the fitness of the individuals.
|
||||
//!
|
||||
//! At this point you only have to recompile your program and to launch it again - as we are not using a time consuming fitness fitness function, the
|
||||
//! effects might not be visible - you may increase the number of individuals to experiment.
|
||||
|
|
@ -1,110 +0,0 @@
|
|||
/*
|
||||
* <main.cpp>
|
||||
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
|
||||
* (C) OPAC Team, LIFL, 2002-2007
|
||||
*
|
||||
* Sebastien Cahon, Alexandru-Adrian Tantar
|
||||
*
|
||||
* 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 "route.h"
|
||||
#include "route_init.h"
|
||||
#include "route_eval.h"
|
||||
|
||||
#include "order_xover.h"
|
||||
#include "city_swap.h"
|
||||
|
||||
#include "param.h"
|
||||
|
||||
#include <peo>
|
||||
|
||||
|
||||
#define POP_SIZE 10
|
||||
#define NUM_GEN 100
|
||||
#define CROSS_RATE 1.0
|
||||
#define MUT_RATE 0.01
|
||||
|
||||
|
||||
int main( int __argc, char** __argv ) {
|
||||
|
||||
// initializing the ParadisEO-PEO environment
|
||||
peo :: init( __argc, __argv );
|
||||
|
||||
|
||||
// processing the command line specified parameters
|
||||
loadParameters( __argc, __argv );
|
||||
|
||||
|
||||
// init, eval operators, EA operators -------------------------------------------------------------------------------------------------------------
|
||||
|
||||
RouteInit route_init; // random init object - creates random Route objects
|
||||
RouteEval full_eval; // evaluator object - offers a fitness value for a specified Route object
|
||||
|
||||
OrderXover crossover; // crossover operator - creates two offsprings out of two specified parents
|
||||
CitySwap mutation; // mutation operator - randomly mutates one gene for a specified individual
|
||||
// ------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// evolutionary algorithm components --------------------------------------------------------------------------------------------------------------
|
||||
|
||||
eoPop< Route > population( POP_SIZE, route_init ); // initial population for the algorithm having POP_SIZE individuals
|
||||
peoSeqPopEval< Route > eaPopEval( full_eval ); // evaluator object - to be applied at each iteration on the entire population
|
||||
|
||||
eoGenContinue< Route > eaCont( NUM_GEN ); // continuation criterion - the algorithm will iterate for NUM_GEN generations
|
||||
eoCheckPoint< Route > eaCheckpointContinue( eaCont ); // checkpoint object - verify at each iteration if the continuation criterion is met
|
||||
|
||||
eoRankingSelect< Route > selectionStrategy; // selection strategy - applied at each iteration for selecting parent individuals
|
||||
eoSelectNumber< Route > eaSelect( selectionStrategy, POP_SIZE ); // selection object - POP_SIZE individuals are selected at each iteration
|
||||
|
||||
// transform operator - includes the crossover and the mutation operators with a specified associated rate
|
||||
eoSGATransform< Route > transform( crossover, CROSS_RATE, mutation, MUT_RATE );
|
||||
peoSeqTransform< Route > eaTransform( transform ); // ParadisEO transform operator (please remark the peo prefix) - wraps an e EO transform object
|
||||
|
||||
eoPlusReplacement< Route > eaReplace; // replacement strategy - for replacing the initial population with offspring individuals
|
||||
// ------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// ParadisEO-PEO evolutionary algorithm -----------------------------------------------------------------------------------------------------------
|
||||
|
||||
peoEA< Route > eaAlg( eaCheckpointContinue, eaPopEval, eaSelect, eaTransform, eaReplace );
|
||||
|
||||
eaAlg( population ); // specifying the initial population for the algorithm, to be iteratively evolved
|
||||
// ------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
peo :: run( );
|
||||
peo :: finalize( );
|
||||
// shutting down the ParadisEO-PEO environment
|
||||
|
||||
if(getNodeRank()==1)
|
||||
std::cout<<"\n\nPopulation :\n"<<population;
|
||||
|
||||
return 0;
|
||||
}
|
||||
125
trunk/paradiseo-peo/tutorial/Lesson1/mainEA.cpp
Normal file
125
trunk/paradiseo-peo/tutorial/Lesson1/mainEA.cpp
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
/*
|
||||
* <main.cpp>
|
||||
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
|
||||
* (C) OPAC Team, INRIA, 2007
|
||||
*
|
||||
* Clive Canape
|
||||
*
|
||||
* 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 <peo>
|
||||
#include <es.h>
|
||||
|
||||
typedef eoReal<double> Indi;
|
||||
|
||||
//Evaluation function
|
||||
|
||||
double f (const Indi & _indi)
|
||||
{
|
||||
// Rosenbrock function f(x) = 100*(x[1]-x[0]^2)^2+(1-x[0])^2
|
||||
// => optimal : f* = 0 , with x* =(1,1)
|
||||
|
||||
double sum;
|
||||
sum=_indi[1]-pow(_indi[0],2);
|
||||
sum=100*pow(sum,2);
|
||||
sum+=pow((1-_indi[0]),2);
|
||||
return (-sum);
|
||||
}
|
||||
|
||||
int main (int __argc, char *__argv[])
|
||||
{
|
||||
|
||||
|
||||
// Initialization of the parallel environment : thanks this instruction, ParadisEO-PEO can initialize himself
|
||||
peo :: init( __argc, __argv );
|
||||
|
||||
//Parameters
|
||||
|
||||
const unsigned int VEC_SIZE = 2; // Don't change this parameter when you are resolving the Rosenbrock function
|
||||
const unsigned int POP_SIZE = 20; // As with a sequential algorithm, you change the size of the population
|
||||
const unsigned int MAX_GEN = 300; // Define the number of maximal generation
|
||||
const double INIT_POSITION_MIN = -2.0; // For initialize x
|
||||
const double INIT_POSITION_MAX = 2.0; // In the case of the Rosenbrock function : -2 < x[i] < 2
|
||||
const float CROSS_RATE = 0.8; // Crossover rate
|
||||
const double EPSILON = 0.01; // Range for real uniform mutation
|
||||
const float MUT_RATE = 0.3; // Mutation rate
|
||||
rng.reseed (time(0));
|
||||
|
||||
// Stopping
|
||||
eoGenContinue < Indi > genContPara (MAX_GEN);
|
||||
eoCombinedContinue <Indi> continuatorPara (genContPara);
|
||||
eoCheckPoint<Indi> checkpoint(continuatorPara);
|
||||
|
||||
|
||||
/* In this lesson, you should define a peoEvalFunc witch will allow to initialize :
|
||||
*
|
||||
* - peoSeqPopEval : using to the sequential evaluation
|
||||
*
|
||||
* OR
|
||||
*
|
||||
* - peoParaPopEval : using to the parallel evaluation
|
||||
*
|
||||
*/
|
||||
|
||||
// For a parallel evaluation
|
||||
peoEvalFunc<Indi> plainEval(f);
|
||||
peoParaPopEval< Indi > eval(plainEval);
|
||||
|
||||
|
||||
// Initialization
|
||||
eoUniformGenerator < double >uGen (INIT_POSITION_MIN, INIT_POSITION_MAX);
|
||||
eoInitFixedLength < Indi > random (VEC_SIZE, uGen);
|
||||
|
||||
// Selection
|
||||
eoRankingSelect<Indi> selectionStrategy;
|
||||
eoSelectNumber<Indi> select(selectionStrategy,POP_SIZE);
|
||||
|
||||
// Transformation
|
||||
eoSegmentCrossover<Indi> crossover; // Crossover
|
||||
eoUniformMutation<Indi> mutation(EPSILON); // Mutation
|
||||
eoSGATransform<Indi> transform(crossover,CROSS_RATE,mutation,MUT_RATE);
|
||||
peoSeqTransform<Indi> eaTransform(transform);
|
||||
|
||||
// Replacement
|
||||
eoPlusReplacement<Indi> replace;
|
||||
|
||||
// Creation of the population
|
||||
eoPop < Indi > pop;
|
||||
pop.append (POP_SIZE, random);
|
||||
|
||||
//Parallel algorithm
|
||||
peoEA<Indi> Algo(checkpoint,eval,select,eaTransform,replace);
|
||||
Algo(pop);
|
||||
|
||||
peo :: run();
|
||||
peo :: finalize();
|
||||
if(getNodeRank()==1)
|
||||
std::cout << "Final population :\n" << pop << std::endl;
|
||||
}
|
||||
142
trunk/paradiseo-peo/tutorial/Lesson1/mainPSO.cpp
Normal file
142
trunk/paradiseo-peo/tutorial/Lesson1/mainPSO.cpp
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
/*
|
||||
* <main.cpp>
|
||||
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
|
||||
* (C) OPAC Team, INRIA, 2007
|
||||
*
|
||||
* Clive Canape
|
||||
*
|
||||
* 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 <peo>
|
||||
|
||||
typedef eoRealParticle < double >Indi;
|
||||
|
||||
//Evaluation function
|
||||
|
||||
double f (const Indi & _indi)
|
||||
{
|
||||
// Rosenbrock function f(x) = 100*(x[1]-x[0]^2)^2+(1-x[0])^2
|
||||
// => optimal : f* = 0 , with x* =(1,1)
|
||||
|
||||
double sum;
|
||||
sum=_indi[1]-pow(_indi[0],2);
|
||||
sum=100*pow(sum,2);
|
||||
sum+=pow((1-_indi[0]),2);
|
||||
return (-sum);
|
||||
}
|
||||
|
||||
int main (int __argc, char *__argv[])
|
||||
{
|
||||
|
||||
|
||||
// Initialization of the parallel environment : thanks this instruction, ParadisEO-PEO can initialize himself
|
||||
peo :: init( __argc, __argv );
|
||||
|
||||
//Parameters
|
||||
|
||||
const unsigned int VEC_SIZE = 2; // Don't change this parameter when you are resolving the Rosenbrock function
|
||||
|
||||
const unsigned int POP_SIZE = 20; // As with a sequential algorithm, you change the size of the population
|
||||
|
||||
const unsigned int NEIGHBORHOOD_SIZE= 6; // This parameter define the neighborhoods in the PSO's topology
|
||||
|
||||
const unsigned int MAX_GEN = 150; // Define the number of maximal generation
|
||||
|
||||
const double INIT_POSITION_MIN = -2.0; // For initialize x
|
||||
const double INIT_POSITION_MAX = 2.0; // In the case of the Rosenbrock function : -2 < x[i] < 2
|
||||
|
||||
const double INIT_VELOCITY_MIN = -1.; // For initialize PSO's velocity
|
||||
const double INIT_VELOCITY_MAX = 1.; // ie Lesson 6 of ParadisEO-EO
|
||||
|
||||
const double C1 = 0.5; // For calculate the velocity
|
||||
const double C2 = 2.; // ie Lesson 6 of ParadisEO-EO
|
||||
rng.reseed (time(0));
|
||||
|
||||
// Stopping
|
||||
eoGenContinue < Indi > genContPara (MAX_GEN);
|
||||
eoCombinedContinue <Indi> continuatorPara (genContPara);
|
||||
eoCheckPoint<Indi> checkpoint(continuatorPara);
|
||||
|
||||
|
||||
|
||||
|
||||
/* In this lesson, you should define a peoEvalFuncPSO witch will allow to initialize :
|
||||
*
|
||||
* - peoSeqPopEval : using to the sequential evaluation
|
||||
*
|
||||
* OR
|
||||
*
|
||||
* - peoParaPopEval : using to the parallel evaluation
|
||||
*
|
||||
*/
|
||||
|
||||
// For a parallel evaluation
|
||||
peoEvalFuncPSO<Indi, double, const Indi& > plainEval(f);
|
||||
peoParaPopEval< Indi > eval(plainEval);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Initialization
|
||||
eoUniformGenerator < double >uGen (INIT_POSITION_MIN, INIT_POSITION_MAX);
|
||||
eoInitFixedLength < Indi > random (VEC_SIZE, uGen);
|
||||
|
||||
// Velocity (ie Lesson 6 of ParadisEO-PEO)
|
||||
eoUniformGenerator < double >sGen (INIT_VELOCITY_MIN, INIT_VELOCITY_MAX);
|
||||
eoVelocityInitFixedLength < Indi > veloRandom (VEC_SIZE, sGen);
|
||||
|
||||
// Initializing the best (ie Lesson 6 of ParadisEO-PEO)
|
||||
eoFirstIsBestInit < Indi > localInit;
|
||||
|
||||
// Flight (ie Lesson 6 of ParadisEO-PEO)
|
||||
eoRealVectorBounds bndsFlight(VEC_SIZE,INIT_POSITION_MIN,INIT_POSITION_MAX);
|
||||
eoStandardFlight < Indi > flight(bndsFlight);
|
||||
|
||||
// Creation of the population
|
||||
eoPop < Indi > pop;
|
||||
pop.append (POP_SIZE, random);
|
||||
|
||||
// Initialization
|
||||
peoInitializer <Indi> init(eval,veloRandom,localInit,pop);
|
||||
|
||||
// Topology (ie Lesson 6 of ParadisEO-PEO)
|
||||
eoLinearTopology<Indi> topology(NEIGHBORHOOD_SIZE);
|
||||
eoRealVectorBounds bnds(VEC_SIZE,INIT_VELOCITY_MIN,INIT_VELOCITY_MAX);
|
||||
eoStandardVelocity < Indi > velocity (topology,C1,C2,bnds);
|
||||
|
||||
//Parallel algorithm
|
||||
peoPSO < Indi > psa(init,checkpoint, eval, velocity, flight);
|
||||
psa(pop);
|
||||
peo :: run();
|
||||
peo :: finalize();
|
||||
if(getNodeRank()==1)
|
||||
std::cout << "Final population :\n" << pop << std::endl;
|
||||
}
|
||||
|
|
@ -239,3 +239,4 @@ DOT_CLEANUP = YES
|
|||
# Configuration::additions related to the search engine
|
||||
#---------------------------------------------------------------------------
|
||||
SEARCHENGINE = YES
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,4 @@
|
|||
|
||||
--schema=schema.xml
|
||||
|
||||
## parameters
|
||||
|
||||
--inst=../examples/tsp/benchs/eil101.tsp
|
||||
|
||||
|
|
@ -13,6 +13,7 @@
|
|||
</node>
|
||||
<node name="3" num_workers="1">
|
||||
</node>
|
||||
|
||||
</group>
|
||||
</schema>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue