firefly
index
/home/gcallah/TandonDevOps/IndraABM/models/firefly.py

This model illustrates the spontaneous synronization of large crowds without a
central central clock or synronization tool. In this model, the agents
(fireflies) start chaning color (blinkin) at random frequencies. However, at
each simulation run, the agents increase or decrease the frequencies based on
the average ofneighborung agents' blinkin frequencies. After a certain number
of simulation runs, we see that all of the agents are blinking at the pretty
much same frequency.
 
This behaviour is shown by calculating the standard deviation in the blinking
frequencies with the environment action.
 
A related video that explains this phenomena:
https://www.youtube.com/watch?v=t-_VPRCtiUg
 
Another good website to understand this model:
https://1000fireflies.net/about

 
Modules
       
lib.actions
lib.model
random
statistics

 
Classes
       
lib.model.Model(builtins.object)
Firefly

 
class Firefly(lib.model.Model)
    Firefly(model_nm='model', props=None, grp_struct={'def_grp': {'mbr_creator': <function create_agent at 0x7c8bb5aeb790>, 'grp_action': None, 'mbr_action': <function def_action at 0x7c8bb5aeb8b0>, 'num_mbrs': 1, 'num_mbrs_prop': None, 'color': 'blue'}, 'red_grp': {'mbr_creator': <function create_agent at 0x7c8bb5aeb790>, 'grp_action': None, 'mbr_action': <function def_action at 0x7c8bb5aeb8b0>, 'num_mbrs': 1, 'num_mbrs_prop': None, 'color': 'red'}}, env_action=None, random_placing=True, serial_obj=None, exec_key=None, create_for_test=False)
 

 
 
Method resolution order:
Firefly
lib.model.Model
builtins.object

Methods defined here:
handle_props(self, props)
A generic parameter handling method.
We get height and width here, since so many models use them.

Methods inherited from lib.model.Model:
__init__(self, model_nm='model', props=None, grp_struct={'def_grp': {'mbr_creator': <function create_agent at 0x7c8bb5aeb790>, 'grp_action': None, 'mbr_action': <function def_action at 0x7c8bb5aeb8b0>, 'num_mbrs': 1, 'num_mbrs_prop': None, 'color': 'blue'}, 'red_grp': {'mbr_creator': <function create_agent at 0x7c8bb5aeb790>, 'grp_action': None, 'mbr_action': <function def_action at 0x7c8bb5aeb8b0>, 'num_mbrs': 1, 'num_mbrs_prop': None, 'color': 'red'}}, env_action=None, random_placing=True, serial_obj=None, exec_key=None, create_for_test=False)
Initialize self.  See help(type(self)) for accurate signature.
__repr__(self)
This returns a JSON representation of the model.
__str__(self)
Return str(self).
add_child(self, group)
Put a child agent in the womb.
group: which group will add new agent
The womb should move up into model eventually.
add_switch(self, agent_nm, from_grp_nm, to_grp_nm)
Switch agent from 1 group to another.
The agent and groups should be passed by name.
bar_graph(self)
collect_stats(self)
create_anew(self, model_nm, props, grp_struct, exec_key, env_action, random_placing, create_for_test=False)
Create the model for the first time.
create_env(self, env_action=None, random_placing=True)
Override this method to create a unique env...
but this one will already set the model name and add
the groups.
create_from_serial_obj(self, serial_obj)
Restore the model from its serialized version.
create_groups(self)
Override this method in your model to create all of your groups.
In general, you shouldn't need to: fill in the grp_struct instead.
create_pop_hist(self)
There are several methods that still (like in V2) reside in
Env, but which we mean to move to Model. So we provide an interface to
them here so when we move them other code won't break.
`create_pop_hist()` is such a method.
create_user(self)
This will create a user of the correct type.
from_json(self, jrep)
This method restores a model from its JSON rep.
get_locations(self)
get_periods(self)
get_pop_hist(self)
get_prop(self, prop_nm, default=None)
Have a way to get a prop through the model to hide props structure.
get_user_msgs(self)
handle_args(self)
handle_switches(self)
This will actually process the pending switches.
handle_womb(self)
This method adds new agents from the womb.
The womb should move up into model eventually.
is_api_user(self)
is_test_user(self)
line_graph(self)
pending_switches(self)
How many switches are there to execute?
rpt_census(self, acts, moves)
This is the default census report.
Right now, `acts` is not used: do we need it?
Return: a string saying what happened in a period.
rpt_stats(self)
This is a "wrap up" report on the results of a model run.
Each model can do what it wants here.
perhaps will take an iterator object?
a file?
Function takes in a CSV formatted string from function
collect_stats() and writes it to a csv file.
 
Note: added logic so func will not write to stdout
rpt_switches(self)
Generate a string to report our switches.
run(self, periods=None)
This method runs the model. If `periods` is not None,
it will run it for that many periods. Otherwise, on
a terminal, it will display the menu.
Return: 0 if run was fine.
runN(self, periods=10)
Run our model for N periods.
Return the total number of actions taken.
run_batch(self, runs, steps)
Run our model for N periods X steps.
Writes the period specific model statistics to a CSV file.
Files are saved as input filename-[integer-counter].csv
Returns the total number of actions taken.
scatter_plot(self)
to_json(self)
This method generates the JSON representation for this model.
update_pop_hist(self)
This method records our populations each period.

Data descriptors inherited from lib.model.Model:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
Functions
       
adjust_blink_freq(firefly)
Inreases or decreases the firefly's blinking frequency based on the average
of its neighbors.
blink_now(firefly)
Predicate asking if the time is now.
calc_blink_dev(meadow, **kwargs)
create_firefly(name, i, props=None, action=None, exec_key=0)
Create a trendsetter: all RED to start.
create_model(serial_obj=None, props=None, create_for_test=False, exec_key=None)
This is for the sake of the API server:
firefly_action(firefly, **kwargs)
A firefly decides whether to blink or not.
get_blink_freq()
main()
reset_time_to_blink(firefly)
Update this fly's last blink time.
switch_state(firefly, curr_state, new_state)
Actually swap states.
time_to_next_blink(firefly)
How long before this bug blinks?
to_blink_or_not(firefly)
The passed firefly may blink by changing its group.
If the firefly is
already ON, this function turns if OFF. Otherwise, checks if the
the time passed since last blink time of the firefly is
greater than the firefly's blinking frequency.
Return the new firefly state.

 
Data
        BLINK_FREQ = 'blink_freq'
DEBUG = <lib.utils.Debug object>
DEF_DENSITY = 0.5
DEF_HOOD_SIZE = 2
DEF_MAX_BLINK_FREQ = 10
DEF_MIN_BLINK_FREQ = 1
DEF_NUM_FIREFLY = 50
MODEL_NAME = 'firefly'
OFF = 0
OFF_GRP = 'Firefly Off'
ON = 1
ON_GRP = 'Firefly On'
OTHER_WEIGHT = 0.4
SELF_WEIGHT = 0.6
STATE = 'state'
STATE_MAP = {0: 'Firefly Off', 1: 'Firefly On'}
TIME_TO_BLINK = 'last_blinked_at'
firefly_grps = {'Firefly Off': {'color': 'black', 'mbr_action': <function firefly_action>, 'mbr_creator': <function create_firefly>, 'num_mbrs': 50}, 'Firefly On': {'color': 'yellow', 'mbr_creator': <function create_firefly>, 'num_mbrs': 1}}