Add the SMP module
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@2714 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
parent
409a1b21b8
commit
0890c67d31
43 changed files with 4030 additions and 41 deletions
|
|
@ -18,7 +18,7 @@ enable_language(CXX)
|
|||
|
||||
## Test the presence of a compiler
|
||||
if("${CMAKE_CXX_COMPILER}" STREQUAL "" OR "${CMAKE_C_COMPILER}" STREQUAL "")
|
||||
message(FATAL_ERROR "No compiler founded !")
|
||||
message(FATAL_ERROR "No compiler found !")
|
||||
endif()
|
||||
|
||||
## Versioning
|
||||
|
|
@ -54,11 +54,14 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Target.cmake)
|
|||
set(EO_SRC_DIR "${CMAKE_SOURCE_DIR}/eo" CACHE PATH "ParadisEO-EO source directory" FORCE)
|
||||
set(EO_BIN_DIR "${CMAKE_BINARY_DIR}/eo" CACHE PATH "ParadisEO-EO binary directory" FORCE)
|
||||
|
||||
set(MO_SRC_DIR "${CMAKE_SOURCE_DIR}/mo" CACHE PATH "ParadisMO-MO source directory" FORCE)
|
||||
set(MO_BIN_DIR "${CMAKE_BINARY_DIR}/mo" CACHE PATH "ParadisMO-MO binary directory" FORCE)
|
||||
set(MO_SRC_DIR "${CMAKE_SOURCE_DIR}/mo" CACHE PATH "ParadisEO-MO source directory" FORCE)
|
||||
set(MO_BIN_DIR "${CMAKE_BINARY_DIR}/mo" CACHE PATH "ParadisEO-MO binary directory" FORCE)
|
||||
|
||||
set(MOEO_SRC_DIR "${CMAKE_SOURCE_DIR}/moeo" CACHE PATH "ParadisMOEO-MOEO source directory" FORCE)
|
||||
set(MOEO_BIN_DIR "${CMAKE_BINARY_DIR}/moeo" CACHE PATH "ParadisMOEO-MOEO binary directory" FORCE)
|
||||
set(MOEO_SRC_DIR "${CMAKE_SOURCE_DIR}/moeo" CACHE PATH "ParadisEO-MOEO source directory" FORCE)
|
||||
set(MOEO_BIN_DIR "${CMAKE_BINARY_DIR}/moeo" CACHE PATH "ParadisEO-MOEO binary directory" FORCE)
|
||||
|
||||
set(SMP_SRC_DIR "${CMAKE_SOURCE_DIR}/smp" CACHE PATH "ParadisEO-SMP source directory" FORCE)
|
||||
set(SMP_BIN_DIR "${CMAKE_BINARY_DIR}/smp" CACHE PATH "ParadisEO-SMP binary directory" FORCE)
|
||||
|
||||
set(PROBLEMS_SRC_DIR "${CMAKE_SOURCE_DIR}/problems" CACHE PATH "Problems dependant source directory" FORCE)
|
||||
|
||||
|
|
@ -79,12 +82,19 @@ if(NOT EO_ONLY)
|
|||
set(MOEO_MODULE_NAME "ParadisEO-MOEO")
|
||||
set(CMAKE_SOURCE_DIR ${MOEO_SRC_DIR})
|
||||
add_subdirectory(${MOEO_SRC_DIR})
|
||||
|
||||
## SMP Module
|
||||
if(SMP)
|
||||
set(SMP_MODULE_NAME "ParadisEO-SMP")
|
||||
set(CMAKE_SOURCE_DIR ${SMP_SRC_DIR})
|
||||
add_subdirectory(${SMP_SRC_DIR})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
######################################################################################
|
||||
### 5) Packaging : only in release !
|
||||
######################################################################################
|
||||
|
||||
if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
|
||||
if("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Package.cmake)
|
||||
endif()
|
||||
|
|
|
|||
|
|
@ -38,9 +38,6 @@ Then, follow UNIX instructions.
|
|||
------------------------------------------------------------------------------------------
|
||||
1.0 DEPENDENCIES
|
||||
------------------------------------------------------------------------------------------
|
||||
Required
|
||||
- Compiler with OpenMP implementation (hence, Clang isn't supported atm).
|
||||
|
||||
Optionnal
|
||||
- Doxygen for documentation
|
||||
- lcov for coverage
|
||||
|
|
@ -74,7 +71,7 @@ To compile ParadisEO simply compile sources using your generator. For instance,
|
|||
------------------------------------------------------------------------------------------
|
||||
|
||||
There are 2 types of build : Release or Debug.
|
||||
To explicitly change the type, add -DCMAKE_BUILD_TYPE=Debug for instance.
|
||||
To explicitly change the type, add -DDEBUG=true, otherwise, it will be the Release type.
|
||||
|
||||
------------------------------------------------------------------------------------------
|
||||
1.3 INSTALLATION
|
||||
|
|
@ -97,6 +94,7 @@ doc for all documentations
|
|||
doc-eo for building EO documentation
|
||||
doc-mo for MO
|
||||
doc-moeo for MOEO
|
||||
doc-smp for SMP
|
||||
|
||||
Each documentation are generated separatly in the module build folder.
|
||||
For instance, after the generation of the MO documentation, you will find it in build/paradise-mo/doc.
|
||||
|
|
|
|||
|
|
@ -34,8 +34,13 @@ endif()
|
|||
######################################################################################
|
||||
|
||||
add_definitions(-DDEPRECATED_MESSAGES)
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "-Wunknown-pragmas -O0 -g -Wall -Wextra -ansi -pedantic -fopenmp -std=c++0x" CACHE STRING "" FORCE)
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "-Wunknown-pragmas -O2 -fopenmp -std=c++0x" CACHE STRING "" FORCE)
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "-Wunknown-pragmas -O0 -g -Wall -Wextra -ansi -pedantic" CACHE STRING "" FORCE)
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "-Wunknown-pragmas -O2" CACHE STRING "" FORCE)
|
||||
|
||||
if(SMP)
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11 -pthread" CACHE STRING "" FORCE)
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11 -pthread" CACHE STRING "" FORCE)
|
||||
endif(SMP)
|
||||
|
||||
######################################################################################
|
||||
### 1) Define installation type
|
||||
|
|
@ -55,6 +60,7 @@ endif()
|
|||
|
||||
if(PROFILING)
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pg --coverage" CACHE STRING "" FORCE)
|
||||
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "" FORCE)
|
||||
set(ENABLE_CMAKE_TESTING "true" CACHE STRING "" FORCE)
|
||||
endif(PROFILING)
|
||||
|
||||
|
|
@ -67,16 +73,6 @@ if(ENABLE_CMAKE_TESTING)
|
|||
include(CTest REQUIRED)
|
||||
endif(ENABLE_CMAKE_TESTING)
|
||||
|
||||
######################################################################################
|
||||
### 4) Set DEBUG or RELEASE build type depending testing and profiling
|
||||
######################################################################################
|
||||
|
||||
if(NOT DEFINED DEBUG)
|
||||
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)
|
||||
else( NOT DEFINED DEBUG)
|
||||
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "" FORCE)
|
||||
endif(NOT DEFINED DEBUG)
|
||||
|
||||
######################################################################################
|
||||
### 5) Build examples ?
|
||||
######################################################################################
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@ macro(add_lesson module target files)
|
|||
target_link_libraries(${i} eoutils ga eo)
|
||||
elseif(${module} MATCHES moeo)
|
||||
target_link_libraries(${i} moeo flowshop eo eoutils)
|
||||
elseif(${module} MATCHES smp)
|
||||
target_link_libraries(${i} smp eo eoutils)
|
||||
endif()
|
||||
install(TARGETS ${i} RUNTIME DESTINATION share${INSTALL_SUB_DIR}/${module}/tutorial/${target} COMPONENT examples)
|
||||
endforeach(i)
|
||||
|
|
|
|||
|
|
@ -11,11 +11,20 @@ endif(UNIX)
|
|||
######################################################################################
|
||||
|
||||
if(DOXYGEN_FOUND AND DOXYGEN_EXECUTABLE)
|
||||
add_custom_target(doc
|
||||
COMMAND make doc-eo
|
||||
COMMAND make doc-mo
|
||||
COMMAND make doc-moeo
|
||||
)
|
||||
if(SMP)
|
||||
add_custom_target(doc
|
||||
COMMAND make doc-eo
|
||||
COMMAND make doc-mo
|
||||
COMMAND make doc-moeo
|
||||
COMMAND make doc-smp
|
||||
)
|
||||
else()
|
||||
add_custom_target(doc
|
||||
COMMAND make doc-eo
|
||||
COMMAND make doc-mo
|
||||
COMMAND make doc-moeo
|
||||
)
|
||||
endif()
|
||||
endif(DOXYGEN_FOUND AND DOXYGEN_EXECUTABLE)
|
||||
|
||||
######################################################################################
|
||||
|
|
|
|||
|
|
@ -81,13 +81,6 @@ ENDIF (WIN32)
|
|||
|
||||
######################################################################################
|
||||
|
||||
|
||||
#####################################################################################
|
||||
### 3) Manage the build type
|
||||
#####################################################################################
|
||||
|
||||
INCLUDE(BuildConfig.cmake)
|
||||
|
||||
######################################################################################
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -216,7 +216,7 @@ public:
|
|||
|
||||
}
|
||||
|
||||
private:
|
||||
protected:
|
||||
|
||||
eoInitializerBase <POT> &init;
|
||||
eoContinue < POT > &continuator;
|
||||
|
|
|
|||
|
|
@ -6,12 +6,6 @@ include_directories(${EO_SRC_DIR}/src)
|
|||
include_directories(${MOEO_SRC_DIR}/src)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
######################################################################################
|
||||
### 1) Link EO library
|
||||
######################################################################################
|
||||
|
||||
link_directories(${EO_BIN_DIR}/lib)
|
||||
|
||||
######################################################################################
|
||||
### 2) Define output paths
|
||||
######################################################################################
|
||||
|
|
|
|||
18
trunk/smp/CMakeLists.txt
Normal file
18
trunk/smp/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
######################################################################################
|
||||
### 0) Include subdirectories
|
||||
######################################################################################
|
||||
add_subdirectory(doc)
|
||||
add_subdirectory(src)
|
||||
|
||||
if(ENABLE_CMAKE_TESTING)
|
||||
add_subdirectory(test)
|
||||
endif(ENABLE_CMAKE_TESTING)
|
||||
|
||||
if(ENABLE_CMAKE_EXAMPLE)
|
||||
if(${CMAKE_VERBOSE_MAKEFILE})
|
||||
message("PEO Examples :")
|
||||
endif(${CMAKE_VERBOSE_MAKEFILE})
|
||||
add_subdirectory(tutorial)
|
||||
endif(ENABLE_CMAKE_EXAMPLE)
|
||||
|
||||
|
||||
37
trunk/smp/doc/CMakeLists.txt
Normal file
37
trunk/smp/doc/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
######################################################################################
|
||||
### 0) Documentation
|
||||
######################################################################################
|
||||
|
||||
if(DOXYGEN_FOUND)
|
||||
# Directory where the generation will be launched
|
||||
set(SMP_DOC_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH "Documentation directory")
|
||||
# Name of the doxygene configuration file
|
||||
set(SMP_DOC_CONFIG_FILE "smp.doxyfile" CACHE PATH "Documentation configuration file")
|
||||
if(DOXYGEN_EXECUTABLE)
|
||||
# Creating the custom target
|
||||
if(UNIX AND NOT ${CMAKE_VERBOSE_MAKEFILE})
|
||||
add_custom_target(doc-smp
|
||||
COMMAND ${DOXYGEN_EXECUTABLE} ${SMP_DOC_CONFIG_FILE} 2> /dev/null > /dev/null
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
||||
else(UNIX AND NOT ${CMAKE_VERBOSE_MAKEFILE})
|
||||
add_custom_target(doc-smp
|
||||
COMMAND ${DOXYGEN_EXECUTABLE} ${SMP_DOC_CONFIG_FILE}
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
||||
endif(UNIX AND NOT ${CMAKE_VERBOSE_MAKEFILE})
|
||||
endif(DOXYGEN_EXECUTABLE)
|
||||
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${SMP_DOC_CONFIG_FILE}.cmake"
|
||||
"${SMP_DOC_DIR}/${SMP_DOC_CONFIG_FILE}")
|
||||
install(
|
||||
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||
DESTINATION share${INSTALL_SUB_DIR}/smp COMPONENT doc
|
||||
PATTERN "CMakeFiles" EXCLUDE
|
||||
PATTERN "cmake_install.cmake" EXCLUDE
|
||||
PATTERN "CTestTestfile.cmake" EXCLUDE
|
||||
PATTERN "Makefile" EXCLUDE
|
||||
PATTERN "smp.cfg" EXCLUDE
|
||||
PATTERN "smp.doxytag" EXCLUDE
|
||||
)
|
||||
else(DOXYGEN_FOUND)
|
||||
message(STATUS "Unable to generate the documentation, Doxygen package not found")
|
||||
endif(DOXYGEN_FOUND)
|
||||
|
||||
47
trunk/smp/doc/index.h
Normal file
47
trunk/smp/doc/index.h
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/** @mainpage Welcome to ParadisEO-SMP
|
||||
|
||||
@section Introduction
|
||||
|
||||
ParadisEO-SMP for Symmetric MultiProcessing is a module for multicores computations on several algorithms proposed by ParadiseEO. By simply wrapping the algorithms, it allows you to refer only to the API of your algorithm instead of learning a new one.
|
||||
|
||||
@section tutorials Tutorials
|
||||
|
||||
Tutorials for ParadisEO-SMP are available in the "Tutorials section" of the <a href="http://paradiseo.gforge.inria.fr">ParadisEO website</a>.
|
||||
|
||||
@section LICENSE
|
||||
|
||||
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
|
||||
|
||||
*/
|
||||
|
||||
/** @page webpages Related webpages
|
||||
|
||||
- ParadisEO <a href="http://paradiseo.gforge.inria.fr">homepage</a>
|
||||
- INRIA GForge <a href="http://gforge.inria.fr/projects/paradiseo/">project page</a>
|
||||
- <a href="../../README">README</a>
|
||||
*/
|
||||
1524
trunk/smp/doc/smp.doxyfile.cmake
Normal file
1524
trunk/smp/doc/smp.doxyfile.cmake
Normal file
File diff suppressed because it is too large
Load diff
43
trunk/smp/src/CMakeLists.txt
Normal file
43
trunk/smp/src/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
######################################################################################
|
||||
### 0) Include directories
|
||||
######################################################################################
|
||||
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
######################################################################################
|
||||
### 1) Set paths
|
||||
######################################################################################
|
||||
|
||||
set(SMP_LIB_OUTPUT_PATH ${SMP_BIN_DIR}/lib)
|
||||
set(LIBRARY_OUTPUT_PATH ${SMP_LIB_OUTPUT_PATH})
|
||||
|
||||
######################################################################################
|
||||
### 2) Build lib
|
||||
######################################################################################
|
||||
|
||||
set (SMP_FILE
|
||||
thread.cpp
|
||||
MWModel.h
|
||||
scheduler.h)
|
||||
|
||||
add_library(smp STATIC ${SMP_FILE})
|
||||
|
||||
install(TARGETS smp ARCHIVE DESTINATION ${LIB} COMPONENT libraries)
|
||||
|
||||
######################################################################################
|
||||
### 3) Look for headers
|
||||
######################################################################################
|
||||
|
||||
file(GLOB HDRS SMP)
|
||||
install(FILES ${HDRS} DESTINATION include${INSTALL_SUB_DIR}/smp COMPONENT headers)
|
||||
|
||||
######################################################################################
|
||||
### 4) Install directories
|
||||
######################################################################################
|
||||
|
||||
install(DIRECTORY algo
|
||||
DESTINATION include${INSTALL_SUB_DIR}/smp
|
||||
COMPONENT headers
|
||||
FILES_MATCHING PATTERN "*.h"
|
||||
)
|
||||
|
||||
37
trunk/smp/src/MWAlgo/MWAlgo.h
Normal file
37
trunk/smp/src/MWAlgo/MWAlgo.h
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
<MWAlgo.h>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012
|
||||
|
||||
Alexandre Quemy
|
||||
|
||||
This software is governed by the CeCILL license under French law and
|
||||
abiding by the rules of distribution of free software. You can ue,
|
||||
modify and/ or redistribute the software under the terms of the CeCILL
|
||||
license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
"http://www.cecill.info".
|
||||
|
||||
In this respect, the user's attention is drawn to the risks associated
|
||||
with loading, using, modifying and/or developing or reproducing the
|
||||
software by the user in light of its specific status of free software,
|
||||
that may mean that it is complicated to manipulate, and that also
|
||||
therefore means that it is reserved for developers and experienced
|
||||
professionals having in-depth computer knowledge. Users are therefore
|
||||
encouraged to load and test the software's suitability as regards their
|
||||
requirements in conditions enabling the security of their systems and/or
|
||||
data to be ensured and, more generally, to use and operate it in the
|
||||
same conditions as regards security.
|
||||
The fact that you are presently reading this means that you have had
|
||||
knowledge of the CeCILL license and that you accept its terms.
|
||||
|
||||
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
|
||||
Contact: paradiseo-help@lists.gforge.inria.fr
|
||||
*/
|
||||
|
||||
#ifndef MWAlGO_H
|
||||
#define MWAlGO_H
|
||||
|
||||
#include <MWAlgo/eoEasyEA.cpp>
|
||||
#include <MWAlgo/eoEasyPSO.cpp>
|
||||
#include <MWAlgo/eoSyncEasyPSO.cpp>
|
||||
|
||||
#endif
|
||||
43
trunk/smp/src/MWAlgo/eoEasyEA.cpp
Normal file
43
trunk/smp/src/MWAlgo/eoEasyEA.cpp
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
template<template <class> class EOAlgo, class EOT>
|
||||
void paradiseo::smp::MWModel<EOAlgo,EOT>::operator()(eoPop<EOT>& _pop, const eoEasyEA_tag&)
|
||||
{
|
||||
|
||||
if (this->isFirstCall)
|
||||
{
|
||||
size_t total_capacity = _pop.capacity() + this->offspring.capacity();
|
||||
_pop.reserve(total_capacity);
|
||||
this->offspring.reserve(total_capacity);
|
||||
this->isFirstCall = false;
|
||||
}
|
||||
|
||||
eoPop<EOT> empty_pop;
|
||||
|
||||
this->popEval(empty_pop, _pop); // A first eval of pop.
|
||||
|
||||
do
|
||||
{
|
||||
try
|
||||
{
|
||||
unsigned pSize = _pop.size();
|
||||
this->offspring.clear(); // new offspring
|
||||
this->breed(_pop, this->offspring);
|
||||
|
||||
scheduler(EOAlgo<EOT>::eval, this->offspring); // eval of parents + offspring if necessary
|
||||
|
||||
this->replace(_pop, this->offspring); // after replace, the new pop. is in _pop
|
||||
|
||||
if (pSize > _pop.size())
|
||||
throw std::runtime_error("Population shrinking!");
|
||||
else if (pSize < _pop.size())
|
||||
throw std::runtime_error("Population growing!");
|
||||
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
std::string s = e.what();
|
||||
s.append( " in eoEasyEA");
|
||||
throw std::runtime_error( s );
|
||||
}
|
||||
}
|
||||
while (this->continuator( _pop ) );
|
||||
}
|
||||
35
trunk/smp/src/MWAlgo/eoEasyPSO.cpp
Normal file
35
trunk/smp/src/MWAlgo/eoEasyPSO.cpp
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
template<template <class> class EOAlgo, class EOT>
|
||||
void paradiseo::smp::MWModel<EOAlgo,EOT>::operator()(eoPop<EOT>& _pop, const eoEasyPSO_tag&)
|
||||
{
|
||||
try
|
||||
{
|
||||
// initializes the topology, velocity, best particle(s)
|
||||
this->init();
|
||||
do
|
||||
{
|
||||
// loop over all the particles for the current iteration
|
||||
for (unsigned idx = 0; idx < _pop.size (); idx++)
|
||||
{
|
||||
// perform velocity evaluation
|
||||
this->velocity (_pop[idx],idx);
|
||||
|
||||
// apply the flight
|
||||
this->flight (_pop[idx]);
|
||||
}
|
||||
|
||||
// evaluate the position
|
||||
scheduler(EOAlgo<EOT>::eval, _pop);
|
||||
|
||||
for (unsigned idx = 0; idx < _pop.size (); idx++)
|
||||
// update the topology (particle and local/global best(s))
|
||||
this->velocity.updateNeighborhood(_pop[idx],idx);
|
||||
|
||||
} while (this->continuator (_pop));
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::string s = e.what ();
|
||||
s.append (" in eoEasyPSO");
|
||||
throw std::runtime_error (s);
|
||||
}
|
||||
}
|
||||
36
trunk/smp/src/MWAlgo/eoSyncEasyPSO.cpp
Normal file
36
trunk/smp/src/MWAlgo/eoSyncEasyPSO.cpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
template<template <class> class EOAlgo, class EOT>
|
||||
void paradiseo::smp::MWModel<EOAlgo,EOT>::operator()(eoPop<EOT>& _pop, const eoSyncEasyPSO_tag&)
|
||||
{
|
||||
try
|
||||
{
|
||||
// initializes the topology, velocity, best particle(s)
|
||||
this->init();
|
||||
|
||||
// just to use a loop eval
|
||||
eoPop<EOT> empty_pop;
|
||||
|
||||
do
|
||||
{
|
||||
// perform velocity evaluation
|
||||
//scheduler(this->velocity, _pop);
|
||||
this->velocity.apply(_pop);
|
||||
|
||||
// apply the flight
|
||||
this->flight.apply(_pop);
|
||||
|
||||
// evaluate the position (with a loop eval, empty_swarm IS USELESS)
|
||||
scheduler(EOAlgo<EOT>::eval, _pop);
|
||||
|
||||
// update the topology (particle and local/global best(s))
|
||||
this->velocity.updateNeighborhood(_pop);
|
||||
|
||||
} while (this->continuator(_pop));
|
||||
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
std::string s = e.what ();
|
||||
s.append (" in eoSyncEasyPSO");
|
||||
throw std::runtime_error (s);
|
||||
}
|
||||
}
|
||||
71
trunk/smp/src/MWModel.cpp
Normal file
71
trunk/smp/src/MWModel.cpp
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
<MWModel.cpp>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012
|
||||
|
||||
Alexandre Quemy
|
||||
|
||||
This software is governed by the CeCILL license under French law and
|
||||
abiding by the rules of distribution of free software. You can ue,
|
||||
modify and/ or redistribute the software under the terms of the CeCILL
|
||||
license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
"http://www.cecill.info".
|
||||
|
||||
In this respect, the user's attention is drawn to the risks associated
|
||||
with loading, using, modifying and/or developing or reproducing the
|
||||
software by the user in light of its specific status of free software,
|
||||
that may mean that it is complicated to manipulate, and that also
|
||||
therefore means that it is reserved for developers and experienced
|
||||
professionals having in-depth computer knowledge. Users are therefore
|
||||
encouraged to load and test the software's suitability as regards their
|
||||
requirements in conditions enabling the security of their systems and/or
|
||||
data to be ensured and, more generally, to use and operate it in the
|
||||
same conditions as regards security.
|
||||
The fact that you are presently reading this means that you have had
|
||||
knowledge of the CeCILL license and that you accept its terms.
|
||||
|
||||
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
|
||||
Contact: paradiseo-help@lists.gforge.inria.fr
|
||||
*/
|
||||
|
||||
template<template <class> class EOAlgo, class EOT>
|
||||
template<class... Args>
|
||||
paradiseo::smp::MWModel<EOAlgo,EOT>::MWModel (unsigned workersNb, Args&... args) :
|
||||
EOAlgo<EOT>(args...),
|
||||
scheduler(workersNb-1)
|
||||
{ assert(workersNb > 0); }
|
||||
|
||||
template<template <class> class EOAlgo, class EOT>
|
||||
template<class... Args>
|
||||
paradiseo::smp::MWModel<EOAlgo,EOT>::MWModel (Args&... args) :
|
||||
MWModel(Thread::hardware_concurrency(), args...)
|
||||
{}
|
||||
|
||||
template<template <class> class EOAlgo, class EOT>
|
||||
paradiseo::smp::MWModel<EOAlgo,EOT>::~MWModel ()
|
||||
{}
|
||||
|
||||
template<template <class> class EOAlgo, class EOT>
|
||||
void paradiseo::smp::MWModel<EOAlgo,EOT>::apply(eoUF<EOT&, void>& func, eoPop<EOT>& pop)
|
||||
{
|
||||
scheduler(func, pop);
|
||||
}
|
||||
|
||||
template<template <class> class EOAlgo, class EOT>
|
||||
void paradiseo::smp::MWModel<EOAlgo,EOT>::evaluate(eoPop<EOT>& pop)
|
||||
{
|
||||
scheduler(this->eval, pop);
|
||||
}
|
||||
|
||||
template<template <class> class EOAlgo, class EOT>
|
||||
void paradiseo::smp::MWModel<EOAlgo,EOT>::operator()(eoPop<EOT>& pop)
|
||||
{
|
||||
// Call the tag dispatcher
|
||||
operator()(pop,typename traits<EOAlgo,EOT>::type());
|
||||
}
|
||||
|
||||
template<template <class> class EOAlgo, class EOT>
|
||||
void paradiseo::smp::MWModel<EOAlgo,EOT>::operator()(eoPop<EOT>& pop, const error_tag&)
|
||||
{
|
||||
throw std::runtime_error("This is not a valid algorithm");
|
||||
}
|
||||
|
||||
127
trunk/smp/src/MWModel.h
Normal file
127
trunk/smp/src/MWModel.h
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
<MWModel.h>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012
|
||||
|
||||
Alexandre Quemy
|
||||
|
||||
This software is governed by the CeCILL license under French law and
|
||||
abiding by the rules of distribution of free software. You can ue,
|
||||
modify and/ or redistribute the software under the terms of the CeCILL
|
||||
license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
"http://www.cecill.info".
|
||||
|
||||
In this respect, the user's attention is drawn to the risks associated
|
||||
with loading, using, modifying and/or developing or reproducing the
|
||||
software by the user in light of its specific status of free software,
|
||||
that may mean that it is complicated to manipulate, and that also
|
||||
therefore means that it is reserved for developers and experienced
|
||||
professionals having in-depth computer knowledge. Users are therefore
|
||||
encouraged to load and test the software's suitability as regards their
|
||||
requirements in conditions enabling the security of their systems and/or
|
||||
data to be ensured and, more generally, to use and operate it in the
|
||||
same conditions as regards security.
|
||||
The fact that you are presently reading this means that you have had
|
||||
knowledge of the CeCILL license and that you accept its terms.
|
||||
|
||||
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
|
||||
Contact: paradiseo-help@lists.gforge.inria.fr
|
||||
*/
|
||||
|
||||
#ifndef MWMODEL_H_
|
||||
#define MWMODEL_H_
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include <scheduler.h>
|
||||
#include <tagDispatching.h>
|
||||
|
||||
|
||||
|
||||
#include <eo>
|
||||
|
||||
|
||||
namespace paradiseo
|
||||
{
|
||||
namespace smp
|
||||
{
|
||||
|
||||
/** MWModel: Master / Worker Model for multicore computation
|
||||
|
||||
The MW Model wraps any algorithm in order to apply it on several populations.
|
||||
|
||||
@see smp::Worker, smp::Thread
|
||||
*/
|
||||
|
||||
template<template <class> class EOAlgo, class EOT>
|
||||
class MWModel : public EOAlgo<EOT>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Constructor
|
||||
* @param workersNb the number of workers including the master
|
||||
* @param args... list of parameters according to the constructor of your algorithm
|
||||
*/
|
||||
template<class... Args>
|
||||
MWModel(unsigned workersNb, Args&... args);
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param args... list of parameters according to the constructor of your algorithm
|
||||
*/
|
||||
template<class... Args>
|
||||
MWModel(Args&... args);
|
||||
|
||||
~MWModel();
|
||||
|
||||
/**
|
||||
* Apply an unary functor to the population
|
||||
* @param func unary functor
|
||||
* @param pop population
|
||||
*/
|
||||
void apply(eoUF<EOT&, void>& func, eoPop<EOT>& pop);
|
||||
|
||||
/**
|
||||
* Evaluate the population
|
||||
* @param pop population to evaluate
|
||||
*/
|
||||
void evaluate(eoPop<EOT>& pop);
|
||||
|
||||
/**
|
||||
* Run the algorithm on population
|
||||
* @param pop population to run the algorithm
|
||||
*/
|
||||
void operator()(eoPop<EOT>& pop);
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Specific algorithm for eoEasyEA
|
||||
*/
|
||||
void operator()(eoPop<EOT>& pop, const eoEasyEA_tag&);
|
||||
|
||||
/**
|
||||
* Specifid algorithm for EasyPSO
|
||||
*/
|
||||
void operator()(eoPop<EOT>& pop, const eoEasyPSO_tag&);
|
||||
|
||||
/**
|
||||
* Specific algorithm for eoSyncEasyPSO
|
||||
*/
|
||||
void operator()(eoPop<EOT>& pop, const eoSyncEasyPSO_tag&);
|
||||
|
||||
/**
|
||||
* If we don't know the algorithm type
|
||||
*/
|
||||
void operator()(eoPop<EOT>& pop,const error_tag&);
|
||||
|
||||
std::vector<Thread*> workers;
|
||||
Scheduler<EOT> scheduler;
|
||||
};
|
||||
|
||||
#include <MWModel.cpp>
|
||||
#include <MWAlgo/MWAlgo.h>
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
97
trunk/smp/src/scheduler.cpp
Normal file
97
trunk/smp/src/scheduler.cpp
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
<scheduler.cpp>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012
|
||||
|
||||
Alexandre Quemy
|
||||
|
||||
This software is governed by the CeCILL license under French law and
|
||||
abiding by the rules of distribution of free software. You can ue,
|
||||
modify and/ or redistribute the software under the terms of the CeCILL
|
||||
license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
"http://www.cecill.info".
|
||||
|
||||
In this respect, the user's attention is drawn to the risks associated
|
||||
with loading, using, modifying and/or developing or reproducing the
|
||||
software by the user in light of its specific status of free software,
|
||||
that may mean that it is complicated to manipulate, and that also
|
||||
therefore means that it is reserved for developers and experienced
|
||||
professionals having in-depth computer knowledge. Users are therefore
|
||||
encouraged to load and test the software's suitability as regards their
|
||||
requirements in conditions enabling the security of their systems and/or
|
||||
data to be ensured and, more generally, to use and operate it in the
|
||||
same conditions as regards security.
|
||||
The fact that you are presently reading this means that you have had
|
||||
knowledge of the CeCILL license and that you accept its terms.
|
||||
|
||||
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
|
||||
Contact: paradiseo-help@lists.gforge.inria.fr
|
||||
*/
|
||||
|
||||
template<class EOT>
|
||||
paradiseo::smp::Scheduler<EOT>::Scheduler(unsigned workersNb) :
|
||||
workers(workersNb),
|
||||
popPackages(workersNb),
|
||||
done(false),
|
||||
planning(workersNb),
|
||||
idWaitingThread(-1)
|
||||
{ }
|
||||
|
||||
template<class EOT>
|
||||
paradiseo::smp::Scheduler<EOT>::~Scheduler()
|
||||
{ }
|
||||
|
||||
template<class EOT>
|
||||
void paradiseo::smp::Scheduler<EOT>::operator()(eoUF<EOT&, void>& func, eoPop<EOT>& pop)
|
||||
{
|
||||
done = false;
|
||||
idWaitingThread = -1;
|
||||
|
||||
for(unsigned i = 0; i < workers.size(); i++)
|
||||
{
|
||||
planning[i] = 2;
|
||||
workers[i].start(&Scheduler<EOT>::apply, this, std::ref(func), std::ref(popPackages[i]), i);
|
||||
}
|
||||
|
||||
unsigned counter = 0;
|
||||
unsigned j = 0;
|
||||
|
||||
while(counter < pop.size())
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m);
|
||||
cvt.wait(lock, [this]() -> bool {return (int)idWaitingThread != -1;});
|
||||
j = 0;
|
||||
while (j < planning[idWaitingThread] && counter < pop.size())
|
||||
{
|
||||
popPackages[idWaitingThread].push_back(&pop[counter]);
|
||||
counter++;
|
||||
j++;
|
||||
}
|
||||
planning[idWaitingThread] *= 2;
|
||||
idWaitingThread = -1;
|
||||
cv.notify_one();
|
||||
}
|
||||
|
||||
done = true;
|
||||
idWaitingThread = -1;
|
||||
cv.notify_all();
|
||||
|
||||
for(unsigned i = 0; i < workers.size(); i++)
|
||||
workers[i].join();
|
||||
}
|
||||
|
||||
template<class EOT>
|
||||
void paradiseo::smp::Scheduler<EOT>::apply(eoUF<EOT&, void>& func, std::vector<EOT*>& pop, int id)
|
||||
{
|
||||
while(!done || !pop.empty())
|
||||
{
|
||||
for(unsigned i = 0; i < pop.size(); i++)
|
||||
func(*pop[i]);
|
||||
pop.clear();
|
||||
std::unique_lock<std::mutex> lock(m);
|
||||
idWaitingThread = id;
|
||||
// We notify the scheduler we finished the package
|
||||
cvt.notify_one();
|
||||
// We wait for a new package
|
||||
cv.wait(lock, [this]() -> bool {return (int)idWaitingThread == -1 || (bool)done;});
|
||||
}
|
||||
}
|
||||
105
trunk/smp/src/scheduler.h
Normal file
105
trunk/smp/src/scheduler.h
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
<scheduler.h>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012
|
||||
|
||||
Alexandre Quemy
|
||||
|
||||
This software is governed by the CeCILL license under French law and
|
||||
abiding by the rules of distribution of free software. You can ue,
|
||||
modify and/ or redistribute the software under the terms of the CeCILL
|
||||
license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
"http://www.cecill.info".
|
||||
|
||||
In this respect, the user's attention is drawn to the risks associated
|
||||
with loading, using, modifying and/or developing or reproducing the
|
||||
software by the user in light of its specific status of free software,
|
||||
that may mean that it is complicated to manipulate, and that also
|
||||
therefore means that it is reserved for developers and experienced
|
||||
professionals having in-depth computer knowledge. Users are therefore
|
||||
encouraged to load and test the software's suitability as regards their
|
||||
requirements in conditions enabling the security of their systems and/or
|
||||
data to be ensured and, more generally, to use and operate it in the
|
||||
same conditions as regards security.
|
||||
The fact that you are presently reading this means that you have had
|
||||
knowledge of the CeCILL license and that you accept its terms.
|
||||
|
||||
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
|
||||
Contact: paradiseo-help@lists.gforge.inria.fr
|
||||
*/
|
||||
|
||||
#ifndef SCHEDULER_H_
|
||||
#define SCHEDULER_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
#include <thread.h>
|
||||
|
||||
#include <eoEvalFunc.h>
|
||||
#include <eoPop.h>
|
||||
|
||||
namespace paradiseo
|
||||
{
|
||||
namespace smp
|
||||
{
|
||||
/**
|
||||
A scheduler class
|
||||
*/
|
||||
template<class EOT>
|
||||
class Scheduler
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Constructor
|
||||
* @param workersNb number of workers to perform tasks
|
||||
*/
|
||||
Scheduler(unsigned workersNb);
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
~Scheduler();
|
||||
|
||||
/**
|
||||
* Start an unary functor on workers on all the pop
|
||||
* @param func unary functor
|
||||
* @param pop reference to the population
|
||||
*/
|
||||
void operator()(eoUF<EOT&, void>& func, eoPop<EOT>& pop);
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Apply an unary functor on a sub-group of population
|
||||
* @param func unary functor
|
||||
* @param pop reference to the sub-group
|
||||
* @param id id of the thread
|
||||
*/
|
||||
void apply(eoUF<EOT&, void>& func, std::vector<EOT*>& pop, int id);
|
||||
|
||||
/**
|
||||
* Create sub-groups with similar size from a population.
|
||||
* @param pop reference to the pop
|
||||
*/
|
||||
std::vector<std::vector<EOT*>> subGroups(eoPop<EOT>& pop);
|
||||
|
||||
std::vector<Thread> workers;
|
||||
std::vector<std::vector<EOT*>> popPackages;
|
||||
std::mutex m;
|
||||
std::atomic<bool> done;
|
||||
std::vector<unsigned> planning;
|
||||
std::atomic<int> idWaitingThread;
|
||||
|
||||
std::condition_variable cv;
|
||||
std::condition_variable cvt;
|
||||
};
|
||||
|
||||
#include <scheduler.cpp>
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif /*SCHEDULER_H_*/
|
||||
35
trunk/smp/src/smp
Normal file
35
trunk/smp/src/smp
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
<smp>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012
|
||||
|
||||
Alexandre Quemy
|
||||
|
||||
This software is governed by the CeCILL license under French law and
|
||||
abiding by the rules of distribution of free software. You can ue,
|
||||
modify and/ or redistribute the software under the terms of the CeCILL
|
||||
license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
"http://www.cecill.info".
|
||||
|
||||
In this respect, the user's attention is drawn to the risks associated
|
||||
with loading, using, modifying and/or developing or reproducing the
|
||||
software by the user in light of its specific status of free software,
|
||||
that may mean that it is complicated to manipulate, and that also
|
||||
therefore means that it is reserved for developers and experienced
|
||||
professionals having in-depth computer knowledge. Users are therefore
|
||||
encouraged to load and test the software's suitability as regards their
|
||||
requirements in conditions enabling the security of their systems and/or
|
||||
data to be ensured and, more generally, to use and operate it in the
|
||||
same conditions as regards security.
|
||||
The fact that you are presently reading this means that you have had
|
||||
knowledge of the CeCILL license and that you accept its terms.
|
||||
|
||||
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
|
||||
Contact: paradiseo-help@lists.gforge.inria.fr
|
||||
*/
|
||||
|
||||
#ifndef SMP_INCLUDE
|
||||
#define SMP_INCLUDE
|
||||
|
||||
#include <smp.h>
|
||||
|
||||
#endif
|
||||
37
trunk/smp/src/smp.h
Normal file
37
trunk/smp/src/smp.h
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
<smp.h>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012
|
||||
|
||||
Alexandre Quemy
|
||||
|
||||
This software is governed by the CeCILL license under French law and
|
||||
abiding by the rules of distribution of free software. You can ue,
|
||||
modify and/ or redistribute the software under the terms of the CeCILL
|
||||
license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
"http://www.cecill.info".
|
||||
|
||||
In this respect, the user's attention is drawn to the risks associated
|
||||
with loading, using, modifying and/or developing or reproducing the
|
||||
software by the user in light of its specific status of free software,
|
||||
that may mean that it is complicated to manipulate, and that also
|
||||
therefore means that it is reserved for developers and experienced
|
||||
professionals having in-depth computer knowledge. Users are therefore
|
||||
encouraged to load and test the software's suitability as regards their
|
||||
requirements in conditions enabling the security of their systems and/or
|
||||
data to be ensured and, more generally, to use and operate it in the
|
||||
same conditions as regards security.
|
||||
The fact that you are presently reading this means that you have had
|
||||
knowledge of the CeCILL license and that you accept its terms.
|
||||
|
||||
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
|
||||
Contact: paradiseo-help@lists.gforge.inria.fr
|
||||
*/
|
||||
|
||||
#ifndef SMP_H
|
||||
#define SMP_H
|
||||
|
||||
#include <thread.h>
|
||||
#include <MWModel.h>
|
||||
#include <scheduler.h>
|
||||
|
||||
#endif
|
||||
68
trunk/smp/src/tagDispatching.h
Normal file
68
trunk/smp/src/tagDispatching.h
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
<tagDispatching.h>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012
|
||||
|
||||
Alexandre Quemy
|
||||
|
||||
This software is governed by the CeCILL license under French law and
|
||||
abiding by the rules of distribution of free software. You can ue,
|
||||
modify and/ or redistribute the software under the terms of the CeCILL
|
||||
license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
"http://www.cecill.info".
|
||||
|
||||
In this respect, the user's attention is drawn to the risks associated
|
||||
with loading, using, modifying and/or developing or reproducing the
|
||||
software by the user in light of its specific status of free software,
|
||||
that may mean that it is complicated to manipulate, and that also
|
||||
therefore means that it is reserved for developers and experienced
|
||||
professionals having in-depth computer knowledge. Users are therefore
|
||||
encouraged to load and test the software's suitability as regards their
|
||||
requirements in conditions enabling the security of their systems and/or
|
||||
data to be ensured and, more generally, to use and operate it in the
|
||||
same conditions as regards security.
|
||||
The fact that you are presently reading this means that you have had
|
||||
knowledge of the CeCILL license and that you accept its terms.
|
||||
|
||||
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
|
||||
Contact: paradiseo-help@lists.gforge.inria.fr
|
||||
*/
|
||||
|
||||
#include <eo>
|
||||
|
||||
namespace paradiseo
|
||||
{
|
||||
namespace smp
|
||||
{
|
||||
/** Tag Dispatching
|
||||
* The Tag Dispatching enable to choose the algorithm to call at compile time
|
||||
**/
|
||||
|
||||
// Main algorithms tags
|
||||
struct error_tag {};
|
||||
struct eoEasyEA_tag {};
|
||||
struct eoEasyPSO_tag {};
|
||||
struct eoSyncEasyPSO_tag {};
|
||||
|
||||
template<template <class> class A, class B>
|
||||
struct traits {
|
||||
typedef error_tag type;
|
||||
};
|
||||
|
||||
template<class B>
|
||||
struct traits<eoEasyEA,B> {
|
||||
typedef eoEasyEA_tag type;
|
||||
};
|
||||
|
||||
template<class B>
|
||||
struct traits<eoEasyPSO,B> {
|
||||
typedef eoEasyPSO_tag type;
|
||||
};
|
||||
|
||||
template<class B>
|
||||
struct traits<eoSyncEasyPSO,B> {
|
||||
typedef eoSyncEasyPSO_tag type;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
73
trunk/smp/src/thread.cpp
Normal file
73
trunk/smp/src/thread.cpp
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
<thread.cpp>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012
|
||||
|
||||
Alexandre Quemy
|
||||
|
||||
This software is governed by the CeCILL license under French law and
|
||||
abiding by the rules of distribution of free software. You can ue,
|
||||
modify and/ or redistribute the software under the terms of the CeCILL
|
||||
license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
"http://www.cecill.info".
|
||||
|
||||
In this respect, the user's attention is drawn to the risks associated
|
||||
with loading, using, modifying and/or developing or reproducing the
|
||||
software by the user in light of its specific status of free software,
|
||||
that may mean that it is complicated to manipulate, and that also
|
||||
therefore means that it is reserved for developers and experienced
|
||||
professionals having in-depth computer knowledge. Users are therefore
|
||||
encouraged to load and test the software's suitability as regards their
|
||||
requirements in conditions enabling the security of their systems and/or
|
||||
data to be ensured and, more generally, to use and operate it in the
|
||||
same conditions as regards security.
|
||||
The fact that you are presently reading this means that you have had
|
||||
knowledge of the CeCILL license and that you accept its terms.
|
||||
|
||||
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
|
||||
Contact: paradiseo-help@lists.gforge.inria.fr
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <thread.h>
|
||||
|
||||
namespace paradiseo
|
||||
{
|
||||
namespace smp
|
||||
{
|
||||
|
||||
Thread::Thread(Thread&& other)
|
||||
{
|
||||
t = std::move(other.t);
|
||||
}
|
||||
|
||||
Thread& Thread::operator=(Thread&& other)
|
||||
{
|
||||
t = std::move(other.t);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const std::thread::id paradiseo::smp::Thread::getId() const
|
||||
{
|
||||
return t.get_id();
|
||||
}
|
||||
|
||||
bool paradiseo::smp::Thread::joinable() const
|
||||
{
|
||||
return t.joinable();
|
||||
}
|
||||
|
||||
void paradiseo::smp::Thread::join()
|
||||
{
|
||||
t.join();
|
||||
}
|
||||
|
||||
int paradiseo::smp::Thread::hardware_concurrency()
|
||||
{
|
||||
return std::thread::hardware_concurrency();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
112
trunk/smp/src/thread.h
Normal file
112
trunk/smp/src/thread.h
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
<thread.h>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2012
|
||||
|
||||
Alexandre Quemy
|
||||
|
||||
This software is governed by the CeCILL license under French law and
|
||||
abiding by the rules of distribution of free software. You can ue,
|
||||
modify and/ or redistribute the software under the terms of the CeCILL
|
||||
license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
"http://www.cecill.info".
|
||||
|
||||
In this respect, the user's attention is drawn to the risks associated
|
||||
with loading, using, modifying and/or developing or reproducing the
|
||||
software by the user in light of its specific status of free software,
|
||||
that may mean that it is complicated to manipulate, and that also
|
||||
therefore means that it is reserved for developers and experienced
|
||||
professionals having in-depth computer knowledge. Users are therefore
|
||||
encouraged to load and test the software's suitability as regards their
|
||||
requirements in conditions enabling the security of their systems and/or
|
||||
data to be ensured and, more generally, to use and operate it in the
|
||||
same conditions as regards security.
|
||||
The fact that you are presently reading this means that you have had
|
||||
knowledge of the CeCILL license and that you accept its terms.
|
||||
|
||||
ParadisEO WebSite : http://paradiseo.gforge.inria.fr
|
||||
Contact: paradiseo-help@lists.gforge.inria.fr
|
||||
*/
|
||||
|
||||
#ifndef THREAD_H_
|
||||
#define THREAD_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace paradiseo
|
||||
{
|
||||
namespace smp
|
||||
{
|
||||
/**
|
||||
A thread class which encapsulate the std::thread behaviour for more flexibility
|
||||
@see smp::Worker, smp::MWModel
|
||||
*/
|
||||
class Thread
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Default constructor
|
||||
*/
|
||||
Thread() = default;
|
||||
|
||||
/**
|
||||
* Constructor that will immediatly start the thread
|
||||
* @param f represente any callable object such as function or class method
|
||||
* @param args... reference object and parameters for f
|
||||
*/
|
||||
template< class Callable, class... Args >
|
||||
explicit Thread(Callable&& f, Args&&... args) : t(std::thread(std::forward<Callable>(f), std::forward<Args>(args)...)) {}
|
||||
|
||||
Thread(Thread&& other);
|
||||
|
||||
Thread(const Thread&) = delete;
|
||||
Thread& operator=(const Thread&) = delete;
|
||||
|
||||
virtual ~Thread() = default;
|
||||
|
||||
Thread& operator=(Thread&& other);
|
||||
|
||||
/**
|
||||
* Start the thread according to parameters
|
||||
* If the thread is running, it will wait until the end of its task
|
||||
* @param f represente any callable object such as function or class method
|
||||
* @param args... reference object and parameters for f
|
||||
*/
|
||||
template<class Callable,class... Args>
|
||||
void start(Callable&& f,Args&&... args);
|
||||
|
||||
/**
|
||||
* Get the id of the thread
|
||||
* @return id of the thread
|
||||
*/
|
||||
const std::thread::id getId() const;
|
||||
|
||||
/**
|
||||
* Get the state of the thread
|
||||
* @return true if the thread is running, false otherwise
|
||||
*/
|
||||
bool joinable() const;
|
||||
|
||||
/**
|
||||
* Wait until the end of the task
|
||||
*/
|
||||
void join();
|
||||
|
||||
static int hardware_concurrency();
|
||||
|
||||
protected:
|
||||
std::thread t;
|
||||
};
|
||||
|
||||
template<class Callable,class... Args>
|
||||
void paradiseo::smp::Thread::start(Callable&& f,Args&&... args)
|
||||
{
|
||||
t = std::thread(std::forward<Callable>(f), std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif /*THREAD_H_*/
|
||||
40
trunk/smp/test/CMakeLists.txt
Normal file
40
trunk/smp/test/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
######################################################################################
|
||||
### 0) Include headers
|
||||
######################################################################################
|
||||
|
||||
include_directories(${EO_SRC_DIR}/src)
|
||||
include_directories(${SMP_SRC_DIR}/src)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
######################################################################################
|
||||
### 1) Define test list
|
||||
######################################################################################
|
||||
|
||||
set (TEST_LIST
|
||||
t-smpThread
|
||||
t-smpScheduler
|
||||
t-smpMW_eoEasyEA
|
||||
t-smpMW_eoEasyPSO
|
||||
t-smpMW_eoSyncEasyPSO
|
||||
)
|
||||
|
||||
######################################################################################
|
||||
### 3) Create each test
|
||||
######################################################################################
|
||||
|
||||
foreach (test ${TEST_LIST})
|
||||
set ("T_${test}_SOURCES" "${test}.cpp")
|
||||
add_executable(${test} ${T_${test}_SOURCES})
|
||||
target_link_libraries(${test} smp eo eoutils)
|
||||
add_test(${test} ${test})
|
||||
endforeach (test)
|
||||
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/t-data.dat
|
||||
${CMAKE_CURRENT_BINARY_DIR}/t-data.dat)
|
||||
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/bench.sh
|
||||
${CMAKE_CURRENT_BINARY_DIR}/bench.sh)
|
||||
272
trunk/smp/test/smpTestClass.h
Normal file
272
trunk/smp/test/smpTestClass.h
Normal file
|
|
@ -0,0 +1,272 @@
|
|||
#ifndef _SMPTEST_H
|
||||
#define _SMPTEST_H
|
||||
#include <stdlib.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int n = 10;
|
||||
int** a;
|
||||
int** b;
|
||||
|
||||
int bkv; //best known value
|
||||
|
||||
struct parameters
|
||||
{
|
||||
unsigned seed ;
|
||||
int popSize;
|
||||
int tSize;
|
||||
string inst;
|
||||
string loadName;
|
||||
string schema;
|
||||
double pCross;
|
||||
double pMut;
|
||||
int minGen;
|
||||
int maxGen;
|
||||
};
|
||||
|
||||
class Indi : public EO<eoMinimizingFitness> {
|
||||
|
||||
public:
|
||||
|
||||
int* solution;
|
||||
int evalNb = 0;
|
||||
|
||||
Indi () {
|
||||
solution = new int[n];
|
||||
create();
|
||||
}
|
||||
|
||||
Indi (const Indi & _problem){ // copy constructor
|
||||
solution = new int[n];
|
||||
for (int i = 0; i < n ; i++){
|
||||
solution[i] = _problem.solution[i];
|
||||
}
|
||||
if (!_problem.invalid()) // if the solution has already been evaluated
|
||||
fitness(_problem.fitness()); // copy the fitness
|
||||
}
|
||||
|
||||
~Indi(){ // destructor
|
||||
delete[] solution;
|
||||
}
|
||||
|
||||
void operator= (const Indi & _problem){ // copy assignment operator
|
||||
for (int i = 0; i < n ; i++){
|
||||
solution[i] = _problem.solution[i];
|
||||
}
|
||||
fitness(_problem.fitness()); // copy the fitness
|
||||
}
|
||||
|
||||
int& operator[] (unsigned i)
|
||||
{
|
||||
return solution[i];
|
||||
}
|
||||
|
||||
|
||||
void create(){ // create and initialize a solution
|
||||
int random, temp;
|
||||
for (int i=0; i< n; i++)
|
||||
solution[i]=i;
|
||||
|
||||
// we want a random permutation so we shuffle
|
||||
for (int i = 0; i < n; i++){
|
||||
random = rand()%(n-i) + i;
|
||||
temp = solution[i];
|
||||
solution[i] = solution[random];
|
||||
solution[random] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
int evaluate() { // evaluate the solution
|
||||
int cost=0;
|
||||
for (int i=0; i<n; i++)
|
||||
for (int j=0; j<n; j++)
|
||||
cost += a[i][j] * b[solution[i]][solution[j]];
|
||||
evalNb++;
|
||||
//std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
//std::cout << "Evaluation " << evalNb << std::endl;
|
||||
|
||||
return cost;
|
||||
}
|
||||
|
||||
|
||||
void printSolution() {
|
||||
for (int i = 0; i < n ; i++)
|
||||
std::cout << solution[i] << " " ;
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
class IndiInit : public eoInit<Indi>
|
||||
{
|
||||
public:
|
||||
|
||||
void operator()(Indi & _problem)
|
||||
{
|
||||
_problem.create();
|
||||
}
|
||||
};
|
||||
|
||||
class IndiEvalFunc : public eoEvalFunc<Indi>
|
||||
{
|
||||
public:
|
||||
|
||||
void operator()(Indi & _problem)
|
||||
{
|
||||
_problem.fitness(_problem.evaluate());
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
class IndiXover : public eoQuadOp<Indi> {
|
||||
public:
|
||||
|
||||
/* The two parameters in input are the parents.
|
||||
The first parameter is also the output ie the child
|
||||
*/
|
||||
bool operator()(Indi & _problem1, Indi & _problem2)
|
||||
{
|
||||
int i;
|
||||
int random, temp;
|
||||
std::vector<int> unassigned_positions(n);
|
||||
std::vector<int> remaining_items(n);
|
||||
int j = 0;
|
||||
|
||||
/* 1) find the items assigned in different positions for the 2 parents */
|
||||
for (i = 0 ; i < n ; i++){
|
||||
if (_problem1.solution[i] != _problem2.solution[i]){
|
||||
unassigned_positions[j] = i;
|
||||
remaining_items[j] = _problem1.solution[i];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
/* 2) shuffle the remaining items to ensure that remaining items
|
||||
will be assigned at random positions */
|
||||
for (i = 0; i < j; i++){
|
||||
random = rand()%(j-i) + i;
|
||||
temp = remaining_items[i];
|
||||
remaining_items[i] = remaining_items[random];
|
||||
remaining_items[random] = temp;
|
||||
}
|
||||
|
||||
/* 3) copy the shuffled remaining items at unassigned positions */
|
||||
for (i = 0; i < j ; i++)
|
||||
_problem1.solution[unassigned_positions[i]] = remaining_items[i];
|
||||
|
||||
// crossover in our case is always possible
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
class IndiSwapMutation: public eoMonOp<Indi> {
|
||||
public:
|
||||
|
||||
bool operator()(Indi& _problem) {
|
||||
int i,j;
|
||||
int temp;
|
||||
|
||||
// generate two different indices
|
||||
i=rand()%n;
|
||||
do
|
||||
j = rand()%n;
|
||||
while (i == j);
|
||||
|
||||
// swap
|
||||
temp = _problem.solution[i];
|
||||
_problem.solution[i] = _problem.solution[j];
|
||||
_problem.solution[j] = temp;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
void parseFile(eoParser & parser, parameters & param)
|
||||
{
|
||||
|
||||
// For each parameter, you can in on single line
|
||||
// define the parameter, read it through the parser, and assign it
|
||||
|
||||
param.seed = parser.createParam(unsigned(time(0)), "seed", "Random number seed", 'S').value(); // will be in default section General
|
||||
|
||||
// init and stop
|
||||
param.loadName = parser.createParam(string(""), "Load","A save file to restart from",'L', "Persistence" ).value();
|
||||
|
||||
param.inst = parser.createParam(string(""), "inst","a dat file to read instances from",'i', "Persistence" ).value();
|
||||
|
||||
param.schema = parser.createParam(string(""), "schema","an xml file mapping process",'s', "Persistence" ).value();
|
||||
|
||||
param.popSize = parser.createParam(unsigned(10), "popSize", "Population size",'P', "Evolution engine" ).value();
|
||||
|
||||
param.tSize = parser.createParam(unsigned(2), "tSize", "Tournament size",'T', "Evolution Engine" ).value();
|
||||
|
||||
param.minGen = parser.createParam(unsigned(100), "minGen", "Minimum number of iterations",'g', "Stopping criterion" ).value();
|
||||
|
||||
param.maxGen = parser.createParam(unsigned(300), "maxGen", "Maximum number of iterations",'G', "Stopping criterion" ).value();
|
||||
|
||||
param.pCross = parser.createParam(double(0.6), "pCross", "Probability of Crossover", 'C', "Genetic Operators" ).value();
|
||||
|
||||
param.pMut = parser.createParam(double(0.1), "pMut", "Probability of Mutation", 'M', "Genetic Operators" ).value();
|
||||
|
||||
|
||||
// the name of the "status" file where all actual parameter values will be saved
|
||||
string str_status = parser.ProgramName() + ".status"; // default value
|
||||
string statusName = parser.createParam(str_status, "status","Status file",'S', "Persistence" ).value();
|
||||
|
||||
// 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 (statusName != "")
|
||||
{
|
||||
ofstream os(statusName.c_str());
|
||||
os << parser; // and you can use that file as parameter file
|
||||
}
|
||||
}
|
||||
|
||||
void loadInstances(const char* filename, int& n, int& bkv, int** & a, int** & b)
|
||||
{
|
||||
|
||||
ifstream data_file;
|
||||
int i, j;
|
||||
data_file.open(filename);
|
||||
if (! data_file.is_open())
|
||||
{
|
||||
cout << "\n Error while reading the file " << filename << ". Please check if it exists !" << endl;
|
||||
exit (1);
|
||||
}
|
||||
data_file >> n;
|
||||
data_file >> bkv; // best known value
|
||||
// ****************** dynamic memory allocation ****************** /
|
||||
a = new int* [n];
|
||||
b = new int* [n];
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
a[i] = new int[n];
|
||||
b[i] = new int[n];
|
||||
}
|
||||
|
||||
// ************** read flows and distanceMatrixs matrices ************** /
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < n; j++)
|
||||
data_file >> a[i][j];
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < n; j++)
|
||||
data_file >> b[i][j];
|
||||
|
||||
data_file.close();
|
||||
}
|
||||
|
||||
#endif
|
||||
27
trunk/smp/test/t-data.dat
Executable file
27
trunk/smp/test/t-data.dat
Executable file
|
|
@ -0,0 +1,27 @@
|
|||
12 224416
|
||||
|
||||
0 27 85 2 1 15 11 35 11 20 21 61
|
||||
27 0 80 58 21 76 72 44 85 94 90 51
|
||||
85 80 0 3 48 29 90 66 41 15 83 96
|
||||
2 58 3 0 74 45 65 40 54 83 14 71
|
||||
1 21 48 74 0 77 36 53 37 26 87 76
|
||||
15 76 29 45 77 0 91 13 29 11 77 32
|
||||
11 72 90 65 36 91 0 87 67 94 79 2
|
||||
35 44 66 40 53 13 87 0 10 99 56 70
|
||||
11 85 41 54 37 29 67 10 0 99 60 4
|
||||
20 94 15 83 26 11 94 99 99 0 56 2
|
||||
21 90 83 14 87 77 79 56 60 56 0 60
|
||||
61 51 96 71 76 32 2 70 4 2 60 0
|
||||
|
||||
0 21 95 82 56 41 6 25 10 4 63 6
|
||||
21 0 44 40 75 79 0 89 35 9 1 85
|
||||
95 44 0 84 12 0 26 91 11 35 82 26
|
||||
82 40 84 0 69 56 86 45 91 59 18 76
|
||||
56 75 12 69 0 39 18 57 36 61 36 21
|
||||
41 79 0 56 39 0 71 11 29 82 82 6
|
||||
6 0 26 86 18 71 0 71 8 77 74 30
|
||||
25 89 91 45 57 11 71 0 89 76 76 40
|
||||
10 35 11 91 36 29 8 89 0 93 56 1
|
||||
4 9 35 59 61 82 77 76 93 0 50 4
|
||||
63 1 82 18 36 82 74 76 56 50 0 36
|
||||
6 85 26 76 21 6 30 40 1 4 36 0
|
||||
72
trunk/smp/test/t-smpMW_eoEasyEA.cpp
Normal file
72
trunk/smp/test/t-smpMW_eoEasyEA.cpp
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#include <MWModel.h>
|
||||
#include <cassert>
|
||||
#include <eo>
|
||||
|
||||
#include "smpTestClass.h"
|
||||
|
||||
using namespace paradiseo::smp;
|
||||
using namespace std;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
typedef struct {
|
||||
unsigned popSize = 10;
|
||||
unsigned tSize = 2;
|
||||
double pCross = 0.8;
|
||||
double pMut = 0.7;
|
||||
unsigned maxGen = 1000;
|
||||
} Param;
|
||||
|
||||
Param param;
|
||||
|
||||
rng.reseed(42);
|
||||
|
||||
loadInstances("t-data.dat", n, bkv, a, b);
|
||||
|
||||
// Evaluation function
|
||||
IndiEvalFunc plainEval;
|
||||
|
||||
// Init a solution
|
||||
IndiInit chromInit;
|
||||
|
||||
// Define selection
|
||||
eoDetTournamentSelect<Indi> selectOne(param.tSize);
|
||||
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
||||
|
||||
// Define operators for crossover and mutation
|
||||
IndiXover Xover; // CROSSOVER
|
||||
IndiSwapMutation mutationSwap; // MUTATION
|
||||
|
||||
// Encapsule in a tranform operator
|
||||
eoSGATransform<Indi> transform(Xover, param.pCross, mutationSwap, param.pMut);
|
||||
|
||||
// Define replace operator
|
||||
eoPlusReplacement<Indi> replace;
|
||||
|
||||
eoGenContinue<Indi> genCont(param.maxGen); // generation continuation
|
||||
|
||||
// Define population
|
||||
eoPop<Indi> pop(param.popSize, chromInit);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
MWModel<eoEasyEA,Indi> mw(genCont, plainEval, select, transform, replace);
|
||||
mw.evaluate(pop);
|
||||
std::cout << "Initial population :" << std::endl;
|
||||
pop.sort();
|
||||
std::cout << pop << std::endl;
|
||||
mw(pop);
|
||||
std::cout << "Final population :" << std::endl;
|
||||
pop.sort();
|
||||
std::cout << pop << std::endl;
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
assert(pop.nth_element_fitness(0) == 229092);
|
||||
|
||||
return 0;
|
||||
}
|
||||
104
trunk/smp/test/t-smpMW_eoEasyPSO.cpp
Normal file
104
trunk/smp/test/t-smpMW_eoEasyPSO.cpp
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// BinaryPSO.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// An instance of a VERY simple Real-coded binary Particle Swarm Optimization Algorithm
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include <eo>
|
||||
#include <smp>
|
||||
|
||||
// Use functions from namespace std
|
||||
using namespace std;
|
||||
using namespace paradiseo::smp;
|
||||
|
||||
typedef eoMinimizingFitness FitT;
|
||||
typedef eoBitParticle < FitT > Particle;
|
||||
|
||||
double binary_value (const Particle & _particle)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _particle.size(); i++)
|
||||
sum +=_particle[i];
|
||||
return (sum);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
|
||||
const unsigned int MAX_GEN=500;
|
||||
const unsigned int VEC_SIZE = 10;
|
||||
const unsigned int POP_SIZE = 20;
|
||||
const unsigned int NEIGHBORHOOD_SIZE= 3;
|
||||
|
||||
const double VELOCITY_INIT_MIN= -1;
|
||||
const double VELOCITY_INIT_MAX= 1;
|
||||
|
||||
const double VELOCITY_MIN= -1.5;
|
||||
const double VELOCITY_MAX= 1.5;
|
||||
|
||||
const double INERTIA= 1;
|
||||
const double LEARNING_FACTOR1= 1.7;
|
||||
const double LEARNING_FACTOR2= 2.3;
|
||||
|
||||
rng.reseed(SEED);
|
||||
|
||||
eoPop<Particle> pop;
|
||||
|
||||
eoEvalFuncPtr<Particle, double, const Particle& > eval(binary_value);
|
||||
eoRingTopology<Particle> topology(NEIGHBORHOOD_SIZE);
|
||||
|
||||
eoUniformGenerator<bool> uGen;
|
||||
eoInitFixedLength < Particle > random (VEC_SIZE, uGen);
|
||||
pop.append (POP_SIZE, random);
|
||||
|
||||
eoUniformGenerator < double >sGen (VELOCITY_INIT_MIN, VELOCITY_INIT_MAX);
|
||||
eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen);
|
||||
|
||||
|
||||
eoFirstIsBestInit < Particle > localInit;
|
||||
|
||||
eoInitializer <Particle> fullInit(eval,veloRandom,localInit,topology,pop);
|
||||
|
||||
fullInit();
|
||||
|
||||
pop.sort();
|
||||
|
||||
cout << "INITIAL POPULATION:" << endl;
|
||||
for (unsigned i = 0; i < pop.size(); ++i)
|
||||
cout << "\t best fit=" << pop[i] << endl;
|
||||
|
||||
eoRealVectorBounds bnds(VEC_SIZE,VELOCITY_MIN,VELOCITY_MAX);
|
||||
|
||||
eoStandardVelocity <Particle> velocity (topology,INERTIA,LEARNING_FACTOR1,LEARNING_FACTOR2,bnds);
|
||||
|
||||
eoSigBinaryFlight <Particle> flight;
|
||||
|
||||
eoGenContinue <Particle> genCont (MAX_GEN);
|
||||
|
||||
try
|
||||
{
|
||||
MWModel<eoEasyPSO, Particle> pso(genCont, eval, velocity, flight);
|
||||
|
||||
pso(pop);
|
||||
|
||||
pop.sort();
|
||||
cout << "FINAL POPULATION:" << endl;
|
||||
for (unsigned i = 0; i < pop.size(); ++i)
|
||||
{
|
||||
cout << "\t best fit=" << pop[i] << endl;
|
||||
}
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
//-----------------------------------------------------------------------------
|
||||
172
trunk/smp/test/t-smpMW_eoSyncEasyPSO.cpp
Normal file
172
trunk/smp/test/t-smpMW_eoSyncEasyPSO.cpp
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// BinaryPSO.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// An instance of a VERY simple Real-coded binary Particle Swarm Optimization Algorithm
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include <eo>
|
||||
#include <smp>
|
||||
|
||||
// Use functions from namespace std
|
||||
using namespace std;
|
||||
using namespace paradiseo::smp;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
typedef eoMinimizingFitness FitT;
|
||||
typedef eoBitParticle < FitT > Particle;
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
// EVALFUNC
|
||||
//-----------------------------------------------------------------------------
|
||||
// Just a simple function that takes binary value of a chromosome and sets
|
||||
// the fitness
|
||||
double binary_value (const Particle & _particle)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _particle.size(); i++)
|
||||
sum +=_particle[i];
|
||||
return (sum);
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// PARAMETRES
|
||||
// all parameters are hard-coded!
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
|
||||
const unsigned int MAX_GEN=50;
|
||||
const unsigned int VEC_SIZE = 10;
|
||||
const unsigned int POP_SIZE = 20;
|
||||
const unsigned int NEIGHBORHOOD_SIZE= 3;
|
||||
|
||||
const double VELOCITY_INIT_MIN= -1;
|
||||
const double VELOCITY_INIT_MAX= 1;
|
||||
|
||||
const double VELOCITY_MIN= -1.5;
|
||||
const double VELOCITY_MAX= 1.5;
|
||||
|
||||
const double INERTIA= 1;
|
||||
const double LEARNING_FACTOR1= 1.7;
|
||||
const double LEARNING_FACTOR2= 2.3;
|
||||
|
||||
|
||||
//////////////////////////
|
||||
// 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);
|
||||
|
||||
|
||||
/// SWARM
|
||||
// population <=> swarm
|
||||
eoPop<Particle> pop;
|
||||
|
||||
/// EVALUATION
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
eoEvalFuncPtr<Particle, double, const Particle& > eval( binary_value );
|
||||
|
||||
|
||||
///////////////
|
||||
/// TOPOLOGY
|
||||
//////////////
|
||||
// ring topology
|
||||
eoRingTopology<Particle> topology(NEIGHBORHOOD_SIZE);
|
||||
|
||||
|
||||
/////////////////////
|
||||
// INITIALIZATION
|
||||
////////////////////
|
||||
// position initialization
|
||||
eoUniformGenerator<bool> uGen;
|
||||
eoInitFixedLength < Particle > random (VEC_SIZE, uGen);
|
||||
pop.append (POP_SIZE, random);
|
||||
|
||||
// velocities initialization component
|
||||
eoUniformGenerator < double >sGen (VELOCITY_INIT_MIN, VELOCITY_INIT_MAX);
|
||||
eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen);
|
||||
|
||||
// first best position initialization component
|
||||
eoFirstIsBestInit < Particle > localInit;
|
||||
|
||||
// Create an eoInitialier that:
|
||||
// - performs a first evaluation of the particles
|
||||
// - initializes the velocities
|
||||
// - the first best positions of each particle
|
||||
// - setups the topology
|
||||
eoInitializer <Particle> fullInit(eval,veloRandom,localInit,topology,pop);
|
||||
|
||||
// Full initialization here to be able to print the initial population
|
||||
// Else: give the "init" component in the eoEasyPSO constructor
|
||||
fullInit();
|
||||
|
||||
/////////////
|
||||
// OUTPUT
|
||||
////////////
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
|
||||
// Print (sorted) the initial population (raw printout)
|
||||
cout << "INITIAL POPULATION:" << endl;
|
||||
for (unsigned i = 0; i < pop.size(); ++i)
|
||||
cout << "\t best fit=" << pop[i] << endl;
|
||||
|
||||
|
||||
///////////////
|
||||
/// VELOCITY
|
||||
//////////////
|
||||
// Create the bounds for the velocity not go to far away
|
||||
eoRealVectorBounds bnds(VEC_SIZE,VELOCITY_MIN,VELOCITY_MAX);
|
||||
|
||||
// the velocity itself that needs the topology and a few constants
|
||||
eoStandardVelocity <Particle> velocity (topology,INERTIA,LEARNING_FACTOR1,LEARNING_FACTOR2,bnds);
|
||||
|
||||
|
||||
///////////////
|
||||
/// FLIGHT
|
||||
//////////////
|
||||
// Binary flight based on sigmoid function
|
||||
eoSigBinaryFlight <Particle> flight;
|
||||
|
||||
////////////////////////
|
||||
/// STOPPING CRITERIA
|
||||
///////////////////////
|
||||
// the algo will run for MAX_GEN iterations
|
||||
eoGenContinue <Particle> genCont (MAX_GEN);
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
// standard PSO requires
|
||||
// stopping criteria, evaluation,velocity, flight
|
||||
try
|
||||
{
|
||||
MWModel<eoSyncEasyPSO,Particle> pso(genCont, eval, velocity, flight);
|
||||
|
||||
// Apply the algo to the swarm - that's it!
|
||||
pso(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
|
||||
pop.sort();
|
||||
cout << "FINAL POPULATION:" << endl;
|
||||
for (unsigned i = 0; i < pop.size(); ++i)
|
||||
cout << "\t best fit=" << pop[i] << endl;
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
//-----------------------------------------------------------------------------
|
||||
40
trunk/smp/test/t-smpScheduler.cpp
Normal file
40
trunk/smp/test/t-smpScheduler.cpp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#include <cassert>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <smp>
|
||||
#include <eo>
|
||||
|
||||
#include "smpTestClass.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace paradiseo::smp;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
srand(time(NULL));
|
||||
|
||||
loadInstances("t-data.dat", n, bkv, a, b);
|
||||
// Evaluation function
|
||||
IndiEvalFunc plainEval;
|
||||
// Init a solution
|
||||
IndiInit chromInit;
|
||||
|
||||
eoPop<Indi> pop(100, chromInit);
|
||||
|
||||
|
||||
int nbWorkers = 4;
|
||||
|
||||
|
||||
Scheduler<Indi> sched(nbWorkers);
|
||||
|
||||
sched(plainEval, pop);
|
||||
|
||||
std::cout << pop << std::endl;
|
||||
|
||||
// All indi would be evaluate once
|
||||
for( unsigned i = 0; i < pop.size(); i++)
|
||||
assert(pop[i].evalNb == 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
60
trunk/smp/test/t-smpThread.cpp
Normal file
60
trunk/smp/test/t-smpThread.cpp
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
#include <cassert>
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
|
||||
#include <smp>
|
||||
|
||||
#include "smpTestClass.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace paradiseo::smp;
|
||||
|
||||
void f(std::atomic<int> &x)
|
||||
{
|
||||
for(int i = 0; i < 100; i++) {
|
||||
cout << x << endl;
|
||||
x++;
|
||||
}
|
||||
}
|
||||
|
||||
void g(std::atomic<int> &x)
|
||||
{
|
||||
for(int i = 0; i < 100; i++)
|
||||
x--;
|
||||
}
|
||||
|
||||
void foo()
|
||||
{
|
||||
std::cout << "Foo" << std::endl;
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
//---------------------------------------------------
|
||||
std::atomic<int> nb(0);
|
||||
|
||||
Thread t1(&f,std::ref(nb));
|
||||
Thread t2(&g,std::ref(nb));
|
||||
|
||||
t1.join();
|
||||
t2.join();
|
||||
|
||||
assert(nb == 0); // Test atomic_barrier
|
||||
|
||||
//--------------------------------------------------
|
||||
try
|
||||
{
|
||||
t1.start(foo);
|
||||
|
||||
t1.join();
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
5
trunk/smp/tutorial/CMakeLists.txt
Normal file
5
trunk/smp/tutorial/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
######################################################################################
|
||||
### 0) Include lessons subdirectories
|
||||
######################################################################################
|
||||
|
||||
add_subdirectory(Lesson1)
|
||||
29
trunk/smp/tutorial/Lesson1/CMakeLists.txt
Normal file
29
trunk/smp/tutorial/Lesson1/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# Lesson 1
|
||||
|
||||
######################################################################################
|
||||
### 0) Define files
|
||||
######################################################################################
|
||||
|
||||
set(files
|
||||
lesson1_eoEasyEA
|
||||
)
|
||||
|
||||
######################################################################################
|
||||
### 1) Create the lesson
|
||||
######################################################################################
|
||||
|
||||
add_lesson(smp Lesson1 "${files}")
|
||||
|
||||
######################################################################################
|
||||
### 2) Include dependencies
|
||||
######################################################################################
|
||||
|
||||
include_directories(${EO_SRC_DIR}/src
|
||||
${SMP_SRC_DIR}/src
|
||||
${PROBLEMS_SRC_DIR})
|
||||
|
||||
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/lesson1_data.dat
|
||||
${CMAKE_CURRENT_BINARY_DIR}/lesson1_data.dat)
|
||||
118
trunk/smp/tutorial/Lesson1/QAP.h
Executable file
118
trunk/smp/tutorial/Lesson1/QAP.h
Executable file
|
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
<QAP.h>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille Nord Europe, 2006-2009
|
||||
(C) OPAC Team, LIFL, 2002-2009
|
||||
|
||||
The Van LUONG, (The-Van.Luong@inria.fr)
|
||||
Mahmoud FATENE, (mahmoud.fatene@inria.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 QAP_H
|
||||
#define QAP_H
|
||||
|
||||
#include <eo>
|
||||
|
||||
/* global variables */
|
||||
extern int n; // size
|
||||
extern int** a; // matrix A
|
||||
extern int** b; // matrix B
|
||||
|
||||
class Problem : public EO<eoMinimizingFitness> {
|
||||
|
||||
public:
|
||||
|
||||
int* solution;
|
||||
|
||||
Problem () {
|
||||
solution = new int[n];
|
||||
create();
|
||||
}
|
||||
|
||||
Problem (const Problem & _problem){ // copy constructor
|
||||
solution = new int[n];
|
||||
for (int i = 0; i < n ; i++){
|
||||
solution[i] = _problem.solution[i];
|
||||
}
|
||||
if (!_problem.invalid()) // if the solution has already been evaluated
|
||||
fitness(_problem.fitness()); // copy the fitness
|
||||
}
|
||||
|
||||
~Problem(){ // destructor
|
||||
delete[] solution;
|
||||
}
|
||||
|
||||
void operator= (const Problem & _problem){ // copy assignment operator
|
||||
for (int i = 0; i < n ; i++){
|
||||
solution[i] = _problem.solution[i];
|
||||
}
|
||||
fitness(_problem.fitness()); // copy the fitness
|
||||
}
|
||||
|
||||
int& operator[] (unsigned i)
|
||||
{
|
||||
return solution[i];
|
||||
}
|
||||
|
||||
|
||||
void create(){ // create and initialize a solution
|
||||
int random, temp;
|
||||
for (int i=0; i< n; i++)
|
||||
solution[i]=i;
|
||||
|
||||
// we want a random permutation so we shuffle
|
||||
for (int i = 0; i < n; i++){
|
||||
random = rand()%(n-i) + i;
|
||||
temp = solution[i];
|
||||
solution[i] = solution[random];
|
||||
solution[random] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
int evaluate() { // evaluate the solution
|
||||
int cost=0;
|
||||
for (int i=0; i<n; i++)
|
||||
for (int j=0; j<n; j++)
|
||||
cost += a[i][j] * b[solution[i]][solution[j]];
|
||||
|
||||
return cost;
|
||||
}
|
||||
|
||||
|
||||
void printSolution() {
|
||||
for (int i = 0; i < n ; i++)
|
||||
std::cout << solution[i] << " " ;
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
#endif
|
||||
128
trunk/smp/tutorial/Lesson1/QAPGA.h
Normal file
128
trunk/smp/tutorial/Lesson1/QAPGA.h
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
<QAPGA.h>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille Nord Europe, 2006-2009
|
||||
(C) OPAC Team, LIFL, 2002-2009
|
||||
|
||||
The Van LUONG, (The-Van.Luong@inria.fr)
|
||||
Mahmoud FATENE, (mahmoud.fatene@inria.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 _QAPGA_h
|
||||
#define _QAPGA_h
|
||||
|
||||
class ProblemInit : public eoInit<Problem>
|
||||
{
|
||||
public:
|
||||
|
||||
void operator()(Problem & _problem)
|
||||
{
|
||||
_problem.create();
|
||||
}
|
||||
};
|
||||
|
||||
class ProblemEvalFunc : public eoEvalFunc<Problem>
|
||||
{
|
||||
public:
|
||||
|
||||
void operator()(Problem & _problem)
|
||||
{
|
||||
_problem.fitness(_problem.evaluate());
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
class ProblemXover : public eoQuadOp<Problem> {
|
||||
public:
|
||||
|
||||
/* The two parameters in input are the parents.
|
||||
The first parameter is also the output ie the child
|
||||
*/
|
||||
bool operator()(Problem & _problem1, Problem & _problem2)
|
||||
{
|
||||
int i;
|
||||
int random, temp;
|
||||
int unassigned_positions[n];
|
||||
int remaining_items[n];
|
||||
int j = 0;
|
||||
|
||||
/* 1) find the items assigned in different positions for the 2 parents */
|
||||
for (i = 0 ; i < n ; i++){
|
||||
if (_problem1.solution[i] != _problem2.solution[i]){
|
||||
unassigned_positions[j] = i;
|
||||
remaining_items[j] = _problem1.solution[i];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
/* 2) shuffle the remaining items to ensure that remaining items
|
||||
will be assigned at random positions */
|
||||
for (i = 0; i < j; i++){
|
||||
random = rand()%(j-i) + i;
|
||||
temp = remaining_items[i];
|
||||
remaining_items[i] = remaining_items[random];
|
||||
remaining_items[random] = temp;
|
||||
}
|
||||
|
||||
/* 3) copy the shuffled remaining items at unassigned positions */
|
||||
for (i = 0; i < j ; i++)
|
||||
_problem1.solution[unassigned_positions[i]] = remaining_items[i];
|
||||
|
||||
// crossover in our case is always possible
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
class ProblemSwapMutation: public eoMonOp<Problem> {
|
||||
public:
|
||||
|
||||
bool operator()(Problem& _problem) {
|
||||
int i,j;
|
||||
int temp;
|
||||
|
||||
// generate two different indices
|
||||
i=rand()%n;
|
||||
do
|
||||
j = rand()%n;
|
||||
while (i == j);
|
||||
|
||||
// swap
|
||||
temp = _problem.solution[i];
|
||||
_problem.solution[i] = _problem.solution[j];
|
||||
_problem.solution[j] = temp;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
27
trunk/smp/tutorial/Lesson1/lesson1_data.dat
Normal file
27
trunk/smp/tutorial/Lesson1/lesson1_data.dat
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
12 224416
|
||||
|
||||
0 27 85 2 1 15 11 35 11 20 21 61
|
||||
27 0 80 58 21 76 72 44 85 94 90 51
|
||||
85 80 0 3 48 29 90 66 41 15 83 96
|
||||
2 58 3 0 74 45 65 40 54 83 14 71
|
||||
1 21 48 74 0 77 36 53 37 26 87 76
|
||||
15 76 29 45 77 0 91 13 29 11 77 32
|
||||
11 72 90 65 36 91 0 87 67 94 79 2
|
||||
35 44 66 40 53 13 87 0 10 99 56 70
|
||||
11 85 41 54 37 29 67 10 0 99 60 4
|
||||
20 94 15 83 26 11 94 99 99 0 56 2
|
||||
21 90 83 14 87 77 79 56 60 56 0 60
|
||||
61 51 96 71 76 32 2 70 4 2 60 0
|
||||
|
||||
0 21 95 82 56 41 6 25 10 4 63 6
|
||||
21 0 44 40 75 79 0 89 35 9 1 85
|
||||
95 44 0 84 12 0 26 91 11 35 82 26
|
||||
82 40 84 0 69 56 86 45 91 59 18 76
|
||||
56 75 12 69 0 39 18 57 36 61 36 21
|
||||
41 79 0 56 39 0 71 11 29 82 82 6
|
||||
6 0 26 86 18 71 0 71 8 77 74 30
|
||||
25 89 91 45 57 11 71 0 89 76 76 40
|
||||
10 35 11 91 36 29 8 89 0 93 56 1
|
||||
4 9 35 59 61 82 77 76 93 0 50 4
|
||||
63 1 82 18 36 82 74 76 56 50 0 36
|
||||
6 85 26 76 21 6 30 40 1 4 36 0
|
||||
136
trunk/smp/tutorial/Lesson1/lesson1_eoEasyEA.cpp
Normal file
136
trunk/smp/tutorial/Lesson1/lesson1_eoEasyEA.cpp
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
/*
|
||||
<main.cpp>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille Nord Europe, 2006-2009
|
||||
(C) OPAC Team, LIFL, 2002-2009
|
||||
|
||||
The Van LUONG, (The-Van.Luong@inria.fr)
|
||||
Mahmoud FATENE, (mahmoud.fatene@inria.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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Declaration of the necessary headers: In these are defined the class QAP,
|
||||
* redefinition of the crossover, mutation, initialisation of solution.
|
||||
*
|
||||
*/
|
||||
#include <smp>
|
||||
#include "QAP.h"
|
||||
#include "QAPGA.h"
|
||||
#include <string>
|
||||
using namespace std;
|
||||
using namespace paradiseo::smp;
|
||||
|
||||
/** Set of parameters wrapped into a structure. We pass then the structure
|
||||
* to a function which parses the parameters file. Doing so helps cleaning
|
||||
* the code from the parts of reading the inputs.
|
||||
*/
|
||||
#include "parserStruct.h"
|
||||
#include "utils.h"
|
||||
/** The actual reading and parameters parsing is done inside this class utilities
|
||||
*/
|
||||
|
||||
// Global variables
|
||||
int n; // problem size
|
||||
int** a;
|
||||
int** b; // a and matrices
|
||||
|
||||
int bkv; //best known value
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2){
|
||||
cout << "Please give a param file" << endl;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
eoParser parser(argc, argv);
|
||||
parameters param;
|
||||
parseFile(parser, param);
|
||||
rng.reseed(param.seed);
|
||||
|
||||
// Reading the a and b matrices of the QAP problem
|
||||
loadInstances(param.inst.c_str(), n, bkv, a, b);
|
||||
|
||||
// Declaration of class wrapping the evaluation function of the QAP
|
||||
ProblemEvalFunc plainEval;
|
||||
eoEvalFuncCounter<Problem> eval(plainEval);
|
||||
|
||||
// Class involving a simple call to the function of initialisation of a solution
|
||||
ProblemInit chromInit;
|
||||
|
||||
eoPop<Problem> pop(param.popSize, chromInit); // Initialise the population
|
||||
|
||||
// The robust tournament selection
|
||||
eoDetTournamentSelect<Problem> selectOne(param.tSize);
|
||||
// is now encapsulated in a eoSelectPerc (entage)
|
||||
eoSelectPerc<Problem> select(selectOne);// by default rate==1
|
||||
|
||||
ProblemXover Xover; // CROSSOVER
|
||||
ProblemSwapMutation mutationSwap; // MUTATION
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object
|
||||
eoSGATransform<Problem> transform(Xover, param.pCross, mutationSwap, param.pMut);
|
||||
|
||||
// REPLACE
|
||||
eoPlusReplacement<Problem> replace;
|
||||
|
||||
eoGenContinue<Problem> genCont(param.maxGen); // generation continuation
|
||||
|
||||
try
|
||||
{
|
||||
// Create the algorithm
|
||||
MWModel<eoEasyEA,Problem> mw(genCont, plainEval, select, transform, replace);
|
||||
// Start a parallel evaluation on the population
|
||||
mw.evaluate(pop);
|
||||
std::cout << "Initial population :" << std::endl;
|
||||
pop.sort();
|
||||
std::cout << pop << std::endl;
|
||||
// Start the algorithm on the population
|
||||
mw(pop);
|
||||
std::cout << "Final population :" << std::endl;
|
||||
pop.sort();
|
||||
std::cout << pop << std::endl;
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
// desallocate memory
|
||||
for (int i=0; i<n; i++){
|
||||
delete[] a[i];
|
||||
delete[] b[i];
|
||||
}
|
||||
|
||||
delete[] a;
|
||||
delete[] b;
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
37
trunk/smp/tutorial/Lesson1/lesson1_eoEasyEA.param
Executable file
37
trunk/smp/tutorial/Lesson1/lesson1_eoEasyEA.param
Executable file
|
|
@ -0,0 +1,37 @@
|
|||
###### General ######
|
||||
# --help=0 # -h : Prints this message
|
||||
# --stopOnUnknownParam=1 # Stop if unkown param entered
|
||||
# --seed=1104133126 # -S : Random number seed
|
||||
|
||||
###### Evolution Engine ######
|
||||
--popSize=10 # -P : Population Size
|
||||
--tSize=2 #tournament size
|
||||
|
||||
|
||||
###### Output ######
|
||||
# --useEval=1 # Use nb of eval. as counter (vs nb of gen.)
|
||||
# --useTime=1 # Display time (s) every generation
|
||||
# --printBestStat=1 # Print Best/avg/stdev every gen.
|
||||
# --printPop=0 # Print sorted pop. every gen.
|
||||
|
||||
###### Output - Disk ######
|
||||
# --resDir=Res # Directory to store DISK outputs
|
||||
# --eraseDir=1 # erase files in dirName if any
|
||||
# --fileBestStat=0 # Output bes/avg/std to file
|
||||
|
||||
###### Output - Graphical ######
|
||||
# --plotBestStat=0 # Plot Best/avg Stat
|
||||
# --plotHisto=0 # Plot histogram of fitnesses
|
||||
|
||||
###### Persistence ######
|
||||
# --Load=L # -L : A save file to restart from
|
||||
--inst=lesson1_data.dat # -i
|
||||
--schema=schema.xml # -s
|
||||
|
||||
###### Stopping criterion ######
|
||||
--maxGen=10 # -G : Maximum number of generations () = none)
|
||||
--minGen=1 # -g : Minimum number of generations
|
||||
|
||||
###### Variation Operators ######
|
||||
--pCross=0.7 # -C : Probability of Crossover
|
||||
--pMut=0.8 # -M : Probability of Mutation
|
||||
49
trunk/smp/tutorial/Lesson1/parserStruct.h
Normal file
49
trunk/smp/tutorial/Lesson1/parserStruct.h
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
<parserStruct.h>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille Nord Europe, 2006-2009
|
||||
(C) OPAC Team, LIFL, 2002-2009
|
||||
|
||||
The Van LUONG, (The-Van.Luong@inria.fr)
|
||||
Mahmoud FATENE, (mahmoud.fatene@inria.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
|
||||
*/
|
||||
|
||||
struct parameters
|
||||
{
|
||||
unsigned seed ;
|
||||
int popSize;
|
||||
int tSize;
|
||||
string inst;
|
||||
string loadName;
|
||||
string schema;
|
||||
double pCross;
|
||||
double pMut;
|
||||
int minGen;
|
||||
int maxGen;
|
||||
};
|
||||
126
trunk/smp/tutorial/Lesson1/utils.h
Executable file
126
trunk/smp/tutorial/Lesson1/utils.h
Executable file
|
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
<utils.h>
|
||||
Copyright (C) DOLPHIN Project-Team, INRIA Lille Nord Europe, 2006-2009
|
||||
(C) OPAC Team, LIFL, 2002-2009
|
||||
|
||||
The Van LUONG, (The-Van.Luong@inria.fr)
|
||||
Mahmoud FATENE, (mahmoud.fatene@inria.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 _UTILS_H
|
||||
#define _UTILS_H
|
||||
#include <stdlib.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void parseFile(eoParser & parser, parameters & param)
|
||||
{
|
||||
|
||||
// For each parameter, you can in on single line
|
||||
// define the parameter, read it through the parser, and assign it
|
||||
|
||||
param.seed = parser.createParam(unsigned(time(0)), "seed", "Random number seed", 'S').value(); // will be in default section General
|
||||
|
||||
// init and stop
|
||||
param.loadName = parser.createParam(string(""), "Load","A save file to restart from",'L', "Persistence" ).value();
|
||||
|
||||
param.inst = parser.createParam(string(""), "inst","a dat file to read instances from",'i', "Persistence" ).value();
|
||||
|
||||
param.schema = parser.createParam(string(""), "schema","an xml file mapping process",'s', "Persistence" ).value();
|
||||
|
||||
param.popSize = parser.createParam(unsigned(10), "popSize", "Population size",'P', "Evolution engine" ).value();
|
||||
|
||||
param.tSize = parser.createParam(unsigned(2), "tSize", "Tournament size",'T', "Evolution Engine" ).value();
|
||||
|
||||
param.minGen = parser.createParam(unsigned(100), "minGen", "Minimum number of iterations",'g', "Stopping criterion" ).value();
|
||||
|
||||
param.maxGen = parser.createParam(unsigned(300), "maxGen", "Maximum number of iterations",'G', "Stopping criterion" ).value();
|
||||
|
||||
param.pCross = parser.createParam(double(0.6), "pCross", "Probability of Crossover", 'C', "Genetic Operators" ).value();
|
||||
|
||||
param.pMut = parser.createParam(double(0.1), "pMut", "Probability of Mutation", 'M', "Genetic Operators" ).value();
|
||||
|
||||
|
||||
// the name of the "status" file where all actual parameter values will be saved
|
||||
string str_status = parser.ProgramName() + ".status"; // default value
|
||||
string statusName = parser.createParam(str_status, "status","Status file",'S', "Persistence" ).value();
|
||||
|
||||
// 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 (statusName != "")
|
||||
{
|
||||
ofstream os(statusName.c_str());
|
||||
os << parser; // and you can use that file as parameter file
|
||||
}
|
||||
}
|
||||
|
||||
void loadInstances(const char* filename, int& n, int& bkv, int** & a, int** & b)
|
||||
{
|
||||
|
||||
ifstream data_file;
|
||||
int i, j;
|
||||
data_file.open(filename);
|
||||
if (! data_file.is_open())
|
||||
{
|
||||
cout << "\n Error while reading the file " << filename << ". Please check if it exists !" << endl;
|
||||
exit (1);
|
||||
}
|
||||
data_file >> n;
|
||||
data_file >> bkv; // best known value
|
||||
// ****************** dynamic memory allocation ****************** /
|
||||
a = new int* [n];
|
||||
b = new int* [n];
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
a[i] = new int[n];
|
||||
b[i] = new int[n];
|
||||
}
|
||||
|
||||
// ************** read flows and distanceMatrixs matrices ************** /
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < n; j++)
|
||||
data_file >> a[i][j];
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < n; j++)
|
||||
data_file >> b[i][j];
|
||||
|
||||
data_file.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue