env
index
/home/gcallah/TandonDevOps/IndraABM/lib/env.py

This file defines an Env, which is a collection
of agents that share a timeline and a Space.

 
Modules
       
lib.display_methods
json
traceback

 
Classes
       
builtins.object
PopHist
lib.space.Space(lib.group.Group)
Env

 
class Env(lib.space.Space)
    Env(name, action=None, random_placing=True, serial_obj=None, exclude_member=None, members=None, pop_hist_setup=None, **kwargs)
 
A collection of entities that share a space and time.
An env *is* a space and *has* a timeline (PopHist).
That makes the inheritance work out as we want it to.
 
 
Method resolution order:
Env
lib.space.Space
lib.group.Group
lib.agent.Agent
builtins.object

Methods defined here:
__init__(self, name, action=None, random_placing=True, serial_obj=None, exclude_member=None, members=None, pop_hist_setup=None, **kwargs)
Initialize self.  See help(type(self)) for accurate signature.
__repr__(self)
Return repr(self).
add_child(self, group)
Put a child agent in the womb.
group: which group will add new agent
bar_data(self)
This is the data for our scatter plot.
This code assumes the env holds groups, and the groups
hold agents with positions.
This assumption is dangerous, and we should address it.
bar_graph(self)
show the movements of population
construct_anew(self, exclude_member=None)
What we do to create a new env.
create_pop_hist(self)
This is an entry point to the class where one can customize what
occurs with tracking history.
from_json(self, serial_obj)
Recover a space from JSON.
get_census(self, num_moves, num_switches)
Gets the census data for all the agents stored
in the member dictionary.
 
Takes in how many agent has moved from one place to another
and how many agent has switched groups and returns
a string of these census data.
get_marker(self, variety)
Return this group's display marker.
get_periods(self)
get_plot_data(self)
This is the data for our scatter plot.
We have to draw it out of groups into a format for our graphs.
This code assumes the env holds groups, and the groups
hold agents with positions.
This assumption is dangerous, and we should address it.
get_pop_hist(self)
handle_pop_hist(self)
handle_womb(self)
The structure of the womb is:
    {"group_name": #agents_to_create}
has_disp(self)
headless(self)
line_data(self)
line_graph(self)
Show agent populations.
restore_env(self, serial_obj)
scatter_plot(self)
Show agent locations.
to_json(self)
We can order these as we wish to make debugging the API easier:

Methods inherited from lib.space.Space:
__iadd__(self, other)
Add other to set self.
If other is a group, add all its members.
If other is an atom, add it.
add_location(self, x, y, member)
Add a new member to the locations of positions of members.
locations{} stores agents by name, to look up in registry.
consec_place_members(self, members, curr_col=0, curr_row=0)
Locate all members of this space in x, y grid.
Place members consecutively, starting from (0, 0) and
moving to (1, 0), (2, 0), etc
constrain_x(self, x)
Pull x in bounds if it ain't.
constrain_y(self, y)
Pull y in bounds if it ain't.
demo_get_y_hood(self, agent, width=1, pred=None, include_self=False, save_neighbors=False)
Takes in an agent and returns a Group
of its x neighbors.
For example, if the agent is located at (0, 0),
get_y_hood would return neighbors between
(0, -1) and (0, 1).
exists_neighbor(self, agent, pred=None, exclude_self=True, size=1, region_type=None)
gen_new_pos(self, mbr, max_move)
Generate new random position within max_move of current pos.
get_agent_at(self, x, y)
Return agent at cell x,y
If cell is empty return None.
Always make location a str for serialization.
get_center(self)
get_closest_agent(self, agent, size=None)
get_closest_agent_and_dist(self, agent, size=None)
Get the agent' closest to agent on grid.
get_col_view(self, x, y, dist)
get_corners(self, start, size, orient=0)
Get the corners of an x or y run of cells.
I think we could eliminate the if with enough cleverness.
get_locations(self)
get_max_distance(self)
get_moore_hood(self, agent, pred=None, save_neighbors=False, include_self=False, size=1, model_name=None)
Takes in an agent and returns a Group of its Moore neighbors.
Should call the region_factory!
get_neighbor(self, agent, pred=None, exclude_self=True, size=1, region_type=None)
get_neighbor_of_groupX(self, agent, group, save_neighbors=False, hood_size=1)
If the agent has any neighbors in group X, return the first one
encountered.
We may get the groupX object itself, or we may get passed
its name.
get_num_of_neighbors(self, agent, exclude_self=True, pred=None, size=1, region_type=None)
get_row_hood(self, row_num, pred=None, save_neighbors=False)
Collects all agents in row `row_num` into a Group
and returns it.
get_row_view(self, x, y, dist)
get_square_hood(self, agent, pred=None, save_neighbors=False, include_self=False, hood_size=1)
Get a list of the nearby agents in a square neighborhood.
The size of the hood is given by hood_size.
We can filter with pred.
We may or may not save this hood.
get_vonneumann_hood(self, agent, pred=None, save_neighbors=False, size=1)
Takes in an agent and returns a Group of its
Von Neumann neighbors.
`hood_size` is unused at present, but we should use it!
get_x_hood(self, agent, width=1, pred=None, include_self=False, save_neighbors=False)
Takes in an agent and returns a Group
of its x neighbors.
For example, if the agent is located at (0, 0),
get_x_hood would return neighbors between
(-1, 0) and (1, 0).
get_y_hood(self, agent, height=1, pred=None, include_self=False, save_neighbors=False)
Takes in an agent and returns a Group
of its y neighbors.
For example, if the agent is located at (0, 0),
get_y_hood would return agents at (0, 2) and (0, 1).
grid_size(self)
How big is da grid?
is_empty(self, x, y)
See if cell x,y is empty.
Always make location a str for serialization.
is_full(self)
Is da grid full?
is_occupied(self, x, y)
See if (x, y) is occupied.
move_location(self, nx, ny, ox, oy, agent_name='NA')
Move a member to a new position, if that position
is not already occupied.
neighbor_ratio(self, agent, pred_one, pred_two=None, size=1, region_type=None)
place_member(self, mbr, max_move=None, xy=None, attempts=0)
By default, locate a member at a random x, y spot in our grid.
`max_move` constrains where that can be.
Setting `xy` picks a particular spot to place member.
`xy` must be a tuple!
point_from_vector(self, angle, max_move, xy, vector_start=(0, 0))
Given a vector with one end at the origin, find
the other end -- if off grid, pull it back onto the
grid.
rand_place_members(self, members, max_move=None)
Locate all members of this space in x, y grid.
This randomly places members.
rand_x(self, low=0, high=None)
Return a random x-value between 0 and this space's width,
if no constraints are passed.
With constraints, narrow to that range.
rand_y(self, low=0, high=None)
Return a random y-value between 0 and this space's height
if no constraints are passed.
With constraints, narrow to that range.
remove_location(self, coord)
Remove a member from locations.
restore(self, serial_obj)
Here we restore a group from a serialized object.

Methods inherited from lib.group.Group:
__add__(self, other)
This implements set union and returns
a new Group that is self union other.
If other is an atomic agent, just add it to
this group.
__call__(self, **kwargs)
Call the members' functions, and the group's
action func if it has one.
This should return the total of all
agents who acted in a particular call.
__contains__(self, item)
A test whether item is a member of this set.
__delitem__(self, key)
This will delete a member from this group.
__eq__(self, other)
Return self==value.
__getitem__(self, key)
We are going to return the 'key' member
of our member dictionary.
__iter__(self)
__len__(self)
__reversed__(self)
__setitem__(self, key, member)
In contrast to agent, which sets a val
for setitem, for groups, we are going to set
the 'key' member.
__sub__(self, other)
This implements set difference.
add_member(self, member)
Should be called by join()
del_member(self, member)
Should be called by split()
get_color(self)
Return this group's display color.
get_members(self)
has_color(self)
is_active(self)
For now, groups just stay active.
We might want to revisit later the question
of whether a group with all inactive members
should be inactivated.
is_mbr_comp(self, mbr)
ismember(self, agent)
pop_count(self, mbr)
rand_member(self)
rand_subset(self, n, name='rand_subset', exec_key=None)
Choose a random subset of N members from this group.
subset(self, predicate, *args, name=None, exec_key=None)

Data and other attributes inherited from lib.group.Group:
__hash__ = None

Methods inherited from lib.agent.Agent:
__str__(self)
Return str(self).
add_group(self, group)
check_null_pos(fn)
Should be used to decorate any function that uses pos[X] or pos[Y]
del_group(self, group)
die(self)
get(self, key, default=None)
This is a call to get_attr() to not break existing code
that calls get().
get_attr(self, key, default=None)
get_pos(self)
get_x(self)
get_y(self)
group_name(self)
is_located(self)
move(self, max_move=None, angle=None)
Move this agent to a random pos within max_move
of its current pos.
prim_group_nm(self)
prim_group is just a name, but we don't want models
going straight at it!
primary_group(self)
set_attr(self, key, val)
set_pos(self, x, y)
set_prim_group(self, group)
We want this to store the name of the group.
The str() of the group is its name.
The str() of the name is itself.
If we are passed None, set to blank.
switch_groups(self, g1, g2)

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

 
class PopHist(builtins.object)
    PopHist(serial_pops=None, colors={})
 
Data structure to record the fluctuating numbers of various agent
types.
 
  Methods defined here:
__getitem__(self, key)
__init__(self, serial_pops=None, colors={})
Initialize self.  See help(type(self)) for accurate signature.
__iter__(self)
__repr__(self)
Return repr(self).
__str__(self)
Return str(self).
add_color(self, name, color)
Adds coloring for a member of pop hist (for graphing).
add_period(self)
are_pops_colored(self)
Are any of the pops colored?
from_json(self, pop_data)
get_color(self, name)
Get the color for a pop. This assumes it is there!
has_color(self, name)
See if a pop has a specific color.
record_pop(self, mbr, count)
to_json(self)

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
Functions
       
get_color(pop, variety)

 
Data
        API = 'api'
DEF_USER = 'User'
POP_HIST_HDR = 'PopHist for '
POP_SEP = ', '
SEP_STR = '==================\n'
TEST = 'test'
UNLIMITED = 1000
X = 0
Y = 1
color_num = 0