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 run_the_app(): # Draw the UI elements to search for objects (pedestrians, cars, etc.) gsize, s1, s2, s3, i1, i2, i3, r1, r2, r3, run = frame_selector_ui() progress_flu_rule = DiscreteInvMarkovChain('flu-status', { 's': [s1, s2, s3], 'i': [i1, i2, i3], 'r': [r1, r2, r3] }) # s - susceptible # i - infectious # r - recovered sites = { 'home': Site('h'), 'work': Site('w') } probe_grp_size_flu = GroupSizeProbe.by_attr('flu', 'flu-status', progress_flu_rule.get_states(), msg_mode=ProbeMsgMode.DISP, memo='Mass distribution across flu status') probe_grp_size_site = GroupSizeProbe.by_rel('site', Site.AT, sites.values(), msg_mode=ProbeMsgMode.DISP, memo='Mass distribution across sites') data = "" s = Simulation() s.add_rule(progress_flu_rule) s.add_probe(probe_grp_size_flu) s.add_group(Group('g0', gsize, { 'flu-status': 's' })) sys.stdout = open('out.dat', 'w') s.run(int(run)) sys.stdout.close() with open('out.dat') as file: data = file.readlines() for line in data: st.write(line)
def simple(self): progress_flu_rule = DiscreteInvMarkovChain('flu-status', { 's': [0.95, 0.05, 0.00], 'i': [0.00, 0.50, 0.50], 'r': [0.10, 0.00, 0.90] }) # s - susceptible # i - infectious # r - recovered sites = { 'home': Site('h'), 'work': Site('w') } probe_grp_size_flu = GroupSizeProbe.by_attr('flu', 'flu-status', progress_flu_rule.get_states(), msg_mode=ProbeMsgMode.DISP, memo='Mass distribution across flu status') probe_grp_size_site = GroupSizeProbe.by_rel('site', Site.AT, sites.values(), msg_mode=ProbeMsgMode.DISP, memo='Mass distribution across sites') # ---------------------------------------------------------------------------------------------------------------------- # (1) Simulations testing the basic operations on groups and rules: # (1.1) A single-group, single-rule (1g.1r) simulation: s = Simulation() s.add_rule(progress_flu_rule) s.add_probe(probe_grp_size_flu) s.add_group(Group('g0', 1000, { 'flu-status': 's' })) sys.stdout = open('out.dat', 'w') s.run(24) sys.stdout.close() with open('out.dat') as file: self.data = file.readlines() self.data = str(self.data) apple = self.data.replace('\\n', '<br>') # print(apple) return apple
def add_initial_rules(time_offset): "Imports rules built into pram and inserts them into the rules dictionary" global sites sites = { s: Site(s) for s in ['home', 'work-a', 'work-b', 'work-c', 'store-a', 'store-b'] } mall_sites_names = [ "big_theater", "down_store1", "down_store2", "down_store3", "down_store4", "down_store5", "down_store6", "down_store7", "down_store8", "down_store9", "big_down_store10", "big_down_store11", "big_down_store12", "up_store1", "up_store2", "up_store3", "up_store4", "up_store5", "up_store6", "up_store7", "up_store8", "up_store9", "big_down_courtyard_1", "big_down_courtyard_2", "big_down_courtyard_3", "big_down_courtyard_4" ] mall_sites = [Site(s) for s in mall_sites_names] for i in range(len(mall_sites)): sites[mall_sites_names[i]] = mall_sites[i] #rules["Mall Movement"] = [MallMovement(0.2, mall_sites)] rules["Mall Flu"] = [MallFlu(1, 0.2, mall_sites)] rules["Simple Flu Progress Rule"] = [ SimpleFluProgress( 'flu-status', { 's': [0.95, 0.05, 0.00], 'i': [0.00, 0.50, 0.50], 'r': [0.10, 0.00, 0.90] }, sites['home']) ] # rules["Home-Work-School Rules"] = [SimpleGoTo(TimeInt( (8 - time_offset)%24,(12 - time_offset)%24), 0.4, 'home', 'work', 'Some agents leave home to go to work'), # SimpleGoTo(TimeInt((16- time_offset)%24,(20- time_offset)%24), 0.4, 'work', 'home', 'Some agents return home from work'), # SimpleGoTo(TimeInt((16- time_offset)%24,(21- time_offset)%24), 0.2, 'home', 'store', 'Some agents go to a store after getting back home'), # SimpleGoTo(TimeInt((17- time_offset)%24,(23- time_offset)%24), 0.3, 'store', 'home', 'Some shopping agents return home from a store'), # SimpleGoTo(TimePoint((24- time_offset)%25), 1.0, 'store', 'home', 'All shopping agents return home after stores close'), # SimpleGoTo(TimePoint( (2- time_offset)%25), 1.0, None, 'home', 'All still-working agents return home')] global probe_grp_size_site probe_grp_size_site = GroupSizeProbe.by_rel( 'site', Site.AT, sites.values(), msg_mode=ProbeMsgMode.DISP, memo='Mass distribution across sites') return
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()
}) # s - susceptible # i - infectious # r - recovered sites = {'home': Site('h'), 'work': Site('w')} probe_grp_size_flu = GroupSizeProbe.by_attr( 'flu', 'flu-status', progress_flu_rule.get_states(), msg_mode=ProbeMsgMode.DISP, memo='Mass distribution across flu status') probe_grp_size_site = GroupSizeProbe.by_rel( 'site', Site.AT, sites.values(), msg_mode=ProbeMsgMode.DISP, memo='Mass distribution across sites') # ---------------------------------------------------------------------------------------------------------------------- # (1) Simulations testing the basic operations on groups and rules: # (1.1) A single-group, single-rule (1g.1r) simulation: s = Simulation() s.add_rule(progress_flu_rule) s.add_probe(probe_grp_size_flu) s.add_group(Group('g0', 1000, {'flu-status': 's'})) s.run(24) # (1.2) A single-group, two-rule (1g.2r) simulation: # s = Simulation()
import random from scipy.stats import poisson from pram.data import GroupSizeProbe, ProbeMsgMode from pram.entity import Group, GroupDBRelSpec, GroupQry, GroupSplitSpec, Site from pram.rule import SegregationModel from pram.sim import Simulation # ---------------------------------------------------------------------------------------------------------------------- # (1) Simulation (two locations) loc = [Site('a'), Site('b')] probe_loc = GroupSizeProbe.by_rel('loc', Site.AT, loc, msg_mode=ProbeMsgMode.DISP) probe_sim = GroupSizeProbe(name='sim', queries=[ GroupQry(attr={'team': 'blue'}, rel={Site.AT: loc[0]}), GroupQry(attr={'team': 'red'}, rel={Site.AT: loc[0]}), GroupQry(attr={'team': 'blue'}, rel={Site.AT: loc[1]}), GroupQry(attr={'team': 'red'}, rel={Site.AT: loc[1]}) ], qry_tot=None, msg_mode=ProbeMsgMode.DISP)
def main(): # SAMPLE SIMULATION FROM 09-segregation import os from pram.data import GroupSizeProbe, ProbeMsgMode, ProbePersistenceDB from pram.entity import Group, GroupQry, Site, GroupSplitSpec from pram.rule import SegregationModel from pram.sim import Simulation # ----------------------------------------------------------------------------------------------------------------- # (1) Simulation (two locations) loc = [Site('a'), Site('b')] def gs(pop, group): return [ GroupSplitSpec(p=0.7, attr_set={'foobar': 'foo'}), GroupSplitSpec(p=0.3, attr_set={'foobar': 'bar'}) ] probe_loc = GroupSizeProbe.by_rel('loc', Site.AT, loc, msg_mode=ProbeMsgMode.DISP) probe_sim = GroupSizeProbe(name='sim', queries=[ GroupQry(attr={'team': 'blue'}, rel={Site.AT: loc[0]}), GroupQry(attr={'team': 'red'}, rel={Site.AT: loc[0]}), GroupQry(attr={'team': 'blue'}, rel={Site.AT: loc[1]}), GroupQry(attr={'team': 'red'}, rel={Site.AT: loc[1]}) ], qry_tot=None, persistence=ProbePersistenceDB(), msg_mode=ProbeMsgMode.DISP) s = ( Simulation().set().pragma_autocompact(True).pragma_live_info(False). fn_group_setup(gs).done().add([ SegregationModel('team', len(loc)), # TranslateEverything(), # LambdasAndArguments(), Group(m=200, attr={'team': 'blue'}, rel={ Site.AT: loc[0], 'origin': loc[0] }), Group(m=300, attr={'team': 'blue'}, rel={ Site.AT: loc[1], 'origin': loc[1] }), Group(m=100, attr={'team': 'red'}, rel={ Site.AT: loc[0], 'origin': loc[0] }), Group(m=400, attr={'team': 'red'}, rel={ Site.AT: loc[1], 'origin': loc[1] }), probe_loc, # the distribution should tend to 50%-50% probe_sim # mass should tend to move towards two of the four sites # fixed naming issue in pram/sim.py line 815 ])) pram2mesa(s, 'TestSimultaneousGrammar')