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 main(): """Block main.""" iteration = 5000 # Create a test environment to visualize env = ViewerModel(100, width, height, 10, iter=iteration, viewer=viewer) # Build environment from json env.build_environment_from_json() # Load a json file containing the phenotype pfileloc = ('/home/aadeshnpn/Documents/BYU/hcmi/swarm/' + 'results/1550156973273193-5000ValidateSForge/') jname = pfileloc + '1550156973273193-4999.json' phenotypes = JsonPhenotypeData.load_json_file(jname)['phenotypes'] # Create the agents in the environment from the sampled behaviors # print (len(phenotypes)) env.create_agents(phenotypes=phenotypes) # Overiding the default viewer properties env.ui = UI((width, height), [env.hub], env.agents, [env.site], food=env.foods) # Iterate and execute each step in the environment for i in range(iteration): env.step() # print('Execution done') # Find if food has been deposited in the hub print('foraging percent', env.foraging_percent())
def main(): """Block main.""" iteration = 9000 # Create a test environment to visualize env = ViewerModel(50, width, height, 10, iter=iteration, viewer=viewer) # Build environment from json env.build_environment_from_json() # Load a json file containing the phenotype pfileloc = '/home/aadeshnpn/Documents/BYU/hcmi/hri/cooperative_transport/1538447335350COT/' jname = pfileloc + '1538447335350.json' phenotypes = JsonPhenotypeData.load_json_file(jname)['phenotypes'] # Create the agents in the environment from the sampled behaviors # print (len(phenotypes)) env.create_agents(phenotypes=phenotypes[30:90]) # Overiding the default viewer properties env.ui = UI((width, height), [env.hub], env.agents, [env.site], food=env.foods) # Iterate and execute each step in the environment for i in range(iteration): env.step() print('foraging percent', env.foraging_percent()) # print('Execution done') # Find if food has been deposited in the hub grid = env.grid neighbours = grid.get_neighborhood(env.hub.location, 10) food_objects = grid.get_objects_from_list_of_grid('Food', neighbours)
def __init__(self, N, width, height, grid=10, seed=None, viewer=False): """Initialize the environment methods.""" if seed is None: super(RandomWalkSwarmEnvironmentModel, self).__init__(seed=None) else: super(RandomWalkSwarmEnvironmentModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.viewer = viewer self.schedule = SimultaneousActivation(self) self.hub = Hub(id=2, location=(0, 0), radius=5) self.grid.add_object_to_grid(self.hub.location, self.hub) self.site = Sites(id=2, location=(25, 25), radius=5) self.grid.add_object_to_grid(self.site.location, self.site) self.agents = [] for i in range(self.num_agents): a = SwarmAgentRandomWalk(i, self) self.schedule.add(a) x = 0 y = 0 a.location = (x, y) a.direction = -2.3561944901923448 self.grid.add_object_to_grid((x, y), a) self.agents.append(a) if self.viewer: self.ui = UI((width, height), [self.hub], self.agents, [self.site])
def main(): """Block main.""" iteration = 5000 # Create a test environment to visualize env = ViewerModel(100, width, height, 20, iter=iteration, viewer=viewer) # Build environment from json env.build_environment_from_json() # Load a json file containing the phenotype pfileloc = '/home/aadeshnpn/Documents/BYU/hcmi/hri/thesis/sf/' jname = pfileloc + '1538473090382007.json' phenotypes = JsonPhenotypeData.load_json_file(jname)['phenotypes'] # Create the agents in the environment from the sampled behaviors env.create_agents(phenotypes=phenotypes) # Overiding the default viewer properties env.ui = UI((width, height), [env.hub], env.agents, env.sites, food=env.foods, debris=[]) # Iterate and execute each step in the environment for i in range(iteration): env.step() # print('Execution done') # Find if food has been deposited in the hub grid = env.grid neighbours = grid.get_neighborhood(env.hub.location, 10) food_objects = grid.get_objects_from_list_of_grid('Food', neighbours) for food in food_objects: print('food', food.id, food.location) """
def main(): """Block main.""" iteration = 5000 # Create a test environment to visualize env = ViewerModel(100, width, height, 10, iter=iteration, viewer=viewer) # Build environment from json env.build_environment_from_json() # Load a json file containing the phenotype pfileloc = '/home/aadeshnpn/Documents/BYU/hcmi/hri/nest_maint/1539014820252NestM/' jname = pfileloc + '1539014820252.json' phenotypes = JsonPhenotypeData.load_json_file(jname)['phenotypes'] # Create the agents in the environment from the sampled behaviors print(len(phenotypes)) env.create_agents(phenotypes=phenotypes) # Overiding the default viewer properties env.ui = UI((width, height), [env.hub], env.agents, [], food=[], debris=env.debris) # Iterate and execute each step in the environment for i in range(iteration): env.step() # print('Execution done') # Find if food has been deposited in the hub print('Cleaning Percentage', env.foraging_percent()) print(len(env.debris_cleaned()))
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 __init__(self, N, width, height, grid=10, seed=None, viewer=False): if seed is None: super(SingleCarryDropReturnSwarmEnvironmentModel, self).__init__(seed=None) else: super(SingleCarryDropReturnSwarmEnvironmentModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.viewer = viewer self.schedule = SimultaneousActivation(self) self.hub = Hub(id=2, location=(0, 0), radius=5) self.grid.add_object_to_grid(self.hub.location, self.hub) self.site = Sites(id=3, location=(-35, -5), radius=5) self.grid.add_object_to_grid(self.site.location, self.site) self.agents = [] self.foods = [] for i in range(self.num_agents): f = Food(i, location=(-35, -5), radius=5) self.grid.add_object_to_grid(f.location, f) self.foods.append(f) for i in range(self.num_agents): # a = SwarmAgentRandomSingleCarryDropReturn(i, self) a = SwarmAgentHandCodedForaging(i, self) self.schedule.add(a) x = -45 y = -45 a.location = (x, y) a.direction = -2.3561944901923448 self.grid.add_object_to_grid((x, y), a) self.agents.append(a) if self.viewer: self.ui = UI((width, height), [self.hub], self.agents, [self.site], food=self.foods)
def create_agents(self, random_init=False, phenotypes=None): """Initialize agents in the environment.""" # Variable to tell how many agents will have the same phenotype bound = np.ceil((self.num_agents * 1.0) / len(phenotypes)) j = 0 # Create agents for i in range(self.num_agents): # print (i, j, self.xmlstrings[j]) a = ExecutingAgent(i, self, xmlstring=phenotypes[j]) self.schedule.add(a) # Add the hub to agents memory a.shared_content['Hub'] = {self.hub} # Initialize the BT. Since the agents are normal agents just # use the phenotype a.construct_bt() if random_init: # Add the agent to a random grid cell x = self.random.randint(-self.grid.width / 2, self.grid.width / 2) y = self.random.randint(-self.grid.height / 2, self.grid.height / 2) else: try: x, y = self.hub.location except AttributeError: x, y = 0, 0 a.location = (x, y) self.grid.add_object_to_grid((x, y), a) a.operation_threshold = 2 # self.num_agents // 10 self.agents.append(a) if (i + 1) % bound == 0: j += 1 if self.viewer: self.ui = UI((100, 100), [self.hub], self.agents, self.site, food=self.foods)