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

This file defines Space, which is a collection
of agents related spatially.

 
Modules
       
lib.agent
lib.group
json
math
random
registry.registry
lib.utils

 
Classes
       
builtins.Exception(builtins.BaseException)
SpaceFull
builtins.object
Region
CircularRegion
CompositeRegion
lib.group.Group(lib.agent.Agent)
Space

 
class CircularRegion(Region)
    CircularRegion(space=None, center=None, radius=None, agents_move=True, **kwargs)
 

 
 
Method resolution order:
CircularRegion
Region
builtins.object

Methods defined here:
__init__(self, space=None, center=None, radius=None, agents_move=True, **kwargs)
Construct a sub-region of a space.
We expect the center to be a tuple.
NW, NE, SW, and SE are the corners of the region.
size is len(side) / 2
check_out_bounds(self, coord)
contains(self, coord)
create_sub_reg(self, space=None, center=None, radius=None, agents_move=True, **kwargs)
get_agents(self, exclude_self=True, pred=None)
Get agents in region filtered by `pred()`:
    if agents don't move, use saved set.

Methods inherited from Region:
__repr__(self)
Return repr(self).
__str__(self)
Return str(self).
agents_move_switch(self)
check_bounds(self)
Puzzling behavior here:
    If the constrain calls are wronmg, just fix them.
    If they are not, why the adjustments?
exists_neighbor(self, exclude_self=True, pred=None)
get_group(self)
get_neighbor(self, no_self=True, pred=None)
get_num_of_agents(self, exclude_self=False, pred=None)
get_ratio(self, pred_one, pred_two=None)

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

 
class CompositeRegion(Region)
    CompositeRegion(region_set=None)
 

 
 
Method resolution order:
CompositeRegion
Region
builtins.object

Methods defined here:
__init__(self, region_set=None)
Construct a sub-region of a space.
We expect the center to be a tuple.
NW, NE, SW, and SE are the corners of the region.
size is len(side) / 2
add_region(self, region)
contains(self, coord)
exists_neighbor(self, exclude_self=False, pred=None)
get_agents(self, exclude_self=False, pred=None)
Get agents in region filtered by `pred()`:
    if agents don't move, use saved set.
remove_region(self, region)

Methods inherited from Region:
__repr__(self)
Return repr(self).
__str__(self)
Return str(self).
agents_move_switch(self)
check_bounds(self)
Puzzling behavior here:
    If the constrain calls are wronmg, just fix them.
    If they are not, why the adjustments?
create_sub_reg(self, space=None, NW=None, NE=None, SW=None, SE=None, center=None, size=None, agents_move=True)
get_group(self)
get_neighbor(self, no_self=True, pred=None)
get_num_of_agents(self, exclude_self=False, pred=None)
get_ratio(self, pred_one, pred_two=None)

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

 
class Region(builtins.object)
    Region(space=None, NW=None, NE=None, SW=None, SE=None, center=None, size=None, agents_move=True, **kwargs)
 
This is the base of all regions used for neighborhoods, obstacles,
and other sub-divisions of space.
 
Region(space, NW=(2, 3), NE=(4, 5), SW=(7, 8), SE=(8, 9))
or
Region(space, xy=(2, 3), size=7)
 
  Methods defined here:
__init__(self, space=None, NW=None, NE=None, SW=None, SE=None, center=None, size=None, agents_move=True, **kwargs)
Construct a sub-region of a space.
We expect the center to be a tuple.
NW, NE, SW, and SE are the corners of the region.
size is len(side) / 2
__repr__(self)
Return repr(self).
__str__(self)
Return str(self).
agents_move_switch(self)
check_bounds(self)
Puzzling behavior here:
    If the constrain calls are wronmg, just fix them.
    If they are not, why the adjustments?
contains(self, coord)
create_sub_reg(self, space=None, NW=None, NE=None, SW=None, SE=None, center=None, size=None, agents_move=True)
exists_neighbor(self, exclude_self=True, pred=None)
get_agents(self, exclude_self=False, pred=None)
Get agents in region filtered by `pred()`:
    if agents don't move, use saved set.
get_group(self)
get_neighbor(self, no_self=True, pred=None)
get_num_of_agents(self, exclude_self=False, pred=None)
get_ratio(self, pred_one, pred_two=None)

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

 
class Space(lib.group.Group)
    Space(name, width=10, height=10, attrs=None, members=None, action=None, random_placing=True, serial_obj=None, reg=True, **kwargs)
 
A collection of entities that share a space.
The way we handle space assignment is, default to random,
and assign locations after we get our members.
 
 
Method resolution order:
Space
lib.group.Group
lib.agent.Agent
builtins.object

Methods defined here:
__iadd__(self, other)
Add other to set self.
If other is a group, add all its members.
If other is an atom, add it.
__init__(self, name, width=10, height=10, attrs=None, members=None, action=None, random_placing=True, serial_obj=None, reg=True, **kwargs)
Initialize self.  See help(type(self)) for accurate signature.
__repr__(self)
Return repr(self).
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)
from_json(self, rep)
Recover a space from JSON.
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.
to_json(self)
We can order these as we wish to make debugging the API easier:

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_marker(self)
Return this group's display marker.
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 SpaceFull(builtins.Exception)
    SpaceFull(message)
 
Exception to raise when a space fills up.
 
 
Method resolution order:
SpaceFull
builtins.Exception
builtins.BaseException
builtins.object

Methods defined here:
__init__(self, message)
Initialize self.  See help(type(self)) for accurate signature.

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Static methods inherited from builtins.Exception:
__new__(*args, **kwargs) from builtins.type
Create and return a new object.  See help(type) for accurate signature.

Methods inherited from builtins.BaseException:
__delattr__(self, name, /)
Implement delattr(self, name).
__getattribute__(self, name, /)
Return getattr(self, name).
__reduce__(...)
Helper for pickle.
__repr__(self, /)
Return repr(self).
__setattr__(self, name, value, /)
Implement setattr(self, name, value).
__setstate__(...)
__str__(self, /)
Return str(self).
with_traceback(...)
Exception.with_traceback(tb) --
set self.__traceback__ to tb and return self.

Data descriptors inherited from builtins.BaseException:
__cause__
exception cause
__context__
exception context
__dict__
__suppress_context__
__traceback__
args

 
Functions
       
bound(point, lower, upper)
circ_region_factory(space=None, center=None, radius=None, agents_move=False, **kwargs)
debug_agent_pos(agent)
degrees_between(x_dif, y_dif)
distance(a1, a2)
We're going to return the distance between two objects. That calculation
is easy if they are both located in space, but what if one of them is
not? For now, we will return FAR_AWAY, but is that right?
exists_neighbor(agent, pred=None, exclude_self=True, size=1, region_type=None, **kwargs)
fill_neighbor_coords(agent, height, include_self)
gen_region_name(NW=None, NE=None, SW=None, SE=None, center=None, size=None)
gen_region_name_for_model_at_exec_key(model_name, exec_key, NW=None, NE=None, SW=None, SE=None, center=None, size=None)
gen_temp_grp_nm(s)
This makes "sure" that grp names are unique.
Of course, roughly 1 / MAX_TEMP_NUM times we will
create a duplicate name... but that shouldn't happen
before the heat death of the universe.
get_agents_env(agent)
get_move_angle(agent, agents_in_range)
get_neighbor(agent, pred=None, exclude_self=True, size=1, region_type=None, **kwargs)
get_neighbors(agent, pred=None, exclude_self=True, size=1, region_type='Moore', model_name=None)
Get the Moore neighbors for an agent.
We might expand this in the future to allow von Neumann hoods!
Also, we have a messed up situation with some funcs taking
`include_self` and some taking `exclude_self`: for sweet love of Jesus, let
us use one or the other!
get_num_of_neighbors(agent, exclude_self=False, pred=None, size=1, region_type=None, **kwargs)
get_xy_from_str(coord_str)
in_hood(agent, other, hood_sz)
Check whether agent and other are within a certain distance
of each other.
neighbor_ratio(agent, pred_one, pred_two=None, size=1, region_type=None, **kwargs)
opposing_angle(pos1, pos2)
Returns the inverse of the angle of
the other_agent relative to the agent.
out_of_bounds(x, y, x1, y1, x2, y2)
Is point x, y off the grid defined by x1, y1, x2, y2?
region_factory(space=None, NW=None, NE=None, SW=None, SE=None, center=None, size=1, agents_move=True, **kwargs)
return_region(space=None, NW=None, NE=None, SW=None, SE=None, center=None, size=None, agents_move=True)

 
Data
        ALL_FULL = "Can't fit more agents in this space!"
DEBUG = <lib.utils.Debug object>
DEF_HEIGHT = 10
DEF_MAX_MOVE = 2
DEF_WIDTH = 10
FAR_AWAY = 100000000
MAX_HEIGHT = 200
MAX_PLACE_ATTEMPTS = 100
MAX_TEMP_NUM = 18446744073709551616
MAX_WIDTH = 200
MOORE = 'Moore'
VON_N = 'VonNeumann'
X = 0
Y = 1
region_dict = {}