-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericCrowding.cpp
More file actions
197 lines (159 loc) · 5.14 KB
/
GenericCrowding.cpp
File metadata and controls
197 lines (159 loc) · 5.14 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
/*
* GenericCrowding.cpp
* ABQ
*
* Created by Elliot Meyerson on 3/24/15.
* Copyright 2015 Elliot Meyerson. All rights reserved.
*
*/
#include "GenericCrowding.h"
#include <iostream>
#include "rutil.h"
GenericCrowding::GenericCrowding(Tartarus &task) {
task_ = task;
num_iterations_ = 1000000;
mutation_rate_ = 0.3;
tournament_size_ = 10;
population_size_ = 100;
genome_size_ = 30*5;
action_history_size_ = 80*100;
curr_iteration_ = 0;
total_fitness_ = 0;
best_fitness_ = 0;
population_.resize(population_size_+2);
childA_ = population_size_;
childB_ = population_size_ + 1;
InitPopulation();
}
void GenericCrowding::InitPopulation() {
for (int i = 0; i < population_size_; i++) {
RandomIndividual(i);
Evaluate(i);
total_fitness_ += population_[i].fitness;
if (population_[i].fitness > best_fitness_) {
best_fitness_ = population_[i].fitness;
}
}
RandomIndividual(childA_);
RandomIndividual(childB_);
}
void GenericCrowding::RandomIndividual(int i) {
population_[i].genome.resize(genome_size_);
population_[i].action_history.resize(action_history_size_);
for (int g = 0; g < genome_size_; g++) {
population_[i].genome[g] = RandomWeight();
}
}
void GenericCrowding::Next() {
//std::cout << "TournamentSelecting..\n";
int parentA = TournamentSelect();
int parentB = TournamentSelect();
//std::cout << "Crossover..\n";
Crossover(parentA,parentB);
//std::cout << "Mutating..\n";
Mutate(childA_);
Mutate(childB_);
//std::cout << "Evaluating..\n";
Evaluate(childA_);
Evaluate(childB_);
//std::cout << "CrowdSelecting..\n";
int loserA = CrowdingSelect(childA_);
int loserB = CrowdingSelect(childB_);
//std::cout << "Replacing..\n";
Replace(loserA, childA_);
Replace(loserB, childB_);
curr_iteration_++;
}
void GenericCrowding::Evaluate(int i) {
task_.Reset();
brain_.SetWeights(population_[i].genome);
int action;
for (int a = 0; a < action_history_size_; a++) {
if (a % 80 == 0) { brain_.Flush(); }
brain_.SetInput(task_.Sense());
brain_.Step();
action = brain_.GetAction();
//std::cout << " Action: ";
//std::cout << action;
//std::cout << "\n";
task_.Act(action);
population_[i].action_history[a] = action;
}
population_[i].fitness = task_.fitness();
//std::cout << task_.fitness();
//std::cout << "\n";
}
int GenericCrowding::TournamentSelect() {
int max_score = MIN_FITNESS;
int winner = -1;
for (int i = 0; i < tournament_size_; i++) {
int j = rutil::pick_a_number(0,population_size_);
if (population_[j].fitness > max_score) {
max_score = population_[j].fitness;
winner = j;
}
}
return winner;
}
void GenericCrowding::Crossover(int parentA, int parentB) {
int point = rutil::pick_a_number(0, genome_size_);
for (int i = 0; i < point; i++) {
population_[childA_].genome[i] = population_[parentA].genome[i];
population_[childB_].genome[i] = population_[parentB].genome[i];
}
for (int i = point; i < genome_size_; i++) {
population_[childA_].genome[i] = population_[parentB].genome[i];
population_[childB_].genome[i] = population_[parentA].genome[i];
}
}
void GenericCrowding::Mutate(int i) {
for (int g = 0; g < genome_size_; g++) {
if (rutil::pick_a_number(0.0, 1.0) < mutation_rate_){
population_[i].genome[g] = RandomWeight();
}
}
}
int GenericCrowding::CrowdingSelect(int child) {
int min_distance = MAX_DISTANCE;
int loser = -1;
for (int i = 0; i < tournament_size_; i++) {
int j = rutil::pick_a_number(0, population_size_);
int dist = HammingDistance(child, j);
if (dist < min_distance) {
min_distance = dist;
loser = j;
}
}
//std::cout << "Loser: ";
//std::cout << loser;
//std::cout << "\n";
return loser;
}
void GenericCrowding::Replace(int loser, int winner) {
if (population_[winner].fitness > best_fitness_) {
best_fitness_ = population_[winner].fitness;
}
total_fitness_ -= population_[loser].fitness;
total_fitness_ += population_[winner].fitness;
Individual tmp = population_[loser];
population_[loser] = population_[winner];
population_[winner] = tmp;
}
double GenericCrowding::RandomWeight() {
double weight = rutil::pick_a_number(-10.0, 10.0);//(dis_(gen_)*20)-10;
//std::cout << weight;
//std::cout << "\n";
return weight;
}
bool GenericCrowding::Done() { return curr_iteration_ == num_iterations_; }
double GenericCrowding::best_fitness() { return best_fitness_; }
double GenericCrowding::avg_fitness() { return total_fitness_ / population_size_; }
int GenericCrowding::HammingDistance(int i, int j) {
int dist = 0;
for (int a = 0; a < action_history_size_; a++) {
if (population_[i].action_history[a] != population_[j].action_history[a]) {
dist++;
}
}
return dist;
}