Repository navigation
Expand file tree
/
Copy pathIndividual.cpp
More file actions
170 lines (138 loc) · 4.12 KB
/
Copy pathIndividual.cpp
File metadata and controls
170 lines (138 loc) · 4.12 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
#include "Individual.hpp"
#include "MyEvaluator.hpp"
#include <algorithm>
Individual::Individual(size_t genotype_size, int lower_bound, int upper_bound, std::mt19937 &engine)
: genotype_(std::vector<int>(genotype_size)),
fitness_(DEFAULT_FITNESS),
cross_prob_(0.0),
mut_prob_(0.0),
has_fitness_(false)
{
std::uniform_int_distribution<int> gene_dist(lower_bound, upper_bound);
for (size_t i = 0; i < genotype_.size(); ++i)
{
genotype_[i] = gene_dist(engine);
}
std::uniform_real_distribution<float> mut_cross_dist(MIN_PROB, MAX_PROB);
mut_prob_ = mut_cross_dist(engine);
cross_prob_ = mut_cross_dist(engine);
}
Individual::Individual(const std::vector<int> &genotype, float cross_prob, float mut_prob)
: genotype_(genotype),
fitness_(DEFAULT_FITNESS),
cross_prob_(cross_prob),
mut_prob_(mut_prob),
has_fitness_(false)
{
}
Individual::Individual(std::vector<int> &&genotype, float cross_prob, float mut_prob)
: genotype_(std::move(genotype)),
fitness_(DEFAULT_FITNESS),
cross_prob_(cross_prob),
mut_prob_(mut_prob),
has_fitness_(false)
{
}
bool Individual::operator<(const Individual &other) const
{
return fitness_ < other.fitness_;
}
double Individual::calcFitness(MyEvaluator &eval)
{
if (!has_fitness_)
{
fitness_ = eval.evaluate(*this);
has_fitness_ = true;
}
return fitness_;
}
void Individual::crossWith(const Individual &other, Individual &child1, Individual &child2, std::mt19937 &engine, float cross_prob) const
{
std::bernoulli_distribution cross_trial(cross_prob > 0 ? cross_prob : cross_prob_);
if (!cross_trial(engine))
{
child1 = *this;
child2 = other;
return;
}
std::uniform_int_distribution<int> cut_dist(1, static_cast<int>(genotype_.size()) - 1);
int cut_idx = cut_dist(engine);
std::copy(genotype_.begin(), genotype_.begin() + cut_idx, child1.genotype_.begin());
std::copy(genotype_.begin() + cut_idx, genotype_.end(), child2.genotype_.begin() + cut_idx);
std::copy(other.genotype_.begin() + cut_idx, other.genotype_.end(), child1.genotype_.begin() + cut_idx);
std::copy(other.genotype_.begin(), other.genotype_.begin() + cut_idx, child2.genotype_.begin());
child1.has_fitness_ = false;
child2.has_fitness_ = false;
std::bernoulli_distribution avg_strat_trial(0.3);
if (avg_strat_trial(engine))
{
float avg = (mut_prob_ + other.mut_prob_) / 2;
child1.mut_prob_ = avg;
child2.mut_prob_ = avg;
}
else
{
child1.mut_prob_ = mut_prob_;
child2.mut_prob_ = other.mut_prob_;
}
if (avg_strat_trial(engine))
{
float avg = (cross_prob_ + other.cross_prob_) / 2;
child1.cross_prob_ = avg;
child2.cross_prob_ = avg;
}
else
{
child1.cross_prob_ = cross_prob_;
child2.cross_prob_ = other.cross_prob_;
}
}
void Individual::mutateSimple(int lower_bound, int upper_bound, std::mt19937 &engine, float mut_prob)
{
std::normal_distribution<float> param_delta_dist(0.0f, 0.01f);
float cross_delta = param_delta_dist(engine);
float mut_delta = param_delta_dist(engine);
cross_prob_ += cross_delta;
if (cross_prob_ < MIN_PROB) { cross_prob_ = MIN_PROB; }
if (cross_prob_ > MAX_PROB) { cross_prob_ = MAX_PROB; }
mut_prob_ += mut_delta;
if (mut_prob_ < MIN_PROB) { mut_prob_ = MIN_PROB; }
if (mut_prob_ > MAX_PROB) { mut_prob_ = MAX_PROB; }
std::bernoulli_distribution mutation_trial(mut_prob > 0 ? mut_prob : mut_prob_);
std::uniform_int_distribution<int> gene_dist(lower_bound, upper_bound);
for (size_t i = 0; i < genotype_.size(); ++i)
{
if (mutation_trial(engine))
{
genotype_[i] = gene_dist(engine);
}
}
has_fitness_ = false;
}
void Individual::mutateClose(MyEvaluator &eval, std::mt19937 &engine, float mut_prob)
{
std::bernoulli_distribution mutation_trial(mut_prob > 0 ? mut_prob : mut_prob_);
size_t size = genotype_.size();
for (size_t i = 0; i < size; ++i)
{
if (mutation_trial(engine))
{
size_t take_from = i;
double min_distance = eval.getMaxDistance();
for (size_t j = 0; j < size; ++j)
{
if (i != j)
{
double distance = eval.getEdgeWeights().get(i, j);
if (distance < min_distance)
{
min_distance = distance;
take_from = j;
has_fitness_ = false;
}
}
}
genotype_[i] = genotype_[take_from];
}
}
}