コード例 #1
0
    def render(self, mode='human'):
        from gym.envs.classic_control import rendering
        if self.viewer is None:
            self.viewer = rendering.Viewer(250, 100)
            self.viewer.set_bounds(-5.0, 5.0, -2.0, 2.0)
            w = 0.6; h = 0.4
            l, r, t, b = -w/2, w/2, h/2, -h/2
            box = rendering.make_polygon([(l,b), (l,t), (r,t), (r,b)])
            box.set_color(.8, .3, .3)
            self.box_transform = rendering.Transform()
            box.add_attr(self.box_transform)
            self.viewer.add_geom(box)
            
            line = rendering.make_polyline([(-5,-h/2), (5,-h/2)])
            line.set_color(0, 0, 0)
            self.viewer.add_geom(line)

        self.box_transform.set_translation(self.state[0], 0)
        if self.last_u is not None:
            arrow = rendering.make_polyline([(self.state[0], 0), 
                                             (self.state[0]+self.last_u, 0)])
            arrow.set_linewidth(2)
            self.viewer.add_onetime(arrow)
        
        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #2
0
ファイル: rendering.py プロジェクト: TSL-UOB/CAV-Gym
    def __init__(self, dynamic_body, ego, road):
        super().__init__(occlusion=dynamic_body, ego=ego)

        self.body = rendering.make_polygon(list(dynamic_body.bounding_box()))
        if dynamic_body is ego:
            self.body.set_color(*RGB.RED.value)

        self.focal_road = road
        if dynamic_body.bounding_box().intersects(
                self.focal_road.bounding_box()):
            self.road_angle = rendering.make_polyline(list())
        else:
            self.road_angle = rendering.make_polyline(
                list(dynamic_body.line_anchor(self.focal_road)))
        self.road_angle.set_color(*RGB.MAGENTA.value)

        if dynamic_body.target_spline:
            self.target_spline = rendering.make_polyline(
                [tuple(point) for point in dynamic_body.target_spline])
            self.target_spline.set_color(*RGB.GREEN.value)
        else:
            self.target_spline = None

        if dynamic_body.planner_spline:
            self.planner_spline = rendering.make_polyline(
                [tuple(point) for point in dynamic_body.planner_spline])
            self.planner_spline_markers = make_markers(
                dynamic_body.planner_spline)
        else:
            self.planner_spline = rendering.make_polyline(list())
            self.planner_spline_markers = make_markers(list())
        self.planner_spline.set_color(*RGB.BLUE.value)
        self.planner_spline_markers.set_color(*RGB.BLUE.value)
コード例 #3
0
ファイル: rendering.py プロジェクト: TSL-UOB/CAV-Gym
    def __init__(self, road_map, body):
        self.major_road_view = RoadView(road_map.major_road)
        self.minor_road_views = [
            RoadView(minor_road) for minor_road in road_map.minor_roads
        ] if road_map.minor_roads is not None else list()

        if self.minor_road_views:
            self.clear_intersections = rendering.Compound([
                rendering.make_polyline([
                    tuple(bounding_box.front_left),
                    tuple(bounding_box.front_right)
                ]) for bounding_box in road_map.intersection_bounding_boxes
            ])
            self.clear_intersections.set_color(*RGB.WHITE.value)

            self.intersection_markings = rendering.Compound([
                rendering.make_polyline([
                    tuple(bounding_box.rear_left),
                    tuple(bounding_box.rear_right)
                ]) for bounding_box in
                road_map.inbound_intersection_bounding_boxes
            ])
            self.intersection_markings.add_attr(
                mods.FactoredLineStyle(0x0F0F, 2))

        self.obstacle_view = None
        if road_map.obstacle is not None:
            self.obstacle_view = ObstacleView(road_map.obstacle, body)
コード例 #4
0
ファイル: RockSampleEnv.py プロジェクト: lsmcolab/adleap-mas
    def draw_grid(self):
        grid = []
        linewidth = 2
        for x in range(self.dim + 1):
            grid.append(
                rendering.make_polyline([
                    (x * self.draw_scale + self.draw_start_x,
                     self.draw_start_y),
                    (x * self.draw_scale + self.draw_start_x,
                     self.draw_start_y + self.dim * self.draw_scale)
                ]))
            grid[-1].set_linewidth(linewidth)
            self.viewer.add_geom(grid[-1])

        for y in range(self.dim + 1):
            grid.append(
                rendering.make_polyline([
                    (self.draw_start_x,
                     y * self.draw_scale + self.draw_start_y),
                    (self.draw_start_x + self.dim * self.draw_scale,
                     y * self.draw_scale + self.draw_start_y)
                ]))
            grid[-1].set_linewidth(linewidth)
            self.viewer.add_geom(grid[-1])

        return grid
コード例 #5
0
    def render(self, mode='human'):
        screen_width = 600
        screen_height = 400

        world_width = self.max_position - self.min_position
        scaler = screen_width/world_width

        COLORS = np.eye(3)
        upscale = 150

        if self.viewer is None:
            self.lines = []
            carwidth = 20
            carheight = 75
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(screen_width, screen_height)
            xs = np.linspace(self.min_position, self.max_position, 100)
            ys = np.ones(100)*screen_height / 2
            xys = list(zip((xs-self.min_position)*scaler, ys))

            self.track = rendering.make_polyline(xys)
            self.track.set_linewidth(4)
            self.viewer.add_geom(self.track)

            for i in range(3):
                ys = np.ones(100) * upscale * i + upscale / 2
                xys = list(zip((xs-self.min_position)*scaler, ys))

                zeros = rendering.make_polyline(xys)
                self.viewer.add_geom(zeros)

            l,r,t,b = -carwidth/2, carwidth/2, carheight, 0
            car = rendering.FilledPolygon([(l,b), (l,t), (r,t), (r,b)])
            car.add_attr(rendering.Transform(translation=(0, screen_height/2 - carheight/2)))
            self.cartrans = rendering.Transform()
            car.add_attr(self.cartrans)
            self.viewer.add_geom(car)

        pos = self.state[0]
        self.cartrans.set_translation((pos-self.min_position)*scaler, 0)
        
        if self.t > 1:
            from gym.envs.classic_control import rendering
            t = self.t % (screen_width / scaler)
            for i in range(3):
                prev, curr = self.prev_render_info[i], self.curr_render_info[i]
                line = rendering.Line(start=(scaler*(t-1), prev+upscale*i + upscale/2), end=(scaler*t, curr+upscale*i + upscale/2))
                line.set_color(*COLORS[i])
                line.linewidth.stroke = 2
                self.lines.append(line)
                self.viewer.add_geom(line)

            if t == 0:
                self.clear_render()

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

        world_width = self.max_position - self.min_position
        scale = screen_width / world_width
        carlength = self.L * scale
        carwidth = self.W * scale

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(screen_width, screen_height)

            xy_center = list(
                zip((self.track.X - self.min_position) * scale,
                    (self.track.Y - self.min_position) * scale))

            self.track_center = rendering.make_polyline(xy_center)
            self.track_center.set_linewidth(1)
            self.viewer.add_geom(self.track_center)

            xy_o = list(
                zip((self.track.X_boarder_outer - self.min_position) * scale,
                    (self.track.Y_boarder_outer - self.min_position) * scale))

            self.track_outer = rendering.make_polyline(xy_o)
            self.track_outer.set_linewidth(4)
            self.viewer.add_geom(self.track_outer)

            xy_i = list(
                zip((self.track.X_boarder_inner - self.min_position) * scale,
                    (self.track.Y_boarder_inner - self.min_position) * scale))

            self.track_inner = rendering.make_polyline(xy_i)
            self.track_inner.set_linewidth(4)
            self.viewer.add_geom(self.track_inner)

            clearance = 0

            l, r, t, b = -carlength / 2, carlength / 2, -carwidth / 2, carwidth / 2
            car = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)])
            car.add_attr(rendering.Transform(translation=(0, 0)))
            self.cartrans = rendering.Transform()
            car.add_attr(self.cartrans)
            car.set_color(1, 0, 0)
            self.viewer.add_geom(car)

        pos = self.track.fromLocaltoGlobal(self.state)
        self.cartrans.set_translation((pos[0] - self.min_position) * scale,
                                      (pos[1] - self.min_position) * scale)
        self.cartrans.set_rotation(pos[2])

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
    def render(self, mode='human'):
        '''
        This code was taken directly from openai implementation of mountain car just to test my equations in the step function. This was the *only* code taken from openai
        '''
        screen_width = 600
        screen_height = 400

        world_width = self.max_state[0] - self.min_state[0]
        scale = screen_width / world_width
        carwidth = 40
        carheight = 20

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(screen_width, screen_height)
            xs = np.linspace(self.min_state[0], self.max_state[0], 100)
            ys = self._height(xs)
            xys = list(zip((xs - self.min_state[0]) * scale, ys * scale))

            self.track = rendering.make_polyline(xys)
            self.track.set_linewidth(4)
            self.viewer.add_geom(self.track)

            clearance = 10

            l, r, t, b = -carwidth / 2, carwidth / 2, carheight, 0
            car = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)])
            car.add_attr(rendering.Transform(translation=(0, clearance)))
            self.cartrans = rendering.Transform()
            car.add_attr(self.cartrans)
            self.viewer.add_geom(car)
            frontwheel = rendering.make_circle(carheight / 2.5)
            frontwheel.set_color(.5, .5, .5)
            frontwheel.add_attr(
                rendering.Transform(translation=(carwidth / 4, clearance)))
            frontwheel.add_attr(self.cartrans)
            self.viewer.add_geom(frontwheel)
            backwheel = rendering.make_circle(carheight / 2.5)
            backwheel.add_attr(
                rendering.Transform(translation=(-carwidth / 4, clearance)))
            backwheel.add_attr(self.cartrans)
            backwheel.set_color(.5, .5, .5)
            self.viewer.add_geom(backwheel)
            flagx = (0.5 - self.min_state[0]) * scale
            flagy1 = self._height(0.5) * scale
            flagy2 = flagy1 + 50
            flagpole = rendering.Line((flagx, flagy1), (flagx, flagy2))
            self.viewer.add_geom(flagpole)
            flag = rendering.FilledPolygon([(flagx, flagy2),
                                            (flagx, flagy2 - 10),
                                            (flagx + 25, flagy2 - 5)])
            flag.set_color(.8, .8, 0)
            self.viewer.add_geom(flag)

        pos = self.state[0]
        self.cartrans.set_translation((pos - self.min_state[0]) * scale,
                                      self._height(pos) * scale)
        self.cartrans.set_rotation(np.cos(3 * pos))

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #8
0
ファイル: bee_env.py プロジェクト: xwinxu/gym-bee
  def render(self, mode='human'):
    """
    Render environment
    """
    from gym.envs.classic_control import rendering

    flower_width = 10
    flower_height = 10
    bee_width = 10
    bee_height = 10

    if self.viewer is None:
      self.viewer = rendering.Viewer(VIEWPORT_W, VIEWPORT_H)
    self.viewer.set_bounds(self.scroll, VIEWPORT_W / SCALE + self.scroll, 0,
                           VIEWPORT_H / SCALE)

    # add the boundary b/t observing bees and observers
    self.boundary = rendering.make_polyline()

    self.viewer.draw_polygon([
        (0, 0),
        (VIEWPORT_W / SCALE, 0),
        (VIEWPORT_W / SCALE, VIEWPORT_H / SCALE),
        (0, VIEWPORT_H / SCALE),
    ],
                             color=(0.04, 0.196, 0.125))
コード例 #9
0
    def render(self, mode='human'):
        if mode == 'ansi':
            return self._observation()
        elif mode == 'human':
            from gym.envs.classic_control import rendering
            if self.viewer is None:
                self.viewer = rendering.Viewer(self.screen_width, self.screen_height)

            #track the way, the agent has gone
            self.track_way = rendering.make_polyline(np.dot(self.positions, self.scale))
            self.track_way.set_linewidth(4)
            self.viewer.add_geom(self.track_way)

            # draw the agent
            car = rendering.make_circle(5)
            self.agent_trans = rendering.Transform()
            car.add_attr(self.agent_trans)
            car.set_color(0, 0, 255)
            self.viewer.add_geom(car)

            goal = rendering.make_circle(5)
            goal.add_attr(rendering.Transform(translation=(self.goal_x*self.scale, self.goal_y*self.scale)))
            goal.set_color(255, 0, 0)
            self.viewer.add_geom(goal)

            self.agent_trans.set_translation(self.agent_x * self.scale, self.agent_y * self.scale)

            return self.viewer.render(return_rgb_array=mode == 'rgb_array')
        elif mode == "rgb_array":
            super(Nav2dEnv, self).render(mode=mode)
        else:
            super(Nav2dEnv, self).render(mode=mode)
コード例 #10
0
    def render(self, mode='human'):
        screen_width = 600
        screen_height = 400

        world_width = self.x_threshold*2
        scale = screen_width/world_width
        carty = 100 # TOP OF CART
        polewidth = 10.0
        polelen = scale * (2 * self.length)
        cartwidth = 50.0
        cartheight = 30.0

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(screen_width, screen_height)
            xs = np.linspace(self.min_position, self.max_position, 100)
            ys = self._height(xs)
            xys = list(zip((xs-self.min_position)*scale, ys*scale))

            self.track = rendering.make_polyline(xys)
            self.track.set_linewidth(4)
            self.viewer.add_geom(self.track)
	    

            l,r,t,b = -cartwidth/2, cartwidth/2, cartheight/2, -cartheight/2
            axleoffset =cartheight/4.0
            cart = rendering.FilledPolygon([(l,b), (l,t), (r,t), (r,b)])
            self.carttrans = rendering.Transform()
            cart.add_attr(self.carttrans)
            self.viewer.add_geom(cart)
            l,r,t,b = -polewidth/2,polewidth/2,polelen-polewidth/2,-polewidth/2
            pole = rendering.FilledPolygon([(l,b), (l,t), (r,t), (r,b)])
            pole.set_color(.8,.6,.4)
            self.poletrans = rendering.Transform(translation=(0, axleoffset))
            pole.add_attr(self.poletrans)
            pole.add_attr(self.carttrans)
            self.viewer.add_geom(pole)
            self.axle = rendering.make_circle(polewidth/2)
            self.axle.add_attr(self.poletrans)
            self.axle.add_attr(self.carttrans)
            self.axle.set_color(.5,.5,.8)
            self.viewer.add_geom(self.axle)
            self.viewer = rendering.Viewer(screen_width, screen_height)
           

            self._pole_geom = pole

        if self.state is None: return None

        # Edit the pole polygon vertex
        pole = self._pole_geom
        l,r,t,b = -polewidth/2,polewidth/2,polelen-polewidth/2,-polewidth/2
        pole.v = [(l,b), (l,t), (r,t), (r,b)]

        x = self.state
        cartx = x[0]*scale+screen_width/2.0 # MIDDLE OF CART
        self.carttrans.set_translation(cartx, carty)
        self.poletrans.set_rotation(-x[2])

        return self.viewer.render(return_rgb_array = mode=='rgb_array')
コード例 #11
0
    def render(self, mode='human'):
        screen_width = 600
        screen_height = 400

        world_width = self.max_position - self.min_position
        scale = screen_width / world_width
        car_width = 40
        car_height = 20

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(screen_width, screen_height)
            xs = np.linspace(self.min_position, self.max_position, 100)
            ys = self._height(xs)
            xys = list(zip((xs - self.min_position) * scale, ys * scale))

            self.track = rendering.make_polyline(xys)
            self.track.set_linewidth(4)
            self.viewer.add_geom(self.track)

            clearance = 10

            l, r, t, b = -car_width / 2, car_width / 2, car_height, 0
            car = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)])
            car.add_attr(rendering.Transform(translation=(0, clearance)))

            self.car_trans = rendering.Transform()
            car.add_attr(self.car_trans)
            self.viewer.add_geom(car)
            front_wheel = rendering.make_circle(car_height / 2.5)
            front_wheel.set_color(.5, .5, .5)
            front_wheel.add_attr(
                rendering.Transform(translation=(car_width / 4, clearance)))
            front_wheel.add_attr(self.car_trans)
            self.viewer.add_geom(front_wheel)

            back_wheel = rendering.make_circle(car_height / 2.5)
            back_wheel.add_attr(
                rendering.Transform(translation=(-car_width / 4, clearance)))
            back_wheel.add_attr(self.car_trans)
            back_wheel.set_color(.5, .5, .5)
            self.viewer.add_geom(back_wheel)

            flag_x = (self.goal_position - self.min_position) * scale
            flag_y1 = self._height(self.goal_position) * scale
            flag_y2 = flag_y1 + 50
            flagpole = rendering.Line((flag_x, flag_y1), (flag_x, flag_y2))
            self.viewer.add_geom(flagpole)
            flag = rendering.FilledPolygon([(flag_x, flag_y2),
                                            (flag_x, flag_y2 - 10),
                                            (flag_x + 25, flag_y2 - 5)])
            flag.set_color(.8, .8, 0)
            self.viewer.add_geom(flag)

        pos = self.state[0]
        self.car_trans.set_translation((pos - self.min_position) * scale,
                                       self._height(pos) * scale)
        self.car_trans.set_rotation(math.cos(3 * pos))

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

        screen_width = 600
        screen_height = 400

        world_width = self.max_position - self.min_position
        scale = screen_width/world_width
        carwidth=40
        carheight=20


        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(screen_width, screen_height)
            xs = np.linspace(self.min_position, self.max_position, 100)
            ys = self._height(xs)
            xys = list(zip((xs-self.min_position)*scale, ys*scale))

            self.track = rendering.make_polyline(xys)
            self.track.set_linewidth(4)
            self.viewer.add_geom(self.track)

            clearance = 10

            l,r,t,b = -carwidth/2, carwidth/2, carheight, 0
            car = rendering.FilledPolygon([(l,b), (l,t), (r,t), (r,b)])
            car.add_attr(rendering.Transform(translation=(0, clearance)))
            self.cartrans = rendering.Transform()
            car.add_attr(self.cartrans)
            self.viewer.add_geom(car)
            frontwheel = rendering.make_circle(carheight/2.5)
            frontwheel.set_color(.5, .5, .5)
            frontwheel.add_attr(rendering.Transform(translation=(carwidth/4,clearance)))
            frontwheel.add_attr(self.cartrans)
            self.viewer.add_geom(frontwheel)
            backwheel = rendering.make_circle(carheight/2.5)
            backwheel.add_attr(rendering.Transform(translation=(-carwidth/4,clearance)))
            backwheel.add_attr(self.cartrans)
            backwheel.set_color(.5, .5, .5)
            self.viewer.add_geom(backwheel)
            flagx = (self.goal_position-self.min_position)*scale
            flagy1 = self._height(self.goal_position)*scale
            flagy2 = flagy1 + 50
            flagpole = rendering.Line((flagx, flagy1), (flagx, flagy2))
            self.viewer.add_geom(flagpole)
            flag = rendering.FilledPolygon([(flagx, flagy2), (flagx, flagy2-10), (flagx+25, flagy2-5)])
            flag.set_color(.8,.8,0)
            self.viewer.add_geom(flag)

        pos = self.state[0]
        self.cartrans.set_translation((pos-self.min_position)*scale, self._height(pos)*scale)
        self.cartrans.set_rotation(math.cos(3 * pos))

        return self.viewer.render(return_rgb_array = mode=='rgb_array')
コード例 #13
0
ファイル: vis.py プロジェクト: jusjusjus/dynamical-systems-rl
 def __init__(self, offset=0.0, marker=None):
     self._trace = rendering.make_polyline([[0, 0]])
     self.marker = marker
     if self.marker is not None:
         self._hline = rendering.Line(start=(0, 0), end=(0, 0))
     else:
         self._hline = None
     self.offset = offset
コード例 #14
0
 def __init__(self, start, end, color, **kwargs):
     vertice = [start, end, end + [1, 1], start + [1, 1]]
     GeomContainer.__init__(self,
                            rendering.make_polyline(vertice),
                            collider_func=self.collider_func,
                            **kwargs)
     self.set_color(*color)
     self.wall_segment = Segment(start, end)
コード例 #15
0
ファイル: env.py プロジェクト: d3sm0/gym-vdp
def _render_last_state(s):
    ds = 0.05
    square = ((0, 0), (ds, ds))
    attr = rendering.Transform(translation=tuple(s))
    line = rendering.make_polyline(square)
    line.set_linewidth(4)
    line.set_color(0, 0, 1)
    line.add_attr(attr)
    return line
コード例 #16
0
ファイル: rendering.py プロジェクト: TSL-UOB/CAV-Gym
    def __init__(self, road):
        self.area = rendering.make_polygon(list(road.static_bounding_box))
        self.area.set_color(*RGB.WHITE.value)

        coordinates = road.bounding_box()
        self.edge_markings = rendering.Compound([
            rendering.make_polyline(
                [tuple(coordinates.rear_left),
                 tuple(coordinates.front_left)]),
            rendering.make_polyline([
                tuple(coordinates.rear_right),
                tuple(coordinates.front_right)
            ])
        ])

        outbound_coordinates = road.outbound.bounding_box()
        self.centre_markings = rendering.make_polyline([
            tuple(outbound_coordinates.rear_right),
            tuple(outbound_coordinates.front_right)
        ])
        self.centre_markings.add_attr(mods.FactoredLineStyle(0x00FF, 2))

        lane_lines = list()
        for lane in road.outbound.lanes[:-1] + road.inbound.lanes[:-1]:
            lane_coordinates = lane.bounding_box()
            lane_line = rendering.make_polyline([
                tuple(lane_coordinates.rear_right),
                tuple(lane_coordinates.front_right)
            ])
            lane_lines.append(lane_line)
        self.lane_markings = rendering.Compound(lane_lines)
        self.lane_markings.add_attr(mods.FactoredLineStyle(0x00FF, 2))

        self.pelican_crossing_view = None

        self.bus_stop_views = list()
        for direction in [road.outbound, road.inbound]:
            if direction.bus_stop is not None:
                markings = rendering.make_polygon(list(
                    direction.bus_stop.static_bounding_box),
                                                  filled=False)
                self.bus_stop_views.append(markings)
コード例 #17
0
    def draw_grid(self, left):
        x_min = self.get_left_bottom_grid_cell_position(0, 0, left_side=left)[0]
        x_max = self.get_left_bottom_grid_cell_position(self.grid_size[0], 0, left_side=left)[0]
        y_min = self.get_left_bottom_grid_cell_position(0, 0, left_side=left)[1]
        y_max = self.get_left_bottom_grid_cell_position(0, self.grid_size[1], left_side=left)[1]

        for i in range(self.grid_size[1] + 1):
            y_val = self.get_left_bottom_grid_cell_position(0, i, left)[1]
            horizontal_line = rendering.make_polyline(
                [[x_min, y_val], [x_max, y_val]]
            )
            horizontal_line.set_linewidth(self.grid_line_width)
            self.viewer.add_geom(horizontal_line)
        for i in range(self.grid_size[0] + 1):
            x_val = self.get_left_bottom_grid_cell_position(i, 0, left)[0]
            vertical_line = rendering.make_polyline(
                [[x_val, y_min], [x_val, y_max]]
            )
            vertical_line.set_linewidth(self.grid_line_width)
            self.viewer.add_geom(vertical_line)
コード例 #18
0
    def render(self, mode='human'):
        screen_width = 600
        screen_height = 400
        world_width = self.max_position - self.min_position
        scale = screen_width/world_width
        carwidth=5
        carheight=5

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(screen_width, screen_height)
            # xs = np.linspace(self.min_position, self.max_position, 41)
            # ys = self._height(xs)

            with open('/Users/Famosi/Desktop/Bielefeld/Project-Duckietown/lane_following_car/tracks/' + 'track_' + str(1439) + '.pkl', "rb") as pickle_file:
                df = pickle.load(pickle_file)
                line = [tuple(r) for r in df.to_numpy().tolist()]

            xs = []
            ys = []
            for point in line:
                xs.append(float(point[:1][0]))
                ys.append(float(point[1:3][0]))
            xs = np.array(xs)
            ys = np.array(ys)
            xys = list(zip(xs*scale, ys*scale))
            self.track = rendering.make_polyline(xys)
            self.track.set_linewidth(1.)
            self.viewer.add_geom(self.track)

            clearance = -5

            l,r,t,b = -carwidth/2, carwidth/2, carheight, 0
            car = rendering.FilledPolygon([(l,b), (l,t), (r,t), (r,b)])
            car.add_attr(rendering.Transform(translation=(0, clearance)))
            self.cartrans = rendering.Transform()
            car.add_attr(self.cartrans)
            self.viewer.add_geom(car)

            # flagx = self.goal_position[0] * scale
            # flagy1 = self.goal_position[1] * scale
            # flagy2 = flagy1 + 20
            # flagpole = rendering.Line((flagx, flagy1), (flagx, flagy2))
            # self.viewer.add_geom(flagpole)
            # flag = rendering.FilledPolygon([(flagx, flagy2), (flagx, flagy2-10), (flagx+25, flagy2-5)])
            # flag.set_color(.8,.8,0)
            # self.viewer.add_geom(flag)

        pos = self.state[0]

        self.cartrans.set_translation(pos[0]*scale, pos[1]*scale)
        self.cartrans.set_rotation(self.rotation)

        return self.viewer.render(return_rgb_array = mode=='rgb_array')
コード例 #19
0
 def createNodeConnections(self):
     connections = [(0, 1), (0, 3), (1, 2), (1, 4), (2, 3), (2, 4), (2, 5),
                    (2, 6), (3, 6), (4, 7), (4, 8), (5, 8), (6, 8), (6, 9),
                    (7, 8), (7, 10), (8, 9), (9, 10)]
     for connection in connections:
         connectionGeom = rendering.make_polyline([
             self.p1NodeRenderInfo[connection[0]]['translate'].translation,
             self.p1NodeRenderInfo[connection[1]]['translate'].translation
         ])
         connectionGeom.set_color(0, 0, 0)
         self.viewer.add_geom(connectionGeom)
コード例 #20
0
    def render(self, mode='human'):
        screen_width = 600
        screen_height = 600

        world_width = 10.0
        world_height = 10.0
        xscale = screen_width / world_width
        yscale = screen_height / world_height
        robotwidth = 0.25
        robotheight = 0.25

        if self.viewer is None:
            self.viewer = rendering.Viewer(screen_width, screen_height)
            xy = [(0.0, 0.0), (0.0, 10.0), (10.0, 10.0), (10.0, 0.0)]
            xys = [(x * xscale, y * yscale) for x, y in xy]
            self.surface = rendering.make_polyline(xys)
            self.surface.set_linewidth(4)
            self.viewer.add_geom(self.surface)

            l, r, t, b = -robotwidth / 2, robotwidth / 2, robotheight / 2, -robotheight / 2
            self.robot = rendering.FilledPolygon([(l, b), (l, t), (r, t),
                                                  (r, b)])
            self.robot.set_color(0.0, 0.0, 0.8)
            self.robot.add_attr(rendering.Transform(translation=(0, 0)))
            self.robot_trans = rendering.Transform()
            self.robot.add_attr(self.robot_trans)
            self.viewer.add_geom(self.robot)

            l = self.G[0][0, 0] * xscale
            b = self.G[0][1, 0] * yscale
            r = self.G[1][0, 0] * xscale
            t = self.G[1][1, 0] * yscale
            self.goal_area = rendering.FilledPolygon([(l, b), (l, t), (r, t),
                                                      (r, b)])
            self.goal_area.set_color(0.8, 0.0, 0.0)
            self.viewer.add_geom(self.goal_area)

        self._trajectory.append(
            (self.state[0] * xscale, self.state[1] * yscale))
        if len(self._trajectory) >= 2:
            move = rendering.Line(start=self._trajectory[-2],
                                  end=self._trajectory[-1])
            # orange: rgb(244, 215, 66)
            move.set_color(244.0 / 255.0, 215.0 / 255.0, 66.0 / 255.0)
            move.add_attr(rendering.LineStyle(0xAAAA))
            move.add_attr(rendering.LineWidth(4))
            self.viewer.add_geom(move)

        self.robot_trans.set_translation(self.state[0] * xscale,
                                         self.state[1] * yscale)
        self.robot_trans.set_rotation(np.arctan2(self.state[3], self.state[2]))
        self.robot_trans.set_scale(xscale, yscale)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #21
0
ファイル: arrow.py プロジェクト: Xzavier0214/Coplanar_Orbit
def make_arrow(start, end):
    translation = start
    rotation = arctan2(end[1] - start[1], end[0] - start[0])

    l = np.linalg.norm(np.array(end) - np.array(start))

    line = rendering.make_polyline(((0, 0), (l, 0)))
    line.set_linewidth(2)
    line.add_attr(rendering.Transform(translation, rotation))

    x = l / 6 * cos(pi / 6)
    y = l / 6 * sin(pi / 6)
    line_u = rendering.make_polyline(((l, 0), (l - x, y)))
    line_d = rendering.make_polyline(((l, 0), (l - x, -y)))
    line_u.set_linewidth(2)
    line_d.set_linewidth(2)
    line_u.add_attr(rendering.Transform(translation, rotation))
    line_d.add_attr(rendering.Transform(translation, rotation))

    geom = rendering.Compound([line, line_u, line_d])
    return geom
コード例 #22
0
ファイル: rendering.py プロジェクト: TSL-UOB/CAV-Gym
    def __init__(self, pelican_crossing, ego, **kwargs):
        super().__init__(**kwargs)

        coordinates = pelican_crossing.bounding_box()
        self.area = rendering.make_polygon(list(coordinates))
        self.area.set_color(*RGB.WHITE.value)

        self.markings = rendering.Compound([
            rendering.make_polyline([
                tuple(pelican_crossing.outbound_intersection_bounding_box.
                      rear_left),
                tuple(pelican_crossing.outbound_intersection_bounding_box.
                      rear_right)
            ]),
            rendering.make_polyline([
                tuple(pelican_crossing.inbound_intersection_bounding_box.
                      front_left),
                tuple(pelican_crossing.inbound_intersection_bounding_box.
                      front_right)
            ]),
            rendering.make_polyline([
                tuple(pelican_crossing.static_bounding_box.rear_left),
                tuple(pelican_crossing.static_bounding_box.front_left)
            ]),
            rendering.make_polyline([
                tuple(pelican_crossing.static_bounding_box.rear_right),
                tuple(pelican_crossing.static_bounding_box.front_right)
            ])
        ])

        offset_rear_right = Point(
            coordinates.rear_right.x +
            (pelican_crossing.constants.width * 0.15),
            coordinates.rear_right.y)
        offset_rear_left = Point(
            coordinates.rear_left.x +
            (pelican_crossing.constants.width * 0.15), coordinates.rear_left.y)
        offset_front_right = Point(
            coordinates.front_right.x -
            (pelican_crossing.constants.width * 0.15),
            coordinates.front_right.y)
        offset_front_left = Point(
            coordinates.front_left.x -
            (pelican_crossing.constants.width * 0.15),
            coordinates.front_left.y)
        self.inner = rendering.Compound([
            rendering.make_polyline(
                [tuple(offset_rear_right),
                 tuple(offset_rear_left)]),
            rendering.make_polyline(
                [tuple(offset_front_right),
                 tuple(offset_front_left)])
        ])
        self.inner.add_attr(mods.FactoredLineStyle(0x0F0F, 1))

        self.outbound_traffic_light_view = TrafficLightView(
            pelican_crossing.outbound_traffic_light, ego)
        self.inbound_traffic_light_view = TrafficLightView(
            pelican_crossing.inbound_traffic_light, ego)
コード例 #23
0
    def render(self, mode='human', close=False):
        if close:
            if self.viewer is not None:
                self.viewer.close()
                self.viewer = None
            return

        screen_width = 600
        screen_height = 600

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(screen_width, screen_height)

            o = rendering.make_polyline([(0, 0), (0, screen_height),
                                         (screen_width, screen_height),
                                         (screen_width, 0)])
            o.set_color(0, 0, 0)
            self.viewer.add_geom(o)
            r = np.linspace(0, 1, 20)
            for x in r:
                o = rendering.make_circle(min(screen_height, screen_width) * x,
                                          filled=False)
                o.add_attr(
                    rendering.Transform(translation=(screen_width / 2,
                                                     screen_height / 2)))
                o.set_color(0, 0, 0)
                self.viewer.add_geom(o)

            agent = rendering.make_circle(
                min(screen_height, screen_width) * 0.03)
            origin = rendering.make_circle(
                min(screen_height, screen_width) * 0.03)
            trans = rendering.Transform(translation=(0, 0))
            agent.add_attr(trans)
            self.trans = trans
            agent.set_color(1, 0, 0)
            origin.set_color(0, 0, 0)
            origin.add_attr(
                rendering.Transform(translation=(screen_width // 2,
                                                 screen_height // 2)))
            self.viewer.add_geom(agent)
            self.viewer.add_geom(origin)

        # self.trans.set_translation(0, 0)
        self.trans.set_translation(
            (self.state[0] + 1) / 2 * screen_width,
            (self.state[1] + 1) / 2 * screen_height,
        )

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #24
0
ファイル: rendering.py プロジェクト: lyu-xg/gym-gridverse
def make_goal(goal: Goal) -> rendering.Geom:  # pylint: disable=unused-argument
    geom_goal = rendering.make_polygon(
        [(-1.0, -1.0), (-1.0, 1.0), (1.0, 1.0), (1.0, -1.0)],
        filled=True,
    )
    geom_goal.set_color(*GREEN)

    pad = 0.8
    geom_flag = rendering.make_polyline([(0.0, -pad), (0.0, pad),
                                         (pad, pad / 2), (0.0, 0.0)])
    geom_flag.set_linewidth(2)
    geom_flag.add_attr(rendering.Transform(translation=(-pad / 4, 0.0)))

    return Group([geom_goal, geom_flag])
コード例 #25
0
def doRender(episode, numStreaks, mode='human'):
    from gym.envs.classic_control import rendering
    oldViewer = env.viewer
    env.render(mode)
    if oldViewer is None:
        lineStart = -0.5
        lineEnd = -1
        textPosns = [-0.6, -0.9]
        for binBoundsAngle in binBoundsAngles:
            # Draw line
            x1 = np.sin(binBoundsAngle) * lineStart
            y1 = np.cos(binBoundsAngle) * lineStart
            x2 = np.sin(binBoundsAngle) * lineEnd
            y2 = np.cos(binBoundsAngle) * lineEnd
            segLine = rendering.make_polyline([(x1,y1),(x2,y2)])
            segLine.set_color(0/255, 0/255, 255/255)
            env.viewer.add_geom(segLine)
        for binCentreAngle in binCentreAngles:
            # Create kick indicator items
            kickDirnIndicators = []
            for i in range(numDirections):
                xT = np.sin(binCentreAngle) * textPosns[i]
                yT = np.cos(binCentreAngle) * textPosns[i]
                kickIndicator = rendering.make_circle(.05)
                kickTransform = rendering.Transform()
                kickTransform.set_translation(xT, yT)
                kickIndicator.add_attr(kickTransform)
                kickDirnIndicators.append(kickIndicator)
                env.viewer.add_geom(kickIndicator)
            kickIndicators.append(kickDirnIndicators)
    
    for i in range(len(binCentreAngles)):
        for j in range(numDirections):
            qIdx = i+1
            if j > 0:
                qIdx = 2*(len(binCentreAngles)+2)-i-2
            qRowRange = qTable[qIdx][0] - qTable[qIdx][1]
            if qRowRange <= -0.01:
                indHue = indHueMin
            elif qRowRange >= 0.01:
                indHue = indHueMax
            else:
                indHue = (indHueMax + indHueMin) / 2
            rgbColour = matplotlib.colors.hsv_to_rgb((indHue, 1, 1))
            kickIndicators[i][j].set_color(rgbColour[0], rgbColour[1], rgbColour[2])

    retVal = env.render(mode)
    time.sleep(.01)
    return retVal
コード例 #26
0
    def render(self, mode="human"):
        W = 600
        PIXELS = 30 # 600 - (10 + 10)

        if self._viewer is None:
            from gym.envs.classic_control import rendering

            self._viewer = rendering.Viewer(W, W)

            self._viewer_centers = []
            for _ in range(self._agents):
                circle = rendering.make_circle(radius=PIXELS, res=30, filled=True)
                transform = rendering.Transform()
                circle.add_attr(transform)
                self._viewer.add_geom(circle)
                self._viewer_centers.append((circle, transform))

            self._viewer_agents_pos = []
            for _ in range(self._agents):
                circle = rendering.make_circle(radius=0.5 * PIXELS, res=30, filled=True)
                transform = rendering.Transform()
                circle.add_attr(transform)
                self._viewer.add_geom(circle)
                self._viewer_agents_pos.append((circle, transform))

            self._viewer_agents_vel = []
            for i in range(self._agents):
                self._viewer_agents_vel.append([[0, 0], [0, 0]])
                line = rendering.make_polyline(self._viewer_agents_vel[-1])
                line.add_attr(self._viewer_agents_pos[i][1])
                self._viewer.add_geom(line)

        if not hasattr(self, "_centers"):
            return None

        for (circle, transform), center, hit in zip(self._viewer_centers, self._centers, self._centers_hit):
            circle.set_color(0, 1.0 if hit else 0.7, 0)
            transform.set_translation(center[0] * PIXELS + W / 2, center[1] * PIXELS + W / 2)
        for (circle, transform), pos, hit in zip(self._viewer_agents_pos, self._agents_pos, self._agents_hit):
            circle.set_color(1.0 * hit, 0, 0.7 * (1 - hit))
            transform.set_translation(pos[0] * PIXELS + W / 2, pos[1] * PIXELS + W / 2)
        for line, vel in zip(self._viewer_agents_vel, self._agents_vel):
            line[1] = vel * PIXELS

        return self._viewer.render(return_rgb_array=mode == "rgb_array")
コード例 #27
0
    def render(self, mode='human'):
        pos = [self.state[0], self.state[1]]
        old_pos = self.path[-1]

        self.particletrans.set_translation((pos[0]) * self.scale[0],
                                           (pos[1]) * self.scale[1])
        self.particletrans.set_rotation(0)

        xs = np.array([old_pos[0], pos[0]])
        ys = np.array([old_pos[1], pos[1]])

        xys = list(zip(xs * self.scale[0], ys * self.scale[1]))
        path = rendering.make_polyline(xys)
        path.set_linewidth(2)
        path.set_color(self.color[0], self.color[1], self.color[2])
        self.viewer.add_geom(path)
        self.score_label.text = self.name + ": %0.4fs" % self.total_time
        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #28
0
    def create_geometry(self, viewer, PPM):
        self.transforms = []
        self.lines = []
        from gym.envs.classic_control import rendering
        for m in self.masses:
            rad = m.fixtures[0].shape.radius
            circle = rendering.make_circle(PPM * rad)
            transform = rendering.Transform()
            circle.add_attr(transform)
            self.transforms.append(transform)
            viewer.add_geom(circle)

        for muscle, (idx_a, idx_b) in zip(self.joint_muscles, self.joint_defs):
            line = rendering.make_polyline([(0, 0), (1, 0)])
            line.set_linewidth(2)
            line.set_color(*muscle.color)
            viewer.add_geom(line)
            self.lines.append(line)
コード例 #29
0
    def render(self, mode='human', close=False):
        if close:
            if self.viewer is not None:
                self.viewer.close()
                self.viewer = None
            return

        screen_width = 400
        screen_height = 400
        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(screen_width, screen_height)
            #add obstacles
            for obs in self.environment.obstacles:
                points = list(
                    map(lambda p: (p[0] * screen_width, p[1] * screen_height),
                        obs.points))
                obj = rendering.make_polyline(points + [points[0]])
                self.viewer.add_geom(obj)
            #add target
            target_rad = self.environment.target_rad * screen_height
            target = rendering.make_circle(target_rad)
            target.set_color(0, 0, 1)
            self.targettrans = rendering.Transform()
            target.add_attr(self.targettrans)
            self.targettrans.set_translation(
                self.environment.target_pos[0] * screen_width,
                self.environment.target_pos[1] * screen_height)
            self.viewer.add_geom(target)
            #add ball
            ball_rad = self.environment.ball.radius * screen_height
            ball = rendering.make_circle(ball_rad)
            ball.set_color(1, 0, 0)
            self.balltrans = rendering.Transform()
            ball.add_attr(self.balltrans)
            self.viewer.add_geom(ball)
        #update ball location
        self.balltrans.set_translation(self.state[0] * screen_width,
                                       self.state[1] * screen_height)
        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
コード例 #30
0
ファイル: mountain_car.py プロジェクト: RongPeanut/gym
    def _render(self, mode='human', close=False):
        if close:
            if self.viewer is not None:
                self.viewer.close()
            return

        screen_width = 600
        screen_height = 400

        world_width = self.max_position - self.min_position
        scale = screen_width/world_width
        carwidth=40
        carheight=20


        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(screen_width, screen_height)
            xs = np.linspace(self.min_position, self.max_position, 100)
            ys = self._height(xs)
            xys = zip((xs-self.min_position)*scale, ys*scale)

            self.track = rendering.make_polyline(xys)
            self.track.set_linewidth(4)
            self.viewer.add_geom(self.track)

            clearance = 10

            l,r,t,b = -carwidth/2, carwidth/2, carheight, 0
            car = rendering.FilledPolygon([(l,b), (l,t), (r,t), (r,b)])
            car.add_attr(rendering.Transform(translation=(0, clearance)))
            self.cartrans = rendering.Transform()
            car.add_attr(self.cartrans)
            self.viewer.add_geom(car)
            frontwheel = rendering.make_circle(carheight/2.5)
            frontwheel.set_color(.5, .5, .5)
            frontwheel.add_attr(rendering.Transform(translation=(carwidth/4,clearance)))
            frontwheel.add_attr(self.cartrans)
            self.viewer.add_geom(frontwheel)
            backwheel = rendering.make_circle(carheight/2.5)
            backwheel.add_attr(rendering.Transform(translation=(-carwidth/4,clearance)))
            backwheel.add_attr(self.cartrans)
            backwheel.set_color(.5, .5, .5)
            self.viewer.add_geom(backwheel)
            flagx = (self.goal_position-self.min_position)*scale
            flagy1 = self._height(self.goal_position)*scale
            flagy2 = flagy1 + 50
            flagpole = rendering.Line((flagx, flagy1), (flagx, flagy2))
            self.viewer.add_geom(flagpole)
            flag = rendering.FilledPolygon([(flagx, flagy2), (flagx, flagy2-10), (flagx+25, flagy2-5)])
            flag.set_color(.8,.8,0)
            self.viewer.add_geom(flag)

        pos = self.state[0]
        self.cartrans.set_translation((pos-self.min_position)*scale, self._height(pos)*scale)
        self.cartrans.set_rotation(math.cos(3 * pos))

        self.viewer.render()
        if mode == 'rgb_array':
            return self.viewer.get_array()
        elif mode is 'human':
            pass
        else:
            return super(MountainCarEnv, self).render(mode=mode)
コード例 #31
0
    def render(self, mode='human', **kwargs):
        screen_width = 600
        screen_height = 600

        world_width = 2 * X
        scale = screen_width / world_width
        carwidth = 40
        carheight = 20

        if self.viewer is None:
            from gym.envs.classic_control import rendering
            self.viewer = rendering.Viewer(screen_width, screen_height)

            xs = np.linspace(-X, X, 100) - X
            ys = np.linspace(-Y, Y, 100) - Y - 5
            x_scaled = X * scale
            y_scaled = Y * scale
            ones_x = np.ones_like(xs)
            ones_y = np.ones_like(ys)
            lines = [
                list(zip((xs) * scale, 2 * Y * scale * ones_x)),
                list(zip((xs) * scale, 0 * ones_x)),
                list(zip(2 * X * scale * ones_y, (ys) * scale)),
                list(zip(0 * ones_y, (ys) * scale))
            ]
            for line in lines:
                xys = line
                track = rendering.make_polyline(xys)
                track.set_linewidth(4)
                track.set_color(0, .5, 0)
                self.viewer.add_geom(track)
                # clearance = 10
            agent_size = 5
            agent = rendering.make_circle(agent_size, filled=True)
            self.agenttrans = rendering.Transform(
                translation=(0, 0))  # translation = (dx,dy)ずらす
            agent.add_attr(self.agenttrans)
            agent.set_color(.9, .5, .5)
            self.viewer.add_geom(agent)
            horizontal_line = rendering.Line((0, y_scaled),
                                             (2 * x_scaled, y_scaled))
            self.viewer.add_geom(horizontal_line)
            for i in range(20):
                pos_x = i * scale
                line_length = 3 if i % 2 == 0 else 1

                flagpole = rendering.Line((pos_x, y_scaled - line_length),
                                          (pos_x, y_scaled + line_length))

                self.viewer.add_geom(flagpole)
            """
            l,r,t,b = -carwidth/2, carwidth/2, carheight, 0
            car = rendering.FilledPolygon([(l,b), (l,t), (r,t), (r,b)])
            car.add_attr(rendering.Transform(translation=(0, clearance)))
            self.cartrans = rendering.Transform()
            car.add_attr(self.cartrans)
            self.viewer.add_geom(car)
            frontwheel = rendering.make_circle(carheight/2.5)
            frontwheel.set_color(.5, .5, .5)
            frontwheel.add_attr(rendering.Transform(translation=(carwidth/4,clearance)))#移動
            frontwheel.add_attr(self.cartrans)
            self.viewer.add_geom(frontwheel)
            backwheel = rendering.make_circle(carheight/2.5)
            backwheel.add_attr(rendering.Transform(translation=(-carwidth/4,clearance)))
            backwheel.add_attr(self.cartrans)
            backwheel.set_color(.5, .5, .5)
            self.viewer.add_geom(backwheel)
            """
            flag_size = 7

            flag = rendering.make_circle(flag_size, filled=True)
            self.flagtrans = rendering.Transform(
                translation=(0, 0))  # translation = (dx,dy)ずらす
            flag.add_attr(self.flagtrans)
            flag.set_color(.5, .9, .9)
            self.viewer.add_geom(flag)
            """
            self.flagtrans = rendering.Transform(
                )
            flagx = (self.GOAL[0]+X)*scale
            print(self.GOAL)
            flagy1 = (self.GOAL[1] + Y) * scale
            flagy2 = flagy1 + 20
            flagpole = rendering.Line((flagx, flagy1), (flagx, flagy2))
            flagpole.add_attr(rendering.Transform(translation=(0, 0.1)))
            flagpole.add_attr(self.flagtrans)

            self.viewer.add_geom(flagpole)
            flag = rendering.FilledPolygon(
                [(flagx, flagy2), (flagx, flagy2-8), (flagx+20, flagy2-4)])
            flag.set_color(.8, .8, 0)

            flag.add_attr(self.flagtrans)
            self.viewer.add_geom(flag)
            """

        pos = self.state
        self.flagtrans.set_translation((self.GOAL[0] + X) * scale,
                                       (self.GOAL[1] + Y) * scale)
        self.agenttrans.set_translation((pos[0] + X) * scale,
                                        (pos[1] + Y) * scale)

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