-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndividual.cpp
More file actions
55 lines (47 loc) · 1.36 KB
/
Copy pathIndividual.cpp
File metadata and controls
55 lines (47 loc) · 1.36 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
#include "Individual.h"
//Constructor to assign the individual it's chromosome and make sure fitness is
//recalculated whenever an individual is changed
Individual::Individual(std::string chromosome)
{
this->chromosome = chromosome;
fitness = cal_fitness();
}
// Function toPerform mating and produce new offspring with another individual
Individual Individual::mate(Individual par2)
{
// chromosome for offspring
std::string child_chromosome = "";
int len = chromosome.size();
for (int i = 0;i<len;i++)
{
// random probability
float p = Genetics::random_num(0, 100) / 100;
// if prob is less than 0.45, insert gene
// from parent 1
if (p < 0.45)
child_chromosome += chromosome[i];
// if prob is between 0.45 and 0.90, insert
// gene from parent 2
else if (p < 0.90)
child_chromosome += par2.chromosome[i];
// otherwise insert random gene(mutate),
// for maintaining diversity
else
child_chromosome += Genetics::mutated_genes();
}
// create new Individual(offspring) using
// generated chromosome for offspring
return Individual(child_chromosome);
}
// Method to calculate an individuals fitness
int Individual::cal_fitness()
{
int len = Genetics::TARGET.size();
int fitness = 0;
for (int i = 0;i<len;i++)
{
if (chromosome[i] != Genetics::TARGET[i])
fitness++;
}
return fitness;
}