Skip to content

Instantly share code, notes, and snippets.

@BenSchZA
Created May 11, 2021 12:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenSchZA/3c4491a138c506c7944f858abc18450c to your computer and use it in GitHub Desktop.
Save BenSchZA/3c4491a138c506c7944f858abc18450c to your computer and use it in GitHub Desktop.
Access cadCAD state_history
import numpy as np
def p_predator_births(params, substep, state_history, previous_state):
'''Predator Births Policy Function
The predator birth rate (rate of predators born per unit of time) is a product of
the prey population and the predator birth parameter plus a random variable.
i.e. the larger the prey population, the higher the predator birth rate
'''
# Parameters
dt = params['dt']
predator_birth_parameter = params['predator_birth_parameter']
random_predator_birth = params['random_predator_birth']
# State Variables
predator_population = previous_state['predator_population']
prey_population = previous_state['prey_population']
# Calculate the predator birth rate
birth_rate = prey_population * (predator_birth_parameter + np.random.random() * random_predator_birth)
# Calculate change in predator population
births = birth_rate * predator_population * dt
# Example access state history
mean_predator_population = np.mean([timestep[-1]["predator_population"] for timestep in state_history])
print(mean_predator_population)
return {'add_to_predator_population': births}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment