Skip to content

Instantly share code, notes, and snippets.

@danodriscoll
Last active April 7, 2025 13:28
Show Gist options
  • Save danodriscoll/e66b788f3d89553bf6f9247018587f73 to your computer and use it in GitHub Desktop.
Save danodriscoll/e66b788f3d89553bf6f9247018587f73 to your computer and use it in GitHub Desktop.
ABMLP-X Producer Employment Process
def employ_household_process(self):
"""Look to employ an unemployed household, with weighted selection based on employment history."""
# Filter for all unemployed households.
total_unemployed_households = list(
filter(lambda obj: not obj.is_employed,
self.model.agents_by_type[Household])
)
# Separate households by type.
unemployed_alpha_households = list(
filter(lambda obj: obj.type == "alpha", total_unemployed_households)
)
unemployed_beta_households = list(
filter(lambda obj: obj.type == "beta", total_unemployed_households)
)
unemployed_gamma_households = list(
filter(lambda obj: obj.type == "gamma", total_unemployed_households)
)
# We use the employment history for the given type, adding 1 so that a household with 0 past employment
# still has a chance.
def weighted_choice(households, type_key):
weights = [household.employment_record_by_type.get(type_key, 0) + 1 for household in households]
return self.random.choices(households, weights=weights, k=1)[0]
# Prioritize employment: choose from alpha if available, then beta, then gamma.
if unemployed_alpha_households:
new_employee = weighted_choice(unemployed_alpha_households, "alpha")
elif unemployed_beta_households:
new_employee = weighted_choice(unemployed_beta_households, "beta")
elif unemployed_gamma_households:
new_employee = weighted_choice(unemployed_gamma_households, "gamma")
else:
sys.exit("Something has gone wrong. No households available for employment. Exiting!")
# Update the employment history for the selected household.
new_employee.employment_record_by_type[new_employee.type] = new_employee.employment_record_by_type.get(new_employee.type, 0) + 1
return new_employee

Comments are disabled for this gist.