def sim_flu_init(session, do_force=False): if 'sim-flu' in session and not do_force: return if 'sim-flu' in session: session.pop('sim-flu') sites = { s:Site(s) for s in ['home', 'school-a', 'school-b']} probe_grp_size_site = GroupSizeProbe.by_rel('site', Site.AT, sites.values(), msg_mode=ProbeMsgMode.CUMUL) session['sim-flu'] = ( Simulation(). set(). pragma_live_info(False). pragma_live_info_ts(False). done(). add(). rule(ResetSchoolDayRule(TimePoint(7))). rule(GoToAndBackTimeAtRule(t_at_attr='t@school')). probe(probe_grp_size_site). done(). new_group(500). set_rel(Site.AT, sites['home']). set_rel('home', sites['home']). set_rel('school', sites['school-a']). done(). new_group(500). set_rel(Site.AT, sites['home']). set_rel('home', sites['home']). set_rel('school', sites['school-b']). done() )
def sim01(): from pram.data import GroupSizeProbe, ProbeMsgMode from pram.entity import Site from pram.rule import GoToAndBackTimeAtRule, ResetSchoolDayRule, TimePoint from pram.sim import Simulation sites = {s: Site(s) for s in ['home', 'school-a', 'school-b']} probe_grp_size_site = GroupSizeProbe.by_rel('site', Site.AT, sites.values(), msg_mode=ProbeMsgMode.CUMUL) (Simulation().add().rule(ResetSchoolDayRule(TimePoint(7))).rule( GoToAndBackTimeAtRule(t_at_attr='t@school')).probe( probe_grp_size_site).commit().new_group(500).set_rel( Site.AT, sites['home']).set_rel('home', sites['home']).set_rel( 'school', sites['school-a']).commit().new_group(500).set_rel( Site.AT, sites['home']).set_rel('home', sites['home']).set_rel( 'school', sites['school-b']).commit().run(18)) return probe_grp_size_site.get_msg()
(Simulation(6,1,24, rand_seed=rand_seed). new_group('g0', 1000). set_rel(Site.AT, sites['home']). set_rel('home', sites['home']). set_rel('work', sites['work-a']). set_rel('store', sites['store-a']). commit(). new_group('g1', 1000). set_rel(Site.AT, sites['home']). set_rel('home', sites['home']). set_rel('work', sites['work-b']). set_rel('store', sites['store-b']). commit(). new_group('g2', 100). set_rel(Site.AT, sites['home']). set_rel('home', sites['home']). set_rel('work', sites['work-c']). commit(). add_rule(GotoRule(TimeInt( 8,12), 0.4, 'home', 'work', 'Some agents leave home to go to work')). add_rule(GotoRule(TimeInt(16,20), 0.4, 'work', 'home', 'Some agents return home from work')). add_rule(GotoRule(TimeInt(16,21), 0.2, 'home', 'store', 'Some agents go to a store after getting back home')). add_rule(GotoRule(TimeInt(17,23), 0.3, 'store', 'home', 'Some shopping agents return home from a store')). add_rule(GotoRule(TimePoint(24), 1.0, 'store', 'home', 'All shopping agents return home after stores close')). add_rule(GotoRule(TimePoint( 2), 1.0, None, 'home', 'All still-working agents return home')). add_probe(probe_grp_size_site). summary((True, True, True, True, True), (0,1)). run(). summary((False, True, False, False, False), (1,1)). run(4) )
fpath_db_out = os.path.join(os.path.dirname(__file__), 'out.sqlite3') if os.path.isfile(fpath_db_out): os.remove(fpath_db_out) pp = ProbePersistenceDB(fpath_db_out, flush_every=1) probe_school_pop_size = GroupSizeProbe( name='school-pop-size', queries=[GroupQry(rel={Site.AT: s}) for s in sites['school'].values()], persistence=pp, var_names=[f'p{i}' for i in range(len(sites['school']))] + [f'n{i}' for i in range(len(sites['school']))], memo=f'Population size across all schools') # ---------------------------------------------------------------------------------------------------------------------- # (3) Simulation: (Simulation().add_rule(ResetSchoolDayRule(TimePoint(7))).add_rule( GoToAndBackTimeAtRule(t_at_attr='t@school')).add_probe( probe_school_pop_size).gen_groups_from_db( fpath_db_in, tbl='people', attr_fix={}, rel_fix={ 'home': site_home }, attr_db=[], rel_db=[GroupDBRelSpec('school', 'school_id', sites['school'])], rel_at='home').summary().run(3, do_disp_t=True))
# ---------------------------------------------------------------------------------------------------------------------- # (3) Simulation: (Simulation(). set(). rand_seed(rand_seed). pragma_autocompact(True). pragma_live_info(pragma_live_info). pragma_live_info_ts(pragma_live_info_ts). pragma_rule_analysis_for_db_gen(True). done(). add(). rule(SEIRModel()). rule(ResetSchoolDayRule(TimePoint(7))). rule(GoToAndBackTimeAtRule(t_at_attr='t@school')). # rule(AttrRule()). probe(probe_grp_size_schools). done(). gen_groups_from_db( fpath_db_in, tbl = 'people', attr_fix = {}, rel_fix = { 'home': site_home }, attr_db = [], rel_db = [ GroupDBRelSpec('school', 'school_id', sites['school']) ], rel_at = 'home' ).
], rel_at='home') print('done.') print('Saving groups... ', end='') with gzip.GzipFile(fpath_groups, 'wb') as f: pickle.dump(groups, f) print('done.') inf('groups', groups, do_calc_size_groups) # groups: 164,459 (175.2M) # file 5.1M # ---------------------------------------------------------------------------------------------------------------------- # (3) Simulation: n_schools = 8 few_schools = [ sites['school'][k] for k in list(sites['school'].keys())[:n_schools] ] probe_grp_size_schools = GroupSizeProbe( 'school', [GroupQry(rel={Site.AT: v}) for v in few_schools]) # sys.exit(0) (Simulation(6, 1, 16, rand_seed=rand_seed).add_groups(groups).add_rule( ResetDayRule(TimePoint(7))).add_rule( AttendSchoolRule()).add_probe(probe_grp_size_schools).run())
GoToRule(0.2, 'home', 'store', TimeInt(16, 21), memo='Some agents go to a store after getting back home')).rule( GoToRule( 0.3, 'store', 'home', TimeInt(17, 23), memo='Some shopping agents return home from a store')). rule( GoToRule(1.0, 'store', 'home', TimePoint(24), memo='All shopping agents return home after stores close')).rule( GoToRule(1.0, None, 'home', TimePoint(2), memo='All still-working agents return home')). probe(probe_grp_size_site).done().new_group(m=1000).set_rel( Site.AT, sites['home']).set_rel('home', sites['home']).set_rel( 'work', sites['work-a']).set_rel( 'store', sites['store-a']).done().new_group(m=1000).set_rel( Site.AT, sites['home']).set_rel('home', sites['home']).set_rel( 'work', sites['work-b']).set_rel('store', sites['store-b']).done(). new_group(m=100).set_rel(Site.AT, sites['home']).set_rel(