-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.hpp
241 lines (222 loc) · 9.7 KB
/
model.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
#ifndef MODEL_HPP
#define MODEL_HPP
#include "names.hpp"
#include "random.hpp"
#include "control.hpp"
#include "population.hpp"
#include "nature.hpp"
#include "graphics.hpp"
#include "interface.hpp"
#include "setup_params.hpp"
struct Model{
Nature nature;
Population p;
SimVar<int> nkids;
SimVar<int> nstarved;
SimVar<int> ndied;
SimVar<int> nppl;
vector<float> plot_fracs;
float plot_avg;
PersonalityTrait plot_trait;
// Group-checking diagnostics
int ncreated;
int nextant;
int nmerged;
TimerManager timer;
int n_turns;
float carrying_capacity;
bool watch;
int watch_start_year;
int i_turn;
Model(SetupParameters& mp)
: nkids(mp.n_years*4),nstarved(mp.n_years*4),ndied(mp.n_years*4),nppl(mp.n_years*4),
nature(mp.min_food_gen,mp.max_food_gen,mp.climate_type,mp.mapsize,mp.mapwidth),
p(mp.initial_n_ppl,mp.mapsize),
plot_fracs(33),
plot_trait(Extroversion)
{
carrying_capacity = (mp.max_food_gen+mp.min_food_gen)/2/FOOD_TO_SURVIVE; // Assuming avg is avg of min and max
n_turns = mp.n_years*4; // A turn is one season
i_turn = 1;
watch = true;
watch_start_year=100;
// Group checking diagnostics
ncreated=0; nextant=0; nmerged=0;
p.sum_nage=0; p.sum_dage=0;
printf("\n ******** SIMULATION BEGINS ******* \n");
}
void set_fracs(vector<float>& fracs, float& avg){
switch(plot_trait){
case Extroversion:
for (int i=0;i<fracs.size();i++){
fracs[i] = p.frac(i,[](int i,Person& h){return h.extroversion==i;});
}
avg = p.avg([](Person& h){return h.extroversion;});
break;
case Agreeableness:
for (int i=0;i<fracs.size();i++){
fracs[i] = p.frac(i,[](int i,Person& h){return h.agreeableness==i;});
}
avg = p.avg([](Person& h){return h.agreeableness;});
break;
case Conscientiousness:
for (int i=0;i<fracs.size();i++){
fracs[i] = p.frac(i,[](int i,Person& h){return h.conscientiousness==i;});
}
avg = p.avg([](Person& h){return h.conscientiousness;});
break;
case Neuroticism:
for (int i=0;i<fracs.size();i++){
fracs[i] = p.frac(i,[](int i,Person& h){return h.neuroticism==i;});
}
avg = p.avg([](Person& h){return h.neuroticism;});
break;
case Openness:
for (int i=0;i<fracs.size();i++){
fracs[i] = p.frac(i,[](int i,Person& h){return h.openness==i;});
}
avg = p.avg([](Person& h){return h.openness;});
break;
}
}
void advance(){
timer.scope();
timer.start("watch and gen_food");
// Check watch
if (watch && i_turn==(watch_start_year*4-3)){
int first_watch;
if (false){
// Option 1:
// Choose someone random of median age
first_watch=p.person.size()/2;
} else {
// Option 2:
// Find biggest group
int max_size=0;
int max_id=0;
for (int i=0; i<p.groups.size();i++){
if(p.groups[i].memberlist.size()>max_size){
max_id=i;
max_size=p.groups[i].memberlist.size();
}
}
first_watch=p.id2ind[p.groups[max_id].leader];
}
p.person[first_watch].watch=true;
p.person[first_watch].play=true;
}
//*** NATURE ***//
nature.generate_food();
if (watch && i_turn>=(watch_start_year*4-3)){
if(i_turn%4==1){
printf("\nYear %d begins",1+(i_turn-1)/4);
printf("\nFollowing:");
for (int i=0;i<p.person.size();i++)
if (p.person[i].watch) printf(" -%s %s (%d)", names[p.person[i].name].c_str(), gnames[p.groups[p.person[i].mships[0].id].name].c_str(), p.person[i].age/4);
}
}
timer.stop(); timer.start("eval_choices");
p.evaluate_choices();
timer.stop(); timer.start("new_groups");
ncreated+=p.new_groups();
timer.stop(); timer.start("leadership");
p.leadership();
timer.stop(); timer.start("wealth requests");
p.wealth_requests();
timer.stop(); timer.start("task requests");
p.task_requests(i_turn);
timer.stop(); timer.start("long_actions");
p.do_long_actions(nature);
timer.stop(); timer.start("update_residents");
p.update_residents(nature);
timer.stop(); timer.start("Steal");
p.take_by_force(i_turn,nature);
timer.stop(); timer.start("Assess defense");
p.assess_defence();
timer.stop(); timer.start("feed");
p.feed_friends();
timer.stop(); timer.start("socialize");
p.socialize();
timer.stop(); timer.start("erode loyalty");
p.erode_loyalty();
timer.stop(); timer.start("purge");
p.purge_memberships();
timer.stop(); timer.start("merge_groups");
int nexb=p.get_nextant();
p.update_memberlists();
p.merge_groups();
nmerged=nexb-p.get_nextant();
timer.stop(); timer.start("eat/enjoy");
p.survive();
p.luxury();
timer.stop(); timer.start("age");
p.age();
if (i_turn%480==1 || i_turn==n_turns){
printf("\n\nYear: %d, Population: %lu",i_turn/4,p.person.size());
int nowextant = p.get_nextant();
int ndestroyed = ncreated-(nowextant-nextant)-nmerged;
if (i_turn>1) printf("\nGroups info: nextant: %d, created: %d, destroyed: %d, merged %d, avg duration: %.1f years",nowextant, ncreated,ndestroyed, nmerged, (0.25f*p.sum_dage)/p.sum_nage);
ncreated=0;nmerged=0; nextant=nowextant; p.sum_nage=0; p.sum_dage=0;
printf("\nPercent growing: %.1f%%", p.frac([](Person& h){return h.worktype==GROW;})*100);
printf("\nPercent foraging: %.1f%%", p.frac([](Person& h){return h.worktype==FORAGE;})*100);
printf("\nPercent guarding: %.1f%%", p.frac([](Person& h){return h.worktype==GUARD;})*100);
}
// Deaths
int n_died;
int n_starved;
tie(n_died,n_starved) = p.die();
nstarved.add(i_turn,n_starved);
ndied.add(i_turn,n_died);
// Exit simulation if no people
if (p.person.size()==0) {printf("\nPopulation went extinct! :("); return;}
timer.stop(); timer.start("clean up");
// Clean up relationships, memberlists, residence lists
p.purge_rships();
p.update_residents(nature);
p.update_memberlists();
timer.stop(); timer.start("breed");
// Breed
int n_kids = p.breed(nature);
nkids.add(i_turn,n_kids);
timer.stop(); timer.start("tile ownership");
// Determine tile ownership (for map viewing - rate could be less)
determine_owners(p,nature);
timer.stop(); timer.start("plotting");
set_fracs(plot_fracs, plot_avg);
timer.stop();
int n_ppl = p.person.size();
nppl.add(i_turn,n_ppl);
//*** SIMULATION ***//
// Report
if (i_turn%480==1 || i_turn==n_turns){
int extant_groups = p.get_nextant();
printf("\nAverage workrate: %.3f", p.avg([](Person& h){return h.workrate;}));
printf("\nAverage luxrate: %.3f", p.avg([](Person& h){return h.luxrate;}));
printf("\nPercent thieves: %.1f%%", p.frac([](Person& h){return h.agreeableness<=9;})*100);
printf("\nAverage #mships: %.1f", p.avg([](Person& h){return h.mships.size();}));
map_by_geogroup(p,nature);
histograms(p);
}
}
void conclusions(){
//ctrl.active=false; // Exit UI
timer.print();
if (p.person.size()==0) return; // If no population, no final stats
map_by_geogroup(p,nature);
map_by_groups(p,nature);
map_by_population(p,nature);
printf("\nAverage age at final step: %.1f", p.avg([](Person& h){return h.age;})/4);
printf("\nGender ratio at final step: %.1f%% women", p.frac([](Person& h){return h.female;})*100);
float avg_ex=p.avg([](Person& h){return h.extroversion;});
printf("\nPercent intraverts: %.1f%%", p.frac(avg_ex-2,[](int x,Person& h){return h.extroversion<x;})*100);
printf("\nPercent extroverts: %.1f%%", p.frac(avg_ex+2,[](int x,Person& h){return h.extroversion>x;})*100);
printf("\nAverage #rships for intraverts: %.1f", p.avg_in(avg_ex-2,[](int x,Person& h){return make_tuple(h.rships.size(),h.extroversion<x);}));
printf("\nAverage fondness for intraverts: %.1f", p.avg_in(avg_ex-2,[](int x,Person& h){float tfond=0.0; for (int i=0;i<h.rships.size();i++){tfond+=h.rships[i].fondness_to;} return make_tuple(tfond/h.rships.size(),h.extroversion<x);}));
printf("\nAverage #rships for extroverts: %.1f", p.avg_in(avg_ex+2,[](int x,Person& h){return make_tuple(h.rships.size(),h.extroversion>x);}));
printf("\nAverage fondness for extroverts: %.1f", p.avg_in(avg_ex+2,[](int x,Person& h){float tfond=0.0; for (int i=0;i<h.rships.size();i++){tfond+=h.rships[i].fondness_to;} return make_tuple(tfond/h.rships.size(),h.extroversion>x);}));
printf("\nAverage population: %.0f (%.1f%% of carrying capacity)",nppl.eq_avg(),100*nppl.eq_avg()/carrying_capacity);
printf("\nAverage deaths/turn: %.3f, starvation: %.0f%%\n",ndied.eq_avg(), 100*nstarved.eq_avg()/ndied.eq_avg());
//nkids.write("nkids.txt");
}
};
#endif