* New tree configuration of the project:
.../
... + -- EO
| |
| |
+-- src ----- + -- EDO
| |
| |
+-- test + -- MO
| |
| |
+-- tutorial + -- MOEO
| |
| |
+-- doc + -- SMP
| |
| |
... + -- EOMPI
|
|
+ -- EOSERIAL
Question for current maintainers: ./README: new release?
Also:
* Moving out eompi & eoserial modules (issue #2).
* Correction of the errors when executing "make doc" command.
* Adding a solution for the conflicting headers problem (see the two CMake Cache
Values: PROJECT_TAG & PROJECT_HRS_INSTALL_SUBPATH) (issue #1)
* Header inclusions:
** src: changing absolute paths into relative paths ('#include <...>' -> '#include "..."')
** test, tutorial: changing relative paths into absolute paths ('#include "..."' -> '#include <...>')
* Moving out some scripts from EDO -> to the root
* Add a new script for compilation and installation (see build_gcc_linux_install)
* Compilation with uBLAS library or EDO module: now ok
* Minor modifications on README & INSTALL files
* Comment eompi failed tests with no end
*** TODO: CPack (debian (DEB) & RedHat (RPM) packages) (issues #6 & #7) ***
This commit is contained in:
parent
515bd5943d
commit
490e837f7a
2359 changed files with 7688 additions and 16329 deletions
54
test/smp/CMakeLists.txt
Executable file
54
test/smp/CMakeLists.txt
Executable file
|
|
@ -0,0 +1,54 @@
|
|||
######################################################################################
|
||||
### 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-smpScheduler
|
||||
t-smpMW_eoEasyEA
|
||||
t-smpMW_eoEasyPSO
|
||||
t-smpMW_eoSyncEasyPSO
|
||||
t-smpIsland
|
||||
t-smpTopo
|
||||
t-smpMI_Homogeneous
|
||||
t-smpMI_Heterogeneous
|
||||
t-smpMI_Wrapper
|
||||
t-smpCustomTopo
|
||||
)
|
||||
|
||||
######################################################################################
|
||||
### 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})
|
||||
install(TARGETS ${test} RUNTIME DESTINATION share/${PROJECT_TAG}/smp/test COMPONENT tests)
|
||||
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)
|
||||
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/t-data.dat DESTINATION share/${PROJECT_TAG}/smp/test COMPONENT tests)
|
||||
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/data-topo-bool
|
||||
${CMAKE_CURRENT_BINARY_DIR}/data-topo-bool)
|
||||
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data-topo-bool DESTINATION share/${PROJECT_TAG}/smp/test COMPONENT tests)
|
||||
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/data-topo-stoch
|
||||
${CMAKE_CURRENT_BINARY_DIR}/data-topo-stoch)
|
||||
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data-topo-stoch DESTINATION share/${PROJECT_TAG}/smp/test COMPONENT tests)
|
||||
4
test/smp/data-topo-bool
Executable file
4
test/smp/data-topo-bool
Executable file
|
|
@ -0,0 +1,4 @@
|
|||
0 1 0 0
|
||||
1 0 1 1
|
||||
0 1 0 1
|
||||
1 0 1 0
|
||||
3
test/smp/data-topo-stoch
Executable file
3
test/smp/data-topo-stoch
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
.25 .5 .75
|
||||
.2 .1 .05
|
||||
.9 .2 .03
|
||||
272
test/smp/smpTestClass.h
Executable file
272
test/smp/smpTestClass.h
Executable 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
test/smp/t-data.dat
Executable file
27
test/smp/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
|
||||
117
test/smp/t-smpCustomTopo.cpp
Executable file
117
test/smp/t-smpCustomTopo.cpp
Executable file
|
|
@ -0,0 +1,117 @@
|
|||
#include <paradiseo/smp/topology/customBooleanTopology.h>
|
||||
#include <paradiseo/smp/topology/customStochasticTopology.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <assert.h>
|
||||
#include <cmath>
|
||||
|
||||
using namespace paradiseo::smp;
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
std::vector<unsigned> value;
|
||||
|
||||
//TEST OF CUSTOM BOOLEAN TOPOLOGY
|
||||
CustomBooleanTopology topo_bool("data-topo-bool");
|
||||
std::vector<unsigned> neighbors = topo_bool.getIdNeighbors(0);
|
||||
|
||||
value.clear();
|
||||
value.push_back(1);
|
||||
assert(neighbors == value);
|
||||
///////////////////////////////////////////////////////
|
||||
neighbors = topo_bool.getIdNeighbors(1);
|
||||
|
||||
value.clear();
|
||||
value.push_back(0);
|
||||
value.push_back(2);
|
||||
value.push_back(3);
|
||||
assert(neighbors == value);
|
||||
//////////////////////////////////////////////////////
|
||||
neighbors = topo_bool.getIdNeighbors(2);
|
||||
|
||||
value.clear();
|
||||
value.push_back(1);
|
||||
value.push_back(3);
|
||||
assert(neighbors == value);
|
||||
//////////////////////////////////////////////////////
|
||||
neighbors = topo_bool.getIdNeighbors(3);
|
||||
|
||||
value.clear();
|
||||
value.push_back(0);
|
||||
value.push_back(2);
|
||||
assert(neighbors == value);
|
||||
|
||||
|
||||
//TEST OF CUSTOM STOCHASTIC TOPOLOGY
|
||||
CustomStochasticTopology topo_stoch("data-topo-stoch");
|
||||
std::vector<std::vector<double>> matrix;
|
||||
matrix.resize(3);
|
||||
for(auto& line : matrix)
|
||||
line.resize(3);
|
||||
|
||||
//Computation of the mean probability
|
||||
int it_nb = 1000;
|
||||
for(int i = 0 ; i < it_nb ; i++)
|
||||
{
|
||||
for(int j = 0; j < 3; j++)
|
||||
{
|
||||
neighbors = topo_stoch.getIdNeighbors(j);
|
||||
for(auto& node : neighbors)
|
||||
{
|
||||
matrix[j][node]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(auto& line : matrix)
|
||||
{
|
||||
for(auto& edge : line)
|
||||
edge = edge/it_nb;
|
||||
}
|
||||
|
||||
//Reading the actual matrix
|
||||
std::ifstream f("data-topo-stoch");
|
||||
std::vector<std::vector<double>> _matrix;
|
||||
if (f)
|
||||
{
|
||||
double temp;
|
||||
double isNeighbor;
|
||||
std::string line;
|
||||
std::vector<double> lineVector;
|
||||
|
||||
while(getline(f, line))
|
||||
{
|
||||
lineVector.clear();
|
||||
|
||||
//line contains a line of text from the file
|
||||
std::istringstream tokenizer(line);
|
||||
std::string token;
|
||||
|
||||
while(tokenizer >> temp >> std::skipws)
|
||||
{
|
||||
//white spaces are skipped, and the integer is converted to boolean, to be stored
|
||||
|
||||
if(temp<0)
|
||||
isNeighbor = 0;
|
||||
else if(temp>1)
|
||||
isNeighbor = 1;
|
||||
else
|
||||
isNeighbor = (double) temp;
|
||||
lineVector.push_back(isNeighbor);
|
||||
}
|
||||
|
||||
if(!lineVector.empty())
|
||||
_matrix.push_back(lineVector);
|
||||
}
|
||||
|
||||
f.close () ;
|
||||
}
|
||||
|
||||
//Comparison to the actual matrix : _matrix
|
||||
for(unsigned i = 0; i < matrix.size(); i++)
|
||||
for(unsigned j = 0; j < matrix.size(); j++)
|
||||
assert(std::abs(_matrix[i][j] - matrix[i][j]) < .05);
|
||||
}
|
||||
97
test/smp/t-smpIsland.cpp
Executable file
97
test/smp/t-smpIsland.cpp
Executable file
|
|
@ -0,0 +1,97 @@
|
|||
#include <paradiseo/smp.h>
|
||||
#include <paradiseo/eo.h>
|
||||
|
||||
#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 = 100;
|
||||
} 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
|
||||
eoGenContinue<Indi> genCont_2(param.maxGen); // generation continuation
|
||||
|
||||
// Define population
|
||||
eoPop<Indi> pop(param.popSize, chromInit);
|
||||
|
||||
try
|
||||
{
|
||||
// Island 1
|
||||
// // Emigration policy
|
||||
// // // Element 1
|
||||
eoPeriodicContinue<Indi> criteria(5);
|
||||
eoDetTournamentSelect<Indi> selectOne(2);
|
||||
eoSelectNumber<Indi> who(selectOne, 1);
|
||||
|
||||
MigPolicy<Indi> migPolicy;
|
||||
migPolicy.push_back(PolicyElement<Indi>(who, criteria));
|
||||
|
||||
// // Integration policy
|
||||
eoPlusReplacement<Indi> intPolicy;
|
||||
|
||||
eoPop<Indi> pop(param.popSize, chromInit);
|
||||
Island<eoEasyEA,Indi> test(pop, intPolicy, migPolicy, genCont, plainEval, select, transform, replace);
|
||||
|
||||
// Island 2
|
||||
// // Emigration policy
|
||||
// // // Element 1
|
||||
eoPeriodicContinue<Indi> criteria_2(5);
|
||||
eoDetTournamentSelect<Indi> selectOne_2(2);
|
||||
eoSelectNumber<Indi> who_2(selectOne_2, 1);
|
||||
|
||||
MigPolicy<Indi> migPolicy_2;
|
||||
migPolicy_2.push_back(PolicyElement<Indi>(who_2, criteria_2));
|
||||
|
||||
// // Integration policy
|
||||
eoPlusReplacement<Indi> intPolicy_2;
|
||||
|
||||
eoPop<Indi> pop2(param.popSize, chromInit);
|
||||
Island<eoEasyEA,Indi> test2(pop2, intPolicy_2, migPolicy_2, genCont_2, plainEval, select, transform, replace);
|
||||
|
||||
test();
|
||||
test2();
|
||||
|
||||
cout << test.getPop() << endl;
|
||||
cout << test2.getPop() << endl;
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
182
test/smp/t-smpMI_Heterogeneous.cpp
Executable file
182
test/smp/t-smpMI_Heterogeneous.cpp
Executable file
|
|
@ -0,0 +1,182 @@
|
|||
#include <paradiseo/smp.h>
|
||||
#include <paradiseo/eo.h>
|
||||
#include <paradiseo/eo/ga.h>
|
||||
|
||||
#include "smpTestClass.h"
|
||||
|
||||
using namespace paradiseo::smp;
|
||||
using namespace std;
|
||||
|
||||
typedef eoBit<double> Indi2; // A bitstring with fitness double
|
||||
|
||||
// Conversion functions
|
||||
Indi2 fromBase(Indi& i, unsigned size)
|
||||
{
|
||||
(void)i;
|
||||
// Dummy conversion. We just create a new Indi2
|
||||
Indi2 v;
|
||||
for (unsigned ivar=0; ivar<size; ivar++)
|
||||
{
|
||||
bool r = rng.flip(); // new value, random in {0,1}
|
||||
v.push_back(r); // append that random value to v
|
||||
}
|
||||
std::cout << "Convert from base : " << v << std::endl;
|
||||
return v;
|
||||
}
|
||||
|
||||
Indi toBase(Indi2& i)
|
||||
{
|
||||
(void)i;
|
||||
// Dummy conversion. We just create a new Indi
|
||||
Indi v;
|
||||
std::cout << "Convert to base : " << v << std::endl;
|
||||
return v;
|
||||
}
|
||||
|
||||
// Eval function for the PSO
|
||||
// A simple fitness function that computes the number of ones of a bitstring
|
||||
// @param _Indi2 A biststring Indi2vidual
|
||||
|
||||
double binary_value(const Indi2 & _Indi2)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _Indi2.size(); i++)
|
||||
sum += _Indi2[i];
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// PSO PART
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// PSO general parameters
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
const unsigned int VEC_SIZE = 16; // Number of bits in genotypes
|
||||
const unsigned int POP_SIZE = 10; // Size of population
|
||||
const unsigned int MAX_GEN = 10; // Maximum number of generation before STOP
|
||||
const float CROSS_RATE = 0.8; // Crossover rate
|
||||
const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation
|
||||
const float MUT_RATE = 1.0; // mutation rate
|
||||
|
||||
rng.reseed(SEED);
|
||||
|
||||
eoEvalFuncPtr<Indi2> eval(binary_value);
|
||||
|
||||
// PSO population initialization
|
||||
eoPop<Indi2> pop;
|
||||
|
||||
for(unsigned int igeno=0; igeno<POP_SIZE; igeno++)
|
||||
{
|
||||
Indi2 v; // void Indi2vidual, to be filled
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
|
||||
{
|
||||
bool r = rng.flip(); // new value, random in {0,1}
|
||||
v.push_back(r); // append that random value to v
|
||||
}
|
||||
eval(v); // evaluate it
|
||||
pop.push_back(v); // and put it in the population
|
||||
}
|
||||
|
||||
// ISLAND 1 : PSO
|
||||
// // Algorithm part
|
||||
eoDetTournamentSelect<Indi2> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||
eo1PtBitXover<Indi2> xover;
|
||||
eoBitMutation<Indi2> mutation(P_MUT_PER_BIT);
|
||||
eoGenContinue<Indi2> continuator(MAX_GEN);
|
||||
|
||||
// // Emigration policy
|
||||
// // // Element 1
|
||||
eoPeriodicContinue<Indi2> criteria(1);
|
||||
eoDetTournamentSelect<Indi2> selectOne(2);
|
||||
eoSelectNumber<Indi2> who(selectOne, 1);
|
||||
|
||||
MigPolicy<Indi2> migPolicy;
|
||||
migPolicy.push_back(PolicyElement<Indi2>(who, criteria));
|
||||
|
||||
// // Integration policy
|
||||
eoPlusReplacement<Indi2> intPolicy;
|
||||
|
||||
// We bind conversion functions
|
||||
auto frombase = std::bind(fromBase, std::placeholders::_1, VEC_SIZE);
|
||||
auto tobase = std::bind(toBase, std::placeholders::_1);
|
||||
|
||||
Island<eoSGA,Indi2, Indi> gga(frombase, tobase, pop, intPolicy, migPolicy, select, xover, CROSS_RATE, mutation, MUT_RATE, eval, continuator);
|
||||
//////////////////////////////////////////////////////////////////
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// EasyEA PART
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// EA general parameters
|
||||
typedef struct {
|
||||
unsigned popSize = 10;
|
||||
unsigned tSize = 2;
|
||||
double pCross = 0.8;
|
||||
double pMut = 0.7;
|
||||
unsigned maxGen = 10;
|
||||
} Param;
|
||||
|
||||
Param param;
|
||||
loadInstances("t-data.dat", n, bkv, a, b);
|
||||
|
||||
// Evaluation function
|
||||
IndiEvalFunc plainEval;
|
||||
|
||||
// Init a solution
|
||||
IndiInit chromInit;
|
||||
|
||||
// Define selection
|
||||
eoDetTournamentSelect<Indi> selectOne2(param.tSize);
|
||||
eoSelectPerc<Indi> select2(selectOne2);// 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
|
||||
eoPop<Indi> pop2(param.popSize, chromInit);
|
||||
|
||||
// ISLAND 2 : EasyEA
|
||||
// // Emigration policy
|
||||
// // // Element 1
|
||||
eoPeriodicContinue<Indi> criteria2(1);
|
||||
eoDetTournamentSelect<Indi> selectOne3(5);
|
||||
eoSelectNumber<Indi> who2(selectOne3, 2);
|
||||
|
||||
MigPolicy<Indi> migPolicy2;
|
||||
migPolicy2.push_back(PolicyElement<Indi>(who2, criteria2));
|
||||
|
||||
// // Integration policy
|
||||
eoPlusReplacement<Indi> intPolicy2;
|
||||
|
||||
Island<eoEasyEA,Indi> test(pop2, intPolicy2, migPolicy2, genCont, plainEval, select2, transform, replace);
|
||||
|
||||
// MODEL CREATION
|
||||
Topology<Complete> topo;
|
||||
IslandModel<Indi> model(topo);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
model.add(test);
|
||||
model.add(gga);
|
||||
|
||||
model();
|
||||
|
||||
cout << test.getPop() << endl;
|
||||
cout << gga.getPop() << endl;
|
||||
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
117
test/smp/t-smpMI_Homogeneous.cpp
Executable file
117
test/smp/t-smpMI_Homogeneous.cpp
Executable file
|
|
@ -0,0 +1,117 @@
|
|||
#include <paradiseo/smp.h>
|
||||
#include <paradiseo/eo.h>
|
||||
|
||||
#include "smpTestClass.h"
|
||||
|
||||
using namespace paradiseo::smp;
|
||||
using namespace std;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Defining parameters
|
||||
typedef struct {
|
||||
unsigned popSize = 1000;
|
||||
unsigned tSize = 2;
|
||||
double pCross = 0.8;
|
||||
double pMut = 0.7;
|
||||
unsigned maxGen = 1000;
|
||||
} Param;
|
||||
|
||||
Param param;
|
||||
|
||||
// Fixing the seed
|
||||
rng.reseed(42);
|
||||
|
||||
// Load instance
|
||||
loadInstances("t-data.dat", n, bkv, a, b);
|
||||
|
||||
//Common part to all islands
|
||||
IndiEvalFunc plainEval;
|
||||
IndiInit chromInit;
|
||||
eoDetTournamentSelect<Indi> selectOne(param.tSize);
|
||||
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
||||
IndiXover Xover; // CROSSOVER
|
||||
IndiSwapMutation mutationSwap; // MUTATION
|
||||
eoSGATransform<Indi> transform(Xover, param.pCross, mutationSwap, param.pMut);
|
||||
eoPlusReplacement<Indi> replace;
|
||||
|
||||
// MODEL
|
||||
// Topologies
|
||||
Topology<Complete> topo;
|
||||
IslandModel<Indi> model(topo);
|
||||
|
||||
// ISLAND 1
|
||||
// // Algorithm part
|
||||
eoGenContinue<Indi> genCont(param.maxGen+100);
|
||||
eoPop<Indi> pop(param.popSize, chromInit);
|
||||
// // Emigration policy
|
||||
// // // Element 1
|
||||
eoPeriodicContinue<Indi> criteria(5);
|
||||
eoDetTournamentSelect<Indi> selectOne1(20);
|
||||
eoSelectNumber<Indi> who(selectOne1, 3);
|
||||
|
||||
MigPolicy<Indi> migPolicy;
|
||||
migPolicy.push_back(PolicyElement<Indi>(who, criteria));
|
||||
|
||||
// // Integration policy
|
||||
eoPlusReplacement<Indi> intPolicy;
|
||||
|
||||
Island<eoEasyEA,Indi> test(pop, intPolicy, migPolicy, genCont, plainEval, select, transform, replace);
|
||||
|
||||
// ISLAND 1
|
||||
// // Algorithm part
|
||||
eoGenContinue<Indi> genCont_2(param.maxGen); // generation continuation
|
||||
eoPop<Indi> pop2(30, chromInit);
|
||||
// // Emigration policy
|
||||
// // // Element 1
|
||||
eoPeriodicContinue<Indi> criteria_2(5);
|
||||
eoDetTournamentSelect<Indi> selectOne_2(25);
|
||||
eoSelectNumber<Indi> who_2(selectOne_2, 5);
|
||||
|
||||
MigPolicy<Indi> migPolicy_2;
|
||||
migPolicy_2.push_back(PolicyElement<Indi>(who_2, criteria_2));
|
||||
|
||||
// // Integration policy
|
||||
eoPlusReplacement<Indi> intPolicy_2;
|
||||
|
||||
Island<eoEasyEA,Indi> test2(pop2, intPolicy_2, migPolicy_2, genCont_2, plainEval, select, transform, replace);
|
||||
|
||||
// Island 3
|
||||
// // Algorithm part
|
||||
eoGenContinue<Indi> genCont_3(param.maxGen);
|
||||
eoPop<Indi> pop3(30, chromInit);
|
||||
// // Emigration policy
|
||||
// // // Element 1
|
||||
eoPeriodicContinue<Indi> criteria_3(10);
|
||||
eoDetTournamentSelect<Indi> selectOne_3(15);
|
||||
eoSelectNumber<Indi> who_3(selectOne_3, 1);
|
||||
|
||||
MigPolicy<Indi> migPolicy_3;
|
||||
migPolicy.push_back(PolicyElement<Indi>(who_3, criteria_3));
|
||||
|
||||
// // Integration policy
|
||||
eoPlusReplacement<Indi> intPolicy_3;
|
||||
|
||||
Island<eoEasyEA,Indi> test3(pop3, intPolicy_3, migPolicy_3, genCont_3, plainEval, select, transform, replace);
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
model.add(test);
|
||||
model.add(test2);
|
||||
model.add(test3);
|
||||
|
||||
model();
|
||||
|
||||
cout << test.getPop() << endl;
|
||||
cout << test2.getPop() << endl;
|
||||
cout << test3.getPop() << endl;
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
78
test/smp/t-smpMI_Wrapper.cpp
Executable file
78
test/smp/t-smpMI_Wrapper.cpp
Executable file
|
|
@ -0,0 +1,78 @@
|
|||
#include <paradiseo/smp.h>
|
||||
#include <paradiseo/eo.h>
|
||||
|
||||
#include "smpTestClass.h"
|
||||
|
||||
using namespace paradiseo::smp;
|
||||
using namespace std;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Defining parameters
|
||||
typedef struct {
|
||||
unsigned popSize = 1000;
|
||||
unsigned tSize = 2;
|
||||
double pCross = 0.8;
|
||||
double pMut = 0.7;
|
||||
unsigned maxGen = 1000;
|
||||
} Param;
|
||||
|
||||
Param param;
|
||||
|
||||
// Fixing the seed
|
||||
rng.reseed(42);
|
||||
|
||||
// Load instance
|
||||
loadInstances("t-data.dat", n, bkv, a, b);
|
||||
|
||||
//Common part to all islands
|
||||
IndiEvalFunc plainEval;
|
||||
IndiInit chromInit;
|
||||
eoDetTournamentSelect<Indi> selectOne(param.tSize);
|
||||
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
||||
IndiXover Xover; // CROSSOVER
|
||||
IndiSwapMutation mutationSwap; // MUTATION
|
||||
eoSGATransform<Indi> transform(Xover, param.pCross, mutationSwap, param.pMut);
|
||||
eoPlusReplacement<Indi> replace;
|
||||
|
||||
// MODEL
|
||||
// Topologies
|
||||
Topology<Complete> topo;
|
||||
|
||||
// ISLAND 1
|
||||
// // Algorithm part
|
||||
eoGenContinue<Indi> genCont(param.maxGen+100);
|
||||
eoPop<Indi> pop(param.popSize, chromInit);
|
||||
// // Emigration policy
|
||||
// // // Element 1
|
||||
eoPeriodicContinue<Indi> criteria(5);
|
||||
eoDetTournamentSelect<Indi> selectOne1(20);
|
||||
eoSelectNumber<Indi> who(selectOne1, 3);
|
||||
|
||||
MigPolicy<Indi> migPolicy;
|
||||
migPolicy.push_back(PolicyElement<Indi>(who, criteria));
|
||||
|
||||
// // Integration policy
|
||||
eoPlusReplacement<Indi> intPolicy;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
std::vector<eoPop<Indi>> pops = IslandModelWrapper<eoEasyEA,Indi>(50, topo, 100, chromInit, intPolicy, migPolicy, genCont, plainEval, select, transform, replace);
|
||||
|
||||
for(auto& pop : pops)
|
||||
{
|
||||
for(auto& indi : pop)
|
||||
plainEval(indi);
|
||||
pop.sort();
|
||||
std::cout << pop << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
72
test/smp/t-smpMW_eoEasyEA.cpp
Executable file
72
test/smp/t-smpMW_eoEasyEA.cpp
Executable file
|
|
@ -0,0 +1,72 @@
|
|||
#include <paradiseo/smp/MWModel.h>
|
||||
#include <cassert>
|
||||
#include <paradiseo/eo.h>
|
||||
|
||||
#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
test/smp/t-smpMW_eoEasyPSO.cpp
Executable file
104
test/smp/t-smpMW_eoEasyPSO.cpp
Executable 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 <paradiseo/eo.h>
|
||||
#include <paradiseo/smp.h>
|
||||
|
||||
// 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
test/smp/t-smpMW_eoSyncEasyPSO.cpp
Executable file
172
test/smp/t-smpMW_eoSyncEasyPSO.cpp
Executable 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 <paradiseo/eo.h>
|
||||
#include <paradiseo/smp.h>
|
||||
|
||||
// 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;
|
||||
}
|
||||
//-----------------------------------------------------------------------------
|
||||
39
test/smp/t-smpScheduler.cpp
Executable file
39
test/smp/t-smpScheduler.cpp
Executable file
|
|
@ -0,0 +1,39 @@
|
|||
#include <cassert>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <paradiseo/smp.h>
|
||||
#include <paradiseo/eo.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
191
test/smp/t-smpTopo.cpp
Executable file
191
test/smp/t-smpTopo.cpp
Executable file
|
|
@ -0,0 +1,191 @@
|
|||
#include <paradiseo/smp.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <assert.h>
|
||||
|
||||
using namespace paradiseo::smp;
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
std::vector<unsigned> value;
|
||||
|
||||
//Test of Complete Topology
|
||||
n=5;
|
||||
Topology<Complete> topo_comp;
|
||||
topo_comp.construct(n);
|
||||
|
||||
|
||||
std::vector<unsigned> neighbors = topo_comp.getIdNeighbors(1);
|
||||
|
||||
value.clear();
|
||||
value.push_back(0);
|
||||
value.push_back(2);
|
||||
value.push_back(3);
|
||||
value.push_back(4);
|
||||
assert(neighbors == value);
|
||||
|
||||
neighbors=topo_comp.getIdNeighbors(2);
|
||||
|
||||
value.clear();
|
||||
value.push_back(0);
|
||||
value.push_back(1);
|
||||
value.push_back(3);
|
||||
value.push_back(4);
|
||||
assert(neighbors == value);
|
||||
|
||||
//Isolate an node
|
||||
topo_comp.isolateNode(2);
|
||||
neighbors=topo_comp.getIdNeighbors(2);
|
||||
|
||||
assert(neighbors.empty());
|
||||
|
||||
neighbors=topo_comp.getIdNeighbors(3);
|
||||
|
||||
value.clear();
|
||||
value.push_back(0);
|
||||
value.push_back(1);
|
||||
value.push_back(4);
|
||||
assert(neighbors == value);
|
||||
|
||||
//Re-construct Topology with different number of nodes
|
||||
n=3;
|
||||
topo_comp.construct(n);
|
||||
neighbors=topo_comp.getIdNeighbors(2);
|
||||
|
||||
value.clear();
|
||||
value.push_back(0);
|
||||
value.push_back(1);
|
||||
assert(neighbors == value);
|
||||
|
||||
n=8;
|
||||
topo_comp.construct(n);
|
||||
neighbors = topo_comp.getIdNeighbors(3);
|
||||
|
||||
value.clear();
|
||||
value.push_back(0);
|
||||
value.push_back(1);
|
||||
value.push_back(2);
|
||||
value.push_back(4);
|
||||
value.push_back(5);
|
||||
value.push_back(6);
|
||||
value.push_back(7);
|
||||
assert(neighbors == value);
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
//Test of Star Topology
|
||||
n=4;
|
||||
Topology<Star> topo_star;
|
||||
topo_star.construct(n);
|
||||
|
||||
neighbors=topo_star.getIdNeighbors(0);
|
||||
|
||||
value.clear();
|
||||
assert(neighbors == value);
|
||||
//---------------------------------------
|
||||
neighbors=topo_star.getIdNeighbors(2);
|
||||
|
||||
value.clear();
|
||||
value.push_back(0);
|
||||
assert(neighbors == value);
|
||||
//---------------------------------------
|
||||
topo_star.getBuilder().setCenter(2);
|
||||
topo_star.construct(n);
|
||||
|
||||
neighbors=topo_star.getIdNeighbors(0);
|
||||
|
||||
value.clear();
|
||||
value.push_back(2);
|
||||
assert(neighbors == value);
|
||||
//---------------------------------------
|
||||
neighbors=topo_star.getIdNeighbors(2);
|
||||
value.clear();
|
||||
assert(neighbors == value);
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
//Test of Ring Topology
|
||||
n=8;
|
||||
Topology<Ring> topo_ring;
|
||||
topo_ring.construct(n);
|
||||
|
||||
neighbors=topo_ring.getIdNeighbors(4);
|
||||
|
||||
value.clear();
|
||||
value.push_back(5);
|
||||
assert(neighbors == value);
|
||||
//---------------------------------------
|
||||
neighbors=topo_ring.getIdNeighbors(7);
|
||||
|
||||
value.clear();
|
||||
value.push_back(0);
|
||||
assert(neighbors == value);
|
||||
//---------------------------------------
|
||||
neighbors=topo_ring.getIdNeighbors(0);
|
||||
|
||||
value.clear();
|
||||
value.push_back(1);
|
||||
assert(neighbors == value);
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
//Test of Hypercubic Topology
|
||||
n=2;
|
||||
Topology<Hypercubic> topo_hyper;
|
||||
topo_hyper.construct(n);
|
||||
|
||||
neighbors=topo_hyper.getIdNeighbors(0);
|
||||
|
||||
value.clear();
|
||||
value.push_back(1);
|
||||
assert(neighbors == value);
|
||||
//------------------------------------
|
||||
n=4;
|
||||
topo_hyper.construct(n);
|
||||
|
||||
neighbors=topo_hyper.getIdNeighbors(1);
|
||||
|
||||
value.clear();
|
||||
value.push_back(0);
|
||||
value.push_back(3);
|
||||
assert(neighbors == value);
|
||||
//-------------------------------------
|
||||
n=8;
|
||||
topo_hyper.construct(n);
|
||||
|
||||
neighbors=topo_hyper.getIdNeighbors(5);
|
||||
|
||||
value.clear();
|
||||
value.push_back(1);
|
||||
value.push_back(4);
|
||||
value.push_back(7);
|
||||
assert(neighbors == value);
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
//Test of Mesh Topology
|
||||
n=9;
|
||||
Topology<Mesh> topo_mesh;
|
||||
topo_mesh.construct(n);
|
||||
neighbors=topo_mesh.getIdNeighbors(0);
|
||||
|
||||
value.clear();
|
||||
value.push_back(1);
|
||||
value.push_back(3);
|
||||
assert(neighbors == value);
|
||||
//-------------------------------------
|
||||
topo_mesh.getBuilder().setRatio(0.4);
|
||||
topo_mesh.construct(n);
|
||||
neighbors=topo_mesh.getIdNeighbors(5);
|
||||
|
||||
value.clear();
|
||||
value.push_back(6);
|
||||
//assert(neighbors == value);
|
||||
//--------------------------------------
|
||||
n=8;
|
||||
topo_mesh.construct(n);
|
||||
neighbors=topo_mesh.getIdNeighbors(0);
|
||||
|
||||
value.clear();
|
||||
value.push_back(1);
|
||||
value.push_back(4);
|
||||
assert(neighbors == value);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue