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"])
Beispiel #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
    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)
Beispiel #4
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"])
 def _reset_viewers(self):
     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] = RoadViewer(STATE_W, STATE_H)
             self.transforms[i] = rendering.Transform()
Beispiel #6
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
Beispiel #7
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]
Beispiel #8
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)
Beispiel #10
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
Beispiel #11
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')
Beispiel #12
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]
Beispiel #13
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
Beispiel #14
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
    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
Beispiel #16
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
Beispiel #17
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
Beispiel #18
0
    def __init__(self,
                 world,
                 env_spec,
                 reset_callback=None,
                 reward_callback=None,
                 observation_callback=None,
                 info_callback=None,
                 done_callback=None,
                 shared_viewer=True):

        self.world = world
        self.env_spec = env_spec
        self.agents = self.world.policy_agents
        # set required vectorized gym env property
        self.n = len(world.policy_agents)
        # scenario callbacks
        self.reset_callback = reset_callback
        self.reward_callback = reward_callback
        self.observation_callback = observation_callback
        self.info_callback = info_callback
        self.done_callback = done_callback
        # environment parameters
        self.discrete_action_space = True
        # if true, action is a number 0...N, otherwise action is a one-hot N-dimensional vector
        self.discrete_action_input = False
        # if true, even the action is continuous, action will be performed discretely
        self.force_discrete_action = world.discrete_action if hasattr(
            world, 'discrete_action') else False
        # if true, every agent has the same reward
        self.shared_reward = world.collaborative if hasattr(
            world, 'collaborative') else False
        self.time = 0

        # configure spaces
        self.action_space = []
        self.observation_space = []
        for agent in self.agents:
            total_action_space = []
            # physical action space
            if self.discrete_action_space:
                u_action_space = spaces.Discrete(world.dim_p * 2 + 1)
            else:
                u_action_space = spaces.Box(low=-agent.u_range,
                                            high=+agent.u_range,
                                            shape=(world.dim_p, ),
                                            dtype=np.float32)
            if agent.movable:
                total_action_space.append(u_action_space)
            # communication action space
            if self.discrete_action_space:
                c_action_space = spaces.Discrete(world.dim_c)
            else:
                c_action_space = spaces.Box(low=0.0,
                                            high=1.0,
                                            shape=(world.dim_c, ),
                                            dtype=np.float32)
            if not agent.silent:
                total_action_space.append(c_action_space)
            # total action space
            if len(total_action_space) > 1:
                # all action spaces are discrete, so simplify to MultiDiscrete action space
                if all([
                        isinstance(act_space, spaces.Discrete)
                        for act_space in total_action_space
                ]):
                    act_space = MultiDiscrete(
                        [[0, act_space.n - 1]
                         for act_space in total_action_space])
                else:
                    act_space = spaces.Tuple(total_action_space)
                self.action_space.append(act_space)
            else:
                self.action_space.append(total_action_space[0])
            # observation space
            obs_dim = len(observation_callback(agent, self.world))
            self.observation_space.append(
                spaces.Box(low=-np.inf,
                           high=+np.inf,
                           shape=(obs_dim, ),
                           dtype=np.float32))
            agent.action.c = np.zeros(self.world.dim_c)

        # rendering
        self.shared_viewer = shared_viewer

        # import rendering only if needed. TODO Add a global rendering flag
        from multiagent import rendering
        # TODO Create a debug flag for this
        w = self.env_spec.width * 2 + 0.2
        h = self.env_spec.height * 2 + 0.2
        self.world_bound_geom = rendering.make_polygon([(0, 0), (w, 0), (w, h),
                                                        (0, h)], False)
        self.world_bound_geom.set_color(0., 0., 0., alpha=0.3)
        xform = rendering.Transform()
        xform.set_translation(-w / 2, -h / 2)
        self.world_bound_geom.add_attr(xform)
        if self.shared_viewer:
            self.viewers = [rendering.Viewer(700, 700)]
        else:
            self.viewers = [rendering.Viewer(700, 700) for i in self.n]
        for i, viewer in enumerate(self.viewers):
            viewer.cam_range = self.env_spec.zoom
        self._reset_render()
Beispiel #19
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'):
        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
Beispiel #21
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
Beispiel #22
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 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
Beispiel #24
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 = []
            i = 0
            for goal in self.world.goals:
                geom = rendering.make_circle_with_arrow(goal.size, goal.state.p_pos, goal.state.p_ang, filled=True)
                geom.set_color(*goal.color, alpha=0.5)
                xform_goal = rendering.Transform()
                geom.add_attr(xform_goal)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform_goal)

            for agent in self.world.agents:
                geom = rendering.make_circle_with_arrow(agent.size, agent.state.p_pos, agent.state.p_ang, filled=True)
                geom.set_color(*agent.color, alpha=0.5)
                xform_agent = rendering.Transform()
                geom.add_attr(xform_agent)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform_agent)
            
            for human in self.world.humans:
                geom = rendering.make_circle_with_arrow(human.size, human.pos, human.p_ang, filled=True)
                geom.set_color(*human.color, alpha=0.5)
                xform_human = rendering.Transform()
                geom.add_attr(xform_human)
                self.render_geoms.append(geom)
                self.render_geoms_xform.append(xform_human)




            # for entity in self.world.entities:
            #     #print('position:',entity.state.p_pos)
            #     #print('velocity:',entity.state.p_vel)
            #     #if i < 1:
            #     if i < self.world.num_goals+self.world.num_agents:
            #         # render goal and agent
            #         # print("success")
            #         geom = rendering.make_circle_with_arrow(entity.size, entity.state.p_pos, entity.state.p_ang, filled=True)
            #         #geom = rendering.plot_car(entity.state.p_pos, entity.state.p_ang)
            #     else:
            #         # render obstacle
            #         geom = rendering.make_circle(entity.size)
            #     xform = rendering.Transform()

            #     # if 'agent' in entity.name:
            #     if 'human' 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 = []
        # print(len(self.viewers))
        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 g, goal in enumerate(self.world.goals):
                self.render_geoms_xform[g].set_rotation(goal.state.p_ang)
                self.render_geoms_xform[g].set_translation(*goal.state.p_pos)

            for a, agent in enumerate(self.world.agents):
                L = len(self.world.goals)
                self.render_geoms_xform[a+L].set_rotation(agent.state.p_ang)
                self.render_geoms_xform[a+L].set_translation(*agent.state.p_pos)
            
            for h, human in enumerate(self.world.humans):
                l1 = len(self.world.goals)
                l2 = len(self.world.agents)
                self.render_geoms_xform[h+l1+l2].set_rotation(human.p_ang)
                self.render_geoms_xform[h+l1+l2].set_translation(*human.pos)




            # for e, entity in enumerate(self.world.entities):
            #     if (e < self.world.num_goals+self.world.num_agents):
            #         #print("angle:", entity.state.p_ang)
            #         self.render_geoms_xform[e].set_rotation(entity.state.p_ang) 
            #     #print("abc") 
            #     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
                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
Beispiel #27
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
Beispiel #28
0
    def render(self):
        # 每条路长road_length=80,同条路上的两车道相距bi_dis=5,两条路之间相距distance=100
        from multiagent import rendering
        if self.viewer is None:
            self.viewer = rendering.Viewer(600, 600)  # 600x600 是画板的长和框
            for i in range(4):
                for j in range(4):
                    rectangle = rendering.make_polyline([(-15, 15), (15, 15),
                                                         (15, -15), (-15, -15),
                                                         (-15, 15)])
                    rect_transform = rendering.Transform(
                        translation=(100 * i + 50, 100 * j + 50))
                    rectangle.add_attr(rect_transform)
                    self.viewer.add_geom(rectangle)

                    score_label = pyglet.text.Label('0',
                                                    font_size=8,
                                                    x=100 * i + 38,
                                                    y=100 * j + 50,
                                                    anchor_x='left',
                                                    anchor_y='center',
                                                    color=(25, 25, 25, 255))
                    self.label[i][j] = score_label
            self.step_count_label = pyglet.text.Label(
                'step_count:000',
                font_name='Times New Roman',
                font_size=12,
                x=39,
                y=100 * 5 - 20,
                anchor_x='left',
                anchor_y='center',
                color=(25, 25, 25, 255))
            self.step_total_re_label = pyglet.text.Label(
                'step_total_reward:000',
                font_name='Times New Roman',
                font_size=12,
                x=39,
                y=100 * 5 + 5,
                anchor_x='left',
                anchor_y='center',
                color=(25, 25, 25, 255))
            self.total_re_label = pyglet.text.Label(
                'total_reward:000',
                font_name='Times New Roman',
                font_size=12,
                x=39,
                y=100 * 5 + 30,
                anchor_x='left',
                anchor_y='center',
                color=(25, 25, 25, 255))
            for x1 in range(4):
                for y1 in range(3):
                    x2 = x1
                    y2 = y1 + 1
                    self.lines[x1][y1][x2][y2] = self.getNewLine(
                        x1, y1, x2, y2)
                    self.lines[x2][y2][x1][y1] = self.getNewLine(
                        x2, y2, x1, y1)
            for x1 in range(3):
                for y1 in range(4):
                    x2 = x1 + 1
                    y2 = y1
                    self.lines[x1][y1][x2][y2] = self.getNewLine(
                        x1, y1, x2, y2)
                    self.lines[x2][y2][x1][y1] = self.getNewLine(
                        x2, y2, x1, y1)

        for x1 in range(4):
            for y1 in range(3):
                x2 = x1
                y2 = y1 + 1
                self.updateLine(x1, y1, x2, y2)
                self.updateLine(x2, y2, x1, y1)
        for x1 in range(3):
            for y1 in range(4):
                x2 = x1 + 1
                y2 = y1
                self.updateLine(x1, y1, x2, y2)
                self.updateLine(x2, y2, x1, y1)
        arr = None
        win = self.viewer.window
        win.switch_to()
        win.dispatch_events()
        win.clear()
        for geom in self.viewer.geoms:
            geom.render()
        for i in range(4):
            for j in range(4):
                self.label[i][j].text = "{}".format(self.nod_rewards[i][j])
                self.label[i][j].draw()
        self.step_count_label.text = "step_count:{}".format(self.count)
        self.step_count_label.draw()
        self.step_total_re_label.text = "step_total_reward:{}".format(
            self.step_total_re)
        self.step_total_re_label.draw()
        self.total_re_label.text = "total_reward:{}".format(self.total_re)
        self.total_re_label.draw()
        win.flip()
        return arr
Beispiel #29
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
    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