This repository was archived by the owner on Dec 23, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.cpp
More file actions
288 lines (249 loc) · 6.72 KB
/
Copy pathPerson.cpp
File metadata and controls
288 lines (249 loc) · 6.72 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
#include "Person.hpp"
#include <stdio.h>
Person::Person():
energie(0),
id(global_id)
{
global_id++;
makeThisForThat();
}
void Person::makeRandom()
{
for(int i = 0; i < SINGLE_EVALUATION_SIZE*4; i++)
behavior[i] = rand()%4;
for(int i = 0; i < EVALUATION_SIZE; i++)
evaluation[i] = rand()%(SINGLE_EVALUATION_SIZE*2);
for(int i = 0; i < EVALUATION_EXCHANGE_SIZE; i++)
evaluationExchange[i] = rand()%SINGLE_EVALUATION_SIZE;
}
void Person::makeThisForThat()
{
behavior[0] = 1;
behavior[1] = 0;
for(int i = 2; i < (4*SINGLE_EVALUATION_SIZE); i++)
behavior[i] = 1;
for(int j = 0; j < SINGLE_EVALUATION_SIZE; j++)
{
evaluation[j * 2] = 1 + SINGLE_EVALUATION_SIZE;
evaluation[1 + j * 2] = SINGLE_EVALUATION_SIZE;
}
for(int i = 0; i < SINGLE_EVALUATION_SIZE; i++)
for(int j = 0; j < SINGLE_EVALUATION_SIZE; j++)
evaluationExchange[i + j * SINGLE_EVALUATION_SIZE] = j;
}
void Person::makeAlwaysCooperate()
{
for(int i = 0; i < 4*SINGLE_EVALUATION_SIZE; i++)
behavior[i] = 1;
for(int i = 0; i < EVALUATION_SIZE; i++)
evaluation[i] = SINGLE_EVALUATION_SIZE;
for(int i = 0; i < EVALUATION_EXCHANGE_SIZE; i++)
evaluationExchange[i] = 0;
}
void Person::makeAlwaysNotCooperate()
{
for(int i = 0; i < 4*SINGLE_EVALUATION_SIZE; i++)
behavior[i] = 0;
for(int i = 0; i < EVALUATION_SIZE; i++)
evaluation[i] = 0;
for(int i = 0; i < EVALUATION_EXCHANGE_SIZE; i++)
evaluationExchange[i] = 0;
}
int Person::getBehaviorCode(int my_evaluation, bool is_negative_score, bool is_votes)
{
if(!is_negative_score)
{
if(!is_votes)
return (my_evaluation);
else return (my_evaluation + 2*SINGLE_EVALUATION_SIZE);
}
else
{
if(!is_votes)
return (my_evaluation + SINGLE_EVALUATION_SIZE);
else return (my_evaluation + 3*SINGLE_EVALUATION_SIZE);
}
}
bool Person::getAction(int my_behavior)
{
return(behavior[my_behavior] == 1);
}
bool Person::getVote(int my_behavior)
{
return(behavior[my_behavior] == 2);
}
int Person::getNewEvaluation(int my_behavior)
{
return evaluation[my_behavior]%SINGLE_EVALUATION_SIZE;
}
bool Person::getAllowExchange(int my_behavior)
{
if(evaluation[my_behavior]/SINGLE_EVALUATION_SIZE == 1)
return true;
else false;
}
int Person::getNewEvaluationAfterExchange(int my_evaluation)
{
return evaluationExchange[my_evaluation];
}
int Person::getEvaluationAfterExchangeCode(int other_evaluation, int my_evaluation)
{
int code = other_evaluation;
code += my_evaluation * SINGLE_EVALUATION_SIZE;
return code;
}
int Person::getEvaluationCode(bool other_action, int my_evaluation)
{
int code = 0;
if(other_action)
code += 1;
code += my_evaluation * 2;
return code;
}
Person::~Person()
{
reset();
}
void Person::reset()
{
for(std::list<Bewertung*>::iterator i = bewertung.begin(); i != bewertung.end(); i++)
delete (*i);
bewertung.clear();
energie = 0;
}
Bewertung* Person::getEntry(Person* other_person)
{
for(std::list<Bewertung*>::iterator i = bewertung.begin(); i != bewertung.end(); i++)
{
if((*i)->id == other_person->id)
return (*i);
}
Bewertung* entry = new Bewertung();
entry->id = other_person->id;
entry->bewertung = 0;
bewertung.push_back(entry);
return entry;
}
void Person::calculateEnergieChange(bool my_action, bool other_action, int& my_energie, int& other_energie)
{
if(my_action)
{
cooperate++;
if(other_action)
{
cooperate++;
my_energie+=1;
other_energie+=1;
} else
{
my_energie -= 2;
other_energie += 3;
}
} else
{
if(other_action)
{
cooperate++;
my_energie+=3;
other_energie -= 2;
} else
{
my_energie -= 1;
other_energie -= 1;
}
}
}
void Person::resetVotes()
{
for(int i = 0; i < MAX_POP_SIZE; i++)
{
votes[i] = votes2[i];
votes2[i] = 0;
}
}
void Person::exchangeInformation(Person* other_person)
{
for(std::list<Bewertung*>::iterator j = other_person->bewertung.begin(); j != other_person->bewertung.end(); j++)
{
bool known = false;
int my_evaluation = 0;
int other_evaluation = (*j)->bewertung;
Bewertung* my_entry = NULL;
for(std::list<Bewertung*>::iterator i = bewertung.begin(); i != bewertung.end(); i++)
if((*i)->id == (*j)->id)
{
my_entry = (*i);
my_evaluation = my_entry->bewertung;
known = true;
break;
}
int code = getEvaluationAfterExchangeCode(other_evaluation, my_evaluation);
int new_evaluation = getNewEvaluationAfterExchange(code);
if(!known)
{
my_entry = new Bewertung();
my_entry->id = (*j)->id;
bewertung.push_back(my_entry);
}
my_entry->bewertung = new_evaluation;
}
}
void Person::meet(Person* other_person)
{
Bewertung* entry1;
Bewertung* entry2;
entry1 = getEntry(other_person);
entry2 = other_person->getEntry(this); // we need two first_encounters, because one person might know the other person by gossip
int my_behavior_code = getBehaviorCode(entry1->bewertung, energie < 0, Person::votes[other_person->id] > MAX_POP_SIZE/2);
int other_behavior_code = getBehaviorCode(entry2->bewertung, other_person->energie < 0, Person::votes[other_person->id] > MAX_POP_SIZE/2);
int my_action = getAction(my_behavior_code);
int other_action = other_person->getAction(other_behavior_code);
if(getVote(my_behavior_code))
Person::votes2[other_person->id]++;
if(other_person->getVote(other_behavior_code))
Person::votes2[id]++;
int my_evaluation_code = getEvaluationCode(other_action, entry1->bewertung);
int other_evaluation_code = getEvaluationCode(my_action, entry2->bewertung);
entry1->bewertung = getNewEvaluation(my_evaluation_code);
entry2->bewertung = other_person->getNewEvaluation(other_evaluation_code);
calculateEnergieChange(my_action, other_action, energie, other_person->energie);
// TODO globales 'Verbrecherverzeichnis'?
if(other_person->getAllowExchange(other_evaluation_code))
exchangeInformation(other_person);
if(getAllowExchange(my_evaluation_code))
other_person->exchangeInformation(this);
}
void Person::generate(Person* parent, Person* child)
{
for(int i = 0; i < 4*SINGLE_EVALUATION_SIZE; i++)
{
if(rand()%(4*SINGLE_EVALUATION_SIZE) == 0)
{
child->behavior[i] = rand()%4;
// parent->behavior[i] = rand()%2;
}
else child->behavior[i] = parent->behavior[i];
}
for(int i = 0; i < EVALUATION_SIZE; i++)
{
if(rand()%EVALUATION_SIZE == 0)
{
child->evaluation[i] = rand()%(2*SINGLE_EVALUATION_SIZE);
// parent->evaluation[i] = rand()%8;
}
else child->evaluation[i] = parent->evaluation[i];
}
for(int i = 0; i < EVALUATION_EXCHANGE_SIZE; i++)
{
if(rand()%EVALUATION_EXCHANGE_SIZE == 0)
{
child->evaluationExchange[i] = rand()%SINGLE_EVALUATION_SIZE;
// parent->evaluationExchange[i] = rand()%16;
}
else child->evaluationExchange[i] = parent->evaluationExchange[i];
}
}
int Person::global_id = 0;
int Person::cooperate = 0;
int Person::votes[MAX_POP_SIZE];
int Person::votes2[MAX_POP_SIZE];