From affeeb964372538319b939a62ee00d8e45239295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Mon, 4 Apr 2022 16:47:58 +0200 Subject: [PATCH] Update examples --- docs/requirements.txt | 2 +- examples/newsspread/newsspread.py | 13 ++++++++++--- examples/programmatic/programmatic.py | 12 +++++++----- examples/rabbits/rabbit_agents.py | 2 -- examples/rabbits/rabbits.yml | 4 ++-- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/docs/requirements.txt b/docs/requirements.txt index 74fddd8..28a6674 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1 +1 @@ -ipython==7.23 +ipython==7.31.1 diff --git a/examples/newsspread/newsspread.py b/examples/newsspread/newsspread.py index f6188eb..dc77f09 100644 --- a/examples/newsspread/newsspread.py +++ b/examples/newsspread/newsspread.py @@ -17,7 +17,7 @@ class DumbViewer(FSM): def neutral(self): if self['has_tv']: if prob(self.env['prob_tv_spread']): - self.set_state(self.infected) + return self.infected @state def infected(self): @@ -26,6 +26,12 @@ class DumbViewer(FSM): neighbor.infect() def infect(self): + ''' + This is not a state. It is a function that other agents can use to try to + infect this agent. DumbViewer always gets infected, but other agents like + HerdViewer might not become infected right away + ''' + self.set_state(self.infected) @@ -35,12 +41,13 @@ class HerdViewer(DumbViewer): ''' def infect(self): + '''Notice again that this is NOT a state. See DumbViewer.infect for reference''' infected = self.count_neighboring_agents(state_id=self.infected.id) total = self.count_neighboring_agents() prob_infect = self.env['prob_neighbor_spread'] * infected/total self.debug('prob_infect', prob_infect) if prob(prob_infect): - self.set_state(self.infected.id) + self.set_state(self.infected) class WiseViewer(HerdViewer): @@ -75,5 +82,5 @@ class WiseViewer(HerdViewer): 1.0) prob_cure = self.env['prob_neighbor_cure'] * (cured/infected) if prob(prob_cure): - return self.cure() + return self.cured return self.set_state(super().infected) diff --git a/examples/programmatic/programmatic.py b/examples/programmatic/programmatic.py index dd4a3be..7c8d54a 100644 --- a/examples/programmatic/programmatic.py +++ b/examples/programmatic/programmatic.py @@ -18,7 +18,9 @@ class MyAgent(agents.FSM): @agents.default_state @agents.state def neutral(self): - self.info('I am running') + self.debug('I am running') + if agents.prob(0.2): + self.info('This runs 2/10 times on average') s = Simulation(name='Programmatic', @@ -29,10 +31,10 @@ s = Simulation(name='Programmatic', dry_run=True) +# By default, logging will only print WARNING logs (and above). +# You need to choose a lower logging level to get INFO/DEBUG traces logging.basicConfig(level=logging.INFO) envs = s.run() -s.dump_yaml() - -for env in envs: - env.dump_csv() +# Uncomment this to output the simulation to a YAML file +# s.dump_yaml('simulation.yaml') diff --git a/examples/rabbits/rabbit_agents.py b/examples/rabbits/rabbit_agents.py index 1c978b1..056be93 100644 --- a/examples/rabbits/rabbit_agents.py +++ b/examples/rabbits/rabbit_agents.py @@ -12,8 +12,6 @@ class Genders(Enum): class RabbitModel(FSM): - level = logging.INFO - defaults = { 'age': 0, 'gender': Genders.male.value, diff --git a/examples/rabbits/rabbits.yml b/examples/rabbits/rabbits.yml index 1d9421f..16fd125 100644 --- a/examples/rabbits/rabbits.yml +++ b/examples/rabbits/rabbits.yml @@ -1,7 +1,7 @@ --- load_module: rabbit_agents name: rabbits_example -max_time: 150 +max_time: 100 interval: 1 seed: MySeed agent_type: RabbitModel @@ -10,7 +10,7 @@ environment_agents: environment_params: prob_death: 0.001 default_state: - mating_prob: 0.01 + mating_prob: 0.1 topology: nodes: - id: 1