Example #1
0
    def setup_geometry(self, env):
        """ create geoms and transforms for basic agents and landmarks
        """ 
        # lazy import 
        from multiagent import rendering

        if getattr(env, "render_dict", None) is not None:
            return 
        env.render_dict = {}

        # make geometries and transforms
        for entity in env.world.entities:
            name = entity.name
            geom = rendering.make_circle(entity.size)
            xform = rendering.Transform()

            # agent on top, other entity to background 
            alpha = 0.6 if "agent" in name else 0.5
            geom.set_color(*entity.color, alpha=alpha)   

            geom.add_attr(xform)
            env.render_dict[name] = {
                "geom": geom, 
                "xform": xform, 
                "attach_ent": entity
            }

            # VIS: show visual range/receptor field
            if 'agent' in entity.name and env.show_visual_range:
                vis_geom = rendering.make_circle(entity.vision_range)
                vis_geom.set_color(*entity.color, alpha=0.2)
                vis_xform = rendering.Transform()
                vis_geom.add_attr(vis_xform)
                env.render_dict[name+"_vis"] = {
                    "geom": vis_geom, 
                    "xform": vis_xform, 
                    "attach_ent": entity
                }

            # LABEL: display type & numbering 
            prefix = "A" if "agent" in entity.name else "L"
            idx = int(name.split(" ")[-1])
            x = entity.state.p_pos[0] 
            y = entity.state.p_pos[1] 
            label_geom = rendering.Text("{}{}".format(prefix,idx), position=(x,y), font_size=30)
            label_xform = rendering.Transform()
            label_geom.add_attr(label_xform)
            env.render_dict[name+"_label"] = {
                "geom": label_geom, 
                "xform": label_xform, 
                "attach_ent": entity
            }
                    
        
        # add geoms to viewer
        for viewer in env.viewers:
            viewer.geoms = []
            for k, d in env.render_dict.items():
                viewer.add_geom(d["geom"])
Example #2
0
    def getNewLine(self, x1, y1, x2, y2):
        from multiagent import rendering
        x11 = 100 * x1
        y11 = 100 * y1
        x22 = 100 * x2
        y22 = 100 * y2
        m = 20  # 表示每边截掉的长度
        if x11 == x22:
            if y11 > y22:
                long_line = rendering.Line(
                    (x11 - 5, y11 - m), (x22 - 5, y22 + m))  # move left,  r=6
                circle = rendering.make_circle(radius=5, res=30)
                circle_transform = rendering.Transform(
                    translation=(x11 - 5 + 50, y11 - m + 50))
                #circle_transform = rendering.Transform(translation=(x11-5,y11-m)
            elif y11 < y22:
                long_line = rendering.Line((x11 + 5, y11 + m),
                                           (x22 + 5, y22 - m))  # move right
                circle = rendering.make_circle(radius=5, res=30)
                circle_transform = rendering.Transform(
                    translation=(x11 + 5 + 50, y11 + m + 50))
                #short_line = rendering.Line((x11 + 5 - n_vih1, y11 + m), (x11 + 5 + n_vih1, y11 + m))
            else:
                print('1,It is a point rather than a line')
        elif y11 == y22:
            if x11 > x22:
                long_line = rendering.Line((x11 - m, y11 + 5),
                                           (x22 + m, y22 + 5))  # move up
                circle = rendering.make_circle(radius=5, res=30)
                circle_transform = rendering.Transform(
                    translation=(x11 - m + 50, y11 + 5 + 50))
                #short_line = rendering.Line((x11 - m, y11 + 5 - n_vih1), (x11 - m, y11 + 5 + n_vih1))
            elif x11 < x22:
                long_line = rendering.Line((x11 + m, y11 - 5),
                                           (x22 - m, y22 - 5))  # move down
                circle = rendering.make_circle(radius=5, res=30)
                circle_transform = rendering.Transform(
                    translation=(x11 + m + 50, y11 - 5 + 50))
                #short_line = rendering.Line((x11 + m, y11 - 5 - n_vih1), (x11 + m, y11 - 5 + n_vih1))
            else:
                print('2,It is a point rather than a line')
        else:
            print('3,It is not a line')

        self._transform = rendering.Transform(translation=(50, 50))
        long_line.add_attr(self._transform)
        long_line.set_linewidth(4)
        self.viewer.add_geom(long_line)

        # short_line.set_color(0, 0, 0)
        circle.add_attr(circle_transform)
        self.viewer.add_geom(circle)

        newline = []
        newline.append(circle)
        newline.append(long_line)

        return newline
Example #3
0
    def _add_geoms_to_viewer(self):
        black_list = []  # not render agent in its own viewer
        # create rendering geometry
        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.entities:
                geom = rendering.make_circle(
                    entity.size
                ) if 'surface' not in entity.name else rendering.make_polygon_with_hole(
                    entity.poly)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                    black_list.append(geom)
                elif 'surface' in entity.name:
                    geom.set_color(entity.color)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform)

            # add geoms to viewer
            for i, viewer in enumerate(self.viewers):
                viewer.geoms = []
                for geom in self.render_geoms:
                    if geom == black_list[i]: continue
                    viewer.add_geom(geom)
Example #4
0
    def render(self, mode='human'):
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for agent in self.world.agents:
                for other in self.world.agents:
                    if other is agent: continue
                    word = '_'
                    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(800, 800)  # 修改显示框大小

        # create rendering geometry
        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:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                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

            # 设置显示框
            self.viewers[i].set_bounds(-30, +30, -30, +30)
            # 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
Example #5
0
    def render(self):

        # oberservation radius
        geom = rendering.make_circle(self.observation_radius)
        geom.set_color(*self.color, alpha=0.05)
        xform = rendering.Transform()
        xform.set_translation(*self.state.p_pos)
        geom.add_attr(xform)

        return [geom]
Example #6
0
    def render(self, mode='human'):
        from multiagent import rendering
        if self.viewer is None:
            self.viewer = rendering.Viewer(700, 700)

        # create rendering geometry
        if self.render_geoms is None:
            self.render_geoms = []
            self.render_geoms_xform = []
            for entity in self.world.entities:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform)

            # add geoms to viewer
            self.viewer.geoms = []
            for geom in self.render_geoms:
                self.viewer.add_geom(geom)

        results = []
        # update bounds to center around agent
        cam_range = 1
        pos = np.zeros(self.world.dim_p)
        self.viewer.set_bounds(pos[0] - cam_range, pos[0] + cam_range,
                               pos[1] - cam_range, pos[1] + cam_range)
        # update geometry positions

        currentState = []

        for e, entity in enumerate(self.world.entities):  # 4
            self.render_geoms_xform[e].set_translation(*entity.state.p_pos)
            currentState.append(
                list(entity.state.p_pos) + list(entity.state.p_vel))
        # print(currentState)
        # render to display or array

        results.append(
            self.viewer.render(return_rgb_array=mode == 'rgb_array'))

        return currentState


# trajectory[0] = [allAgentsStates, allAgentsActions, allAgentsRewards, allAgentsNextState],
# agentState = [agentPos1, agentPos2, agentVel1
    def observation(self, agent, world):
        # get positions of all entities in this agent's reference frame
        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(200, 200)

        # create rendering geometry
        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 world.agents + world.landmarks:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                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
            pos = np.zeros(world.dim_p)
            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(world.agents + world.landmarks):
                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=True))

        return np.concatenate(results)
Example #8
0
    def render(self, mode='human'):
        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
        if self.render_geoms is None:
            # import rendering only if we need it (and don't import for headless machines)
            from multiagent import rendering
            self.render_geoms = []
            self.render_geoms_xform = []
            for entity in self.world.entities:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                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
Example #9
0
    def render(self, mode='human'):

        if self.viewer 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.viewer = rendering.Viewer(700, 700)

        # create rendering geometry
        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:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform)

            # add geoms to viewer
            self.viewer.geoms = []
            for geom in self.render_geoms:
                self.viewer.add_geom(geom)

        from multiagent import rendering
        # update bounds to center around agent
        if self.world.boundary:
            self.viewer.set_bounds(self.world.boundary[0][0],
                                   self.world.boundary[1][0],
                                   self.world.boundary[0][1],
                                   self.world.boundary[1][1])
        else:
            cam_range = 1.5  #1
            self.viewer.set_bounds(-cam_range, cam_range, -cam_range,
                                   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
        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
Example #10
0
    def setup_geometry(self):
        # lazy import
        from multiagent import rendering
        if self.render_dict is not None:
            return
        self.render_dict = {}

        # make geometries and transforms
        for entity in self.world.entities:
            name = entity.name
            geom = rendering.make_circle(entity.size)
            xform = rendering.Transform()

            # agent on top, other entity to background
            alpha = 1.0 if "agent" in name else 0.5
            geom.set_color(*entity.color, alpha=alpha)

            geom.add_attr(xform)
            self.render_dict[name] = {
                "geom": geom,
                "xform": xform,
                "attach_ent": entity
            }

            # LABEL: display type & numbering
            prefix = "A" if "agent" in entity.name else "L"
            idx = int(name.split(" ")[-1])
            x = entity.state.p_pos[0]
            y = entity.state.p_pos[1]
            label_geom = rendering.Text("{}{}".format(prefix, idx),
                                        position=(x, y),
                                        font_size=30)
            label_xform = rendering.Transform()
            label_geom.add_attr(label_xform)
            self.render_dict[name + "_label"] = {
                "geom": label_geom,
                "xform": label_xform,
                "attach_ent": entity
            }

        # add geoms to viewer
        for viewer in self.viewers:
            viewer.geoms = []
            for k, d in self.render_dict.items():
                viewer.add_geom(d["geom"])
Example #11
0
    def render(self):

        # debug
        geom = rendering.make_circle(self.size * 2.0)
        geom.set_color(*self.color, alpha=0.2)
        xform = rendering.Transform()
        xform.set_translation(*self.state.p_pos)
        geom.add_attr(xform)

        # visual quadrants
        axis_length = 2
        x_axis = rendering.Line(start=(self.state.p_pos[0] - axis_length,
                                       self.state.p_pos[1]),
                                end=(self.state.p_pos[0] + axis_length,
                                     self.state.p_pos[1]))
        x_axis.set_color(*self.color, alpha=0.3)

        y_axis = rendering.Line(start=(self.state.p_pos[0],
                                       self.state.p_pos[1] - axis_length),
                                end=(self.state.p_pos[0],
                                     self.state.p_pos[1] + axis_length))
        y_axis.set_color(*self.color, alpha=0.3)

        return [geom, x_axis, y_axis]
Example #12
0
    def render(self, mode='human'):
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for agent in self.world.agents:
                comm = []
                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 + '   ')
            # if(message is not ''):
            #    print(message)

        # create rendering geometry
        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:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                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)

        # Add debug geoms
        # TODO This needs to change to the parent -> child render() tree
        debug_geoms = []
        for e, entity in enumerate(self.world.entities):
            debug_geoms.extend(entity.render())

        for i, geom in enumerate(debug_geoms):
            self.viewers[0].add_onetime(geom)

        # TODO Add a debug flag
        self.viewers[0].add_onetime(self.world_bound_geom)

        results = []
        for i in range(len(self.viewers)):
            from multiagent import rendering
            # update bounds to center around agent
            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], pos[0], pos[1], pos[1])
            # 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
Example #13
0
    def render(self, mode='human'):
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for agent in self.world.agents:
                comm = []
                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
        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 = []
            self.render_range_geoms = []
            self.render_range_geoms_xform = []
            for entity in self.world.entities:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.3)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform)

                #if 'agent' in entity.name and entity.alive:
                if 'agent' in entity.name:
                    range_geom = rendering.make_circle(
                        self.world.observable_range)
                    range_xform = rendering.Transform()
                    if entity.alive:
                        range_geom.set_color(1, 1, 1, alpha=0.6)
                    else:
                        range_geom.set_color(0.1, 0.1, 0.1, alpha=0)
                    range_geom.add_attr(range_xform)
                    self.render_range_geoms.append(range_geom)
                    self.render_range_geoms_xform.append(range_xform)

            for wall in self.world.walls:
                corners = ((wall.axis_pos - 0.5 * wall.width,
                            wall.endpoints[0]),
                           (wall.axis_pos - 0.5 * wall.width,
                            wall.endpoints[1]),
                           (wall.axis_pos + 0.5 * wall.width,
                            wall.endpoints[1]),
                           (wall.axis_pos + 0.5 * wall.width,
                            wall.endpoints[0]))
                if wall.orient == 'H':
                    corners = tuple(c[::-1] for c in corners)
                geom = rendering.make_polygon(corners)
                if wall.hard:
                    geom.set_color(*wall.color)
                else:
                    geom.set_color(*wall.color, alpha=0.5)
                self.render_geoms.append(geom)

            # add geoms to viewer
            for viewer in self.viewers:
                viewer.geoms = []
                for geom in self.render_range_geoms:
                    viewer.add_geom(geom)
                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 color
            from multiagent import rendering
            for entity, g in zip(self.world.entities, self.render_geoms):
                if 'agent' in entity.name:
                    if entity.hold > 0:
                        g.set_color(*entity.color, alpha=0.7)
                    else:
                        g.set_color(*entity.color, alpha=0.3)

            # update geometry positions
            for e, entity in enumerate(self.world.entities):
                self.render_geoms_xform[e].set_translation(*entity.state.p_pos)
            for a, agent in enumerate(self.world.agents):
                if agent.alive:
                    self.render_range_geoms_xform[a].set_translation(
                        *agent.state.p_pos)
                    self.render_range_geoms[a].set_color(1, 1, 1, alpha=0.6)
                else:
                    self.render_range_geoms[a].set_color(0.1,
                                                         0.1,
                                                         0.1,
                                                         alpha=0)

            # render to display or array
            results.append(
                self.viewers[i].render(return_rgb_array=mode == 'rgb_array'))

        return results
    def render_whole_field(self, mode='human'):
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for agent in self.world.agents:
                comm = []
                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 + '   ')

        # 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)

        if self.viewer is None:
            from multiagent import rendering
            self.viewer = rendering.Viewer(53 * 7, 120 * 7)

        # create rendering geometry
        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:
                size = 2 * entity.size
                # if entity.position == 'q_back':
                #     size = 2*size
                geom = rendering.make_circle(size)
                xform = rendering.Transform()
                if 'q_back' == entity.position:
                    geom.set_color(0, 1, 0, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform)

            self.viewer.geoms = []
            for geom in self.render_geoms:
                self.viewer.add_geom(geom)

        line_of_scrimmage = self.world.line_of_scrimmage
        first_down_line = line_of_scrimmage + self.world.first_down_line

        self.viewer.draw_line((0, line_of_scrimmage), (53, line_of_scrimmage))
        self.viewer.draw_line((0, first_down_line), (53, first_down_line))

        results = []
        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.viewer.set_bounds(0, 53, 0, 120)
        # 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.viewer.render(return_rgb_array=mode == 'rgb_array'))

        return results
Example #15
0
    def render(self, mode='human'):
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for agent in self.world.agents:
                comm = []
                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:
                self.viewers[i] = rendering.Viewer(700, 700)

        # create rendering geometry
        if self.render_geoms is None:
            self.render_geoms = []
            self.render_geoms_xform = []
            for entity in self.world.entities:
                xform = rendering.Transform()
                geom = rendering.make_circle(entity.size)
                obs_window = None
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                    obs_window = make_obs_window(0.5, entity.state.p_ang_pos,
                                                 2 * pi / 3)
                    obs_window.set_color(0.5, 0.5, 0.5, alpha=0.1)
                    obs_window.add_attr(xform)
                    self.render_geoms.append(obs_window)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                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)):
            # 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
    def render(self, mode='human'):
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for agent in self.world.agents:
                comm = []
                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
        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 = []
            i = 0
            for entity in self.world.entities:
                if i < 8:
                    #geom = rendering.make_circle_with_arrow(radius= entity.size,p_pos=entity.state.p_pos,filled=False)
                    geom = rendering.make_circle_with_arrow(entity.size,
                                                            entity.state.p_pos,
                                                            filled=True)
                else:
                    geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()  # 6 entities

                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform)
                i = i + 1

            # 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):
                # u
                if (e < 4):
                    v_angle = np.arctan2(entity.state.p_vel[1],
                                         entity.state.p_vel[0])
                    self.render_geoms_xform[e].set_rotation(v_angle +
                                                            1 / 2 * math.pi)
                if (e >= 4 and e < 8):
                    v_angle = np.arctan2(entity.state.p_vel[1],
                                         entity.state.p_vel[0])
                    self.render_geoms_xform[e].set_rotation(1 / 2 * math.pi +
                                                            v_angle)

                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
    def render(self, mode='human'):
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for agent in self.world.agents:
                comm = []
                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
                print("initialize viewer")
                self.viewers[i] = rendering.Viewer(700, 700)

        # create rendering geometry
        if self.render_geoms is None or self.render_geoms is not 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_attack = []
            self.render_geoms_line = []
            self.render_geoms_range = []
            self.render_geoms_xform = []
            self.render_geoms_attack_xform = []
            self.render_geoms_line_xform = []
            self.render_geoms_range_xform = []
            for entity in self.world.entities:
                if "agent" in entity.name:
                    geom = rendering.make_circle(entity.size)
                else:
                    geom = rendering.make_polygon([[0.1, -0.1], [0.1, 0.1],
                                                   [-0.1, 0.1], [-0.1, -0.1]])
                xform = rendering.Transform()
                xform_atk = rendering.Transform()
                x_form_line = rendering.Transform()
                # xform_range = rendering.Transform()
                geom_attack, geom_line, geom_range = None, None, None
                if 'agent' in entity.name:
                    if entity.num_balloons != 0:
                        geom.set_color(*entity.color,
                                       alpha=0.0 + 0.2 * entity.num_balloons)
                    if entity.action.a[0] and entity.action.a[1]:
                        geom_attack = rendering.make_circle(entity.size / 4)
                        geom_attack.add_attr(xform_atk)

                        geom_line = rendering.make_line(
                            (0, 0),
                            (entity.action.a[0] - entity.state.p_pos[0],
                             entity.action.a[1] - entity.state.p_pos[1]))
                        geom_line.add_attr(x_form_line)

                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)

                self.render_geoms_line.append(geom_line)
                self.render_geoms_line_xform.append(x_form_line)

                self.render_geoms_attack.append(geom_attack)
                self.render_geoms_attack_xform.append(xform_atk)

                self.render_geoms.append(geom)
                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)

                for atk_geom in self.render_geoms_attack:
                    if atk_geom:
                        viewer.add_geom(atk_geom)

                for line_geom in self.render_geoms_line:
                    if line_geom:
                        viewer.add_geom(line_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)

            for a, agent in enumerate(self.world.agents):
                self.render_geoms_attack_xform[a].set_translation(
                    *agent.action.a)

            for a, agent in enumerate(self.world.agents):
                self.render_geoms_line_xform[a].set_translation(
                    *agent.state.p_pos)

            # render to display or array
            results.append(
                self.viewers[i].render(return_rgb_array=mode == 'rgb_array'))

        return results
    def render(self, mode='human'):
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for agent in self.world.agents:
                comm = []
                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
        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:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                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
Example #19
0
    def render(self, mode='human', VP_W=700, VP_H=700, print_text=False):
        if print_text and mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            message_constructed = False
            for agent in self.world.agents:
                comm = []
                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 + '   ')
                    message_constructed = True
            if message_constructed:
                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(VP_W, VP_H)

        # create rendering geometry
        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 = []
            self.sensor_render_geoms = []
            self.sensor_render_geoms_xform = []
            for entity in self.world.entities:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform)
                if entity.sensor is not None:
                    v = self._make_receptor_locations(
                        entity, self.world.position_scale)
                    sensor_geom = rendering.make_polygon(v=v, filled=True)
                    sensor_geom.set_color(*entity.color, alpha=0.2)
                    sensor_xform = rendering.Transform()
                    sensor_geom.add_attr(sensor_xform)
                    self.sensor_render_geoms.append(sensor_geom)
                    self.sensor_render_geoms_xform.append(sensor_xform)

            # add geoms to viewer
            for viewer in self.viewers:
                viewer.geoms = []
                viewer.sensor_geoms = []
                for sensor_geom in self.sensor_render_geoms:
                    viewer.add_geom(sensor_geom)
                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.world.position_scale
            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 / self.world.position_scale))
                if (entity.rwr
                        is not None) and (len(entity.rwr.observers) > 0):
                    one_time_geom = self.viewers[i].draw_circle(
                        radius=entity.size * 1.5, res=30, filled=False)
                    xform = rendering.Transform()
                    xform.set_translation(*(entity.state.p_pos /
                                            self.world.position_scale))
                    one_time_geom.add_attr(xform)
                    one_time_geom.set_color(0.75, 0.25, 0.25)
                    one_time_geom.set_linewidth(5.0)
                if entity.sensor is not None:
                    self.sensor_render_geoms_xform[e].set_translation(
                        *(entity.state.p_pos / self.world.position_scale))
                    rotation = np.sign(entity.state.p_vel[1]) * np.arccos(
                        entity.state.p_vel[0] /
                        np.linalg.norm(entity.state.p_vel))
                    self.sensor_render_geoms_xform[e].set_rotation(rotation)
                    if len(entity.sensor.detections) > 0:
                        self.sensor_render_geoms[e].set_color(0.75,
                                                              0.25,
                                                              0.25,
                                                              alpha=0.2)
                    else:
                        self.sensor_render_geoms[e].set_color(*entity.color,
                                                              alpha=0.2)
                    if print_text:
                        print("Detecions for entity: ", entity.name,
                              entity.sensor.detections)
                # render expendables
                for missile in entity.state.missiles_in_flight:
                    one_time_geom = self.viewers[i].draw_circle(
                        radius=missile.size, res=30, filled=False)
                    xform = rendering.Transform()
                    xform.set_translation(*(missile.state.p_pos /
                                            self.world.position_scale))
                    one_time_geom.add_attr(xform)
                    one_time_geom.set_color(*missile.color)
                    one_time_geom.set_linewidth(5.0)
                # render to display or array
            results.append(self.viewers[i].render(
                return_rgb_array=mode == 'rgb_array', VP_W=VP_W, VP_H=VP_H))

        return results
    def render(self, mode='human', attn=None):
        # attn: matrix of size (num_agents, num_agents)

        # if mode == 'human':
        #     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
        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:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform)

            self.render_count = len(self.render_geoms)
            # render attn graph
            if attn is not None:
                # initialize render geoms for line
                for i in range(self.n):
                    for j in range(i + 1, self.n):
                        geom = rendering.Line(
                            start=self.world.agents[i].state.p_pos,
                            end=self.world.agents[j].state.p_pos,
                            linewidth=2)
                        color = (1.0, 0.0, 0.0)
                        alpha = 0
                        geom.set_color(*color, alpha)
                        xform = rendering.Transform()
                        self.render_geoms.append(geom)
                        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)

        if attn is not None:
            self._add_lines(attn)

        results = []
        for i in range(len(self.viewers)):
            from multiagent import rendering
            # update bounds to center around agent
            cam_range = self.cam_range
            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
    def setup_geometry(self, env):
        """ create geoms and transforms for basic agents and landmarks
        """
        # lazy import
        from multiagent import rendering

        if getattr(env, "render_dict", None) is not None:
            return
        env.render_dict = {}

        # make geometries and transforms
        for entity in env.world.entities:
            name = entity.name
            geom = rendering.make_circle(entity.size)
            xform = rendering.Transform()

            # agent on top, other entity to background
            alpha = 0.6 if "agent" in name else 0.5
            geom.set_color(*entity.color, alpha=alpha)

            geom.add_attr(xform)
            env.render_dict[name] = {
                "geom": geom,
                "xform": xform,
                "attach_ent": entity
            }

            # VIS: show visual range/receptor field
            if 'agent' in entity.name and env.show_visual_range:
                vis_geom = rendering.make_circle(entity.vision_range)
                vis_geom.set_color(*entity.color, alpha=0.2)
                vis_xform = rendering.Transform()
                vis_geom.add_attr(vis_xform)
                env.render_dict[name + "_vis"] = {
                    "geom": vis_geom,
                    "xform": vis_xform,
                    "attach_ent": entity
                }

            # GOAL: highlight goal entities
            if "agent" in entity.name and entity.goal_b is not None:
                for goal in [entity.goal_a, entity.goal_b]:
                    goal_geom = rendering.make_circle(goal.size * 1.5)
                    goal_geom.set_color(*goal.color, alpha=0.2)
                    goal_xform = rendering.Transform()
                    goal_geom.add_attr(goal_xform)
                    env.render_dict[goal.name + "_highlight"] = {
                        "geom": goal_geom,
                        "xform": goal_xform,
                        "attach_ent": goal
                    }

            # LABEL: display comm message
            if "agent" in entity.name and entity.goal_b is not None:
                # if "agent" in entity.name and entity.goal_b is None:
                x = entity.state.p_pos[0] + 50
                y = entity.state.p_pos[1] + 50
                comm_geom = rendering.Text("_", position=(x, y), font_size=36)
                comm_xform = rendering.Transform()
                comm_geom.add_attr(comm_xform)
                env.render_dict[name + "_comm"] = {
                    "geom": comm_geom,
                    "xform": comm_xform,
                    "attach_ent": entity
                }

        # add geoms to viewer
        for viewer in env.viewers:
            viewer.geoms = []
            for k, d in env.render_dict.items():
                viewer.add_geom(d["geom"])
Example #22
0
    def render(self, mode='human'):
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for agent in self.world.agents:
                comm = []
                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(900, 900)

        # create rendering geometry
        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 'landmark' in entity.name:
                    geom = rendering.make_circle(entity.size)
                    # xform = rendering.Transform()

                    #eom4 = rendering.make_cone(0.1 + entity.size, [entity.state.p_pos], [entity.state.p_vel])
                    xform4 = rendering.Transform()

                    # if 'agent' in entity.name:
                    #     geom.set_color(*entity.color, alpha=0.5)
                    #     # geom4.set_color(*entity.color, alpha=0.5)

                    # geom.set_color(*entity.color)

                    geom.add_attr(xform4)
                    # geom4.add_attr(xform4)

                    self.render_geoms.append(geom)
                    # self.render_geoms_xform.append(xform)

                    # self.render_geoms.append(geom4)
                    self.render_geoms_xform.append(xform4)

                else:
                    #print(entity.render_vel)
                    geom = rendering.make_circle(entity.size)
                    # xform = rendering.Transform()
                    print(entity.render_vel)
                    #geom4 = rendering.make_cone(0.1 + entity.size, entity.state.p_pos[0],entity.state.p_pos[1], entity.render_vel[0],entity.render_vel[1])
                    geom4 = rendering.make_circle(0.2 + entity.size,
                                                  filled=False)

                    xform4 = rendering.Transform()

                    if 'agent' in entity.name:
                        geom.set_color(*entity.color, alpha=0.5)
                        geom4.set_color(*entity.color, alpha=0.5)

                    # geom.set_color(*entity.color)

                    geom.add_attr(xform4)
                    geom4.add_attr(xform4)

                    self.render_geoms.append(geom)
                    # self.render_geoms_xform.append(xform)

                    self.render_geoms.append(geom4)
                    self.render_geoms_xform.append(xform4)

            # for entity in self.world.entities:
            #
            #     geom4 = rendering.make_cone(0.1 + entity.size, entity.state.p_pos,
            #                                 [entity.state.p_vel[0], entity.state.p_vel[1]])
            #
            #
            #
            #     if 'agent' in entity.name:
            #         geom4.set_color(*entity.color)
            #
            #     # geom.add_attr(xform)
            #     xform4 = rendering.Transform()
            #     #geom4.add_attr(xform4)
            #
            #     self.render_geoms.append(geom4)
            #     # self.render_geoms_xform.append(xform)
            #
            #     self.render_geoms_xform.append(xform4)

            geom1 = rendering.make_wall(0.5)
            self.render_geoms.append(geom1)
            xform1 = rendering.Transform()
            self.render_geoms_xform.append(xform1)
            ############################# Environment 1 ############################################################
            # geom2 = rendering.make_obstacles(0.1)
            # self.render_geoms.append(geom2)
            # xform2 = rendering.Transform()
            # self.render_geoms_xform.append(xform2)
            ############################ Environment 2 ############################################################
            # geom2, geom3 = rendering.make_obstacles1(0.1)
            # self.render_geoms.append(geom2)
            # xform2 = rendering.Transform()
            # self.render_geoms_xform.append(xform2)
            # self.render_geoms.append(geom3)
            # xform3 = rendering.Transform()
            # self.render_geoms_xform.append(xform3)
            # geom4 = rendering.make_cone(0.1 + entity.size, entity.state.p_pos[0], entity.state.p_pos[1],
            #                             entity.render_vel[0], entity.render_vel[1])

            # 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)):
            # 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
                vel = self.agents[i].state.p_vel
            self.viewers[i].set_bounds(pos[0] - cam_range, pos[0] + cam_range,
                                       pos[1] - cam_range, pos[1] + cam_range)
            # update geometry positions

            from multiagent import rendering
            for e, entity in enumerate(self.world.entities):
                self.render_geoms_xform[e].set_translation(*entity.state.p_pos)

                #self.render_geoms[e].add_attr(self.render_geoms_xform[e])

            # render to display or array
            results.append(
                self.viewers[i].render(return_rgb_array=mode == 'rgb_array'))

        return results
Example #23
0
    def render(self, mode='human'):
        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
                from multirobot.environment import rendering as mrrendering
                self.viewers[i] = mrrendering.Viewer(700, 700)

            # create rendering geometry
        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
            from multirobot.environment import rendering as mrrendering
            self.render_geoms = []
            self.render_geoms_xform = []
            for entity in self.world.entities:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform)

            # render fov of vehicles
            for vehicle in self.world.vehicles:
                geom = mrrendering.make_fov(vehicle.fov, 30)
                xform = rendering.Transform()
                geom.set_color(*vehicle.color, alpha=0.5)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                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)

        for i in range(len(self.viewers)):
            # update bounds to center around agent
            if self.shared_viewer:
                self.viewers[i].set_bounds(-self.world.size_x - 1,
                                           self.world.size_x + 1,
                                           -self.world.size_y - 1,
                                           self.world.size_y + 1)
            else:
                cam_range = 1
                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)

            # update fov sectors
            e = len(self.world.entities)
            for v, vehicle in enumerate(self.world.vehicles):
                self.render_geoms_xform[e + v].set_translation(
                    *vehicle.state.p_pos)
                self.render_geoms_xform[e + v].set_rotation(
                    vehicle.state.p_ang)

            # render to display or array
            # return image of viewer[0], as [0] consider as global viewer
        result = self.viewers[0].render(return_rgb_array=(mode == 'rgb_array'))

        return result
    def render(self, mode='human'):
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for i, agent in enumerate(self.world.agents):
                comm = []
                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
        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 = []
            #zgy
            self.render_lines = []
            for entity in self.world.entities:
                geom = rendering.make_circle(entity.size)
                geom_line = rendering.make_line((0, 0), (fire_range, 0))
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                    geom_line.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                    geom_line.set_color(*entity.color)
                geom.add_attr(xform)
                geom_line.add_attr(xform)
                self.render_geoms.append(geom)
                self.render_geoms.append(geom_line)
                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)
                for geom_line in self.render_geoms:
                    viewer.add_geom(geom_line)

        results = []

        # zgy
        agents = self.world.agents

        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):
                my_chi = np.zeros(1)
                if abs(agents[e].state.p_vel[0]) < 1e-5 and abs(
                        agents[e].state.p_vel[1]) < 1e-5:
                    my_chi[0] = 0
                else:
                    my_chi[0] = math.atan2(agents[e].state.p_vel[1],
                                           agents[e].state.p_vel[0])

                self.render_geoms_xform[e].set_translation(*entity.state.p_pos)
                self.render_geoms_xform[e].set_rotation(my_chi[0])
            # render to display or array
            results.append(
                self.viewers[i].render(return_rgb_array=mode == 'rgb_array'))

        return results
Example #25
0
    def render(self, mode='human'):
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for agent in self.world.agents:
                comm = []
                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
        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:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform)
            '''UAV = self.world.entities[0]
            for entity in self.world.entities:  # world.entities:
                if entity.name == 'agent 0':
                    continue
                else:
                    geom = rendering.make_line(UAV.state.p_pos, entity.state.p_pos)
                    geom.set_color(*entity.color)
                    self.render_geoms.append(geom)'''

            # 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)

            if self.viewers[i].geoms is not None:
                for line in range(len(self.world.entities), len(self.viewers[i].geoms)):
                    self.viewers[i].geoms.pop()
            uav_position = self.world.entities[0].state.p_pos
            '''for l, landmark in enumerate(self.world.landmarks):
                    dis = np.sqrt(np.sum(np.square([uav_position - landmark.state.p_pos])))
                    if dis <= 0.7:
                        temp = rendering.make_line(uav_position, landmark.state.p_pos)
                        temp.set_color(0, 245, 255)
                        self.viewers[i].geoms.append(temp)'''

            # render to display or array
            results.append(self.viewers[i].render(return_rgb_array=mode == 'rgb_array'))

        return results
Example #26
0
    def render(self, mode='human'):
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for agent in self.world.agents:
                comm = []
                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
        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 = []

            # VIS: show visual range/receptor field
            self.vis_render_geoms = []
            self.vis_render_geoms_xform = []

            for entity in self.world.entities:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    # geom.set_color(*entity.color, alpha=0.5)
                    geom.set_color(*entity.color)  # agent on top
                else:
                    # geom.set_color(*entity.color)
                    geom.set_color(*entity.color,
                                   alpha=0.5)  # entity to background
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform)

                # VIS: show visual range/receptor field
                if 'agent' in entity.name and self.show_visual_range:
                    vis_range = entity.vision_range
                    vis_geom = rendering.make_circle(vis_range)
                    vis_xform = rendering.Transform()
                    vis_geom.set_color(*entity.color, alpha=0.2)
                    vis_geom.add_attr(vis_xform)
                    self.vis_render_geoms.append(vis_geom)
                    self.vis_render_geoms_xform.append(vis_xform)
                else:
                    self.vis_render_geoms.append(None)
                    self.vis_render_geoms_xform.append(None)

            # add geoms to viewer
            for viewer in self.viewers:
                viewer.geoms = []
                for geom in self.render_geoms:
                    viewer.add_geom(geom)

                # VIS: show visual range/receptor field
                for vis_geom in self.vis_render_geoms:
                    if vis_geom is not None:
                        viewer.add_geom(vis_geom)

        results = []
        for i in range(len(self.viewers)):
            from multiagent import rendering
            # update bounds to center around agent
            # cam_range = 1
            cam_range = self.cam_range
            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)
            self.viewers[i].set_bounds(-cam_range, cam_range, -cam_range,
                                       cam_range)
            # update geometry positions
            for e, entity in enumerate(self.world.entities):
                self.render_geoms_xform[e].set_translation(*entity.state.p_pos)

                # VIS: show visual range/receptor field
                if self.vis_render_geoms_xform[e] is not None:
                    self.vis_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
    def render(self, mode='human'):  #, close=False): #close is dummy
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

        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
        if self.world.goals is not None:
            vis_entities = self.world.entities + self.world.goals
        else:
            vis_entities = self.world.entities

        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 vis_entities:
                if "border" in entity.name:
                    geom = rendering.make_polygon(entity.shape)
                else:
                    geom = rendering.make_circle(entity.size)

                xform = rendering.Transform()
                if 'goal' in entity.name or "target" in entity.name or 'landmark' in entity.name:
                    geom.set_color(*entity.color, alpha=0.3)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                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 = self.world.x_range[1] / 2.0 + 0.5
            if self.shared_viewer:
                if self.world.dim_p == 1:
                    pos = np.array([self.world.x_range[1] / 2.0, 0])
                else:
                    pos = np.array([
                        self.world.x_range[1] / 2.0,
                        self.world.y_range[1] / 2.0
                    ])
            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(vis_entities):
                if self.world.dim_p == 1:
                    translation = np.concatenate(
                        (entity.state.p_pos, np.array([0])), axis=0)
                    self.render_geoms_xform[e].set_translation(*translation)
                else:
                    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
Example #28
0
    def render(self, mode='human', close=False):
        if close:
            # close any existic renderers
            for i, viewer in enumerate(self.viewers):
                if viewer is not None:
                    viewer.close()
                self.viewers[i] = None
            return []

        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for agent in self.world.agents:
                comm = []
                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
        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 = []
            self.comm_geoms = []
            for entity in self.world.entities:
                alpha = 0 if not entity.visible else 1
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                entity_comm_geoms = []
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=alpha * 0.5)
                    if not entity.silent:
                        dim_c = self.world.dim_c
                        # make circles to represent communication
                        for ci in range(dim_c):
                            comm = rendering.make_circle(entity.size / dim_c)
                            comm.set_color(1, 1, 1)
                            comm.add_attr(xform)
                            offset = rendering.Transform()
                            comm_size = (entity.size / dim_c)
                            offset.set_translation(
                                ci * comm_size * 2 - entity.size + comm_size,
                                0)
                            comm.add_attr(offset)
                            entity_comm_geoms.append(comm)
                else:
                    geom.set_color(*entity.color, alpha=alpha)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform)
                self.comm_geoms.append(entity_comm_geoms)
            if hasattr(self.world, 'walls'):
                for wall in self.world.walls:
                    corners = ((wall.axis_pos - 0.5 * wall.width,
                                wall.endpoints[0]),
                               (wall.axis_pos - 0.5 * wall.width,
                                wall.endpoints[1]),
                               (wall.axis_pos + 0.5 * wall.width,
                                wall.endpoints[1]),
                               (wall.axis_pos + 0.5 * wall.width,
                                wall.endpoints[0]))
                    if wall.orient == 'H':
                        corners = tuple(c[::-1] for c in corners)
                    geom = rendering.make_polygon(corners)
                    if wall.hard:
                        geom.set_color(*wall.color)
                    else:
                        geom.set_color(*wall.color, alpha=0.5)
                    self.render_geoms.append(geom)

            # add geoms to viewer
            for viewer in self.viewers:
                viewer.geoms = []
                for geom in self.render_geoms:
                    viewer.add_geom(geom)
                for entity_comm_geoms in self.comm_geoms:
                    for geom in entity_comm_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):
                alpha = 0 if not entity.visible else 1
                self.render_geoms_xform[e].set_translation(*entity.state.p_pos)
                if 'agent' in entity.name:
                    self.render_geoms[e].set_color(*entity.color,
                                                   alpha=alpha * 0.5)
                    if not entity.silent:
                        for ci in range(self.world.dim_c):
                            color = 1 - entity.state.c[ci]
                            self.comm_geoms[e][ci].set_color(
                                color, color, color)
                else:
                    self.render_geoms[e].set_color(*entity.color, alpha=alpha)
            # render to display or array
            results.append(
                self.viewers[i].render(return_rgb_array=mode == 'rgb_array'))

        return results
Example #29
0
    def render(self, mode='human'):

        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
        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:
                # xform = rendering.Transform()
                # line = None
                #! render landmark according to their position
                if entity in self.world.landmarks:
                    w, h = entity.shape
                    w_half, h_half = w / 2, h / 2
                    geom = rendering.make_polygon([[w_half, -h_half],
                                                   [w_half, h_half],
                                                   [-w_half, h_half],
                                                   [-w_half, -h_half]])
                else:  #! render agents according to their position
                    geom = rendering.make_circle(entity.size)
                    #! render sensor readings
                    # sensor1, sensor2, sensor3, sensor4 = entity.sensors(self.world)
                    # sens1 = rendering.make_circle(entity.size/4, x=sensor1[0],y=sensor1[1])
                    # sens2 = rendering.make_circle(entity.size/4, x=sensor2[0],y=sensor2[1])
                    # sens3 = rendering.make_circle(entity.size/4, x=sensor3[0],y=sensor3[1])
                    # sens4 = rendering.make_circle(entity.size/4, x=sensor4[0],y=sensor4[1])

                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                    # sens1.set_color(*entity.color, alpha=0.5)
                    # sens2.set_color(*entity.color, alpha=0.5)
                    # sens3.set_color(*entity.color, alpha=0.5)
                    # sens4.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)

                geom.add_attr(xform)
                # sens1.add_attr(xform)
                # sens2.add_attr(xform)
                # sens3.add_attr(xform)
                # sens4.add_attr(xform)
                # if line:line.add_attr(xform)
                self.render_geoms.append(geom)
                # self.render_geoms.append(sens1)
                # self.render_geoms.append(sens2)
                # self.render_geoms.append(sens3)
                # self.render_geoms.append(sens4)

                # if line:self.render_geoms.append(line)
                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
Example #30
0
    def _render(self, mode='human', close=True):
        if mode == 'human':
            alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            message = ''
            for agent in self.world.agents:
                comm = []
                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)

        if close:
            # close any existic renderers
            for i,viewer in enumerate(self.viewers):
                if viewer is not None:
                    viewer.close()
                self.viewers[i] = None
            return []

        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
        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:
                geom = rendering.make_circle(entity.size)
                xform = rendering.Transform()
                if 'agent' in entity.name:
                    geom.set_color(*entity.color, alpha=0.5)
                else:
                    geom.set_color(*entity.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                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)):
            # 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
Example #31
0
    def render(self, mode='human'):

        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
        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 agent in self.world.agents:
                geom = rendering.make_circle(agent.size)
                if agent.armed:
                    # render arms
                    radius0 = agent.size * 1.3
                    radius1 = agent.size * 1.5
                    aim = rendering.make_polygon([
                        (np.cos(-.1) * radius1, np.sin(-.1) * radius0),
                        (radius0, 0),
                        (np.cos(.1) * radius1, np.sin(.1) * radius0)
                    ])
                    geom = rendering.Compound([geom, aim])
                xform = rendering.Transform()
                geom.set_color(*agent.color)
                geom.add_attr(xform)
                self.render_geoms.append(geom)
                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
            pos = np.zeros(self.world.dim_p)
            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 a, agent in enumerate(self.world.agents):
                xform = self.render_geoms_xform[a]
                xform.set_translation(*agent.state.p_pos)
                if agent.state.aim_heading is not None:
                    xform.set_rotation(agent.state.aim_heading)

            for projectile in self.world.projectiles:
                geom = rendering.Line(start=tuple(projectile[:2]),
                                      end=tuple(projectile[2:]))
                for viewer in self.viewers:
                    viewer.add_onetime(geom)

            # render to display or array
            results.append(
                self.viewers[i].render(return_rgb_array=mode == 'rgb_array'))

        return results