-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.cpp
449 lines (392 loc) · 11.4 KB
/
agent.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
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
#include "agent.h"
bool operator!=(const pos &p1, const pos &p2) {
if(p1.x == p2.x && p1.y == p2.y){
return false;
}else return true;
}
Agent::Agent(char sex, int name, Environment* e){
static bool seeded = false;
if(!seeded) {
srand(time(NULL));
seeded = true;
}
walking_pattern = rand() % 7;
my_id.sex = sex;
my_id.name = name;
my_position.x = -1;
my_position.y = -1;
env = e;
ps = INIT;
status = SINGLE;
new_partner = 0;
asked_divorce = 0;
partner = NULL;
reg.x = 0;
reg.y = 0;
}
Agent::~Agent() {
}
void Agent::init_position(int x, int y) {
my_position.x = x;
my_position.y = y;
}
void Agent::run() {
Agent* neighbor = NULL;
switch (ps) {
case INIT:
ps = WANDER_S;
break;
case WANDER_S:
printf("Single wandering <%c,%d>\n", get_id().sex, get_id().name);
if (status == TAKEN) {
// Said yes to someone
reg = env->get_nearest_registry(my_position);
ps = TO_REGISTRY;
} else {
step();
neighbor = env->get_nearest_agent(my_position,get_opposed_sex());
if ( neighbor != NULL) {
// Found someone cool
printf("Found agent around me <%c,%d>\n", get_id().sex, get_id().name);
if (neighbor->marry_me(this) == 1) {
// Uhuuul! It said YES :)
printf("<%c,%d> said yes to <%c,%d>\n", neighbor->get_id().sex,neighbor->get_id().name, get_id().sex, get_id().name);
partner = neighbor;
status = TAKEN;
reg = env->get_nearest_registry(my_position);
ps = TO_REGISTRY;
} else {
printf("<%c,%d> was not interested in <%c,%d>\n", neighbor->get_id().sex, neighbor->get_id().name, get_id().sex, get_id().name);
ps = WANDER_S;
}
} else {
ps = WANDER_S;
printf("Nobody around me <%c,%d>\n", get_id().sex, get_id().name);
}
}
break;
case TO_REGISTRY:
if(partner->reg != reg){
set_register(partner, reg); // Define the same target registry to the partner
} else {
printf("Path to registry at %d,%d <%c,%d>\n", reg.x, reg.y, get_id().sex, get_id().name);
env->get_path_to_reg(my_position, reg);
PrivatePath = env->get_path();
if (!PrivatePath.empty()){
ps = ROUTE_TO_REG;
} else {
cout << "ERROR GETTING PATH TO REGISTRY!" << endl;
}
}
break;
case ROUTE_TO_REG:
printf("In route to registry %d,%d <%c,%d>\n", reg.x, reg.y, get_id().sex, get_id().name);
// code for agent to go to registry goes here
pos p;
p = PrivatePath.top();
if(p.x == reg.x && p.y == reg.y){
// Quando notar que chegou no cartório, para antes de entrar nele
PrivatePath.pop();
ps = WAIT_FIANCE;
}else{
env->update_position(this, p.x, p.y);
my_position.x = p.x;
my_position.y = p.y;
PrivatePath.pop();
}
break;
case WAIT_FIANCE:
printf("Waiting for my fiance <%c,%d>\n", get_id().sex, get_id().name);
if (env->is_agent_here(partner,reg.x, reg.y)){
if (status == TAKEN ) {
// Already completed the private path to registry, release the exclusive path cells from grid
env->clear_path(PrivatePath);
ps = MARRY;
}
else {
// Falhou por algum motivo no meio do caminho
ps = WANDER_S;
status = SINGLE;
partner = NULL;
}
} else {
ps = WAIT_FIANCE;
}
break;
case MARRY:
// Both update positions to same location, and walk together
printf("Getting married <%c,%d>\n", get_id().sex, get_id().name);
if (partner->get_partner() == this) {
status = MARRIED;
// partner->status = MARRIED;
if( my_id.sex == FEMALE) {
env->update_position_partner(this, partner->get_position().x, partner->get_position().y);
env->clean_position(my_position.x, my_position.y);
}
ps = WANDER_M;
}
break;
case DIVORCE:
// printf("Getting divorce <%c,%d>\n", get_id().sex, get_id().name);
// status = SINGLE;
// // partner = NULL;
// env->clean_position_partner(my_position.x,my_position.y);
// // Walk some distance, so they dont remarry
// step();
// step();
// step();
// step();
// ps = WANDER_S;
break;
case WANDER_M:
printf("Married wandering <%c,%d>\n", get_id().sex, get_id().name);
if (my_id.sex == MALE) {
// Not to be sexist, but we had to pick one to "lead" the walk
// and male agents are in the first half positions on the vector
// (because of the input file)
step();
} else {
env->update_position_partner(this, partner->get_position().x, partner->get_position().y);
}
neighbor = env->get_nearest_agent(my_position,get_opposed_sex());
if ( neighbor != NULL && neighbor != partner ) {
// Somebody new
printf("Found agent around me <%c,%d>\n", get_id().sex, get_id().name);
if (greater_pref(neighbor)) {
// Found someone cooler
if (neighbor->marry_me(this)) {
// Uhuuul! It said YES :)
if (partner->get_partner() == this && partner->get_status() == MARRIED) {
partner->divorce_me();
}
printf("<%c,%d> said yes to <%c,%d>\n", neighbor->get_id().sex,
neighbor->get_id().name, get_id().sex, get_id().name);
partner = neighbor;
status = MARRIED;
ps = WANDER_M;
} else {
printf("<%c,%d> was not interested in <%c,%d>\n", neighbor->get_id().sex,
neighbor->get_id().name, get_id().sex, get_id().name);
}
} else {
printf("Was not worthy <%c,%d>\n", get_id().sex, get_id().name);
}
} else {
printf("Nobody around me <%c,%d>\n", get_id().sex, get_id().name);
}
ps = WANDER_M;
break;
}
}
id Agent::get_id() {
return my_id;
}
pos Agent::get_registry() {
return reg;
}
char Agent::get_status(){
return status;
}
int Agent::get_state() {
return ps;
}
int Agent::marry_me(Agent* proposer) {
if(status == TAKEN){
// Neighbor is already heading to register to get married with another agent, ignores the proposal
return 0;
} else if(status == SINGLE) {
// Neighbor will accept any proposal if single or divorced
partner = proposer;
status = TAKEN;
return 1;
} else if(greater_pref(proposer) && status == MARRIED) {
// Married couples can find someone more interesting
printf("<%c,%d> is accepting new partner <%c,%d>\n", proposer->get_id().sex,proposer->get_id().name, partner->get_id().sex, partner->get_id().name);
if (partner->get_partner() == this) {
partner->divorce_me();
}
partner = proposer;
status = MARRIED;
ps = WANDER_M;
return 1;
}
return 0;
}
void Agent::divorce_me() {
printf("Getting divorce <%c,%d>\n", get_id().sex, get_id().name);
if (my_id.sex == FEMALE) {
env->clean_position_partner(partner->get_position().x,partner->get_position().y);
}
status = SINGLE;
partner = NULL;
// Walk some distance, so they dont remarry
step();
step();
step();
step();
ps = WANDER_S;
}
int Agent::greater_pref(Agent* a) {
int partner_it = -1;
int a_it = -1;
printf("Pref sizes -> %ld : ", preferences.size());
for (int i=0;i<preferences.size(); i++) {
printf("%d ", preferences[i]);
if(a->get_id().name == preferences[i])
a_it = i;
if(partner->get_id().name == preferences[i])
partner_it = i;
}
printf("a -> %d \n", a->get_id().name);
if(a_it == -1 || partner_it == -1) {
printf("Error getting preferences!\n");
return 0;
}
if(a_it < partner_it)
return 1;
else
return 0;
}
void Agent::step() {
// 8 possible walk patterns + 1 to randomize it
// Left/right/top/bottom & diagonals
bool success = false;
int temp_x = 0;
int temp_y = 0;
int try_out = 0;
while(!success) {
switch (walking_pattern) {
case 0: // E
if(my_position.x+1 < N){
temp_x = my_position.x+1;
temp_y = my_position.y;
}else {
// Matrix bound
temp_x = 0;
temp_y = my_position.y+1;
}
break;
case 1: // NW
if(my_position.x-1 >= 0 && my_position.y-1 >= 0){
temp_x = my_position.x-1;
temp_y = my_position.y-1;
}else{
// Matrix bound
temp_x = N-1;
temp_y = my_position.y-1;
}
break;
case 2: // W
if(my_position.x-1 >= 0){
temp_x = my_position.x-1;
temp_y = my_position.y;
}else {
// Matrix bound
temp_x = N-1;
temp_y = my_position.y+1;
}
break;
case 3: // SE
if(my_position.x+1 < N && my_position.y+1 < N){
temp_x = my_position.x+1;
temp_y = my_position.y+1;
}else{
// Matrix bound
temp_x = my_position.x+1;
temp_y = 0;
}
break;
case 4: // N
if(my_position.y-1 >= 0){
temp_x = my_position.x;
temp_y = my_position.y-1;
}else {
// Matrix bound
temp_x = my_position.x+1;
temp_y = N-1;
}
break;
case 5: // NE
if(my_position.x+1 < N && my_position.y-1 >= 0){
temp_x = my_position.x+1;
temp_y = my_position.y-1;
}else{
// Matrix bound
temp_x = my_position.x+1;
temp_y = N-1;
}
break;
case 6: // S
if(my_position.y+1 < N){
temp_x = my_position.x;
temp_y = my_position.y+1;
}else {
// Matrix bound
temp_x = my_position.x+1;
temp_y = 0;
}
break;
case 7: // SW
if(my_position.x-1 >= 0 && my_position.y+1 < N){
temp_x = my_position.x-1;
temp_y = my_position.y+1;
}else{
// Matrix bound
temp_x = my_position.x-1;
temp_y = 0;
}
break;
case 8:
temp_x = rand() % N-1;
temp_y = rand() % N-1;
break;
default:
printf("Error walking!!!\n");
break;
}
if (env->free_position(temp_x,temp_y) == 1){
env->update_position(this, temp_x, temp_y);
my_position.x = temp_x;
my_position.y = temp_y;
success = true;
if (walking_pattern == 8)
walking_pattern = 0;
} else {
// change walking pattern and try again
walking_pattern = rand() % 7;
success = false;
try_out++;
if (try_out >= 5)
walking_pattern = 8; //randomize if not able to walk in any direction
}
}
}
pos Agent::get_position() {
return my_position;
}
int Agent::init_prefs(vector<int> p) {
for(int i=0; i<p.size(); i++) {
preferences.push_back(p[i]);
}
}
void Agent::print_prefs() {
for(int i=0; i<preferences.size(); i++) {
printf("Ag<%c,%d> preferences[%d] = %d\n", my_id.sex, my_id.name, i, preferences[i]);
}
}
int Agent::get_pref_at(int i) {
return preferences[i];
}
Agent* Agent::get_partner() {
return partner;
}
char Agent::get_opposed_sex() {
if( my_id.sex == MALE) {
return FEMALE;
} else {
return MALE;
}
}
void Agent::set_register(Agent* partner, pos reg){
partner->reg = reg;
}