def place_member(self, mbr, max_move=None): """ By default, locate a member at a random x, y spot in our grid. `max_move` constrains where that can be. """ if self.is_full(): self.user.log("Can't fit no more folks in this space!") return None if not is_composite(mbr): (x, y) = self.gen_new_pos(mbr, max_move) if (x, y) not in self.locations: if mbr.islocated(): self.move_location(x, y, mbr.get_x(), mbr.get_y()) else: self.add_location(x, y, mbr) # if I am setting pos, I am agent's locator! mbr.set_pos(self, x, y) return (x, y) elif max_move is None: # if the random position is already taken, # find the member a new position # but if max_move is not None, the hood might be filled! return self.place_member(mbr, max_move) else: return self.place_members(mbr.members, max_move)
def __call__(self): """ Call the members' functions. Later, this will just call agents' funcs. This should return the total of all agents who acted in a particular call. """ total_acts = 0 del_list = [] self.duration -= 1 if self.duration > 0: if self.action is not None: # the action was defined outside this class, so pass self: self.action(self) for (key, member) in self.members.items(): if member.isactive(): total_acts += member() else: # delete agents but not composites: if not is_composite(member): if DEBUG: print("Marking " + key + " for deletion.") del_list.append(key) for key in del_list: del self.members[key] return total_acts
def __call__(self, **kwargs): """ Call the members' functions, and the composite's action func if it has one. This should return the total of all agents who acted in a particular call. """ total_acts = 0 total_moves = 0 del_list = [] self.duration -= 1 if self.duration > 0: if self.action is not None: # the action was defined outside this class, so pass self: self.action(self, **kwargs) for (key, member) in self.members.items(): if member.is_active(): (acted, moved) = member(**kwargs) total_acts += acted total_moves += moved else: # delete agents but not composites: if not is_composite(member): if DEBUG: print("Marking " + key + " for deletion.") del_list.append(key) for key in del_list: del self.members[key] return total_acts, total_moves
def place_member(self, mbr, max_move=None, xy=None): """ 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! """ if self.is_full(): self.user.log("Can't fit no more folks in this space!") return None if not is_composite(mbr): if xy is not None: (x, y) = xy # it had better be a tuple! else: (x, y) = self.gen_new_pos(mbr, max_move) if self.is_empty(x, y): if mbr.islocated(): self.move_location(x, y, mbr.get_x(), mbr.get_y()) else: self.add_location(x, y, mbr) # if I am setting pos, I am agent's locator! mbr.set_pos(self, x, y) return (x, y) elif (max_move is None) and (xy is None): # if the random position is already taken, # find the member a new position # but if max_move is not None, the hood might be filled! # so we need something to detect a full neighborhood as well. # and if xy is not None, the user asked for a particular # spot: don't give them another, but return None. return self.place_member(mbr, max_move) else: return self.place_members(mbr.members, max_move)
def place_member(self, mbr, max_move=None, xy=None): """ 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! """ if self.is_full(): self.user.log("Can't fit no more folks in this space!") return None if not is_composite(mbr): if xy is not None: (x, y) = xy # it had better be a tuple! else: (x, y) = self.gen_new_pos(mbr, max_move) if self.is_empty(x, y): if mbr.islocated(): self.move_location(x, y, mbr.get_x(), mbr.get_y()) else: self.add_location(x, y, mbr) # if I am setting pos, I am agent's locator! mbr.set_pos(self, x, y) return (x, y) elif (max_move is None) and (xy is None): # if the random position is already taken, # find the member a new position # but if max_move is not None, the hood might be filled! # so we need something to detect a full neighborhood as well. # and if xy is not None, the user asked for a particular # spot: don't give them another, but return None. return self.place_member(mbr, max_move) else: return self.place_members(mbr.members, max_move)
def place_members(self, members, max_move=None): """ Locate all members of this space in x, y grid. Default is to randomly place members. """ if members is not None: for nm, mbr in members.items(): if not is_composite(mbr): # by default don't locate groups self.place_member(mbr, max_move) else: # place composite's members self.place_members(mbr.members, max_move)
def __isub__(self, other): """ Remove item(s) in other if there, otherwise do nothing. """ if is_composite(other): for member in other.members: self.members.pop(member, None) else: if other.name in self.members: del self[other.name] return self
def place_members(self, members, max_move=None): """ Locate all members of this space in x, y grid. Default is to randomly place members. """ if members is not None: for nm, mbr in members.items(): if not is_composite(mbr): # by default don't locate groups self.place_member(mbr, max_move) else: # place composite's members self.place_members(mbr.members, max_move)
def __iadd__(self, other): """ Add other to set self. If other is a composite, add all its members. If other is an atom, add it. """ if is_composite(other): for key in other: join(self, other[key]) else: join(self, other) return self
def __sub__(self, other): """ This implements set difference and returns a new Composite that is self - other. """ new_dict = copy(self.members) if is_composite(other): for mem in other.members: if mem in self.members: del new_dict[mem] else: if other.name in self: del new_dict[other.name] return grp_from_nm_dict(self.name + "-" + other.name, new_dict)
def __add__(self, other): """ This implements set union and returns a new Composite that is self union other. If other is an atomic agent, just add it to this group. """ new_dict = copy(self.members) if is_composite(other): new_dict.update(other.members) else: new_dict[other.name] = other new_grp = grp_from_nm_dict(self.name + "+" + other.name, new_dict) self.add_group(new_grp) other.add_group(new_grp) return new_grp
def 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 """ if members is not None: for nm, mbr in members.items(): if not is_composite(mbr): if curr_col < self.width: self.place_member(mbr, xy=(curr_col, curr_row)) if DEBUG: print("Placing member at (" + str(curr_col) + "," + str(curr_row) + ")") curr_col += 1 if curr_col == self.width: if DEBUG: print("Moving up a row from", curr_row, "to", curr_row + 1) curr_col = 0 curr_row += 1 else: # place composite's members self.consec_place_members(mbr.members, curr_col, curr_row)
def is_mbr_comp(self, mbr): return is_composite(self.members[mbr])