def test_sensing_limited_agents_3(self): self.ag3.state.p_pos = np.array([ 0.31806357, -0.27622187]) self.ag4.state.p_pos = np.array([-0.48336693, 0.33957211]) self.ag3.silent = False self.ag3.blind = True self.ag3.deaf = False self.ag4.silent = True self.ag4.blind = False self.ag4.deaf = True self.assertFalse(self.ag3.is_entity_observable(self.ag4)) self.assertTrue(self.ag4.is_entity_observable(self.ag3)) self.assertFalse(self.ag3.is_entity_transmittable(self.ag4)) self.assertFalse(self.ag4.is_entity_transmittable(self.ag3)) # self.assertFalse(self.ag3.is_entity_communicable(self.ag4)) # self.assertFalse(self.ag4.is_entity_communicable(self.ag3)) self.assertFalse(check_2way_communicability(self.ag3, self.ag4)) self.assertFalse(check_2way_communicability(self.ag4, self.ag3)) self.ag3.silent = False self.ag3.blind = False self.ag3.deaf = True self.ag4.silent = False self.ag4.blind = False self.ag4.deaf = False self.assertTrue(self.ag3.is_entity_observable(self.ag4)) self.assertTrue(self.ag4.is_entity_observable(self.ag3)) self.assertTrue(self.ag3.is_entity_transmittable(self.ag4)) self.assertFalse(self.ag4.is_entity_transmittable(self.ag3)) # self.assertFalse(self.ag3.is_entity_communicable(self.ag4)) # self.assertFalse(self.ag4.is_entity_communicable(self.ag3)) self.assertFalse(check_2way_communicability(self.ag3, self.ag4)) self.assertFalse(check_2way_communicability(self.ag4, self.ag3))
def test_sensing_limited_agents_2(self): self.ag1.state.p_pos = np.array([-1.0, 0.0]) self.ag2.state.p_pos = np.array([1.0, 0.0]) self.assertTrue(self.ag1.is_entity_observable(self.ag2)) self.assertTrue(self.ag2.is_entity_observable(self.ag1)) self.assertFalse(self.ag1.is_entity_transmittable(self.ag2)) self.assertFalse(self.ag2.is_entity_transmittable(self.ag1)) # self.assertFalse(self.ag1.is_entity_communicable(self.ag2)) # self.assertFalse(self.ag2.is_entity_communicable(self.ag1)) self.assertFalse(check_2way_communicability(self.ag1, self.ag2)) self.assertFalse(check_2way_communicability(self.ag2, self.ag1))
def _create_network(self, world): ''' Establish connectivity network at every time step ''' # define nodes in simple connectivity network # by construction, node 0 is origin landmark, node 1 is destination landmark # terminated agents are not part of network nodes = [ world.origin_terminal_landmark, world.destination_terminal_landmark ] nodes.extend([a for a in world.agents if not a.terminated]) n_nodes = len(nodes) comm_net = SimpleNetwork(nodes) # init list to hold direct communication distance values between agents # there is no direct communication between origin and destination n_pairs = int(n_nodes * (n_nodes + 1) / 2) # calculate direct communication resistance between agents for k in range(n_pairs): i, j = linear_index_to_lower_triangular(k) if i == 1 and j == 0: continue # enforce that origin & destination don't directly connect if check_2way_communicability(nodes[i], nodes[j]): comm_net.add_edge(i, j) # systemic reward is inverse of resistance (conductance) return comm_net
def communications_observed(other_comm_node): ''' Communication between agents is just the conductance Notes: - inverse of comm resistance (i.e. conductance) used so that we can use zero for out of range comms - noisy measurement of heading - TODO: observation of failures ''' # check if node is terminated is_terminated = 0 if isinstance(other_comm_node, MortalAgent) and other_comm_node.terminated: is_terminated = 1 dx = dy = dvx = dvy = 0. if not is_terminated: dx, dy = delta_pos(other_comm_node, agent) dvx, dvy = delta_vel(other_comm_node, agent) comms = [is_terminated, dx, dy, dvx, dvy] # set comms to zero if out for range # if distance(agent, other_comm_node) > agent.max_observation_distance: if not check_2way_communicability(agent, other_comm_node): comms = [0] * len(comms) return comms
def test_sensing_limited_agents_1(self): self.ag1.state.p_pos = np.array([0.0, 0.0]) self.ag2.state.p_pos = np.array([0.0, 0.0]) self.assertTrue(self.ag1.is_entity_observable(self.ag2)) self.assertTrue(self.ag2.is_entity_observable(self.ag1)) self.assertTrue(self.ag1.is_entity_transmittable(self.ag2)) self.assertTrue(self.ag2.is_entity_transmittable(self.ag1)) # self.assertTrue(self.ag1.is_entity_communicable(self.ag2)) # self.assertTrue(self.ag2.is_entity_communicable(self.ag1)) self.assertTrue(check_2way_communicability(self.ag1, self.ag2)) self.assertTrue(check_2way_communicability(self.ag2, self.ag1)) self.ag3.state.p_pos = np.array([0.17336086, 0.86521205]) self.ag4.state.p_pos = np.array([0.85691831, 0.09510027]) self.assertTrue(self.ag3.is_entity_observable(self.ag4)) self.assertTrue(self.ag4.is_entity_observable(self.ag3)) self.assertTrue(self.ag3.is_entity_transmittable(self.ag4)) self.assertTrue(self.ag4.is_entity_transmittable(self.ag3)) # self.assertTrue(self.ag3.is_entity_communicable(self.ag4)) # self.assertTrue(self.ag4.is_entity_communicable(self.ag3)) self.assertTrue(check_2way_communicability(self.ag3, self.ag4)) self.assertTrue(check_2way_communicability(self.ag4, self.ag2))
def test_sensing_limited_agents_landmarks_1(self): self.ag1.state.p_pos = np.array([-0.40324806, -0.77577631]) self.ag2.state.p_pos = np.array([-3.65523633, 1.6878564 ]) self.ag3.state.p_pos = np.array([-5.92980527, 1.8940491 ]) self.ag4.state.p_pos = np.array([ 2.72110377, -4.32510047]) self.ag5.state.p_pos = np.array([8.59344534, 8.94993718]) self.ag6.state.p_pos = np.array([ 7.2732259 , -9.33902694]) self.lnd1.state.p_pos = np.array([3.42048654, 5.90631669]) lnd1_dists = ( 7.698786472677766, 8.237794689803087, 10.174784918902095, 10.255292943690849, 6.001927060007084, 15.724633635569218) self.assertTrue(self.ag1.is_entity_observable(self.lnd1)) self.assertTrue(self.ag2.is_entity_observable(self.lnd1)) self.assertFalse(self.ag3.is_entity_observable(self.lnd1)) self.assertFalse(self.ag4.is_entity_observable(self.lnd1)) self.assertTrue(self.ag5.is_entity_observable(self.lnd1)) self.assertTrue(self.ag6.is_entity_observable(self.lnd1)) self.assertTrue(self.ag1.is_entity_transmittable(self.lnd1)) self.assertFalse(self.ag2.is_entity_transmittable(self.lnd1)) self.assertFalse(self.ag3.is_entity_transmittable(self.lnd1)) self.assertFalse(self.ag4.is_entity_transmittable(self.lnd1)) self.assertTrue(self.ag5.is_entity_transmittable(self.lnd1)) self.assertFalse(self.ag6.is_entity_transmittable(self.lnd1)) # self.assertTrue(self.ag1.is_entity_communicable(self.lnd1)) # self.assertFalse(self.ag2.is_entity_communicable(self.lnd1)) # self.assertFalse(self.ag3.is_entity_communicable(self.lnd1)) # self.assertFalse(self.ag4.is_entity_communicable(self.lnd1)) # self.assertTrue(self.ag5.is_entity_communicable(self.lnd1)) # self.assertFalse(self.ag6.is_entity_communicable(self.lnd1)) self.assertTrue(check_2way_communicability(self.ag1, self.lnd1)) self.assertFalse(check_2way_communicability(self.ag2, self.lnd1)) self.assertFalse(check_2way_communicability(self.ag3, self.lnd1)) self.assertFalse(check_2way_communicability(self.ag4, self.lnd1)) self.assertTrue(check_2way_communicability(self.ag5, self.lnd1)) self.assertFalse(check_2way_communicability(self.ag6, self.lnd1))
def test_sensing_limited_agents_0(self): self.ag1.state.p_pos = np.array([-3.76250553, -2.23691489]) self.assertTrue(self.ag1.is_entity_observable(self.ag1)) self.assertTrue(self.ag1.is_entity_transmittable(self.ag1)) # self.assertTrue(self.ag1.is_entity_communicable(self.ag1)) self.assertTrue(check_2way_communicability(self.ag1, self.ag1))
def render(self, mode='human'): ''' render environment Notes: - Needed to completely overwrite MultiAgentEnv.step function due to multiple discrepancies ''' # print messages if mode == 'human' and self.world.dim_c > 0: alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' message = '' for agent in self.world.agents: for other in self.world.agents: if other is agent: continue if np.all(other.state.c == 0): word = '_' else: word = alphabet[np.argmax(other.state.c)] message += (other.name + ' to ' + agent.name + ': ' + word + ' ') print(message) for i in range(len(self.viewers)): # create viewers (if necessary) if self.viewers[i] is None: # import rendering only if we need it (and don't import for headless machines) #from gym.envs.classic_control import rendering from multiagent import rendering self.viewers[i] = rendering.Viewer(700, 700) # create rendering geometry self.render_geoms = None if self.render_geoms is None: # import rendering only if we need it (and don't import for headless machines) #from gym.envs.classic_control import rendering from multiagent import rendering self.render_geoms = [] self.render_geoms_xform = [] for entity in self.world.entities: if hasattr(entity, 'terminated') and entity.terminated: # render as x xvertices = entity.size * np.array( [[-1, 1], [1, -1], [0, 0], [1, 1], [-1, -1]]) geom = rendering.make_polyline(xvertices) else: # render as o geom = rendering.make_circle(entity.size) # render connections between entities connection_geoms = [] if hasattr(self.world, 'render_connections' ) and self.world.render_connections: for ag in self.world.agents: if entity == ag: continue if hasattr(entity, 'ignore_connection_rendering' ) and entity.ignore_connection_rendering: continue if check_2way_communicability(entity, ag): connection_geoms.append( rendering.make_polyline([ entity.state.p_pos.tolist(), ag.state.p_pos.tolist() ])) xform = rendering.Transform() if 'agent' in entity.name: geom.set_color(*entity.color, alpha=0.5) if hasattr(entity, 'terminated') and entity.terminated: geom.set_color(1, 0, 0, alpha=0.5) else: geom.set_color(*entity.color) geom.add_attr(xform) self.render_geoms.append(geom) self.render_geoms.extend(connection_geoms) self.render_geoms_xform.append(xform) # add geoms to viewer for viewer in self.viewers: viewer.geoms = [] for geom in self.render_geoms: viewer.add_geom(geom) results = [] for i in range(len(self.viewers)): from multiagent import rendering # update bounds to center around agent cam_range = 1 if self.shared_viewer: pos = np.zeros(self.world.dim_p) else: pos = self.agents[i].state.p_pos self.viewers[i].set_bounds(pos[0] - cam_range, pos[0] + cam_range, pos[1] - cam_range, pos[1] + cam_range) # update geometry positions for e, entity in enumerate(self.world.entities): self.render_geoms_xform[e].set_translation(*entity.state.p_pos) # render to display or array results.append( self.viewers[i].render(return_rgb_array=mode == 'rgb_array')) return results