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 | class FriendshipDistributor:
"""Distributes friendships among a list of people based on configurable criteria.
This class assigns friendships to individuals in the given list, ensuring
constraints such as minimum and maximum number of friends, age tolerance,
and minimum age for friendship are respected.
Attributes
----------
people : list
List of Person instances to assign friendships to.
min_friends : int
Minimum number of friends an individual can have.
max_friends : int
Maximum number of friends an individual can have.
age_tolerance : int
Maximum allowed age difference between friends.
min_friend_age : int
Minimum age for a person to be eligible for friendship.
"""
def __init__(self, people):
"""
Initialises the FriendshipDistributor.
Parameters
----------
people : list
List of Person instances.
"""
min_friends = 1
max_friends = 5
age_tolerance = 5
min_friend_age = 12
self.people = people
self.min_friends = min_friends
self.max_friends = max_friends
self.age_tolerance = age_tolerance
self.min_friend_age = min_friend_age
self.friendship_counts = {} # Track friendships per super area
def link_all_friends(self, super_areas):
"""Trigger the entire friendship linking process with optimised pre-filtering.
Args:
super_areas:
"""
logger.info("Starting the friendship linking process...")
# Initialise friendship counts once
self.friendship_counts = {super_area.name: 0 for super_area in super_areas}
# Pre-filter eligible people across the entire population
logger.info("Pre-filtering eligible people...")
# Use dictionaries for faster lookups
eligible_people = {} # person_id -> person
age_ranges = {} # person_id -> (min_age, max_age)
# Optimise for specific activity types
activity_by_type = {
"company": {}, # company_id -> [people]
"school": {}, # school_id -> [people]
"university": {}, # university_id -> [people]
"none": [] # people with no activity
}
area_people = {} # area_id -> [list of eligible people]
super_area_people = {} # super_area_id -> [list of eligible people]
# Single pass through all people to build all indexes
total_people = 0
eligible_count = 0
# Process each super area efficiently
for super_area in super_areas:
super_id = id(super_area)
super_area_people[super_id] = []
# Process all areas in the super area
for area in super_area.areas:
area_id = id(area)
area_people[area_id] = []
total_people += len(area.people)
# Process each person
for person in area.people:
# Initialise friends dictionary if needed
if person.friends is None:
person.friends = {}
# Skip ineligible people immediately
if person.age < self.min_friend_age:
continue
eligible_count += 1
person_id = person.id
# Store person reference and precalculate age range
eligible_people[person_id] = person
age_ranges[person_id] = (
max(self.min_friend_age, person.age - self.age_tolerance),
person.age + self.age_tolerance
)
# Add to area and super area indexes
area_people[area_id].append(person)
super_area_people[super_id].append(person)
# Classify by activity type
if person.subgroups.primary_activity is not None:
activity = getattr(person.subgroups.primary_activity, 'group', None)
if activity is not None:
# Get activity type and ID
activity_spec = getattr(activity, 'spec', None)
activity_id = id(activity)
if activity_spec in activity_by_type:
if activity_id not in activity_by_type[activity_spec]:
activity_by_type[activity_spec][activity_id] = []
activity_by_type[activity_spec][activity_id].append(person)
else:
# Default to no activity if spec not recognised
activity_by_type["none"].append(person)
else:
activity_by_type["none"].append(person)
else:
activity_by_type["none"].append(person)
logger.info(f"Found {eligible_count} eligible people out of {total_people} total")
# 1. Link friends in primary activity groups (highest priority) - optimised by activity type
logger.info("Linking friends in primary activity groups...")
activity_start = perf_counter()
# Process schools first (typically smaller, more age-homogeneous groups)
school_count = sum(len(group) for group in activity_by_type["school"].values())
logger.info(f"Processing {len(activity_by_type['school'])} schools with {school_count} people")
self._process_activity_type(
activity_by_type["school"],
eligible_people,
age_ranges,
"activity",
batch_size=50 # Smaller batch size for schools
)
# Process universities (medium-sized, somewhat age-homogeneous)
uni_count = sum(len(group) for group in activity_by_type["university"].values())
logger.info(f"Processing {len(activity_by_type['university'])} universities with {uni_count} people")
self._process_activity_type(
activity_by_type["university"],
eligible_people,
age_ranges,
"activity",
batch_size=20 # Medium batch size for universities
)
# Process companies (can be very large and diverse)
company_count = sum(len(group) for group in activity_by_type["company"].values())
logger.info(f"Processing {len(activity_by_type['company'])} companies with {company_count} people")
self._process_activity_type(
activity_by_type["company"],
eligible_people,
age_ranges,
"activity",
batch_size=10 # Smaller batch size for companies (which can be large)
)
activity_time = perf_counter() - activity_start
logger.info(f"Activity linking completed in {activity_time:.2f} seconds")
# 2. Link friends within areas for those who still need friends
logger.info("Linking friends within areas...")
area_start = perf_counter()
# Filter people needing more friends after activity linking
for area_id in area_people:
# Skip empty areas
if not area_people[area_id]:
continue
# Only process people who still need friends
need_more_friends = [p for p in area_people[area_id] if len(p.friends) < self.max_friends]
if need_more_friends:
self._link_friends_directly(
people=need_more_friends,
eligible_people=eligible_people,
age_ranges=age_ranges,
context="area"
)
area_time = perf_counter() - area_start
logger.info(f"Area linking completed in {area_time:.2f} seconds")
# 3. Link friends within super areas for remaining cases
logger.info("Linking friends within super areas...")
super_area_start = perf_counter()
for super_id in super_area_people:
# Skip empty super areas
if not super_area_people[super_id]:
continue
# Only process people who still need friends
need_more_friends = [p for p in super_area_people[super_id] if len(p.friends) < self.max_friends]
if len(need_more_friends) >= 2: # Need at least 2 people to form friendships
self._link_friends_directly(
people=need_more_friends,
eligible_people=eligible_people,
age_ranges=age_ranges,
context="super_area"
)
super_area_time = perf_counter() - super_area_start
logger.info(f"Super area linking completed in {super_area_time:.2f} seconds")
# Calculate friendship statistics
total_time = activity_time + area_time + super_area_time
total_friendships = sum(self.friendship_counts.values())
logger.info(f"Friendship linking complete: {total_friendships} friendships created in {total_time:.2f} seconds")
# Check for people without friends (sample for debugging)
no_friends = self.find_people_without_friends(list(eligible_people.values()))
if no_friends:
logger.info(f"{len(no_friends)} eligible people have no friends.")
else:
logger.info("All eligible people have at least one friend.")
# Debug Print: Sample Friendships Visualization
logger.info("Visualising a sample of friendships...")
all_people = [
person
for super_area in super_areas
for area in super_area.areas
for person in area.people
]
# Randomly select up to 10 people
sample_people = random.sample(
all_people,
min(15, len(all_people))
)
def get_person_info(person):
"""Retrieve a formatted string with a person's details.
Args:
person:
"""
hobbies = ", ".join(person.hobbies) if person.hobbies else "None"
return (
f"ID {person.id} (Sex: {person.sex}, Age: {person.age}, "
f"Primary Activity: {getattr(person.subgroups.primary_activity, 'group', None)}, "
f"Area: {person.area.name if person.area else 'Unknown'}, "
f"Super Area: {person.area.super_area.name if person.area and person.area.super_area else 'Unknown'}, "
f"Hobbies: {hobbies})"
)
def get_friend_info(friend_id):
"""Retrieve a formatted string with a person's details.
Args:
friend_id:
"""
friend=Person.find_by_id(friend_id)
hobbies = ", ".join(friend.hobbies) if friend.hobbies else "None"
return (
f"ID {friend.id} (Sex: {friend.sex}, Age: {friend.age}, "
f"Primary Activity: {getattr(friend.subgroups.primary_activity, 'group', None)}, "
f"Area: {friend.area.name if person.area else 'Unknown'}, "
f"Super Area: {friend.area.super_area.name if friend.area and friend.area.super_area else 'Unknown'}, "
f"Hobbies: {hobbies})"
)
for person in sample_people:
if person.friends:
print(
f"Person {get_person_info(person)} is friends with:"
)
for friend_id, friend_data in person.friends.items():
# Handle both old format (just home_rank) and new format (dict)
if isinstance(friend_data, dict):
home_rank = friend_data.get("home_rank", 0)
friend_hobbies = friend_data.get("hobbies", [])
hobbies_str = ", ".join(friend_hobbies) if friend_hobbies else "None"
print(f" - Friend {get_friend_info(friend_id)} (Home Rank: {home_rank}, Stored Hobbies: {hobbies_str})")
else:
# Old format - just home_rank
print(f" - Friend {get_friend_info(friend_id)} (Home Rank: {friend_data})")
def _process_activity_type(self, activity_groups, eligible_people, age_ranges, context, batch_size=20):
"""Process activities of a specific type.
Parameters:
-----------
activity_groups : dict
Dictionary mapping activity_id -> [list of people]
eligible_people : dict
Dictionary mapping person_id -> person
age_ranges : dict
Dictionary mapping person_id -> (min_age, max_age)
context : str
Context for friendship linking
batch_size : int
Number of groups to process in each batch
Args:
activity_groups:
eligible_people:
age_ranges:
context:
batch_size: (Default value = 20)
"""
# Get sorted list of activity IDs, prioritising by size (smallest first)
activity_ids = sorted(
activity_groups.keys(),
key=lambda x: len(activity_groups[x])
)
# Process in batches
for batch_start in range(0, len(activity_ids), batch_size):
batch_end = min(batch_start + batch_size, len(activity_ids))
batch_activity_ids = activity_ids[batch_start:batch_end]
for activity_id in batch_activity_ids:
people = activity_groups[activity_id]
# Skip groups with fewer than 2 people
if len(people) < 2:
continue
# For large groups, we'll need to be more selective
if len(people) > 200:
# For large groups, pre-filter by age buckets
age_buckets = {}
for person in people:
age_bucket = person.age // 5 # Group ages in 5-year buckets
if age_bucket not in age_buckets:
age_buckets[age_bucket] = []
age_buckets[age_bucket].append(person)
# Process each age bucket separately
for bucket in age_buckets.values():
if len(bucket) >= 2: # Only process buckets with at least 2 people
self._link_friends_directly(
people=bucket,
eligible_people=eligible_people,
age_ranges=age_ranges,
context=context
)
else:
# For smaller groups, process all at once
self._link_friends_directly(
people=people,
eligible_people=eligible_people,
age_ranges=age_ranges,
context=context
)
def _link_friends_directly(self, people, eligible_people, age_ranges, context="generic"):
"""Optimised method to link friends using pre-calculated indexes.
Args:
people:
eligible_people:
age_ranges:
context: (Default value = "generic")
"""
# Create a cache for area-specific eligible people
area_cache = {} # area_id -> list of eligible people
# Process people in batches for improved performance
batch_size = min(1000, len(people)) # Adjust batch size based on memory constraints
for batch_start in range(0, len(people), batch_size):
batch_end = min(batch_start + batch_size, len(people))
batch = people[batch_start:batch_end]
for person in batch:
# Skip if already at max friends
remaining_slots = self.max_friends - len(person.friends)
if remaining_slots <= 0:
continue
person_id = person.id
min_age, max_age = age_ranges[person_id]
# Find compatible potential friends based on context
if context == "activity":
# For activity, we're already working with people in the same group
potential_friends = [
p for p in batch # Only search within the current batch
if (p.id != person_id and
p.id not in person.friends and
len(p.friends) < self.max_friends and
min_age <= p.age <= max_age)
]
elif context == "area":
# For area, use area-specific filtering with external cache
area = person.area
area_id = id(area)
# Build cache for this area if not already done
if area_id not in area_cache:
area_cache[area_id] = [
p for p in area.people
if p.id in eligible_people and p.age >= self.min_friend_age
]
potential_friends = [
p for p in area_cache[area_id]
if (p.id != person_id and
p.id not in person.friends and
len(p.friends) < self.max_friends and
min_age <= p.age <= max_age)
]
elif context == "super_area":
# For super area, use more selective filtering
super_area = person.area.super_area
potential_friends = [
p for p in people # people is already filtered to super area
if (p.id != person_id and
p.id not in person.friends and
len(p.friends) < self.max_friends and
min_age <= p.age <= max_age and
p.area.super_area == super_area)
]
else:
# Generic context
potential_friends = [
p for p in people
if (p.id != person_id and
p.id not in person.friends and
len(p.friends) < self.max_friends and
min_age <= p.age <= max_age)
]
# Skip if no potential friends found
n_potential = len(potential_friends)
if n_potential == 0:
continue
# Calculate weights with numpy for speed
weights = np.ones(n_potential, dtype=np.float32)
for i, friend in enumerate(potential_friends):
# Prioritise connections based on multiple factors
# 1. Super area match (highest priority)
if (person.area and friend.area and
person.area.super_area == friend.area.super_area):
weights[i] *= 3.0
# 2. Shared hobbies (significant boost)
if hasattr(person, "hobbies") and hasattr(friend, "hobbies"):
if person.hobbies and friend.hobbies:
shared_count = len(set(person.hobbies) & set(friend.hobbies))
if shared_count > 0:
weights[i] *= (1.5 + 0.5 * shared_count)
else:
weights[i] *= 0.7
# 3. Work sector match (modest boost)
if person.sector == friend.sector and person.sector is not None:
weights[i] *= 1.2
# 4. Age similarity (sliding scale)
age_diff = abs(person.age - friend.age)
weights[i] *= max(0.5, 1.0 - (age_diff / max(1, self.age_tolerance)))
# Determine number of friends to add
n_friends = min(
random.randint(1, remaining_slots),
n_potential
)
# Select friends using weighted probabilities
if n_potential > 1:
probs = weights / np.sum(weights)
try:
# Try to use faster numpy selection when possible
indices = np.random.choice(
n_potential,
size=n_friends,
replace=False,
p=probs
)
selected_friends = [potential_friends[i] for i in indices]
except ValueError:
# Fallback to standard random.choices if numpy fails
selected_friends = random.choices(
potential_friends,
weights=weights.tolist(),
k=min(n_friends, n_potential)
)
else:
# Just one potential friend
selected_friends = potential_friends
# Establish friendships (bidirectional)
for friend in selected_friends:
# Get friend's hobbies for storage
friend_hobbies = getattr(friend, 'hobbies', []) or []
person_hobbies = getattr(person, 'hobbies', []) or []
# Store friendship with home rank and hobbies
person.friends[friend.id] = {
"home_rank": 0, # Default home rank to 0, will be updated later
"hobbies": friend_hobbies.copy() # Store friend's hobbies
}
if friend.friends is None: # Safety check
friend.friends = {}
friend.friends[person.id] = {
"home_rank": 0, # Default home rank to 0, will be updated later
"hobbies": person_hobbies.copy() # Store person's hobbies
}
# Track friendship counts if needed
if hasattr(self, 'friendship_counts') and person.area and friend.area:
p_super = getattr(person.area, 'super_area', None)
f_super = getattr(friend.area, 'super_area', None)
if p_super and p_super.name in self.friendship_counts:
self.friendship_counts[p_super.name] += 1
if f_super and p_super != f_super and f_super.name in self.friendship_counts:
self.friendship_counts[f_super.name] += 1
def find_people_without_friends(self, people):
"""Find and report individuals aged 12 or older without any friends.
Args:
people (list): List of all people in the simulation.
Returns:
list: List of individuals aged 12 or older who have no friends.
"""
no_friends = [
person for person in people
if (
person.age >= self.min_friend_age)
and
(not person.friends or len(person.friends) == 0
)
]
return no_friends
|