-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsland.h
108 lines (85 loc) · 2.39 KB
/
Island.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Island.h
*
*
* Created on: 24-nov-2017
* Author: rafael
*
*/
#ifndef ISLAND_H_
#define ISLAND_H_
#include <condition_variable>
#include <mutex>
#include <functional>
#include "log4cxx/logger.h"
#include "ga.h"
#include "Population.h"
#include "Problem.h"
#include "OperatorFactory.h"
#include "Config.h"
#include "Individual.h"
#include "Output.h"
using namespace std;
/*
* An island has a GA and also a neighborhood of islands where individuals of the population can emigrate.
*/
class Island: public GA {
vector<Island *> neighborhood;
static int count;
static std::mutex sync_mutex;
static std::mutex guard_mutex;
static std::condition_variable sync;
int num_islands;
int id;
int num_emigrants;
std::function<IContainer* (Population*, unsigned int)> emigrationSelection;
std::function<IContainer* (Population*, unsigned int)> immigrationSelection;
static log4cxx::LoggerPtr logger;
int maxGenerations;
int num_migration = 0;
bool valid = true;
static constexpr const char* MAX_GENERATIONS_PARAM = "NumberGenerations";
static constexpr const char * PARAM_RUN_FOR_N_GENERATIONS =
"MigrationEveryGenerations";
static constexpr const char* NUMBER_MIGRANTS_PARAM = "NumberMigrants";
/*
* Method to synchronize all threads before the migration process starts and after the migration process.
*/
static void migration_sync(int id, int num_islands);
/*
* TODO the stop condition should be provided from outside by the problem.
*
* Determine if the algorithm has finished.
* @return True if the algorithm has reached its end condition, false otherwise.
*/
bool isEnd();
void emigration();
/*
* Select individuals for the emigration process.
*
* @return A set of individuals, those individuals are removed from the population.
*/
IContainer * getEmigrants();
/*
* Select individuals for the immigration process.
*
* @return A set of individuals, those individuals are removed from the population.
*/
IContainer * getInmigrants();
bool ready();
public:
Island(int id, Problem * problem, OperatorFactory * operatorFactory,
Config * config, Output * output, int num_islands);
virtual ~Island();
/*
* set the neighbors islands.
*/
void set_neighborhood(vector<Island *> neighborhood);
/*
* Evolve the island population until the end of the algorithm.
*/
void evolveIsland();
void init();
void invalidate();
};
#endif /* ISLAND_H_ */