def cycle_system(self, ca: cab_ca.CabCA): """ Cycles through all agents and has them perceive and act in the world """ # Have all agents perceive and act in a random order # While we're at it, look for dead agents to remove # changed_agents = [] self.new_agents = list() self.dead_agents = list() for a in self.agent_set: a.perceive_and_act(self, ca) if a.x != a.prev_x or a.y != a.prev_y: self.update_agent_position(a) # self.agent_set = set([agent for agent in self.agent_set if not agent.dead]) # cab_log.trace("[ABM] agent set = {0}".format(self.agent_set)) self.dead_agents = [agent for agent in self.agent_set if agent.dead] # cab_log.trace("[ABM] dead agents = {0}".format(self.dead_agents)) for agent in self.dead_agents: cab_log.trace("[ABM] removing agent {0} from position {1},{2}".format(agent, agent.x, agent.y)) self.remove_agent(agent) self.agent_set = set([agent for agent in self.agent_set if not agent.dead]) # cab_log.trace("[ABM] agent set = {0}".format(self.agent_set)) self.schedule_new_agents()
def cycle_system(self, ca: cab_ca.CabCA): """ Cycles through all agents and has them perceive and act in the world """ # Have all agents perceive and act in a random order # While we're at it, look for dead agents to remove # changed_agents = [] self.new_agents = list() self.dead_agents = list() for a in self.agent_set: a.perceive_and_act(self, ca) if a.x != a.prev_x or a.y != a.prev_y: self.update_agent_position(a) # self.agent_set = set([agent for agent in self.agent_set if not agent.dead]) # cab_log.trace("[ABM] agent set = {0}".format(self.agent_set)) self.dead_agents = [agent for agent in self.agent_set if agent.dead] # cab_log.trace("[ABM] dead agents = {0}".format(self.dead_agents)) for agent in self.dead_agents: cab_log.trace("[ABM] removing agent {0} from position {1},{2}".format( agent, agent.x, agent.y)) self.remove_agent(agent) self.agent_set = set( [agent for agent in self.agent_set if not agent.dead]) # cab_log.trace("[ABM] agent set = {0}".format(self.agent_set)) self.schedule_new_agents()
def schedule_new_agents(self): """ Adds an agent to be scheduled by the abm. """ for agent in self.new_agents: # pos = (agent.x, agent.y) self.agent_set.add(agent) cab_log.trace("[ABM] agent {0} scheduled by the ABM".format(agent))
def add_agent(self, agent: cab_agent.CabAgent): """ Add an agent to the system. """ self.new_agents.append(agent) pos = (agent.x, agent.y) if agent.x is not None and agent.y is not None: if self.gc.ONE_AGENT_PER_CELL: if pos not in self.agent_locations: self.agent_locations[pos] = agent # Can't insert agent if cell is already occupied. else: if pos in self.agent_locations: self.agent_locations[pos].add(agent) else: self.agent_locations[pos] = {agent} cab_log.trace("[ABM] agent added to position {0}, {1}".format(agent.x, agent.y))
def update_agent_position(self, agent: cab_agent.CabAgent): """ Update all agent positions in the location map. """ if self.gc.ONE_AGENT_PER_CELL: self.agent_locations.pop((agent.prev_x, agent.prev_y)) self.agent_locations[agent.x, agent.y] = agent cab_log.trace("[ABM] moving agent from = {0}, {1}".format(agent.prev_x, agent.prev_y)) else: self.agent_locations[agent.prev_x, agent.prev_y].remove(agent) if not self.agent_locations[agent.prev_x, agent.prev_y]: cab_log.trace("[ABM] position {0}, {1} empty, removing from location map".format(agent.prev_x, agent.prev_y)) self.agent_locations.pop((agent.prev_x, agent.prev_y)) try: self.agent_locations[agent.x, agent.y].add(agent) except KeyError: self.agent_locations[agent.x, agent.y] = {agent} # set([agent])
def __init__(self, gc: cab_gc.GlobalConstants, proto_agent: cab_agent.CabAgent=None): """ Initializes an abm with the given number of agents and returns it. :param gc: Global Constants, Parameters for the ABM. :return: An initialized ABM. """ self.agent_set = set() self.agent_locations = dict() self.gc = gc self.new_agents = list() self.dead_agents = list() if proto_agent is not None: cab_log.trace('[ABM] have proto agent {0}'.format(proto_agent)) self.add_agent(proto_agent) self.schedule_new_agents() else: cab_log.trace('[ABM] have NO proto agent')
def add_agent(self, agent: cab_agent.CabAgent): """ Add an agent to the system. """ self.new_agents.append(agent) pos = (agent.x, agent.y) if agent.x is not None and agent.y is not None: if self.gc.ONE_AGENT_PER_CELL: if pos not in self.agent_locations: self.agent_locations[pos] = agent # Can't insert agent if cell is already occupied. else: if pos in self.agent_locations: self.agent_locations[pos].add(agent) else: self.agent_locations[pos] = {agent} cab_log.trace( "[ABM] agent added to position {0}, {1}".format(agent.x, agent.y))
def schedule_new_agents(self): """ Adds an agent to be scheduled by the abm. """ for agent in self.new_agents: # pos = (agent.x, agent.y) self.agent_set.add(agent) # if agent.x is not None and agent.y is not None: # if self.gc.ONE_AGENT_PER_CELL: # if pos not in self.agent_locations: # self.agent_locations[pos] = agent # # Can't insert agent if cell is already occupied. # else: # if pos in self.agent_locations: # self.agent_locations[pos].add(agent) # else: # self.agent_locations[pos] = {agent} # self.new_agents = list() cab_log.trace("[ABM] agent {0} scheduled by the ABM".format(agent))
def update_agent_position(self, agent: cab_agent.CabAgent): """ Update all agent positions in the location map. """ if self.gc.ONE_AGENT_PER_CELL: self.agent_locations.pop((agent.prev_x, agent.prev_y)) self.agent_locations[agent.x, agent.y] = agent cab_log.trace("[ABM] moving agent from = {0}, {1}".format( agent.prev_x, agent.prev_y)) else: self.agent_locations[agent.prev_x, agent.prev_y].remove(agent) if not self.agent_locations[agent.prev_x, agent.prev_y]: cab_log.trace("[ABM] position {0}, {1} empty, removing from location map".format( agent.prev_x, agent.prev_y)) self.agent_locations.pop((agent.prev_x, agent.prev_y)) try: self.agent_locations[agent.x, agent.y].add(agent) except KeyError: self.agent_locations[agent.x, agent.y] = { agent} # set([agent])
def __init__(self, gc: cab_gc.GlobalConstants, cab_core): """ Initializes the visualization. """ super().__init__(gc, cab_core) self.abm = cab_core.abm self.ca = cab_core.ca self.surface = None self.io_handler = cab_pygame_io.InputHandler(cab_core) # Initialize UI components. pygame.init() # pygame.display.init() if self.gc.USE_HEX_CA: offset_x = int((math.sqrt(3) / 2) * (self.gc.CELL_SIZE * 2) * (self.gc.DIM_X - 1)) offset_y = int((3 / 4) * (self.gc.CELL_SIZE * 2) * (self.gc.DIM_Y - 1)) # print(offset) # self.screen = pygame.display.set_mode((self.gc.GRID_WIDTH, self.gc.GRID_HEIGHT), pygame.RESIZABLE, 32) else: offset_x = self.gc.CELL_SIZE * self.gc.DIM_X offset_y = self.gc.CELL_SIZE * self.gc.DIM_Y self.surface = pygame.display.set_mode((offset_x, offset_y), pygame.locals.HWSURFACE | pygame.locals.DOUBLEBUF, 32) pygame.display.set_caption('Complex Automaton Base') cab_log.trace("[PygameIO] initializing done")
def render_simulation(self): cab_log.trace("[PygameIO] start rendering simulation") while True: self.render_frame()
def __init__(self, global_constants: cab_gc.GlobalConstants, **kwargs): """ Standard initializer. TODO: Add option to start in headless mode. :param global_constants: All constants or important variables that control the simulation. :param kwargs**: cell prototype, agent prototype, visualizer prototype and IO handler prototype. """ self.gc: cab_gc.GlobalConstants = global_constants cab_rng.seed_RNG(self.gc.RNG_SEED) # Check whether we have any custom agents in the simulation. if 'proto_agent' in kwargs: cab_log.trace('[ComplexAutomaton] have proto agent {0}'.format(kwargs['proto_agent'])) self.abm = cab_abm.ABM(self.gc, proto_agent=kwargs['proto_agent']) self.proto_agent = kwargs['proto_agent'] else: self.abm = cab_abm.ABM(self.gc) self.proto_agent = None # Check whether we have any custom cells in the simulation. if 'proto_cell' in kwargs: # If so, initialize either a hexagonal or rectangular grid with the given cells. cab_log.trace('[ComplexAutomaton] have proto cell {0}'.format(kwargs['proto_cell'])) if self.gc.USE_HEX_CA: cab_log.trace('[ComplexAutomaton] initializing hexagonal CA') self.ca = ca_hex.CAHex(self, proto_cell=kwargs['proto_cell']) else: cab_log.trace('[ComplexAutomaton] initializing rectangular CA') self.ca = ca_rect.CARect(self, proto_cell=kwargs['proto_cell']) self.proto_cell = kwargs['proto_cell'] else: # Otherwise initialize the respective grid with default cells. if self.gc.USE_HEX_CA: cab_log.trace('[ComplexAutomaton] initializing hexagonal CA') self.ca = ca_hex.CAHex(self) else: cab_log.trace('[ComplexAutomaton] initializing rectangular CA') self.ca = ca_rect.CARect(self) self.proto_cell = None # Check for the UI that we want to use. if self.gc.GUI == None: import cab.util.io_headless as cab_io_hl cab_log.trace('[ComplexAutomaton] initializing headless CLI') # TODO: Make num steps (=100) variable. self.visualizer = cab_io_hl.IoHeadless(self.gc, self, 100) elif self.gc.GUI == "TK": import cab.util.io_tk as cab_io_tk cab_log.trace('[ComplexAutomaton] initializing Tk IO') self.visualizer = cab_io_tk.TkIO(self.gc, self) elif self.gc.GUI == "PyGame": import cab.util.io_pygame as cab_io_pg cab_log.trace('[ComplexAutomaton] initializing Pygame IO') self.visualizer = cab_io_pg.PygameIO(self.gc, self) self.display_info()
def __init__(self, global_constants: cab_gc.GlobalConstants, **kwargs): """ Standard initializer. TODO: Add option to start in headless mode. :param global_constants: All constants or important variables that control the simulation. :param kwargs**: cell prototype, agent prototype, visualizer prototype and IO handler prototype. """ self.gc: cab_gc.GlobalConstants = global_constants cab_rng.seed_RNG(self.gc.RNG_SEED) # Check whether we have any custom agents in the simulation. if 'proto_agent' in kwargs: cab_log.trace('[ComplexAutomaton] have proto agent {0}'.format( kwargs['proto_agent'])) self.abm = cab_abm.ABM(self.gc, proto_agent=kwargs['proto_agent']) self.proto_agent = kwargs['proto_agent'] else: self.abm = cab_abm.ABM(self.gc) self.proto_agent = None # Check whether we have any custom cells in the simulation. if 'proto_cell' in kwargs: # If so, initialize either a hexagonal or rectangular grid with the given cells. cab_log.trace('[ComplexAutomaton] have proto cell {0}'.format( kwargs['proto_cell'])) if self.gc.USE_HEX_CA: cab_log.trace('[ComplexAutomaton] initializing hexagonal CA') self.ca = ca_hex.CAHex(self, proto_cell=kwargs['proto_cell']) else: cab_log.trace('[ComplexAutomaton] initializing rectangular CA') self.ca = ca_rect.CARect(self, proto_cell=kwargs['proto_cell']) self.proto_cell = kwargs['proto_cell'] else: # Otherwise initialize the respective grid with default cells. if self.gc.USE_HEX_CA: cab_log.trace('[ComplexAutomaton] initializing hexagonal CA') self.ca = ca_hex.CAHex(self) else: cab_log.trace('[ComplexAutomaton] initializing rectangular CA') self.ca = ca_rect.CARect(self) self.proto_cell = None # Check for the UI that we want to use. if self.gc.GUI == None: import cab.util.io_headless as cab_io_hl cab_log.trace('[ComplexAutomaton] initializing headless CLI') # TODO: Make num steps (=100) variable. self.visualizer = cab_io_hl.IoHeadless(self.gc, self, 100) elif self.gc.GUI == "TK": import cab.util.io_tk as cab_io_tk cab_log.trace('[ComplexAutomaton] initializing Tk IO') self.visualizer = cab_io_tk.TkIO(self.gc, self) elif self.gc.GUI == "PyGame": import cab.util.io_pygame as cab_io_pg cab_log.trace('[ComplexAutomaton] initializing Pygame IO') self.visualizer = cab_io_pg.PygameIO(self.gc, self) self.display_info()