-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGA.cpp~
More file actions
349 lines (287 loc) · 10.7 KB
/
Copy pathGA.cpp~
File metadata and controls
349 lines (287 loc) · 10.7 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <fstream>
#include <time.h> //Used for random operator power
#include "GabilInd.cpp"
using namespace std;
const float OFFSPRING_AMOUNT = 0.2;
const int ORIGINAL_SIZE = 200;
class Population {
public:
vector<Individual> pop;
vector<int> attrSize; //the size of each rule attribute (used on evaluation methods)
//vector<float> fitWeight; //Aggregate weight of the fitness of each value
vector<vector<int> > trainer; //training data
float bigWeight; //parent selection accumulated weight
float bigUnfitWeight; //survivor selection accumulated weight
/********************************* CONSTRUCTORS ********************************/
Population() {}
Population(vector<Individual> _pop, vector<vector<int> > trainingData, vector<int> attrS) {
pop = _pop;
trainer = trainingData;
attrSize = attrS;
}
/********************************* FUNCTIONS **********************************/
//Evaluates the fitness function to each individual of the population
void setBigWeight() {
bigWeight = 0;
bigUnfitWeight = 0;
for (vector<Individual>::reverse_iterator it = pop.rbegin(); it < pop.rend(); it++) {
bigWeight += it->fitness;
it->fitWeight = bigWeight;
}
for (vector<Individual>::iterator it = pop.begin(); it < pop.end(); it++) {
bigUnfitWeight += it->unFitness;
it->unFitWeight = bigUnfitWeight;
}
}
void fitOut() {
for (vector<Individual>::iterator it = pop.begin(); it < pop.end(); it++) {
if (it->fitness == -1) { //Only if it hasn't been set before
it->getFitness(trainer, attrSize);
}
}
sort(pop.begin(), pop.end());
setBigWeight();
}
vector<Individual> crossover(Individual i1, Individual i2) {
int l1 = i1.lowerBound();
int h1 = i1.upperBound(l1);
int l2 = i2.lowerBoundT(l1, h1);
int h2 = i2.upperBoundT(l1, h1, l2);
//cout << "bounds:" << endl;
//cout << l1 << " " << h1 << " " << l2 << " " << h2 << endl;
//cout << "sizes: " << i1.ruleSet.size() << " " << i2.ruleSet.size() << endl;
vector<int> rs1; //first new ruleset
for (int k = 0; k < l1; k++) {
rs1.push_back(i1.ruleSet.at(k));
}
for (int k = l2; k < h2; k++) {
rs1.push_back(i2.ruleSet.at(k));
}
for (int k = h1; k < i1.ruleSet.size(); k++) {
rs1.push_back(i1.ruleSet.at(k));
}
int s1 = rs1.size();
Individual son1(rs1, i1.setSize);
vector<int>rs2; //second new ruleset
for (int k = 0; k < l2; k ++) {
rs2.push_back(i2.ruleSet.at(k));
}
for (int k = l1; k < h1; k++) {
rs2.push_back(i1.ruleSet.at(k));
}
for (int k = h2; k < i2.ruleSet.size(); k++) {
rs2.push_back(i2.ruleSet.at(k));
}
int s2 = rs2.size();
Individual son2(rs2,i2.setSize);
//cout << "created" << endl;
vector<Individual> sons;
sons.push_back(son1);
sons.push_back(son2);
return sons;
}
//Genetic crossover with roulette selection method
vector<Individual> rouletteCrossover() {
vector<Individual> newGene;
int k = (int) pop.size()*OFFSPRING_AMOUNT; //amount of the pop that will yield offspring
while ( k > 0) {
//Parent Selection
//cout << "SELECTING PARENTS" << endl;
vector<Individual>::iterator it = pop.begin(); //first parent
vector<Individual>::iterator it2 = pop.begin(); //second parent
float r1 = randomF()*(bigWeight);
float r2 = randomF()*(bigWeight);
//cout << r1 << endl;
//cout << r2 << endl;
//cout << bigWeight << endl;
while (it < pop.end()) { //find parent 1
if (r1 < it->fitWeight) {
break;
}
it++;
}
while (it2 < pop.end()) { //find parent 2
if (r2 < it2->fitWeight) {
break;
}
it2++;
}
//cout << "selected" << endl;
vector<Individual> i;
//Crossover
//cout << "crossing over" << endl;
if (it->ruleSet.size() < it2->ruleSet.size()) {
//cout << "wat" << endl;
i = crossover(*it, *it2);
}
else {
//cout << "da" << endl;
i = crossover(*it2, *it);
}
//Mutation
//cout << "mutating" << endl;
i.at(0).mutate();
i.at(1).mutate();
//Adding
newGene.push_back(i.at(0));
newGene.push_back(i.at(1));
k--;
}
return newGene;
}
vector<Individual> tournamentCrossover() {
int k = pop.size()*OFFSPRING_AMOUNT;
vector<Individual> newGene;
for (int i = 0; i < k; i++) {
//get First parent
Individual p1;
p1 = pop.at((int) floor(randomF()*pop.size())); //set an initial
for (int j = 0; j < k/2 - 1; j++) {
int r = (int) floor(randomF()*pop.size());
if (p1.fitness < pop.at(r).fitness) { //compare to new ones
p1 = pop.at(r);
}
}
//get Second parent
Individual p2;
p2 = pop.at((int) floor(randomF()*pop.size())); //set an initial
for (int j = 0; j < k/2 - 1; j++) {
int r = (int) floor(randomF()*pop.size());
if (p2.fitness < pop.at(r).fitness) { //compare to new ones
p2 = pop.at(r);
}
}
vector<Individual> newi;
//crossover
if (p1.ruleSet.size() < p2.ruleSet.size()) {
newi = crossover(p1,p2);
}
else {
newi = crossover(p2,p1);
}
//mutation
newi.at(0).mutate();
newi.at(1).mutate();
//addition
newGene.push_back(newi.at(0));
newGene.push_back(newi.at(1));
}
return newGene;
}
void rouletteSelection() {
int toDel = pop.size() - ORIGINAL_SIZE;
for (int k = 0; k < toDel; k++) {
float r = randomF() * bigUnfitWeight;
vector<Individual>::iterator it = pop.begin();
while (r > it->unFitWeight) {
it++;
}
pop.erase(it);
setBigWeight();
}
return;
}
void tournamentSelection() {
int toDel = pop.size() - ORIGINAL_SIZE;
for (int k = 0; k < toDel; k++) {
int i = (int) floor(randomF()*pop.size());
for (int j = 0; j < 20; j++) {
int r = (int) floor(randomF()*pop.size());
if (pop.at(i).fitness > pop.at(r).fitness) {
i = r;
}
}
pop.erase(pop.begin() + i);
}
setBigWeight();
return;
}
//Creates the new generation, based on crossover and mutation attributes.
//i will be the parameter the defines what selection method to use
void newGen(int i, int j) {
vector<Individual> newGene;
//int rS = pop.at(0).setSize;
if (i == 0) { //Roulette method
newGene = rouletteCrossover();
}
if (i == 1) {
newGene = tournamentCrossover();
}
//cout << "crossed" << endl;
//Create the new population
vector<Individual> newPop;
newPop.reserve(pop.size() + newGene.size());
newPop.insert(newPop.end(), pop.begin(), pop.end());
newPop.insert(newPop.end(), newGene.begin(), newGene.end());
pop = newPop;
fitOut();
//Select survivors
if (j == 0) {
//cout << "SELECTION" << endl << endl;
rouletteSelection();
}
if (j == 1) {
tournamentSelection();
}
fitOut();
}
};
void readTrainingSet(string fileName){
vector<vector<int> > data; //Training Set
string line;
ifstream file (fileName);
if (file.is_open())
{
while (getline(file, line, ','))
{
cout << line << '\n';
}
file.close();
}
}
int main() {
srand(time(NULL)); //Random seed initialization
readTrainingSet("Datos");
int ruleSize = 5; // the size of each rule, including the classification
vector<int> attrS;
attrS.push_back(2);
attrS.push_back(2);
attrS.push_back(-1);
vector<Individual> pops;
for (int i = 0; i < ORIGINAL_SIZE; i++) {
pops.push_back(Individual(ruleSize));
}
vector<int> train;
train.push_back(1);
train.push_back(0);
train.push_back(1);
train.push_back(0);
train.push_back(0);
vector<vector<int> > data;
data.push_back(train);
vector<int> train2;
train2.push_back(1);
train2.push_back(0);
train2.push_back(0);
train2.push_back(1);
train2.push_back(1);
data.push_back(train2);
Population estequetaki(pops, data, attrS); //Palabra aguda
estequetaki.fitOut();
int i = 0;
cout << estequetaki.bigWeight << endl;
for (int i = 0; i < 1000; i ++) {
cout << "iteracion : " << i << ", BW: " << estequetaki.bigWeight << ", UBW: "
<< estequetaki.bigUnfitWeight << endl;
cout << "pop size: " << estequetaki.pop.size() << ", ruleSize: "
<< estequetaki.pop.at(199).setSize << endl;
estequetaki.newGen(1,1);
}
cout << estequetaki.bigWeight << endl;
}