-
Notifications
You must be signed in to change notification settings - Fork 0
/
population.hpp
590 lines (538 loc) · 22.2 KB
/
population.hpp
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#ifndef POPULATION_HPP
#define POPULATION_HPP
#include <vector>
#include <string.h>
#include "random.hpp"
#include "myutils.hpp"
#include "fwd_class_defs.hpp"
#include "group.hpp"
#include "person.hpp"
#include "group.impl.cpp"
#include "person.impl.cpp"
using std::vector;
using std::string;
using std::to_string;
using std::min;
using std::max;
using std::make_tuple;
using std::tie;
using std::tuple;
class Population{
int n_ids;
int lifespan;
int fertility_age;
float fertility_rate; // births/turn
int max_rships;
public:
// Diag
int sum_dage;
int sum_nage;
vector<Person> person; // Public so people can see each other (use friend instead?)
vector<int> id2ind; // map from id to index
vector<Group> groups;
Population(int initial_n_ppl, int map_size) : n_ids(initial_n_ppl) {
// Population-wide traits
lifespan = 240;
fertility_age = 64;
fertility_rate = 4.2/(lifespan-fertility_age);
max_rships=50;
int initial_n_groups=10;
// Initialize individuals
for(int i = 0; i<initial_n_ppl;i++)
person.push_back(Person(i,(i*lifespan)/initial_n_ppl,lifespan,i%map_size)); // 54*22+33
// Initialize id to index mapping
for(int i = 0; i<initial_n_ppl;i++)
id2ind.push_back(i);
// Initialize groups
for(int i = 0; i<initial_n_groups;i++)
groups.push_back(Group(i,0,initial_n_ppl/10));
// Assign people randomly to groups
// for(int i = 0; i<initial_n_ppl;i++)
// person[i].mships.push_back(Membership(rand_int(groups.size()),INIT_LOYALTY));
}
void task_requests(int i_turn) {
for(int i = 0; i<groups.size();i++)
if(groups[i].memberlist.size()>0) groups[i].set_task_request(person[id2ind[groups[i].leader]]);
// Reply from members: randomized
RandPerm rp(person.size());
for(int i = 0; i<person.size();i++){
int ri = rp.x[i];
person[ri].respond_to_task_requests(groups,ri);
}
for(int i = 0; i<groups.size();i++)
if(groups[i].memberlist.size()>0) groups[i].set_tasks(person[id2ind[groups[i].leader]]);
}
void assess_defence() {
for(int i = 0; i<groups.size();i++){
// Convert from people to tiles
vector<int> victim_homes;
vector<int> lused;
vector<int> lundefended;
for (int j=0;j<groups[i].used.size();j++){
int victim_home=person[groups[i].used[j]].home;
int vhome_ind = get_index(victim_homes,victim_home);
if (vhome_ind==-1){ // Add to list of tiles
vhome_ind=victim_homes.size();
victim_homes.push_back(victim_home);
lused.push_back(0);
lundefended.push_back(0);
}
lused[vhome_ind]++;
}
for (int j=0;j<groups[i].undefended.size();j++){
int victim_home=person[groups[i].undefended[j]].home;
int vhome_ind = get_index(victim_homes,victim_home);
if (vhome_ind==-1){ // Add to list of tiles
vhome_ind=victim_homes.size();
victim_homes.push_back(victim_home);
lused.push_back(0);
lundefended.push_back(0);
}
lundefended[vhome_ind]++;
}
vector<int> guards_left(victim_homes.size(),0);
for (int j=0;j<groups[i].guards.size();j++){
if (groups[i].guards[j].nactions==0 && groups[i].guards[j].task==DEFEND) continue; // Guard defended
int guard_station=groups[i].guards[j].station;
int ghome_ind = get_index(victim_homes,guard_station);
if (ghome_ind==-1){ // Add to list of tiles
ghome_ind=victim_homes.size();
victim_homes.push_back(guard_station);
lused.push_back(0);
lundefended.push_back(0);
guards_left.push_back(0);
}
guards_left[ghome_ind]++;
}
// Assess offence:
vector<int> lused_soldier(victim_homes.size(),0);
for (int j=0;j<groups[i].used_soldier.size();j++){
int victim_home=groups[i].used_soldier[j];
int vhome_ind = get_index(victim_homes,victim_home);
if (vhome_ind==-1){ // Add to list of tiles
vhome_ind=victim_homes.size();
victim_homes.push_back(victim_home);
lused.push_back(0);
lundefended.push_back(0);
guards_left.push_back(0);
lused_soldier.push_back(0);
}
lused_soldier[vhome_ind]++;
}
vector<int> lunused_soldier(victim_homes.size(),0);
for (int j=0;j<groups[i].unused_soldier.size();j++){
int victim_home=groups[i].unused_soldier[j];
int vhome_ind = get_index(victim_homes,victim_home);
if (vhome_ind==-1){ // Add to list of tiles
vhome_ind=victim_homes.size();
victim_homes.push_back(victim_home);
lused.push_back(0);
lundefended.push_back(0);
guards_left.push_back(0);
lused_soldier.push_back(0);
lunused_soldier.push_back(0);
}
lunused_soldier[vhome_ind]++;
}
groups[i].assess_defence(victim_homes,lused,lundefended,guards_left, lused_soldier, lunused_soldier);
}
}
void evaluate_choices() {
for(int i = 0; i<person.size();i++)
person[i].evaluate_choices();
}
void leadership() {
// Leadership model: popularity contest
for (int i=0;i<groups.size();i++)
// Leadership for life, popularity contest for next leader
if (id2ind[groups[i].leader]==-1){ // Leader is dead
groups[i].choose_leadership(person);
}
}
void do_long_actions(Nature &nature) {
RandPerm rp(person.size());
for(int i = 0; i<person.size();i++){
int ri = rp.x[i];
person[ri].do_long_action(nature);
}
}
void take_by_force(int i_turn,Nature& nature) {
RandPerm rp(person.size());
for(int i = 0; i<person.size();i++){
int ri = rp.x[i];
person[ri].take_by_force(person, groups, nature);
}
}
void feed_friends() {
for(int i = 0; i<person.size();i++)
person[i].feed_friends(person,id2ind);
}
void socialize() {
for(int i = 0; i<person.size();i++)
person[i].socialize(person,id2ind,groups);
}
void erode_loyalty() {
for(int i = 0; i<person.size();i++)
person[i].erode_loyalty();
}
void purge_memberships() {
for(int i = 0; i<person.size();i++){
person[i].purge_memberships(groups);
}
}
void update_memberlists(){
vector<int> old_nmembers(groups.size());
vector<int> nmembers(groups.size(),0);
// Get old group size
for(int i = 0; i<groups.size();i++){
old_nmembers[i]=groups[i].memberlist.size();
}
// Get new group list
for (int i=0;i<person.size();i++){
for (int j=0;j<person[i].mships.size();j++){
int g = person[i].mships[j].id;
if (nmembers[g]<old_nmembers[g]){ // If not larger than before, overwrite
groups[g].memberlist[nmembers[g]]=i;
}else{ // If larger, allocate more
groups[g].memberlist.push_back(i);
}
nmembers[g]++;
}
}
// If smaller, free up remaining memory
for(int i = 0; i<groups.size();i++){
groups[i].memberlist.resize(nmembers[i]);
}
}
void update_residents(Nature& nature){
// printf ("\nX4 %d, %lu",13,nature.map[13].residents.size()); fflush(stdout);
vector<int> old_nresidents(nature.map.size());
vector<int> nresidents(nature.map.size(),0);
// Get old #residents
for(int i = 0; i<nature.map.size();i++){
old_nresidents[i]=nature.map[i].residents.size();
// printf ("\nX1 %d, %d",i,old_nresidents[i]); fflush(stdout);
}
// Get new resident list
for (int i=0;i<person.size();i++){
int h = person[i].home;
if (nresidents[h]<old_nresidents[h]){ // If not larger than before, overwrite
nature.map[h].residents[nresidents[h]]=i;
}else{ // If larger, allocate more
nature.map[h].residents.push_back(i);
}
nresidents[h]++;
// printf ("\nX2 %d",i); fflush(stdout);
}
// If smaller, free up remaining memory
for(int i = 0; i<nature.map.size();i++){
// printf ("\nX3 %d, %d, %lu, %d",i,old_nresidents[i], nature.map[i].residents.size(),nresidents[i]); fflush(stdout);
nature.map[i].residents.resize(nresidents[i]);
// printf ("\nX4 %d, %lu",i,nature.map[i].residents.size()); fflush(stdout);
}
}
int get_nextant(){
int extant_groups=0;
for (int i=0;i<groups.size();i++)
if (groups[i].memberlist.size()>0) extant_groups++;
return extant_groups;
}
void merge_groups(){
for(int i = 0; i<groups.size();i++){
if (groups[i].memberlist.size()==0) continue; // Don't check empty groups
int similar_group = find_similar_group(groups[i].memberlist,groups[i].id);
if (similar_group>=0){
bool firstwatch=true; // Only announce once (for when watch is on)
// merge smaller group into bigger one
int id_bigger = (groups[i].npaying > groups[similar_group].npaying ? i : similar_group);
int id_smaller = (groups[i].npaying <= groups[similar_group].npaying ? i : similar_group);
for(int j = 0; j<groups[id_smaller].memberlist.size();j++){ // Loop over members of smaller group
int pind = groups[id_smaller].memberlist[j];
int k = person[pind].mship_index(id_smaller);
// If person is in big group too, delete the small group
// (LOSS OF INFO (e.g. loyalty) - could MERGE instead)
bool in_both=person[pind].is_member(id_bigger);
if (in_both){
person[pind].mships.erase(person[pind].mships.begin() + k);
} else { // Join bigger group
person[pind].mships[k].id=id_bigger; // Just change the ID
groups[id_bigger].memberlist.push_back(pind);
}
if (person[pind].watch && firstwatch){
printf("\nGroup %s merged into %s.",gnames[groups[id_smaller].name].c_str(),gnames[groups[id_bigger].name].c_str());
firstwatch=false;
}
}
// Resources/obligations from smaller group go to larger group
groups[id_bigger].wealth += groups[id_smaller].wealth;
groups[id_bigger].nguards+= groups[id_smaller].nguards;
groups[id_bigger].nundefended+= groups[id_smaller].nundefended;
groups[id_bigger].nused+= groups[id_smaller].nused;
groups[id_smaller].memberlist.resize(0);
}
}
}
void survive() {
for(int i = 0; i<person.size();i++){
if (person[i].wealth<FOOD_TO_SURVIVE) person[i].will_starve=true;
float to_eat = min(person[i].wealth,FOOD_TO_SURVIVE);
person[i].wealth-=to_eat;
//if (person[i].watch) printf("\n%s's cness after surviving: %.3f", names[person[i].name].c_str(),person[i].contentedness);
}
}
void luxury() {
for(int i = 0; i<person.size();i++)
person[i].luxury();
}
void wealth_requests(){
for(int i = 0; i<groups.size();i++)
groups[i].set_wealth_request();
for(int i = 0; i<person.size();i++)
person[i].respond_to_wealth_requests(groups);
for(int i = 0; i<groups.size();i++)
groups[i].assess_wealth_request();
}
void age() {
for(int i = 0; i<person.size();i++){
person[i].age++;
}
for(int i = 0; i<groups.size();i++){
groups[i].age++;
if (groups[i].memberlist.size()==0 && groups[i].prevsize !=0){
sum_dage += groups[i].age;
sum_nage +=1;
}
groups[i].prevsize = groups[i].memberlist.size();
}
}
tuple<int,int> die() {
vector<bool> will_die(person.size(),false);
for(int i = 0; i<person.size();i++){
if ( person[i].age>person[i].lifespan // Old age
|| person[i].will_starve ){ // Starvation
will_die[i]=true;
}
}
// Inheritance
for(int i = 0; i<person.size();i++){
if (will_die[i]==true){
float inheritance=person[i].wealth;
if (inheritance==0.0f) continue; // skip if no inheritance
int n_kids=0;
for (int j = 0; j<person[i].rships.size();j++){
if (person[i].rships[j].reltype==Child){
n_kids+=1;
}
}
if (n_kids>0){ // Even distribution to kids
for (int j = 0; j<person[i].rships.size();j++)
if (person[i].rships[j].reltype==Child)
person[id2ind[person[i].rships[j].person_id]].wealth+=inheritance/n_kids;
} else if (person[i].rships.size()>0) { // No kids? Give to random surviving friend for now
int friend_ind = id2ind[person[i].rships[rand_int(person[i].rships.size())].person_id];
if (!will_die[friend_ind])
person[friend_ind].wealth+=inheritance;
}
// Wealth disappears if no kids and no friends (or randomly selected friend also died this turn)
}
}
// Check deaths twice, first in forward order to adjust id2ind...
int deaths_so_far=0;
for(int i = 0; i<person.size();i++){
if (will_die[i]){
deaths_so_far++;
id2ind[person[i].id] = -1;
} else {
id2ind[person[i].id] = i-deaths_so_far;
}
}
// ...then backwards to remove
int n_died=0;
int n_starved=0;
for(int i = (int)(person.size())-1; i>=0;i--){
bool died=false;
if (person[i].age>person[i].lifespan){ // Old age
died=true;
if (person[i].watch) printf("\n%s died of old age",names[person[i].name].c_str());
} else if (person[i].will_starve){ // Starvation
died=true;
if (person[i].watch) printf("\n%s died of starvation",names[person[i].name].c_str());
n_starved++;
}
if (died){
n_died++;
// Remove from vector
person.erase(person.begin() + i);
}
}
return make_tuple(n_died,n_starved);
}
void purge_rships(){
for(int i = 0; i<person.size();i++){
person[i].purge_rships(max_rships, person, id2ind);
}
}
int breed(Nature& nature) {
int n_kids = 0;
for(int i = 0; i<person.size();i++){
n_kids += person[i].breed(n_ids+n_kids,fertility_age, fertility_rate, person,id2ind, nature);
}
// Updated id to index mapping
for(int i = person.size()-n_kids; i<person.size();i++)
id2ind.push_back(i);
n_ids+=n_kids;
return n_kids;
}
void find_potential_group(vector<int>& new_group, Person& p1, int ip1, int j){
int ip2=id2ind[p1.rships[j].person_id]; // ind of P2
new_group.push_back(ip1);
new_group.push_back(ip2);
// If this rship is strong, run through P1's friend list for mutual friends
for(int k = 0; k<p1.rships.size();k++){
if (k!=j && p1.rships[k].fondness_to>3){ // Another close friend of P1, P3
int friend2_id = p1.rships[k].person_id;
for(int l = 0; l<person[ip2].rships.size();l++){ // Look for P3 in P2's list
if (person[ip2].rships[l].person_id==friend2_id && // P3 is friends with P2
person[ip2].rships[l].fondness_to>3){ // Is also a close friend!
new_group.push_back(id2ind[friend2_id]);
break;
}
}
}
}
}
int find_similar_group(vector<int>& new_group, int thisgroupid){
int groupsize = new_group.size();
for (int g=0;g<groupsize;g++){ // Loop over new group members
int ind = new_group[g];
for (int k=0;k<person[ind].mships.size();k++){ // Loop over their group affiliations
int xgroupid = person[ind].mships[k].id;
if (xgroupid == thisgroupid) continue; // If checking for uniqueness (for merge_groups), don't examine own group
int xgroupsize = groups[person[ind].mships[k].id].memberlist.size(); // Size of preexisting group
if (groupsize>2*xgroupsize || xgroupsize>2*groupsize) // If one is more than double the other's size
continue; // Clearly not the same group
// But if they're in the same size range, examine further
int n_common_members=0;
for (int gc=0;gc<groupsize;gc++){
int indc = new_group[gc];
if (person[indc].is_member(xgroupid))
n_common_members++;
}
if (n_common_members*2>groupsize || n_common_members*2>xgroupsize){
// More than half the members in common
// Is the same group
return xgroupid;
}
}
}
return -1;
}
int new_groups(){ // O(N*R^3 + N*R*G^2*M^2)
int nstart = groups.size();
// Have a random 10% of the population look for groups to cut down on run time
int n_group_checkers = person.size()/10;
RandPerm rp(person.size());
vector<int> new_group;
new_group.reserve(person.size());
for(int i = 0; i<n_group_checkers;i++){
int ri = rp.x[i];
// P1 doesn't have enough friends
if (person[ri].rships.size()+1<SIZEFORMGROUP) continue;
for(int j = 0; j<person[ri].rships.size();j++){
if (person[ri].rships[j].fondness_to>=FONDFORMGROUP){ // P1 has a close friend (P2)
// P2 doesn't have enough friends
//if (person[id2ind[person[ri].rships[j].person_id]].rships.size()+1<SIZEFORMGROUP) continue;
new_group.resize(0);
find_potential_group(new_group,person[ri],ri,j);
int groupsize = new_group.size();
if (groupsize>=SIZEFORMGROUP) {
// Check if group already exists
bool found_similar = (find_similar_group(new_group,-1)>=0);
if (!found_similar){
int newgroup_id=groups.size();
groups.push_back(Group(newgroup_id,0,groupsize));
for (int g=0;g<groupsize;g++){ // Loop over new group members
person[new_group[g]].mships.push_back(Membership(newgroup_id,INIT_LOYALTY));
groups[newgroup_id].memberlist[g]=new_group[g];
if (person[new_group[g]].watch) printf("\n%s joined a new group called %s", names[person[new_group[g]].name].c_str(),gnames[groups[newgroup_id].name].c_str());
}
groups[newgroup_id].choose_leadership(person);
}
}
}
}
}
return groups.size()-nstart;
}
template <typename Proc>
float avg(Proc p){
float sum=0.0;
for(int i = 0; i<person.size();i++){
sum += p(person[i]);
}
return sum/person.size();
}
template <typename Proc>
float frac(Proc p){
float sum=0.0;
for(int i = 0; i<person.size();i++){
sum += p(person[i]) ? 1.0 : 0.0;
}
return sum/person.size();
}
template <typename Proc>
float frac(int x, Proc p){
float sum=0.0;
for(int i = 0; i<person.size();i++){
sum += p(x, person[i]) ? 1.0 : 0.0;
}
return sum/person.size();
}
template <typename Proc>
float avg_in(Proc p){
float sum=0.0;
float x;
bool use;
int count=0;
for(int i = 0; i<person.size();i++){
tie(x,use) = p(person[i]);
if (use){
sum+=x;
count+=1;
}
}
return sum/count;
}
template <typename Proc>
float avg_in(int x, Proc p){
float sum=0.0;
float tmp;
bool use;
int count=0;
for(int i = 0; i<person.size();i++){
tie(tmp,use) = p(x,person[i]);
if (use){
sum+=tmp;
count+=1;
}
}
return sum/count;
}
private:
// Write properties to file
void snap_age(const char* filebase, int i_turn){
string turn_str = to_string(i_turn);
char filename[256]="";
strcat(filename, filebase);
strcat(filename, turn_str.c_str());
strcat(filename, ".txt");
FILE * pFile;
pFile = fopen (filename,"w");
for (int i=0 ; i<person.size() ; i++)
{
fprintf (pFile, "%d\n",person[i].age);
}
fclose (pFile);
}
};
#endif