-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyAlgorithm.cpp
More file actions
167 lines (136 loc) · 3.43 KB
/
myAlgorithm.cpp
File metadata and controls
167 lines (136 loc) · 3.43 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "myAlgorithm.h"
#include <fstream>
myAlgorithm::myAlgorithm() :
d_population{}
{
initializePopulation();
calculeProbabilite();
FindBestSolution();
}
//
// GEILLER Valentin
//
void myAlgorithm::initializePopulation() {
d_population.resize(pop_size);
for (int i = 0; i < pop_size; ++i) {
d_population[i] = create_new_individual();
}
}
//
// MUHIRWA GABO Oreste
//
double myAlgorithm::maxFitness() const {
double max = d_population[0].getFitnessValue();
for (int i = 0; i < d_population.size(); ++i) {
if (max <= d_population[i].getFitnessValue()) {
max = d_population[i].getFitnessValue();
}
}
return max;
}
//
// MUHIRWA GABO Oreste
//
int myAlgorithm::indexOfSolutionWhereSuperiorOfLimit() const {
int index = 0;
double max = maxFitness();
for (int i = 0; i < d_population.size(); ++i) {
if (d_population[i].getFitnessValue() == max)
index = i;
}
return index;
}
//
// MUHIRWA GABO Oreste
//
void myAlgorithm::FindBestSolution() {
d_bestFS = d_population[indexOfSolutionWhereSuperiorOfLimit()];
}
//
// MUHIRWA GABO Oreste
//
void myAlgorithm::calculeProbabilite() {
d_probabilite.resize(d_population.size());
double maxfitness = maxFitness();
for (int i = 0; i < d_probabilite.size(); ++i) {
double fit = d_population[i].getFitnessValue();
d_probabilite[i] = 0.9 * (fit / maxfitness) + 0.1;
}
}
//
// GEILLER Valentin
//
void myAlgorithm::employedBeePhase() {
for (int i = 0; i < NB_ABEILLES_EMPLOYE; ++i) {
FoodSource tmpFS = d_population[i];
int variableAChanger = generate_random_int(0,dimension-1);
int partenaire = generate_random_int(0, pop_size - 1);
while (partenaire == i)
partenaire = generate_random_int(0, pop_size - 1);
double Xi = tmpFS.getCoordAt(variableAChanger);
double XPartner = d_population[partenaire].getCoordAt(variableAChanger);
double phi = generate_random_double(-1, 1);
double Xnew = newCoords(Xi, XPartner);
verifLimit(Xnew);
tmpFS.setCoordAt(variableAChanger, Xnew);
auto coords = tmpFS.getCoords();
tmpFS.setObjectifValue(fct_obj(coords));
tmpFS.setFitnessValue(fitness(tmpFS.getObjectifValue()));
if (greedySelection(d_population[i].getFitnessValue(), tmpFS.getFitnessValue())) {
tmpFS.resetTrial();
d_population[i] = tmpFS;
}
else {
d_population[i].incrementeTrial();
}
}
}
//
// MUHIRWA GABO Oreste
//
void myAlgorithm::onLookerBeePhase() {
calculeProbabilite();
int foodSource = 0, onLookerBee = 0;
while (onLookerBee <= NB_ABEILLES_SPECTATEUR) {
double r = generate_random_double(0, 1);
if (r < d_probabilite[foodSource]) {
employedBeePhase();
onLookerBee = onLookerBee + 1;
}
foodSource = foodSource + 1;
if (foodSource >= d_population.size()) {
foodSource = 0;
}
}
}
//
// MUHIRWA GABO Oreste
//
void myAlgorithm::scoutBeePhase() {
auto index = indexOfSolutionWhereSuperiorOfLimit();
FoodSource FS = create_new_individual();
if (FS.getObjectifValue() < d_bestFS.getObjectifValue()) {
d_population[index] = FS;
}
}
//
// MUHIRWA GABO Oreste & GEILLER Valentin
//
double myAlgorithm::solve() {
double meilleur = DBL_MAX;
// GENERATION
for (int iter = 0; iter < total_func_evals; ++iter) {
// EMPLOYES BEES PHASE
employedBeePhase();
// ONLOOKER BEES PHASE
onLookerBeePhase();
// SCOUT BEES PHASE
FindBestSolution();
scoutBeePhase();
if (meilleur > d_bestFS.getObjectifValue()) {
meilleur = d_bestFS.getObjectifValue();
}
}
std::cout << " - Meilleur : " << meilleur << endl;
return meilleur;
}