-
Notifications
You must be signed in to change notification settings - Fork 0
/
Village.cpp
150 lines (129 loc) · 3.76 KB
/
Village.cpp
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
#include "Village.h"
int SIZE_GENOME = 200;
int SIZE_STEP = 2;
int MUTATION_CHANCE = 1;
int SIZE_N = 100;
int SIZE_M = 50;
int HARDNESS = 3;
int SEED = 0;
Village::Village(int id, int population, int seed)
{
SEED = seed;
this->id = id;
this->population = population;
this->map = new Map(SIZE_N, SIZE_M, SEED, HARDNESS);
for(int i=0;i<population;++i){
Genome *g;
if(i < 8)
g = new Genome(SIZE_GENOME, SIZE_STEP, MUTATION_CHANCE, i);
else
g = new Genome(SIZE_GENOME, SIZE_STEP, MUTATION_CHANCE);
Person *p = new Person(i, g, this->map->getStart());
this->people.push_back(make_pair(p, 0));
}
this->reproduce();
}
Map* Village::getMap(){
return this->map;
}
Person* Village::getPersonTest(){
Person* p = this->people.at(0).first;
return p;
}
std::vector<Person*> Village::getPeople(){
std::vector<Person*> p;
for(int i=0;i<this->population;++i){
p.push_back(this->people.at(i).first);
}
return p;
}
int Village::iterate(){
for(int i=0;i<this->population;++i){
this->people.at(i).first->reset(this->map);
for(int j=0;j<SIZE_GENOME; ++j){
this->people.at(i).first->move(this->map);
}
}
return 1;
}
void Village::generate(){
evaluate();
kill();
reproduce();
}
VillageP Village::getVillageP(int num) {
VillageP res;
res.set_id(this->id);
srand(time(NULL));
for(int i = 0; i < num; i++) {
int r = rand()%this->population-1;
vector<Gene> adn = this->people.at(r).first->getGenome()->getAdn();
PersonP* p = res.add_population();
int size = adn.size();
for(int j = 0; j < size; j++) {
GeneP* g = p->add_adn();
g->set_direct(adn[j].direct);
g->set_steps(adn[j].steps);
}
this->people.erase(this->people.begin() + r);
this->population--;
}
return res;
}
void Village::setVillageP(VillageP vil){
for(int i = 0; i < vil.population_size(); i++){
Genome *g = new Genome(SIZE_GENOME, SIZE_STEP, MUTATION_CHANCE, vil.population(i));
Person *p = new Person(this->population, g, this->map->getStart());
this->people.push_back(make_pair(p,0));
this->population++;
}
}
void Village::reproduce(){
int pop = this->population;
srand(time(NULL));
int pt1 = rand()%SIZE_GENOME;
int pt2 = pt1+rand()%(SIZE_GENOME-pt1);
Genome *g;
for(int i=0;i<pop;i=i+2){
if(i%4 == 0)
g = Genome::ChildRandom(this->people.at(rand()%pop).first->getGenome(), this->people.at(rand()%pop).first->getGenome());
else if(i%4 == 1)
g = Genome::ChildCrossOverOnePoint(this->people.at(rand()%pop).first->getGenome(), this->people.at(rand()%pop).first->getGenome(), pt1);
else if(i%4 == 2)
g = Genome::ChildCrossOverTwoPoint(this->people.at(rand()%pop).first->getGenome(), this->people.at(rand()%pop).first->getGenome(), pt1, pt2);
else
g = Genome::ChildCrossOverhalf(this->people.at(rand()%pop).first->getGenome(), this->people.at(rand()%pop).first->getGenome());
Person *p = new Person(this->population, g, this->map->getStart());
this->people.push_back(make_pair(p,0));
this->population++;
}
}
void Village::evaluate(){
int note = 0;
Person *p;
for(int i=0;i<this->population;++i){
p = this->people.at(i).first;
note = -p->getLocation()->distance(this->map->getEnd()) + ((SIZE_GENOME*2) * p->getArrived()) - (p->getGenomePosition() * p->getArrived());
this->people.at(i).second = note;
}
std::sort(this->people.begin(), this->people.end(), pairCompare);
}
void Village::kill(){
int l= ceil(this->population/3);
for(int i=0; i<l; ++i){
this->people.pop_back();
this->population--;
}
}
static bool pairCompare(const std::pair<Person*, int>& firstElem, const std::pair<Person*, int>& secondElem){
return firstElem.second > secondElem.second;
}
int Village::getBestNote(){
return this->people.at(0).second;
}
void Village::setPopulation(int pop){
this->population = pop;
}
int Village::getPopulation(){
return this->population;
}