Skip to content

Social network

SocialNetwork

Source code in june/groups/leisure/social_network.py
 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
class SocialNetwork:
    """ """

    def __init__(self):
        """
        Initialise the SocialNetwork class by loading and precomputing preferences from a YAML file.
        """
        self.household_vs_friends_priorities = self._load_and_precompute_preferences()

    def _load_and_precompute_preferences(self):
        """Load and precompute preferences from the YAML file into a dictionary for fast lookup.
        Precompute using 'm' and 'f' for gender keys.


        Returns:
            dict: Precomputed preferences mapping (activity, sex, age) -> weights.

        """
        with open(default_config_filename, "r") as file:
            raw_preferences = yaml.safe_load(file)

        precomputed = {}
        activity_preferences = raw_preferences["preferences"]["activity"]

        for activity, sexes in activity_preferences.items():
            for sex, age_groups in sexes.items():
                # Map 'male' -> 'm' and 'female' -> 'f'
                short_sex = "m" if sex.lower() == "male" else "f"
                for age_range, probs in age_groups.items():
                    min_age, max_age = map(int, age_range.split("-"))
                    for age in range(min_age, max_age + 1):
                        key = (activity, short_sex, age)
                        precomputed[key] = (probs["household"], probs["friends"])
        return precomputed

    def decide_target_group_invitation(self, person, activity):
        """Decide whether a person invites their household or friends first for an activity.

        Args:
            person: A Person object with
            activity: str

        Returns:
            type: str: "household" or "friends"

        """
        key = (activity, person.sex.lower(), person.age)
        weights = self.household_vs_friends_priorities.get(key)

        if not weights:
            raise ValueError(
                f"No preferences found for activity '{activity}', sex '{person.sex}', and age {person.age}."
            )

        # Decide based on precomputed weights
        choice = random.choices(["household", "friends"], weights=weights, k=1)[0]
        return choice

__init__()

Initialise the SocialNetwork class by loading and precomputing preferences from a YAML file.

Source code in june/groups/leisure/social_network.py
11
12
13
14
15
def __init__(self):
    """
    Initialise the SocialNetwork class by loading and precomputing preferences from a YAML file.
    """
    self.household_vs_friends_priorities = self._load_and_precompute_preferences()

decide_target_group_invitation(person, activity)

Decide whether a person invites their household or friends first for an activity.

Parameters:

Name Type Description Default
person

A Person object with

required
activity

str

required

Returns:

Name Type Description
type

str: "household" or "friends"

Source code in june/groups/leisure/social_network.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def decide_target_group_invitation(self, person, activity):
    """Decide whether a person invites their household or friends first for an activity.

    Args:
        person: A Person object with
        activity: str

    Returns:
        type: str: "household" or "friends"

    """
    key = (activity, person.sex.lower(), person.age)
    weights = self.household_vs_friends_priorities.get(key)

    if not weights:
        raise ValueError(
            f"No preferences found for activity '{activity}', sex '{person.sex}', and age {person.age}."
        )

    # Decide based on precomputed weights
    choice = random.choices(["household", "friends"], weights=weights, k=1)[0]
    return choice