def add_mobility_action(circus, params): logging.info(" creating customer mobility action") mov_prof = [ 1., 1., 1., 1., 1., 1., 1., 1., 5., 10., 5., 1., 1., 1., 1., 1., 1., 5., 10., 5., 1., 1., 1., 1. ] mobility_time_gen = CyclicTimerGenerator( clock=circus.clock, seed=next(circus.seeder), config=CyclicTimerProfile( profile=mov_prof, profile_time_steps="1H", start_date=pd.Timestamp("12 September 2016 00:00.00"), )) gaussian_activity = NumpyRandomGenerator( method="normal", loc=params["mean_daily_customer_mobility_activity"], scale=params["std_daily_customer_mobility_activity"], seed=next(circus.seeder)) mobility_activity_gen = gaussian_activity.map(f=bound_value(lb=.5)) mobility_action = circus.create_story( name="customer_mobility", initiating_actor=circus.actors["customers"], actorid_field="CUST_ID", timer_gen=mobility_time_gen, activity_gen=mobility_activity_gen) logging.info(" adding operations") mobility_action.set_operations( circus.actors["customers"].ops.lookup( id_field="CUST_ID", select={"CURRENT_SITE": "PREV_SITE"}), # selects a destination site (or maybe the same as current... ^^) circus.actors["customers"] \ .get_relationship("POSSIBLE_SITES") \ .ops.select_one(from_field="CUST_ID", named_as="NEW_SITE"), # update the SITE attribute of the customers accordingly circus.actors["customers"] \ .get_attribute("CURRENT_SITE") \ .ops.update( id_field="CUST_ID", copy_from_field="NEW_SITE"), circus.clock.ops.timestamp(named_as="TIME"), # create mobility logs FieldLogger(log_id="customer_mobility_logs", cols=["TIME", "CUST_ID", "PREV_SITE", "NEW_SITE"]), )
def add_mobility_action(circus, params): logging.info(" creating field agent mobility action") # Field agents move only during the work hours mobility_time_gen = WorkHoursTimerGenerator(clock=circus.clock, seed=next(circus.seeder)) fa_mean_weekly_activity = mobility_time_gen.activity( n=params["mean_daily_fa_mobility_activity"], per=pd.Timedelta("1day")) fa_weekly_std = mobility_time_gen.activity( n=params["std_daily_fa_mobility_activity"], per=pd.Timedelta("1day")) gaussian_activity = NumpyRandomGenerator(method="normal", loc=fa_mean_weekly_activity, scale=fa_weekly_std, seed=next(circus.seeder)) mobility_activity_gen = gaussian_activity.map(f=bound_value(lb=1)) field_agents = circus.actors["field_agents"] mobility_action = circus.create_story(name="field_agent_mobility", initiating_actor=field_agents, actorid_field="FA_ID", timer_gen=mobility_time_gen, activity_gen=mobility_activity_gen) logging.info(" adding operations") mobility_action.set_operations( field_agents.ops.lookup( id_field="FA_ID", select={"CURRENT_SITE": "PREV_SITE"}), # selects a destination site (or maybe the same as current... ^^) field_agents \ .get_relationship("POSSIBLE_SITES") \ .ops.select_one(from_field="FA_ID", named_as="NEW_SITE"), # update the SITE attribute of the field agents accordingly field_agents \ .get_attribute("CURRENT_SITE") \ .ops.update( id_field="FA_ID", copy_from_field="NEW_SITE"), circus.clock.ops.timestamp(named_as="TIME"), # create mobility logs FieldLogger(log_id="field_agent_mobility_logs", cols=["TIME", "FA_ID", "PREV_SITE", "NEW_SITE"]), )
def test_populations_during_working_hours(): with path.tempdir() as log_parent_folder: log_folder = os.path.join(log_parent_folder, "logs") circus = Circus(name="tested_circus", master_seed=1, start=pd.Timestamp("8 June 2016"), step_duration=pd.Timedelta("1h")) field_agents = circus.create_population( name="fa", size=100, ids_gen=SequencialGenerator(max_length=3, prefix="id_")) mobility_time_gen = WorkHoursTimerGenerator(clock=circus.clock, seed=next(circus.seeder)) five_per_day = mobility_time_gen.activity(n=5, per=pd.Timedelta("1day")) std_per_day = mobility_time_gen.activity(n=.5, per=pd.Timedelta("1day")) gaussian_activity = NumpyRandomGenerator(method="normal", loc=five_per_day, scale=std_per_day, seed=1) mobility_activity_gen = gaussian_activity.map(bound_value(lb=1)) # just a dummy operation to produce some logs story = circus.create_story(name="test_story", initiating_population=field_agents, member_id_field="some_id", timer_gen=mobility_time_gen, activity_gen=mobility_activity_gen) story.set_operations(circus.clock.ops.timestamp(named_as="TIME"), FieldLogger(log_id="the_logs")) circus.run(duration=pd.Timedelta("30 days"), log_output_folder=log_folder) logging.info("loading produced logs") logs = load_all_logs(log_folder)["the_logs"] logging.info("number of produced logs: {} logs".format(logs.shape[0])) # 30 days of simulation should produce 100 * 5 * 30 == 15k logs assert 14e3 <= logs.shape[0] <= 16e3