def build_environment_from_json(self): """Build env from jsondata.""" jsondata = JsonData.load_json_file(filename) # Create a instance of JsonData to store object that # needs to be sent to UI self.render = JsonData() self.render.objects = {} for name in jsondata.keys(): obj = eval(name.capitalize()) self.render.objects[name] = self.create_environment_object( jsondata, obj) self.hub = self.render.objects['hub'][0] try: self.foods = [] for site in self.render.objects['sites']: self.site = site # self.render.objects['sites'][0] for i in range(self.num_agents): f = Food( i, location=self.site.location, radius=self.site.radius) f.agent_name = None self.grid.add_object_to_grid(f.location, f) self.foods.append(f) except KeyError: pass if self.viewer: self.ui = UI( (self.width, self.height), [self.hub], self.agents, [self.site], food=self.foods)
def build_environment_from_json(self): """Build env from jsondata.""" jsondata = JsonData.load_json_file(filename) # Create a instance of JsonData to store object that # needs to be sent to UI self.render = JsonData() self.render.objects = {} # First create the agents in the environment for name in jsondata.keys(): obj = eval(name.capitalize()) self.render.objects[name] = self.create_environment_object( jsondata, obj) self.hub = self.render.objects['hub'][0] self.total_food_units = 0 self.foods = [] try: self.site = self.render.objects['sites'][0] for i in range(self.num_agents * 1): f = Food(i, location=self.site.location, radius=self.site.radius) f.agent_name = None self.grid.add_object_to_grid(f.location, f) self.total_food_units += f.weight f.phenotype = dict() self.foods.append(f) except KeyError: pass
def build_environment_from_json(self): """Build env from jsondata.""" jsondata = JsonData.load_json_file(filename) # Create a instance of JsonData to store object that # needs to be sent to UI self.render = JsonData() self.render.objects = {} for name in jsondata.keys(): obj = eval(name.capitalize()) self.render.objects[name] = self.create_environment_object( jsondata, obj) self.hub = self.render.objects['hub'][0] try: self.debris = [] for i in range(self.num_agents): dx, dy = self.random.randint(1, 10, 2) dx = self.hub.location[0] + dx dy = self.hub.location[1] + dy d = Debris(i, location=(dx, dy), radius=10) d.agent_name = None self.grid.add_object_to_grid(d.location, d) self.debris.append(d) except KeyError: pass # Create a place for the agents to drop the derbis try: self.obstacles = [] for i in range(1): dx, dy = self.random.randint(5, 10, 2) dx = self.hub.location[0] + 25 + dx dy = self.hub.location[1] + 25 + dy o = Obstacles(id=i, location=(dx, dy), radius=10) self.grid.add_object_to_grid(o.location, o) self.obstacles.append(o) except AttributeError: pass # This doesn't change so no need to compute everytime grid = self.grid hub_loc = self.hub.location neighbours_in = grid.get_neighborhood(hub_loc, 25) neighbours_out = grid.get_neighborhood(hub_loc, 50) self.neighbours = list(set(neighbours_out) - set(neighbours_in))
def build_environment_from_json(self): """Build env from jsondata.""" jsondata = JsonData.load_json_file(filename) # Create a instance of JsonData to store object that # needs to be sent to UI self.render = JsonData() self.render.objects = {} for name in jsondata.keys(): obj = eval(name.capitalize()) self.render.objects[name] = self.create_environment_object( jsondata, obj) self.hub = self.render.objects['hub'][0] try: self.debris = [] for i in range(self.num_agents): dx, dy = self.random.randint(1, 10, 2) dx = self.hub.location[0] + dx dy = self.hub.location[1] + dy d = Debris(i, location=(dx, dy), radius=5) d.agent_name = None self.grid.add_object_to_grid(d.location, d) self.debris.append(d) except KeyError: pass # Create a place for the agents to drop the derbis try: self.obstacles = [] for i in range(1): dx, dy = self.random.randint(5, 10, 2) dx = self.hub.location[0] + 25 + dx dy = self.hub.location[1] + 25 + dy o = Obstacles(id=i, location=(dx, dy), radius=10) self.grid.add_object_to_grid(o.location, o) self.obstacles.append(o) except AttributeError: pass if self.viewer: self.ui = UI((self.width, self.height), [self.hub], self.agents, [], food=[], derbis=self.debris)
def build_environment_from_json(self): """Build env from jsondata.""" jsondata = JsonData.load_json_file(filename) # Create a instance of JsonData to store object that # needs to be sent to UI self.render = JsonData() self.render.objects = {} # First create the agents in the environment for name in jsondata.keys(): obj = eval(name.capitalize()) self.render.objects[name] = self.create_environment_object( jsondata, obj) self.hub = self.render.objects['hub'][0] self.total_debris_units = 0 self.debris = [] try: for i in range(self.num_agents * 1): # dx, dy = self.random.randint(10, 20, 2) dx, dy = self.random.normal(0, 10, 2) dx = self.hub.location[0] + dx dy = self.hub.location[1] + dy d = Debris( i, location=(int(dx), int(dy)), radius=5) d.agent_name = None self.grid.add_object_to_grid(d.location, d) self.total_debris_units += d.weight d.phenotype = dict() self.debris.append(d) except KeyError: pass # Create a place for the agents to drop the derbis try: self.obstacles = [] for i in range(1): dx, dy = self.random.randint(5, 10, 2) dx = self.hub.location[0] + 60 + dx dy = self.hub.location[1] + 60 + dy o = Obstacles(id=i, location=(dx, dy), radius=10) self.grid.add_object_to_grid(o.location, o) self.obstacles.append(o) except AttributeError: pass
def step(self): """UI function. Sending information to UI in a JSON format. """ print(JsonData.to_json( self.width, self.height, self.hub, self.sites, self.obstacles, self.traps, self.cues, self.food, self.debris, self.agents) ) time.sleep(0.00833333)