コード例 #1
0
ファイル: reach_and_avoid_env.py プロジェクト: JayChen35/NMLO
    def render(self, mode='human'):
        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-22, 22, -22, 22)
            self.goal_transform = rendering.Transform()
            goal = rendering.make_circle(self.contact_radius_goal)
            goal.set_color(.1, .7, .1)
            goal.add_attr(self.goal_transform)
            self.viewer.add_geom(goal)
            self.avoid_transform = rendering.Transform()
            avoid = rendering.make_circle(self.contact_radius_avoid)
            avoid.set_color(.7, .1, .1)
            avoid.add_attr(self.avoid_transform)
            self.viewer.add_geom(avoid)
            arm1 = rendering.make_capsule(self.single_arm_length, 1)
            arm2 = rendering.make_capsule(self.single_arm_length, 1)
            arm1.set_color(.1, .4, .7)
            arm2.set_color(.4, .1, .7)
            self.arm1_transform = rendering.Transform()
            arm1.add_attr(self.arm1_transform)
            self.viewer.add_geom(arm1)
            self.arm2_transform = rendering.Transform()
            arm2.add_attr(self.arm2_transform)
            self.viewer.add_geom(arm2)
            axle = rendering.make_circle(0.6)
            axle.set_color(.4, .4, .4)
            self.viewer.add_geom(axle)
            self.end_transform = rendering.Transform()
            end_effector = rendering.make_circle(self.contact_radius_goal)
            end_effector.set_color(.7, .7, .7)
            end_effector.add_attr(self.end_transform)
            self.viewer.add_geom(end_effector)
            bounds = rendering.make_circle(self.env_radius + self.contact_radius_goal/2, filled=False)
            bounds.set_color(.4, .4, .4)
            self.viewer.add_geom(bounds)
                        
        self.arm1_transform.set_rotation(self.state[0])

        arm1_end_pos = (self.single_arm_length * cos(self.state[0]), self.single_arm_length * sin(self.state[0])) 

        self.arm2_transform.set_translation(arm1_end_pos[0], arm1_end_pos[1])
        self.arm2_transform.set_rotation(self.state[1]+self.state[0])

        arm2_end_pos = (arm1_end_pos[0] + (self.single_arm_length * cos(self.state[0] + self.state[1])), arm1_end_pos[1] + (self.single_arm_length * sin(self.state[0] + self.state[1])))         
        self.end_transform.set_translation(arm2_end_pos[0], arm2_end_pos[1])

        self.goal_transform.set_translation(self.state[2], self.state[3])

        self.avoid_transform.set_translation(self.state[4], self.state[5])
        
        if self.state is None:
            return None

        return self.viewer.render(return_rgb_array = False)
コード例 #2
0
    def _render(self, mode='human', close=False):
        if close:
            if self.viewer is not None:
                self.viewer.close()
            return

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(600, 600)
            self.viewer.set_bounds(-10, 10, 0, 20)

            airframe = rendering.make_capsule(2.5, 0.15)
            airframe.add_attr(rendering.Transform(translation=(-1.25, 0.0)))
            airframe.set_color(0, 0, 1)
            self.airframe_transform = rendering.Transform()
            airframe.add_attr(self.airframe_transform)

            left_prop_shaft = rendering.Line((0, 0), (0, 0.25))
            left_prop_shaft.add_attr(
                rendering.Transform(translation=(-0.875, 0.0)))
            left_prop_shaft.add_attr(self.airframe_transform)
            self.viewer.add_geom(left_prop_shaft)

            left_prop = rendering.make_capsule(0.75, 0.1)
            left_prop.add_attr(rendering.Transform(translation=(-1.25, 0.25)))
            left_prop.add_attr(self.airframe_transform)
            left_prop.set_color(0, 1, 0)
            self.viewer.add_geom(left_prop)

            right_prop_shaft = rendering.Line((0, 0), (0, 0.25))
            right_prop_shaft.add_attr(
                rendering.Transform(translation=(0.875, 0.0)))
            right_prop_shaft.add_attr(self.airframe_transform)
            self.viewer.add_geom(right_prop_shaft)

            right_prop = rendering.make_capsule(0.75, 0.1)
            right_prop.add_attr(rendering.Transform(translation=(0.5, 0.25)))
            right_prop.add_attr(self.airframe_transform)
            right_prop.set_color(0, 1, 0)
            self.viewer.add_geom(right_prop)

            self.viewer.add_geom(airframe)

        q = self.state
        self.airframe_transform.set_translation(q[0], q[1])
        self.airframe_transform.set_rotation(q[2])

        self.viewer.render()
        if mode == 'rgb_array':
            return self.viewer.get_array()
        elif mode is 'human':
            pass
        else:
            return super(PlanarQuadEnv, self).render(mode=mode)
コード例 #3
0
ファイル: planar_quad.py プロジェクト: RichardKelley/gym
    def _render(self, mode='human', close=False):
        if close:
            if self.viewer is not None:
                self.viewer.close()
            return

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(600, 600)
            self.viewer.set_bounds(-10, 10, 0, 20)

            airframe = rendering.make_capsule(2.5,0.15)
	    airframe.add_attr(rendering.Transform(translation=(-1.25, 0.0)))
            airframe.set_color(0,0,1)
            self.airframe_transform = rendering.Transform()
            airframe.add_attr(self.airframe_transform)

            left_prop_shaft = rendering.Line((0,0), (0,0.25))
            left_prop_shaft.add_attr(rendering.Transform(translation=(-0.875, 0.0)))
            left_prop_shaft.add_attr(self.airframe_transform)
            self.viewer.add_geom(left_prop_shaft)

            left_prop = rendering.make_capsule(0.75, 0.1)
            left_prop.add_attr(rendering.Transform(translation=(-1.25, 0.25)))
            left_prop.add_attr(self.airframe_transform)
            left_prop.set_color(0,1,0)
            self.viewer.add_geom(left_prop)

            right_prop_shaft = rendering.Line((0,0), (0,0.25))
            right_prop_shaft.add_attr(rendering.Transform(translation=(0.875, 0.0)))
            right_prop_shaft.add_attr(self.airframe_transform)
            self.viewer.add_geom(right_prop_shaft)

            right_prop = rendering.make_capsule(0.75, 0.1)
            right_prop.add_attr(rendering.Transform(translation=(0.5, 0.25)))
            right_prop.add_attr(self.airframe_transform)
            right_prop.set_color(0,1,0)
            self.viewer.add_geom(right_prop)

            self.viewer.add_geom(airframe)

        q = self.state
        self.airframe_transform.set_translation(q[0], q[1])
        self.airframe_transform.set_rotation(q[2])

        self.viewer.render()
        if mode == 'rgb_array':
            return self.viewer.get_array()
        elif mode is 'human':
            pass
        else:
            return super(PlanarQuadEnv, self).render(mode=mode)
コード例 #4
0
    def _render(self, mode='human', close=False):
        if close:
            if self.viewer is not None:
                self.viewer.close()
                self.viewer = None
            return

        x1, y1, z1 = self.body1.getPosition()
        x2, y2, z2 = self.body2.getPosition()
        state = self._get_obs()

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(self.viewerSize, self.viewerSize)
            self.viewer.set_bounds(-self.spaceSize / 2.0, self.spaceSize / 2.0,
                                   -self.spaceSize / 2.0, self.spaceSize / 2.0)

            base = rendering.make_circle(.2)
            base.set_color(0, 0, 0)
            self.viewer.add_geom(base)

            pole = rendering.make_capsule(self.size_pole[0], self.size_pole[1])
            pole.set_color(.8, .6, .4)
            self.pole_trans = rendering.Transform()
            pole.add_attr(self.pole_trans)
            self.viewer.add_geom(pole)

            pole2 = rendering.make_capsule(self.size_pole[0],
                                           self.size_pole[1])
            pole2.set_color(.8, .6, .4)
            self.pole2_trans = rendering.Transform()
            self.axle21_trans = rendering.Transform()
            pole2.add_attr(self.pole2_trans)
            pole2.add_attr(self.axle21_trans)
            pole2.add_attr(self.pole_trans)
            self.viewer.add_geom(pole2)

            axle = rendering.make_circle(self.size_pole[1] / 2.)
            axle.set_color(.5, .5, .8)
            self.viewer.add_geom(axle)

            axle2 = rendering.make_circle(self.size_pole[1] / 2.)
            axle2.set_color(.5, .5, .8)
            axle2.add_attr(self.axle21_trans)
            axle2.add_attr(self.pole_trans)
            self.viewer.add_geom(axle2)

        self.pole_trans.set_rotation(state[0] + math.pi / 2)
        self.axle21_trans.set_translation(self.size_pole[0], 0.)
        self.pole2_trans.set_rotation(state[2])

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #5
0
    def render(self, mode='human'):
        if self.viewer is None:
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)
            rod = rendering.make_capsule(self.LINK_LENGTH,
                                         0.2 * self.LINK_LENGTH)
            rod.set_color(0.8, 0.3, 0.3)
            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            self.viewer.add_geom(rod)
            axle = rendering.make_circle(0.05 * self.LINK_LENGTH)
            axle.set_color(0, 0, 0)
            self.viewer.add_geom(axle)
            fname = path.join(path.dirname(__file__), "assets/clockwise.png")
            self.img = rendering.Image(fname, 1.0, 1.0)
            self.imgtrans = rendering.Transform()
            self.img.add_attr(self.imgtrans)

        self.viewer.add_onetime(self.img)
        self.pole_transform.set_rotation(self.state[0] + 0.5 * np.pi)
        if self.last_u:
            self.imgtrans.scale = (-self.last_u / self.MAX_TORQUE,
                                   np.abs(self.last_u) / self.MAX_TORQUE)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #6
0
    def render(self, mode='human'):

        s = self.state
        w_size = 8

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 250)
            self.viewer.set_bounds(-w_size, w_size, -w_size / 2, w_size / 2)
            # define colors
            rgb_dred = [0.5, 0, 0]
            rgb_gray = [0.5, 0.5, 0.5]

            # create transform
            self.transform = rendering.Transform()
            # circle
            circle = self.viewer.draw_circle(1)
            circle.set_color(*rgb_dred)
            circle.add_attr(self.transform)
            self.viewer.add_geom(circle)
            # indicator line
            line = rendering.make_capsule(0.5, .2)
            line.set_color(*rgb_gray)
            line.add_attr(self.transform)
            self.viewer.add_geom(line)

        # rotoate
        self.transform.set_rotation(s[0])  # pointing up is zero

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #7
0
    def get_viewer(self):
        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)
            rod = rendering.make_capsule(1, .2)
            # rod.set_color(.8, .3, .3)
            if self.color == 'red':
                rod.set_color(.8, .3, .3)
            elif self.color == 'blue':
                rod.set_color(.288, .466, 1.)
            else:
                print('pendulum color err!!!!')
                input()
                return
            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            self.viewer.add_geom(rod)
            axle = rendering.make_circle(.05)
            axle.set_color(0, 0, 0)
            self.viewer.add_geom(axle)
            fname = "/home/wmingd/anaconda3/envs/GAIL/lib/python3.5/site-packages/gym/envs/classic_control/assets/clockwise.png"
            # fname = path.join(path.dirname(__file__), "assets/clockwise.png")
            self.img = rendering.Image(fname, 1., 1.)
            self.imgtrans = rendering.Transform()
            self.img.add_attr(self.imgtrans)

        return self.viewer
コード例 #8
0
    def render(env, mode='human', side=28):
        if env.env.viewer is None:
            from gym.envs.classic_control import rendering
            env.env.viewer = rendering.Viewer(side, side)
            env.env.viewer.set_bounds(-1.2, 1.2, -1.2, 1.2)
            rod = rendering.make_capsule(1, .3)
            rod.set_color(.8, .3, .3)
            env.env.pole_transform = rendering.Transform()
            rod.add_attr(env.env.pole_transform)
            env.env.viewer.add_geom(rod)
            axle = rendering.make_circle(.05)
            axle.set_color(0, 0, 0)
            env.env.viewer.add_geom(axle)
            fname = path.join(
                path.dirname(
                    '/home/christine/projects/hamiltonian/lagrangian-nn/'),
                "assets/clockwise.png")
            env.env.img = rendering.Image(fname, 1., 1.)
            env.env.imgtrans = rendering.Transform()
            env.env.img.add_attr(env.env.imgtrans)

        env.env.viewer.add_onetime(env.env.img)
        env.env.pole_transform.set_rotation(env.env.state[0] + np.pi / 2)
        if env.env.last_u:
            env.env.imgtrans.scale = (-env.env.last_u / 2,
                                      np.abs(env.env.last_u) / 2)

        return env.env.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #9
0
    def render(self, mode='human'):

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)
            rod = rendering.make_capsule(1, .2)
            rod.set_color(.8, .3, .3)
            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            self.viewer.add_geom(rod)
            axle = rendering.make_circle(.05)
            axle.set_color(0, 0, 0)
            self.viewer.add_geom(axle)

            # YOU MUST GIVE THE PATH OF THE IMAGE FILE TO THE ARROW IN THE OPEN AI GYM INSTALATION
            # fname='/home/paulo/.conda/envs/PythonControl/lib/python3.6/site-packages/gym/envs/classic_control/assets/clockwise.png'
            fname = './clockwise.png'
            # fname = path.join(path.dirname(__file__), "assets/clockwise.png")
            self.img = rendering.Image(fname, 1., 1.)
            self.imgtrans = rendering.Transform()
            self.img.add_attr(self.imgtrans)

        self.viewer.add_onetime(self.img)
        self.pole_transform.set_rotation(self.state[0] + np.pi / 2)
        if self.last_u:
            self.imgtrans.scale = (-self.last_u / 2, np.abs(self.last_u) / 2)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #10
0
 def make_oval(self, c1, c2, c3):
     temp = rendering.make_capsule(self.oval_length, self.oval_width)
     temptrans = rendering.Transform()
     temp.add_attr(temptrans)
     temp.set_color(c1, c2, c3)
     self.viewer.add_geom(temp)
     return temptrans
コード例 #11
0
    def render(self, mode='human', title=None):
        data = np.array(self.hist)
        x = np.arange(data.shape[0])
        plt.plot(x, data[:, 0], label="Current")
        plt.plot(x, data[:, 1], label="Desired")
        plt.title(title)
        plt.legend()
        plt.show()

        return
        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)
            rod = rendering.make_capsule(1, .2)
            rod.set_color(.8, .3, .3)
            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            self.viewer.add_geom(rod)
            axle = rendering.make_circle(.05)
            axle.set_color(0, 0, 0)
            self.viewer.add_geom(axle)
            fname = path.join(path.dirname(__file__), "assets/clockwise.png")
            self.img = rendering.Image(fname, 1., 1.)
            self.imgtrans = rendering.Transform()
            self.img.add_attr(self.imgtrans)

        self.viewer.add_onetime(self.img)
        self.pole_transform.set_rotation(self.state[0] + np.pi / 2)
        if self.last_u:
            self.imgtrans.scale = (-self.last_u / 2, np.abs(self.last_u) / 2)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #12
0
 def render(self, mode='human'):
     view_xmax, = 1.1 * self.max_position
     if self.viewer is None:
         # Initialize gym rendering
         from gym.envs.classic_control import rendering
         self.viewer = rendering.Viewer(500, 500)
         self.viewer.set_bounds(-view_xmax, view_xmax, -view_xmax, view_xmax)
          # add body to represent state
         body = rendering.make_circle(0.25)
         body.set_color(0.3, 0.8, 0.3)
         self.body_transform = rendering.Transform()
         body.add_attr(self.body_transform)
         self.viewer.add_geom(body)
         # add body to represent action
         line = rendering.make_capsule(1.0, 0.2)
         line.set_color(0.8, 0.3, 0.8)
         self.line_transform = rendering.Transform()
         line.add_attr(self.line_transform)
         self.viewer.add_geom(line)
     
     # add crosshairs to indicate actions lines and center
     self.viewer.draw_line((0., -view_xmax), (0., view_xmax))
     self.viewer.draw_line((-view_xmax, 0.), (view_xmax, 0.))
     # display state for body transform
     x, = self.state
     self.body_transform.set_translation(x, 0)
     # display state for action transform
     if self.last_u:
         ux, = self.last_u
         ux_normalized,  = self.last_u / self.max_speed
         self.line_transform.set_translation(x + 0.5 * np.sign(ux), 0)
         self.line_transform.set_scale(ux_normalized, 1.0)
     
     return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #13
0
    def render(self, mode="human"):
        """Render pendulum."""
        if self.viewer is None:
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)
            rod = rendering.make_capsule(1, 0.2)
            rod.set_color(0.8, 0.3, 0.3)
            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            self.viewer.add_geom(rod)
            axle = rendering.make_circle(0.05)
            axle.set_color(0, 0, 0)
            self.viewer.add_geom(axle)
            fname = os.path.dirname(
                rendering.__file__) + "/assets/clockwise.png"
            self.img = rendering.Image(fname, 1.0, 1.0)
            self.imgtrans = rendering.Transform()
            self.img.add_attr(self.imgtrans)

        self.viewer.add_onetime(self.img)
        self.pole_transform.set_rotation(self.state[0] + np.pi / 2)
        if self.last_action:
            self.imgtrans.scale = (-self.last_action / 2,
                                   np.abs(self.last_action) / 2)

        return self.viewer.render(return_rgb_array=mode == "rgb_array")
コード例 #14
0
    def __call__(self, trajectory):
        mode = 'human'
        for timeStep in range(len(trajectory)):
            state = trajectory[timeStep][0]
            action = trajectory[timeStep][1]
            action = np.clip(action, -self.max_torque, self.max_torque)[0]

            if self.viewer is None:
                from gym.envs.classic_control import rendering
                self.viewer = rendering.Viewer(500, 500)
                self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)
                rod = rendering.make_capsule(1, .2)
                rod.set_color(.8, .3, .3)
                self.pole_transform = rendering.Transform()
                rod.add_attr(self.pole_transform)
                self.viewer.add_geom(rod)
                axle = rendering.make_circle(.05)
                axle.set_color(0, 0, 0)
                self.viewer.add_geom(axle)
                fname = path.join(path.dirname(__file__), "assets/clockwise.png")
                self.img = rendering.Image(fname, 1., 1.)
                self.imgtrans = rendering.Transform()
                self.img.add_attr(self.imgtrans)

            self.viewer.add_onetime(self.img)
            self.pole_transform.set_rotation(state[0] + np.pi / 2)
            self.imgtrans.scale = (-action/ 2, np.abs(action) / 2)

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

        if self.viewer:
            self.viewer.close()
            self.viewer = None
        return
コード例 #15
0
ファイル: pendulum.py プロジェクト: Aishunk/gym
    def _render(self, mode='human', close=False):
        if close:
            if self.viewer is not None:
                self.viewer.close()
                self.viewer = None
            return

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500,500)
            self.viewer.set_bounds(-2.2,2.2,-2.2,2.2)
            rod = rendering.make_capsule(1, .2)
            rod.set_color(.8, .3, .3)
            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            self.viewer.add_geom(rod)
            axle = rendering.make_circle(.05)
            axle.set_color(0,0,0)
            self.viewer.add_geom(axle)
            fname = path.join(path.dirname(__file__), "assets/clockwise.png")
            self.img = rendering.Image(fname, 1., 1.)
            self.imgtrans = rendering.Transform()
            self.img.add_attr(self.imgtrans)

        self.viewer.add_onetime(self.img)
        self.pole_transform.set_rotation(self.state[0] + np.pi/2)
        if self.last_u:
            self.imgtrans.scale = (-self.last_u/2, np.abs(self.last_u)/2)

        return self.viewer.render(return_rgb_array = mode=='rgb_array')
コード例 #16
0
    def _render_human(self):
        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(600, 600)
            self.viewer.set_bounds(-220, +220, -220, +220)

            truck = rendering.make_capsule(8, 4)
            truck.set_color(0.0, 0.0, 0.0)
            self.truck_transform = rendering.Transform()
            truck.add_attr(self.truck_transform)
            self.viewer.add_geom(truck)

            for node in self.policeman.map['nodes'].values():
                circle = rendering.make_circle(2)
                circle.set_color(0.6, 0.6, 0.6)
                dot_transform = rendering.Transform(
                    (node['position']['x'], -node['position']['z']))
                circle.add_attr(dot_transform)
                self.viewer.add_geom(circle)

        position, orientation = self.data.worldPlacement.position, self.data.worldPlacement.orientation
        self.truck_transform.set_rotation(orientation.heading * math.pi * 2 -
                                          math.pi / 2)
        self.truck_transform.set_translation(position.x, -position.z)
        return self.viewer.render()
コード例 #17
0
    def render(self, mode='human'):

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)
            rod = rendering.make_capsule(1, .2)
            rod.set_color(.8, .3, .3)
            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            self.viewer.add_geom(rod)
            axle = rendering.make_circle(.05)
            axle.set_color(0, 0, 0)
            self.viewer.add_geom(axle)
            fname = path.join(path.dirname(__file__), "assets/clockwise.png")
            self.img = rendering.Image(fname, 1., 1.)
            self.imgtrans = rendering.Transform()
            self.img.add_attr(self.imgtrans)

        self.viewer.add_onetime(self.img)
        self.pole_transform.set_rotation(self.state[0] + np.pi / 2)
        if self.last_u:
            self.imgtrans.scale = (-self.last_u / 2, np.abs(self.last_u) / 2)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #18
0
    def render(self, mode='human'):
        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)
            rod = rendering.make_capsule(1, .2)
            rod.set_color(.8, .3, .3)
            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            self.viewer.add_geom(rod)
            axle = rendering.make_circle(.05)
            axle.set_color(0, 0, 0)
            self.viewer.add_geom(axle)
            # I've commented out the lines that plot the angular direction of the control
            # to uncomment these, I need to relocate the file clockwise.png to a relative location

            #fname = path.join(path.dirname(__file__), "assets/clockwise.png")
            #self.img = rendering.Image(fname, 1., 1.)
            #self.imgtrans = rendering.Transform()
            #self.img.add_attr(self.imgtrans)

        #self.viewer.add_onetime(self.img)
        self.pole_transform.set_rotation(self.state[0] + np.pi / 2)
        #if self.last_u:
        #    self.imgtrans.scale = (-self.last_u/2, np.abs(self.last_u)/2)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #19
0
    def render_goal_2(self, goal1, goal2, end_goal, mode='human'):

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)

            rod = rendering.make_capsule(1, .2)
            rod.set_color(.8, .3, .3)
            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            self.viewer.add_geom(rod)

            ################ goal 1 ################
            rod1 = rendering.make_goal_circ(1, .1)
            rod1.set_color(.8, .8, .3)
            self.pole_transform1 = rendering.Transform()
            rod1.add_attr(self.pole_transform1)
            self.viewer.add_geom(rod1)
            ########################################

            ################ goal 2 ################
            rod2 = rendering.make_goal_circ(1, .1)
            rod2.set_color(.3, .8, .3)
            self.pole_transform2 = rendering.Transform()
            rod2.add_attr(self.pole_transform2)
            self.viewer.add_geom(rod2)
            ########################################

            ############### End Goal ###############
            rod3 = rendering.make_goal_circ(1, .1)
            rod3.set_color(.3, .3, .8)
            self.pole_transform3 = rendering.Transform()
            rod3.add_attr(self.pole_transform3)
            self.viewer.add_geom(rod3)
            ########################################

            axle = rendering.make_circle(.05)
            axle.set_color(0, 0, 0)
            self.viewer.add_geom(axle)
            fname = path.join(path.dirname(__file__), "assets/clockwise.png")
            self.img = rendering.Image(fname, 1., 1.)
            self.imgtrans = rendering.Transform()
            self.img.add_attr(self.imgtrans)

#     self.viewer.add_onetime(self.img)

        self.pole_transform.set_rotation(self.state[0] + np.pi / 2)

        self.pole_transform1.set_rotation(goal1[0] + np.pi / 2)

        self.pole_transform2.set_rotation(goal2[0] + np.pi / 2)

        self.pole_transform3.set_rotation(end_goal[0] + np.pi / 2)

        if self.last_u:
            self.imgtrans.scale = (-self.last_u / 2, np.abs(self.last_u) / 2)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #20
0
ファイル: nservoarm.py プロジェクト: rstager/rnr
    def _render(self, mode='human', close=False):
        if close:
            if self.viewer is not None:
                self.viewer.close()
                self.viewer = None
            return

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(self.width, self.height)
            self.viewer.set_bounds(*self.bounds)
            self.pole_transforms = []

            for i, l in enumerate(self.links):
                rod = rendering.make_capsule(l, 0.2 / (i + 1))
                rod.set_color(.3, .8, .3)
                transform = rendering.Transform()
                rod.add_attr(transform)
                self.pole_transforms.append(transform)
                self.viewer.add_geom(rod)

            axle = rendering.make_circle(.05)
            axle.set_color(0, 0, 0)
            self.viewer.add_geom(axle)

            if self.bar:
                goal = rendering.make_capsule(self.deadband * 2, self.deadband)
                goal.set_color(1, 0, 0)
            else:
                goal = rendering.make_circle(self.deadband)
                goal.set_color(1, 0, 0)
            self.goal_transform = rendering.Transform()
            goal.add_attr(self.goal_transform)
            self.viewer.add_geom(goal)

        for px, x, y, t in zip(self.pole_transforms, *self.node_pos()):
            # there is a race condition of some sort, because
            px.set_rotation(t)
            px.set_translation(x, y)

        self.goal_transform.set_translation(self.goals[self.goalidx][0],
                                            self.goals[self.goalidx][1])
        if self.bar:
            self.goal_transform.set_rotation(self.goals[self.goalidx][2])

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #21
0
    def render(self, mode='human'):

        Tmin_rod = -30.
        Tmax_rod = 100.

        if self.viewer is None:
            self.fig, self.ax_temperature = plt.subplots()
            self.ax_power = self.ax_temperature.twinx()
            plt.show(block=False)

            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)

            rod_target = rendering.make_capsule(1, .04)
            rod_target.set_color(.2, .7, .7)
            self.transform_rod_target = rendering.Transform()
            rod_target.add_attr(self.transform_rod_target)
            self.transform_rod_target.set_rotation(
                (self.T_target - Tmin_rod) / (Tmax_rod - Tmin_rod) * np.pi * 2)

            self.viewer.add_geom(rod_target)

            rod = rendering.make_capsule(1, .04)
            rod.set_color(.8, .3, .3)
            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            self.viewer.add_geom(rod)

        self.viewer.draw_circle(1, filled=False)
        self.pole_transform.set_rotation(
            (self.T - Tmin_rod) / (Tmax_rod - Tmin_rod) * np.pi * 2)

        if self.current_step % 100 == 0:
            self.ax_temperature.clear()
            self.ax_power.clear()
            self.ax_temperature.axhline(self.T_target, color='C0', ls='--')
            self.ax_temperature.axhline(self.Text, color='0.5', ls='--')
            self.ax_temperature.plot(self.temperature_history, 'C0')
            self.ax_power.plot(self.power_history, 'oC1')
            self.ax_temperature.set_xlim(0, 1000)
            self.ax_temperature.set_ylim(-20, 25)
            plt.pause(0.001)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #22
0
ファイル: _reacher.py プロジェクト: hubayirp/deluca
    def render(self, mode="human"):
        if self.viewer is None:
            from gym.envs.classic_control import rendering

            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)

            rod1 = rendering.make_capsule(self.l1, 0.2)
            rod1.set_color(0.8, 0.3, 0.3)
            self.pole_transform1 = rendering.Transform()
            rod1.add_attr(self.pole_transform1)
            self.viewer.add_geom(rod1)

            rod2 = rendering.make_capsule(self.l2, 0.2)
            rod2.set_color(0.3, 0.3, 0.8)
            self.pole_transform2 = rendering.Transform()
            rod2.add_attr(self.pole_transform2)
            self.viewer.add_geom(rod2)

            axle1 = rendering.make_circle(0.05)
            axle1.set_color(0, 0, 0)
            self.viewer.add_geom(axle1)

            axle2 = rendering.make_circle(0.05)
            axle2.set_color(0, 0, 0)
            self.pole_transform3 = rendering.Transform()
            axle2.add_attr(self.pole_transform3)
            self.viewer.add_geom(axle2)

            axle3 = rendering.make_circle(0.1)
            axle3.set_color(0, 0.5, 0)
            self.pole_transform4 = rendering.Transform()
            axle3.add_attr(self.pole_transform4)
            self.viewer.add_geom(axle3)

        self.pole_transform1.set_rotation(self.state[0])
        E1X, E1Y = self.l1 * np.cos(self.state[0]), self.l1 * np.sin(
            self.state[0])
        self.pole_transform2.set_translation(E1X, E1Y)
        self.pole_transform2.set_rotation(self.state[0] + self.state[1])
        self.pole_transform3.set_translation(E1X, E1Y)
        self.pole_transform4.set_translation(self.goal_coord[0],
                                             self.goal_coord[1])
        return self.viewer.render(return_rgb_array=mode == "rgb_array")
コード例 #23
0
    def _render(self, mode='human', close=False):
        if close:
            if self.viewer is not None:
                self.viewer.close()
                self.viewer = None
            return

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(self.XMIN, self.XMAX, self.YMIN, self.YMAX)

            self.goal.shape = rendering.make_circle(self.goal.r)
            self.goal.shape.set_color(0.5, 1, 0.5)
            self.goal.transform = rendering.Transform()
            self.goal.shape.add_attr(self.goal.transform)
            self.viewer.add_geom(self.goal.shape)

            for obstacle in self.obstacles:
                obstacle.shape = rendering.make_circle(obstacle.r)
                obstacle.shape.set_color(0, 0, 0)
                obstacle.transform = rendering.Transform()
                obstacle.shape.add_attr(obstacle.transform)
                self.viewer.add_geom(obstacle.shape)

            self.quad.shape = rendering.make_circle(self.quad.r)
            self.quad.shape.set_color(0, 0, 1)
            self.quad.transform = rendering.Transform()
            self.quad.shape.add_attr(self.quad.transform)
            self.viewer.add_geom(self.quad.shape)

            self.accel_shape = rendering.make_capsule(1.0, 0.3)
            self.accel_shape.set_color(1, 0, 0)
            self.accel_shape_transform = rendering.Transform()
            self.accel_shape.add_attr(self.accel_shape_transform)
            self.viewer.add_geom(self.accel_shape)

        self.quad.transform.set_translation(self.quad.x, self.quad.y)
        self.goal.transform.set_translation(self.goal.x, self.goal.y)

        for obstacle in self.obstacles:
            obstacle.transform.set_translation(*obstacle.state())

        if self.last_u is not None:
            ux, uy = self.last_u
            u_mag = np.sqrt(ux**2 + uy**2)
            self.accel_shape_transform.set_translation(self.quad.x,
                                                       self.quad.y)
            self.accel_shape_transform.set_scale(u_mag, 1.0)
            self.accel_shape_transform.set_rotation(math.atan2(uy, ux))

        self.viewer.render()
        if mode == 'rgb_array':
            return self.viewer.get_array()
        elif mode == 'human':
            pass
コード例 #24
0
ファイル: quad_env.py プロジェクト: hmartiro/gym
    def _render(self, mode='human', close=False):
        if close:
            if self.viewer is not None:
                self.viewer.close()
                self.viewer = None
            return

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(self.XMIN, self.XMAX, self.YMIN, self.YMAX)

            self.goal.shape = rendering.make_circle(self.goal.r)
            self.goal.shape.set_color(0.5, 1, 0.5)
            self.goal.transform = rendering.Transform()
            self.goal.shape.add_attr(self.goal.transform)
            self.viewer.add_geom(self.goal.shape)

            for obstacle in self.obstacles:
                obstacle.shape = rendering.make_circle(obstacle.r)
                obstacle.shape.set_color(0, 0, 0)
                obstacle.transform = rendering.Transform()
                obstacle.shape.add_attr(obstacle.transform)
                self.viewer.add_geom(obstacle.shape)

            self.quad.shape = rendering.make_circle(self.quad.r)
            self.quad.shape.set_color(0, 0, 1)
            self.quad.transform = rendering.Transform()
            self.quad.shape.add_attr(self.quad.transform)
            self.viewer.add_geom(self.quad.shape)

            self.accel_shape = rendering.make_capsule(1.0, 0.3)
            self.accel_shape.set_color(1, 0, 0)
            self.accel_shape_transform = rendering.Transform()
            self.accel_shape.add_attr(self.accel_shape_transform)
            self.viewer.add_geom(self.accel_shape)

        self.quad.transform.set_translation(self.quad.x, self.quad.y)
        self.goal.transform.set_translation(self.goal.x, self.goal.y)

        for obstacle in self.obstacles:
            obstacle.transform.set_translation(*obstacle.state())

        if self.last_u is not None:
            ux, uy = self.last_u
            u_mag = np.sqrt(ux**2 + uy**2)
            self.accel_shape_transform.set_translation(self.quad.x, self.quad.y)
            self.accel_shape_transform.set_scale(u_mag, 1.0)
            self.accel_shape_transform.set_rotation(math.atan2(uy, ux))

        self.viewer.render()
        if mode == 'rgb_array':
            return self.viewer.get_array()
        elif mode == 'human':
            pass
コード例 #25
0
ファイル: pendulum.py プロジェクト: ivy-dl/gym
    def render(self, mode='human'):
        """
        Renders the environment.
        The set of supported modes varies per environment. (And some
        environments do not support rendering at all.) By convention,
        if mode is:

        - human: render to the current display or terminal and
          return nothing. Usually for human consumption.
        - rgb_array: Return an numpy.ndarray with shape (x, y, 3),
          representing RGB values for an x-by-y pixel image, suitable
          for turning into a video.
        - ansi: Return a string (str) or StringIO.StringIO containing a
          terminal-style text representation. The text can include newlines
          and ANSI escape sequences (e.g. for colors).

        :param mode: Render mode, one of [human|rgb_array], default human
        :type mode: str, optional
        :return: Rendered image.
        """
        if self.viewer is None:
            # noinspection PyBroadException
            try:
                from gym.envs.classic_control import rendering
            except:
                if not self._logged_headless_message:
                    print(
                        'Unable to connect to display. Running the Ivy environment in headless mode...'
                    )
                    self._logged_headless_message = True
                return

            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)

            # Pole.
            self.pole_geom = rendering.make_capsule(1, .2)
            self.pole_geom.set_color(.8, .3, .3)
            self.pole_transform = rendering.Transform()
            self.pole_geom.add_attr(self.pole_transform)
            self.viewer.add_geom(self.pole_geom)

            # Axle.
            axle = rendering.make_circle(0.05)
            axle.set_color(0., 0., 0.)
            self.viewer.add_geom(axle)

        self.pole_transform.set_rotation(
            ivy.to_numpy(self.angle)[0] + np.pi / 2)
        rew = ivy.to_numpy(self.get_reward())[0]
        self.pole_geom.set_color(1 - rew, rew, 0.)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #26
0
    def render(self, mode='human'):
        if self.viewer is None:
            # Initialize gym rendering
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            view_xmax, view_ymax = 1.1 * self.max_position
            self.viewer.set_bounds(-view_xmax, view_xmax, -view_ymax,
                                   view_ymax)
            # vertical and horizontal lines for center
            vertical_line = rendering.Line((0., -view_ymax), (0., view_ymax))
            horizontal_line = rendering.Line((-view_xmax, 0.), (view_xmax, 0.))
            self.viewer.add_geom(vertical_line)
            self.viewer.add_geom(horizontal_line)
            # add obstacles
            for xo, ro in self.obstacles:
                obstacle = rendering.make_circle(ro)
                obstacle.set_color(0.8, 0.3, 0.3)
                xo_x, xo_y = xo
                obstacle_transform = rendering.Transform(translation=(xo_x,
                                                                      xo_y))
                obstacle.add_attr(obstacle_transform)
                self.viewer.add_geom(obstacle)
            # add body to represent state
            body = rendering.make_circle(self.body_radius)
            body.set_color(0.3, 0.8, 0.3)
            self.body_transform = rendering.Transform()
            body.add_attr(self.body_transform)
            self.viewer.add_geom(body)
            # add body to represent action
            line = rendering.make_capsule(1.0, 0.2)
            line.set_color(0.8, 0.3, 0.8)
            self.line_transform = rendering.Transform()
            line.add_attr(self.line_transform)
            self.viewer.add_geom(line)

        x, y = self.state

        # display state for body transform
        self.body_transform.set_translation(x, y)
        # display state for action transform
        if not self.last_u is None:

            u_mag = np.linalg.norm(self.last_u)
            ux, uy = self.last_u
            u_theta = np.arctan2(uy, ux)
            self.line_transform.set_scale(u_mag / self.max_speed_norm, 1.0)
            self.line_transform.set_rotation(u_theta)
            self.line_transform.set_translation(x + 0.25 * np.cos(u_theta),
                                                y + 0.25 * np.sin(u_theta))
        else:
            self.line_transform.set_scale(0.0, 0.0)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #27
0
    def render_goal(self, goal, end_goal, mode='human'):

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)

            ################ goal ################
            rod = rendering.make_capsule(1, .2)
            rod.set_color(.8, .3, .3)
            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            self.line0, self.arrow0 = self.create_arrow(rendering, self.pole_transform, self.state[1])

            self.viewer.add_geom(rod)
            self.viewer.add_geom(self.line0)
            self.viewer.add_geom(self.arrow0)
            ######################################

            ################ goal ################
            rod1, self.pole_transform1 = self.create_rod(rendering, 1, .1, .8, .8, .3)
            self.line1, self.arrow1 = self.create_arrow(rendering, self.pole_transform1, goal[1])

            self.viewer.add_geom(rod1)
            self.viewer.add_geom(self.line1)
            self.viewer.add_geom(self.arrow1)
            ######################################

            ############## End Goal ##############
            rod2, self.pole_transform2 = self.create_rod(rendering, 1, .1, .3, .3, .8)
            self.viewer.add_geom(rod2)
            ######################################

            axle = rendering.make_circle(.05)
            axle.set_color(0, 0, 0)
            self.viewer.add_geom(axle)
            fname = path.join(path.dirname(__file__), "assets/clockwise.png")
            self.img = rendering.Image(fname, 1., 1.)
            self.imgtrans = rendering.Transform()
            self.img.add_attr(self.imgtrans)

        self.pole_transform.set_rotation(self.state[0] + np.pi / 2)
        self.pole_transform1.set_rotation(goal[0] + np.pi / 2)
        self.pole_transform2.set_rotation(end_goal[0] + np.pi / 2)

        self.update_arrow(self.line0, self.arrow0, self.state[1])
        self.update_arrow(self.line1, self.arrow1, goal[1])

        if self.last_u:
            self.imgtrans.scale = (-self.last_u / 2, np.abs(self.last_u) / 2)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #28
0
    def render(self, mode='human'):

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)
            rod = rendering.make_capsule(1, .2)
            rod.set_color(.8, .3, .3)
            # ...

        # Do rendering stuffs...

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #29
0
    def geom(self):
        geom = rendering.make_capsule(length=20, width=20)
        geom.set_color(1, 0, 0)
        geom.add_attr(
            rendering.Transform(translation=self.arr(),
                                rotation=0.0,
                                scale=(1, 1)))
        geom.add_attr(
            rendering.Transform(translation=(-10.0, 0),
                                rotation=0.0,
                                scale=(1, 1)))  # center the pill

        return geom
コード例 #30
0
    def render(self, mode='human'):
        if not self.render_init:
            self.render_init = True
            from gym.envs.classic_control import rendering
            # rod
            rod = rendering.make_capsule(1, .2)
            rod.set_color(.8, .3, .3)
            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            # axle
            axle = rendering.make_circle(.05)
            axle.set_color(0, 0, 0)

            self.viewer = rendering.Viewer(*self.render_shape)
            self.viewer.set_bounds(-1.2, 1.2, -1.2, 1.2)
            self.viewer.add_geom(rod)
            if self.model is not None:
                zaxis = rendering.Line((0, 0), (0, 1.2))
                th_axis = rendering.Line((0, 0), (1.2, 0))
                self.pole_pred_transform = rendering.Transform()
                th_axis.add_attr(self.pole_pred_transform)
                self.viewer.add_geom(zaxis)
                self.viewer.add_geom(th_axis)
            else:
                self.pole_pred_transform = None
            self.viewer.add_geom(axle)

        self.pole_transform.set_rotation(self.state[0] + np.pi / 2)

        if self.pole_pred_transform is not None:
            image = self.viewer.render(return_rgb_array=True)
            kwargs = dict(output_shape=(28, 28),
                          mode='edge',
                          order=1,
                          preserve_range=True)
            observation = skimage.transform.resize(image, **kwargs).astype(
                np.float32) / 255
            model_output = self.model.predict(observation[None, :])[0]
            if len(model_output) == 1:
                th_pred = model_output[0]
            elif len(model_output) == 2:
                th_pred = np.arctan2(model_output[0], model_output[1])
            else:
                raise ValueError(
                    'Model output not compatible with simulator. Model should only output [theta] or [sin(theta), cos(theta)] prepended with the batch dimension.'
                )
            self.pole_pred_transform.set_rotation(th_pred + np.pi / 2)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #31
0
    def render(self, mode='human'):
        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)

            self.angle_label = pyglet.text.Label('0000',
                                                 font_size=20,
                                                 x=10 * 2.2 + 40,
                                                 y=500 * 2.2 + 40,
                                                 anchor_x='left',
                                                 anchor_y='center',
                                                 color=(0, 255, 0, 255))
            self.viewer.add_geom(DrawText(self.angle_label))

            rod = rendering.make_capsule(1, .2)
            rod.set_color(.8, .3, .3)

            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            self.viewer.add_geom(rod)
            axle = rendering.make_circle(.05)
            axle.set_color(0, 0, 0)
            self.viewer.add_geom(axle)

            fname = path.join(path.dirname(__file__), "assets/clockwise.png")
            self.img = rendering.Image(fname, 1., 1.)
            self.imgtrans = rendering.Transform()
            self.img.add_attr(self.imgtrans)

        self.viewer.add_onetime(self.img)
        # self.pole_transform.set_rotation( 90 + np.pi / 2 )
        # import time
        # for deg in range(360):
        #     print(deg)
        self.deg = 0
        # self.deg = self.deg / 180 * np.pi
        # self.deg = self.deg + (np.pi / 2)
        # self.pole_transform.set_rotation(np.deg2rad(self.deg) + (np.pi / 2))
        # self.deg = self.deg + 1
        # self.deg = self.deg%360

        # print(self.deg)
        if self.last_u:
            self.imgtrans.scale = (-self.last_u / 2, np.abs(self.last_u) / 2)

        self.angle_label.text = "10"

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #32
0
    def render(self, mode='human'):
        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 250)
            self.viewer.set_bounds(-2.0, 2.0, -1.0, 1.0)

            agent = rendering.make_capsule(.6, .8)
            agent.set_color(.8, .3, .3)
            self.agent_transform = rendering.Transform()
            agent.add_attr(self.agent_transform)

            self.viewer.add_geom(agent)

        self.agent_transform.set_translation(self.state[0], 0.)
        return self.viewer.render(return_rgb_array=(mode == 'rgb_array'))
コード例 #33
0
ファイル: pendulum_pixel.py プロジェクト: mntan3/gym
    def render(self, mode='rgb_array'):
        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(500, 500)
            self.viewer.set_bounds(-2.2, 2.2, -2.2, 2.2)
            rod = rendering.make_capsule(1, .2)
            rod.set_color(.8, .3, .3)
            self.pole_transform = rendering.Transform()
            rod.add_attr(self.pole_transform)
            self.viewer.add_geom(rod)
            axle = rendering.make_circle(.05)
            axle.set_color(0, 0, 0)
            self.viewer.add_geom(axle)

        self.pole_transform.set_rotation(self.state[0] + np.pi / 2)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #34
0
ファイル: hockey2.py プロジェクト: ataitler/DQN
	def _render(self, mode='human', close=False):
		if close:
			if self.viewer is not None:
				self.viewer.close()
				self.viewer = None
			return

		if self.viewer is None:
			from gym.envs.classic_control import rendering
			self.viewer = rendering.Viewer(700,700)
			self.viewer.set_bounds(-80, 80, -80, 80)
			
			# left wall
			left_wall = rendering.make_capsule(self.max_x*2,1)
			left_wall.set_color(0.5,0.3,0.3)
			left_wall_transform = rendering.Transform()
			left_wall.add_attr(left_wall_transform)
			self.viewer.add_geom(left_wall)
			left_wall_transform.set_rotation(np.pi/2)
			left_wall_transform.set_translation(-self.max_y,-self.max_x)
			
			# right wall
			right_wall = rendering.make_capsule(self.max_x*2,1)
			right_wall.set_color(0.5,0.3,0.3)
			right_wall_transform = rendering.Transform()
			right_wall.add_attr(right_wall_transform)
			self.viewer.add_geom(right_wall)
			right_wall_transform.set_rotation(np.pi/2)
			right_wall_transform.set_translation(self.max_y,-self.max_x)
			
			# upper wall (puck's side)
			upper_wall = rendering.make_capsule(self.max_y*2,1)
			upper_wall.set_color(0.5,0.3,0.3)
			upper_wall_transform = rendering.Transform()
			upper_wall.add_attr(upper_wall_transform)
			self.viewer.add_geom(upper_wall)
			upper_wall_transform.set_translation(-self.max_y,-self.max_x)

			# upper goal
			upper_goal = rendering.make_capsule(self.goal_len,1)
			upper_goal.set_color(1.,1.,1.)
			upper_goal_transform = rendering.Transform()
			upper_goal.add_attr(upper_goal_transform)
			self.viewer.add_geom(upper_goal)
			upper_goal_transform.set_translation(-self.goal_len/2., -self.max_x)

			# lower wall (agent's side)
			lower_wall = rendering.make_capsule(self.max_y*2,1)
			lower_wall.set_color(0.5,0.3,0.3)
			lower_wall_transform = rendering.Transform()
			lower_wall.add_attr(lower_wall_transform)
			self.viewer.add_geom(lower_wall)
			lower_wall_transform.set_translation(-self.max_y,self.max_x)

			# lower goal
			lower_goal = rendering.make_capsule(self.goal_len,1)
			lower_goal.set_color(1.,1.,1.)
			lower_goal_transform = rendering.Transform()
			lower_goal.add_attr(lower_goal_transform)
			self.viewer.add_geom(lower_goal)
			lower_goal_transform.set_translation(-self.goal_len/2., self.max_x)
			
			# middle line
			middle_line = rendering.make_capsule(self.max_y*2,1)
			middle_line.set_color(0.1,0.3,0.3)
			middle_line_transform = rendering.Transform()
			middle_line.add_attr(middle_line_transform)
			self.viewer.add_geom(middle_line)
			middle_line_transform.set_translation(-self.max_y,0.)

			middle_circle_big = rendering.make_circle(self.puckR+0.2)
			middle_circle_big.set_color(0.1,0.3,0.3)
			middle_circle_big_transform = rendering.Transform()
			middle_circle_big.add_attr(middle_circle_big_transform)
			self.viewer.add_geom(middle_circle_big)
			middle_circle_big_transform.set_translation(0.,0.)
			
			middle_circle_small = rendering.make_circle(self.puckR-0.5)
			middle_circle_small.set_color(1.,1.,1.)
			middle_circle_small_transform = rendering.Transform()
			middle_circle_small.add_attr(middle_circle_small_transform)
			self.viewer.add_geom(middle_circle_small)
			middle_circle_small_transform.set_translation(0.,0.)
			
			# objects
			puck = rendering.make_circle(self.puckR)
			puck.set_color=(.8,.3,.3)
			self.puck_transform = rendering.Transform()
			puck.add_attr(self.puck_transform)
			self.viewer.add_geom(puck)
			agent = rendering.make_circle(self.agentR)
			agent.set_color(.6,.3,.3)
			self.agent_transform = rendering.Transform()
			agent.add_attr(self.agent_transform)
			self.viewer.add_geom(agent)

		self.puck_transform.set_translation(self.state[5],self.state[4])
		self.agent_transform.set_translation(self.state[1],self.state[0])
		
		return self.viewer.render(return_rgb_array = mode=='rgb_array')