Exemplo n.º 1
0
 def highlight_if_needed(self):
     if self.highlight_needed:
         self.geom.attrs = [rendering.LineWidth(5)] + self.geom_attrs
         self.highlight_needed = False
     else:
         self.geom.attrs = [rendering.LineWidth(1)] + self.geom_attrs
         self.highlight_needed = False
Exemplo n.º 2
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')
Exemplo n.º 3
0
    def _generate_edges_from_vertices(self, vertices: np.array, linewidth=2, color=(0, 0, 0)):
        """
        Takes list of vertices of shape (number of vertices, 2, 2).
        :param vertices: np.array of shape (len, 2, 2)
        :return: list of OpenAI Gym rendering Line
        """
        edges = []
        for vertices_pair in vertices:
            edge = rendering.Line(vertices_pair[0], vertices_pair[1])
            edge.attrs[-1] = rendering.LineWidth(linewidth)  # Line class adds LineWidth attr at init, overriding it
            edge.set_color(*color)
            edges.append(edge)

        return edges
Exemplo n.º 4
0
    def render(self, mode='human', screen_width=1000):
        # whether draw pulse and echo source
        draw_pulse_direction = True
        draw_echo_source = False

        # settings screen
        aspect_ratio = self.world_height / self.world_width
        screen_height = int(aspect_ratio * screen_width)
        scale = screen_width / self.world_width

        # initilize screen
        from gym.envs.classic_control import rendering
        if self.viewer is None:
            self.viewer = rendering.Viewer(screen_width, screen_height)
            r = (self.bat.size * scale) / 2
            wing = 4 * math.pi / 5  # angle [rad]
            nose_x, nose_y = r, 0
            r_x, r_y = r * math.cos(-wing), r * math.sin(-wing)
            l_x, l_y = r * math.cos(+wing), r * math.sin(+wing)
            bat_geom = rendering.FilledPolygon([
                (nose_x, nose_y),
                (r_x, r_y),
                (l_x, l_y)])
            bat_geom.set_color(0, 0, 0)
            self.battrans = rendering.Transform()
            bat_geom.add_attr(self.battrans)
            self.viewer.add_geom(bat_geom)
            self._bat_geom = bat_geom

            for w in self.walls:
                x0, y0, x1, y1 = w.unpack() * scale
                line = rendering.Line((x0, y0), (x1, y1))
                line.linewidth = rendering.LineWidth(10)
                line.set_color(0.5, 0.5, 0.5)
                self.viewer.add_geom(line)

        bat_geom = self._bat_geom
        self.battrans.set_translation(*self.bat.bat_vec * scale)
        self.battrans.set_rotation(self.bat.angle)

        if self.bat.emit == True:
            if draw_pulse_direction == True:
                pulse_length = 0.5
                pulse_vec = pulse_length * cos_sin(self.last_pulse_angle)
                pulse_vec = rotate_vector(
                    pulse_vec, self.bat.angle) + self.bat.bat_vec
                x0, y0 = self.bat.bat_vec * scale
                x1, y1 = pulse_vec * scale
                line = self.viewer.draw_line([x0, y0], [x1, y1])
                self.viewer.add_geom(line)

            if draw_echo_source == True:
                radius = 4  # pixel
                echo_source_vec = self.bat.state[0] * self.bat.lidar_length
                echo_source_vec = rotate_vector(
                    echo_source_vec, self.bat.angle) + self.bat.bat_vec
                x, y = echo_source_vec * scale
                echo_source = rendering.make_circle(radius)
                echo_source.set_color(0.9, 0.65, 0.4)
                echotrans = rendering.Transform()
                echo_source.add_attr(echotrans)
                echotrans.set_translation(x, y)
                self.viewer.add_geom(echo_source)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
Exemplo n.º 5
0
    def _render(self, mode='human', close=False, values=None):
        if close:
            if self.viewer is not None:
                self.viewer.close()
                self.viewer = None
            return

        size = 6
        px_per_cell = 120
        size_px = size * px_per_cell

        from gym.envs.classic_control import rendering

        self.viewer = rendering.Viewer(size_px, size_px)
        for x in range(size + 1):
            for y in range(size + 1):
                x_line = rendering.Line((px_per_cell * x, 0), (px_per_cell * x, size_px))
                y_line = rendering.Line((0, px_per_cell * y), (size_px, px_per_cell * y))
                self.viewer.add_geom(x_line)
                self.viewer.add_geom(y_line)
        robot_width = px_per_cell*3/4
        poly = [(-robot_width/2,-robot_width/2),
                (-robot_width/2,robot_width/2),
                (robot_width/2,robot_width/2),
                (robot_width/2,-robot_width/2)]
        
        
        self.robot = self.viewer.draw_polygon(poly,filled=False)
        self.viewer.add_geom(self.robot)
        self.robot_transform = rendering.Transform()
        self.robot.add_attr(self.robot_transform)
        self.robot.attrs[0].vec4 = (0,0,1,1)
        self.robot.attrs[1] = rendering.LineWidth(5)
        
        self.opponent = self.viewer.draw_polygon(poly,filled=False)
        self.viewer.add_geom(self.opponent)
        self.opponent_transform = rendering.Transform()
        self.opponent.add_attr(self.opponent_transform)
        self.opponent.attrs[0].vec4 = (1,0,0,1)
        self.opponent.attrs[1] = rendering.LineWidth(5)
        

        location = getxy(self.state.blue_data[0])
        self.robot_transform.set_translation(location[0] * px_per_cell + px_per_cell / 2,
                                             size_px - location[1] * px_per_cell - px_per_cell / 2 - px_per_cell/8)
        location = getxy(self.state.red_data[0])
        self.opponent_transform.set_translation(location[0] * px_per_cell + px_per_cell / 2,
                                             size_px - location[1] * px_per_cell - px_per_cell / 2 - px_per_cell/8)
        for i in range(self.red_goals):
            goal = self.viewer.draw_circle(radius=px_per_cell / 5, filled=True)
            self.viewer.add_geom(goal)
            goal_transform = rendering.Transform()
            goal.add_attr(goal_transform)
            goal.attrs[0].vec4 = (1,0,0,0.5)
            
            location = getxy(self.state.goal_data[i][0])
            goal_transform.set_translation(location[0] * px_per_cell + px_per_cell / 2,
                                             size_px - location[1] * px_per_cell - px_per_cell / 2)
            label = pyglet.text.Label(str(self.state.goal_data[i][1]),
                          font_name='Times New Roman',
                          font_size=15,
                          color=(0,0,0,255),
                          x=location[0] * px_per_cell + px_per_cell / 2, y=size_px - location[1] * px_per_cell - px_per_cell / 2,
                          anchor_x='center', anchor_y='center')
            self.viewer.add_geom(label)
            
        for i in range(self.blue_goals):
            goal = self.viewer.draw_circle(radius=px_per_cell / 5, filled=True)
            self.viewer.add_geom(goal)
            goal_transform = rendering.Transform()
            goal.add_attr(goal_transform)
            goal.attrs[0].vec4 = (0,0,1,0.5)
            
            location = getxy(self.state.goal_data[i+self.red_goals][0])
            goal_transform.set_translation(location[0] * px_per_cell + px_per_cell / 2,
                                             size_px - location[1] * px_per_cell - px_per_cell / 2)
            
            label = pyglet.text.Label(str(self.state.goal_data[i+self.red_goals][1]),
                          font_name='Times New Roman',
                          font_size=15,
                          color=(0,0,0,255),
                          x=location[0] * px_per_cell + px_per_cell / 2, y=size_px - location[1] * px_per_cell - px_per_cell / 2,
                          anchor_x='center', anchor_y='center')
            self.viewer.add_geom(label)
        for i in range(self.field_locations):
            if self.state.tile_data[i] >0:
                location = getxy(i)
                label = pyglet.text.Label(str(self.state.tile_data[i]),
                          font_name='Times New Roman',
                          font_size=15,
                          color=(0,0,0,255),
                          x=location[0] * px_per_cell + px_per_cell / 2, y=size_px - location[1] * px_per_cell - px_per_cell / 8,
                          anchor_x='center', anchor_y='center')
                self.viewer.add_geom(label)
        location = getxy(12)       
        redLoads = pyglet.text.Label(str(self.state.red_data[3]),
              font_name='Times New Roman',
              font_size=15,
              color=(255,0,0,255),
              x=location[0] * px_per_cell + px_per_cell / 2, y=size_px - location[1] * px_per_cell - px_per_cell / 8,
              anchor_x='center', anchor_y='center')
        self.viewer.add_geom(redLoads)  
        location = getxy(2)       
        blueLoads = pyglet.text.Label(str(self.state.blue_data[3]),
              font_name='Times New Roman',
              font_size=15,
              color=(0,0,255,255),
              x=location[0] * px_per_cell + px_per_cell / 2, y=size_px - location[1] * px_per_cell - px_per_cell / 8,
              anchor_x='center', anchor_y='center')
        self.viewer.add_geom(blueLoads)
                        
                
                
                
        
        return self.viewer.render(return_rgb_array=(mode == 'rgb_array'))
    def render(self, mode='human'):
        screen_width = 600
        screen_height = 600

        world_width = self.observation_space.high[0] + 2
        world_height = self.observation_space.high[0] + 2
        xscale = screen_width / world_width
        yscale = screen_height / world_height
        robotwidth = 0.5
        robotheight = 0.5
        goalwidth = 0.5
        goalheight = 0.5

        if self.viewer is None:
            self.viewer = rendering.Viewer(screen_width, screen_height)
            #Grid
            for heightX in range(int(self.observation_space.high[0] + 1)):
                heightX = float(heightX)
                for widthX in range(int(self.observation_space.high[0] + 1)):
                    widthX = float(widthX)
                    xy = [(0.5 + widthX, 0.5 + heightX),
                          (0.5 + widthX,
                           self.observation_space.high[0] + 1.5 - heightX),
                          (self.observation_space.high[0] + 1.5 - widthX,
                           self.observation_space.high[0] + 1.5 - heightX),
                          (self.observation_space.high[0] - widthX + 1.5,
                           0.5 + heightX)]
                    xys = [(x * xscale, y * yscale) for x, y in xy]
                    self.surface = rendering.make_polyline(xys)
                    self.surface.set_linewidth(1)
                    self.viewer.add_geom(self.surface)

            xy = [(0.5, 0.5), (0.5, self.observation_space.high[0] + 1.5),
                  (self.observation_space.high[0] + 1.5,
                   self.observation_space.high[0] + 1.5),
                  (self.observation_space.high[0] + 1.5, 0.5)]
            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)

            xy = [(0.5, 0.5), (self.observation_space.high[0] + 1.5, 0.5)]
            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)

            self.obstacle_area = []
            self.obstacle_trans = []
            for k in range(len(self.obstacles)):
                l, r, t, b = -goalwidth, goalwidth, goalheight, -goalheight
                self.obstacle_area.append(
                    rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]))
                self.obstacle_area[k].set_color(0.0, 0.8, 0.0)
                self.obstacle_area[k].add_attr(
                    rendering.Transform(translation=(0, 0)))
                self.obstacle_trans.append(rendering.Transform())
                self.obstacle_area[k].add_attr(self.obstacle_trans[k])
                self.viewer.add_geom(self.obstacle_area[k])

            self.goal_area = []
            self.goal_trans = []
            for k in range(len(self.terminals)):
                l, r, t, b = -goalwidth, goalwidth, goalheight, -goalheight
                self.goal_area.append(
                    rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)]))
                self.goal_area[k].set_color(0.8, 0.0, 0.0)
                self.goal_area[k].add_attr(
                    rendering.Transform(translation=(0, 0)))
                self.goal_trans.append(rendering.Transform())
                self.goal_area[k].add_attr(self.goal_trans[k])
                self.viewer.add_geom(self.goal_area[k])

            l, r, t, b = -robotwidth, robotwidth, robotheight, -robotheight
            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)

        self._trajectory.append(
            ((self.state[0] + 1) * xscale, (self.state[1] + 1) * yscale))
        for k in range(len(self.obstacles)):
            self.obstacle_trans[k].set_translation(
                (self.obstacles[k][0] + 1) * xscale,
                (self.obstacles[k][1] + 1) * yscale)
            self.obstacle_trans[k].set_scale(xscale, yscale)

        for k in range(len(self.terminals)):
            self.goal_trans[k].set_translation(
                (self.terminals[k][0] + 1) * xscale,
                (self.terminals[k][1] + 1) * yscale)
            self.goal_trans[k].set_scale(xscale, 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] + 1) * xscale,
                                         (self.state[1] + 1) * yscale)
        self.robot_trans.set_scale(xscale, yscale)

        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
Exemplo n.º 7
0
    def render_orthographic(self, mode='human', close=False):

        if close:
            if self.viewer_x is not None:
                self.viewer_x.close()
                self.viewer_x = None
            if self.viewer_y is not None:
                self.viewer_y.close()
                self.viewer_y = None
            if self.viewer_orthographic is not None:
                self.viewer_orthographic.close()
                self.viewer_orthographic = None
            return

        screen_width = 600
        screen_height = 600

        world_height = self.max_position_y - self.min_position_y  # same for y
        scale = (screen_height - 120) / world_height
        carwidth = 20
        carheight = 10

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

            # ys = np.linspace(self.min_position_y, self.max_position_y, 100)

            # zs = self._height(ys)
            # xyzs = list(zip((ys-self.min_position_y)*scale, zs*scale))

            # self.track = rendering.make_polyline(xyzs)
            # self.track.set_linewidth(4)
            # self.viewer_orthographic.add_geom(self.track)

            min_x = -math.pi / 6
            min_y = -math.pi / 6

            origin_res = 50
            origin_radius = 2

            origin_circle = rendering.make_circle(radius=origin_radius,
                                                  res=origin_res,
                                                  filled=True)
            origin_circle.set_color(0, 0, 0)
            origin_circle.add_attr(
                rendering.Transform(
                    translation=((min_x - self.min_position_x) * scale,
                                 (min_y - self.min_position_y) * scale)))
            self.viewer_orthographic.add_geom(origin_circle)

            radius_unscaled = math.sqrt((self.goal_position - min_x)**2 +
                                        (self.goal_position - min_y)**2)
            equilline_radius = radius_unscaled * scale
            #
            # points_x = []
            # points_y = []
            # offset_x, offset_y = (min_x - self.min_position_x) * scale, (min_y - self.min_position_y)*scale
            # for i in range(res):
            #     ang = 2 * math.pi * i / res
            #     points_x.append(offset_x + math.cos(ang) * radius)
            #     points_y.append(offset_y + math.sin(ang) * radius)
            #
            # equiline = list(zip(points_x, points_y))
            # self.track = rendering.make_polyline(equiline)
            # self.track.set_linewidth(4)
            equiline = rendering.make_circle(radius=equilline_radius,
                                             res=200,
                                             filled=False)
            equiline.set_color(0, 0, 0)
            equiline.add_attr(
                rendering.Transform(
                    translation=((min_x - self.min_position_x) * scale,
                                 (min_y - self.min_position_y) * scale)))
            equiline.add_attr(
                rendering.LineWidth(10))  # not sure why doesn't work
            self.viewer_orthographic.add_geom(equiline)

            clearance = 5
            clearance_wheels = 0

            l, r, t, b = -carwidth / 2, carwidth / 2, carheight / 2, -carheight / 2
            car = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)])
            car.add_attr(rendering.Transform(translation=(0, clearance)))
            self.cartrans_orth = rendering.Transform()
            car.add_attr(self.cartrans_orth)
            self.viewer_orthographic.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_wheels)))
            frontwheel.add_attr(self.cartrans_orth)
            self.viewer_orthographic.add_geom(frontwheel)
            backwheel = rendering.make_circle(carheight / 2.5)
            backwheel.add_attr(
                rendering.Transform(translation=(-carwidth / 4,
                                                 clearance_wheels)))
            backwheel.add_attr(self.cartrans_orth)
            backwheel.set_color(.5, .5, .5)
            self.viewer_orthographic.add_geom(backwheel)

            flagx = (self.goal_position - self.min_position_x) * scale
            flagy1 = (self.goal_position - self.min_position_y) * scale
            flagy2 = flagy1 + 50
            flagpole = rendering.Line((flagx, flagy1), (flagx, flagy2))
            self.viewer_orthographic.add_geom(flagpole)
            flag = rendering.FilledPolygon([(flagx, flagy2),
                                            (flagx, flagy2 - 10),
                                            (flagx + 25, flagy2 - 5)])
            flag.set_color(1, 0, 0)
            self.viewer_orthographic.add_geom(flag)

        pos_x = self.state[0]
        pos_y = self.state[1]
        self.cartrans_orth.set_translation(
            (pos_x - self.min_position_x) * scale,
            (pos_y - self.min_position_y) * scale)
        # self.cartrans_orth.set_rotation(math.cos(3 * pos))

        return self.viewer_orthographic.render(
            return_rgb_array=mode == 'rgb_array')
Exemplo n.º 8
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 = 800
        screen_height = 800
        from gym.envs.classic_control import rendering

        if self.viewer is None:
            self.viewer = rendering.Viewer(screen_width, screen_height)

            for i in range(len(self.grid_map)):
                for j in range(len(self.grid_map[0])):

                    l = screen_width / 2 + (j - int(
                        (len(self.grid_map)) /
                        2)) * self.grid_vis_size - self.grid_vis_size / 2

                    r = screen_width / 2 + (j - int(
                        (len(self.grid_map)) /
                        2)) * self.grid_vis_size + self.grid_vis_size / 2

                    t = screen_height / 2 + (int(
                        (len(self.grid_map)) /
                        2) - i) * self.grid_vis_size + self.grid_vis_size / 2
                    b = screen_height / 2 + (int(
                        (len(self.grid_map)) /
                        2) - i) * self.grid_vis_size - self.grid_vis_size / 2

                    cell = rendering.FilledPolygon([(l, b), (l, t), (r, t),
                                                    (r, b)])

                    # cell.add_attr(rendering.Transform())
                    draw_cell_edge = True
                    if (self.grid_map[i, j] == 1):
                        # cell.set_color(0, 0, 0)
                        cell.set_color(1, 1, 1)
                        draw_cell_edge = False

                    elif (2 <= self.grid_map[i, j] <= 5):

                        cell.set_color(((self.grid_map[i, j] - 1) / 4.0)**0.6,
                                       0, 0)

                    elif (6 <= self.grid_map[i, j] <= 9):

                        cell.set_color(
                            ((self.grid_map[i, j] - 5) / 4.0)**0.6 - 0.05,
                            ((self.grid_map[i, j] - 5) / 4.0)**0.6 - 0.05,
                            ((self.grid_map[i, j] - 5) / 4.0)**0.6 - 0.05)
                    elif (self.grid_map[i, j] == -1):

                        cell.set_color(1, 0, 0)

                    else:
                        color = np.zeros(3)
                        cell.set_color(color[0], color[1], color[2])

                    self.viewer.add_geom(cell)

                    # if draw_cell_edge:
                    #     left_edge = rendering.FilledPolygon([(l - 1, b), (l - 1, t), (l + 1, t), (l + 1, b)])
                    #     #
                    #     bottom_edge = rendering.FilledPolygon([(l, b - 1), (l, b + 1), (r, b + 1), (r, b - 1)])
                    #
                    #     right_edge = rendering.FilledPolygon([(r - 1, b), (r - 1, t), (r + 1, t), (r + 1, b)])
                    #
                    #     top_edge = rendering.FilledPolygon([(l, t - 1), (l, t + 1), (r, t + 1), (r, t - 1)])
                    #
                    #     # right_edge.set_color(.3, .3, .3)
                    #     # bottom_edge.set_color(.3, .3, .3)
                    #
                    #     self.viewer.add_geom(right_edge)
                    #     self.viewer.add_geom(bottom_edge)
                    #     self.viewer.add_geom(left_edge)
                    #     self.viewer.add_geom(top_edge)

            q = self.point_pos

            q_x = screen_width / 2

            q_y = screen_height / 2

            point_mass = rendering.FilledPolygon([(q_x - 5, q_y - 5),
                                                  (q_x - 5, q_y + 5),
                                                  (q_x + 5, q_y + 5),
                                                  (q_x + 5, q_y - 5)])
            point_mass.set_color(0, 1, 1)
            self.point_mass_trans = rendering.Transform()
            point_mass.add_attr(self.point_mass_trans)

            self.viewer.add_geom(point_mass)

        if len(self.path_data) == 2:
            point_1 = self.path_data[0][0]
            point_2 = self.path_data[1][0]

            color_1 = self.path_data[0][1]
            color_2 = self.path_data[1][1]
            avg_color = ((color_1 + color_2) / 2.0)

            point_1_x = screen_width / 2 + (point_1[0] / self.grid_size *
                                            self.grid_vis_size)

            point_1_y = screen_height / 2 + (point_1[1] / self.grid_size *
                                             self.grid_vis_size)

            point_2_x = screen_width / 2 + (point_2[0] / self.grid_size *
                                            self.grid_vis_size)

            point_2_y = screen_height / 2 + (point_2[1] / self.grid_size *
                                             self.grid_vis_size)

            # path_segment = rendering.FilledPolygon(
            #     [(point_1_x - 1, point_1_y - 1), (point_1_x - 1, point_1_y + 1), (point_1_x + 1, point_1_y + 1),
            #      (point_1_x + 1, point_1_y - 1)])

            path_segment = rendering.Line(start=(point_1_x, point_1_y),
                                          end=(point_2_x, point_2_y))
            path_segment.attrs[-1] = rendering.LineWidth(10)
            # print(avg_color)
            if (avg_color < 0):

                path_segment.set_color(self.trail_color[0],
                                       self.trail_color[1],
                                       self.trail_color[2])
            else:
                path_segment.set_color(0, min(1, avg_color * 0.8), 0)

            self.viewer.add_geom(path_segment)

        q = self.point_pos

        q_x = (q[0] / self.grid_size * self.grid_vis_size)
        q_y = (q[1] / self.grid_size * self.grid_vis_size)

        self.point_mass_trans.set_translation(q_x, q_y)
        return self.viewer.render(return_rgb_array=mode == 'rgb_array')
Exemplo n.º 9
0
    def render_orthographic(self,
                            mode='human',
                            close=False,
                            action_idx=None,
                            action_vec=None):

        if close:
            if self.viewer_x is not None:
                self.viewer_x.close()
                self.viewer_x = None
            if self.viewer_y is not None:
                self.viewer_y.close()
                self.viewer_y = None
            if self.viewer_orthographic is not None:
                self.viewer_orthographic.close()
                self.viewer_orthographic = None
            return

        screen_width = 600
        screen_height = 600

        world_height = self.max_position_y - self.min_position_y  # same for y
        scale = (screen_height - 120) / world_height
        carwidth = 20
        carheight = 10

        if self.viewer_orthographic is None:
            self.viewer_orthographic = rendering.Viewer(
                screen_width, screen_height)

            # ys = np.linspace(self.min_position_y, self.max_position_y, 100)

            # zs = self._height(ys)
            # xyzs = list(zip((ys-self.min_position_y)*scale, zs*scale))

            # self.track = rendering.make_polyline(xyzs)
            # self.track.set_linewidth(4)
            # self.viewer_orthographic.add_geom(self.track)

            min_x = -math.pi / 6
            min_y = -math.pi / 6

            origin_res = 50
            origin_radius = 2

            origin_circle = rendering.make_circle(radius=origin_radius,
                                                  res=origin_res,
                                                  filled=True)
            origin_circle.set_color(0, 0, 0)
            origin_circle.add_attr(
                rendering.Transform(
                    translation=((min_x - self.min_position_x) * scale,
                                 (min_y - self.min_position_y) * scale)))
            self.viewer_orthographic.add_geom(origin_circle)

            radius_unscaled = math.sqrt((self.goal_position - min_x)**2 +
                                        (self.goal_position - min_y)**2)
            equilline_radius = radius_unscaled * scale
            #
            # points_x = []
            # points_y = []
            # offset_x, offset_y = (min_x - self.min_position_x) * scale, (min_y - self.min_position_y)*scale
            # for i in range(res):
            #     ang = 2 * math.pi * i / res
            #     points_x.append(offset_x + math.cos(ang) * radius)
            #     points_y.append(offset_y + math.sin(ang) * radius)
            #
            # equiline = list(zip(points_x, points_y))
            # self.track = rendering.make_polyline(equiline)
            # self.track.set_linewidth(4)
            equiline = rendering.make_circle(radius=equilline_radius,
                                             res=200,
                                             filled=False)
            equiline.set_color(0, 0, 0)
            equiline.add_attr(
                rendering.Transform(
                    translation=((min_x - self.min_position_x) * scale,
                                 (min_y - self.min_position_y) * scale)))
            equiline.add_attr(
                rendering.LineWidth(10))  # not sure why doesn't work
            self.viewer_orthographic.add_geom(equiline)

            clearance = 5
            clearance_wheels = 0

            l, r, t, b = -carwidth / 2, carwidth / 2, carheight / 2, -carheight / 2
            car = rendering.FilledPolygon([(l, b), (l, t), (r, t), (r, b)])
            car.add_attr(rendering.Transform(translation=(0, clearance)))
            self.cartrans_orth = rendering.Transform()
            car.add_attr(self.cartrans_orth)
            self.viewer_orthographic.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_wheels)))
            frontwheel.add_attr(self.cartrans_orth)
            self.viewer_orthographic.add_geom(frontwheel)
            backwheel = rendering.make_circle(carheight / 2.5)
            backwheel.add_attr(
                rendering.Transform(translation=(-carwidth / 4,
                                                 clearance_wheels)))
            backwheel.add_attr(self.cartrans_orth)
            backwheel.set_color(.5, .5, .5)
            self.viewer_orthographic.add_geom(backwheel)

            flagx = (self.goal_position - self.min_position_x) * scale
            flagy1 = (self.goal_position - self.min_position_y) * scale
            flagy2 = flagy1 + 50
            flagpole = rendering.Line((flagx, flagy1), (flagx, flagy2))
            self.viewer_orthographic.add_geom(flagpole)
            flag = rendering.FilledPolygon([(flagx, flagy2),
                                            (flagx, flagy2 - 10),
                                            (flagx + 25, flagy2 - 5)])
            flag.set_color(1, 0, 0)
            self.viewer_orthographic.add_geom(flag)

            # set trails
            if self.trailer:
                self.trail_trans = []
                trail_color_increment = 1.0 / self.trail_num
                for i in range(self.trail_num):
                    trail = rendering.make_circle(radius=5)
                    trail.set_color(1 - trail_color_increment * i, 0, 0)
                    trans = rendering.Transform()
                    trail.add_attr(trans)
                    self.viewer_orthographic.add_geom(trail)
                    self.trail_trans.append(trans)

        if action_idx is not None or action_vec is not None:
            actions = np.array([[0, 0], [-1, 0], [1, 0], [0, -1], [0, 1]])
            if action_idx is not None:
                action_vertex = tuple(actions[action_idx] * scale * 0.1)
            elif action_vec is not None:
                action_vertex = tuple(
                    np.sum(action_vec.reshape(-1, 1) * actions, axis=0) *
                    scale * 0.1)
            action_direction = rendering.make_polyline([(0, 0), action_vertex])
            action_direction.add_attr(self.cartrans_orth)
            action_direction.set_linewidth(2)
            action_direction.set_color(0, 1, 0)
            self.viewer_orthographic.add_onetime(action_direction)

        if self.show_velo == True:
            velocity_vertex = tuple(np.array(self.state[2:4]) * scale * 10)
            velocity = rendering.make_polyline([(0, 0), velocity_vertex])
            velocity.add_attr(self.cartrans_orth)
            velocity.set_linewidth(2)
            velocity.set_color(0, 0, 1)
            self.viewer_orthographic.add_onetime(velocity)

        if self.trailer:
            for i in range(len(self.last_few_positions)):
                self.trail_trans[i].set_translation(
                    (self.last_few_positions[i][0] - self.min_position_x) *
                    scale,
                    (self.last_few_positions[i][1] - self.min_position_y) *
                    scale)

        pos_x = self.state[0]
        pos_y = self.state[1]
        self.cartrans_orth.set_translation(
            (pos_x - self.min_position_x) * scale,
            (pos_y - self.min_position_y) * scale)

        return self.viewer_orthographic.render(
            return_rgb_array=mode == 'rgb_array')
Exemplo n.º 10
0
    def render(self, mode='human'):
        screen_size = 700
        screen_width = screen_size
        screen_height = screen_size
        distance_between = screen_size / ((self.highest - self.lowest) + 1)
        distance_between = int(distance_between)
        distance_start = distance_between / 2
        distance_start = int(distance_start)

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

            # Lines
            for i in range(distance_start, screen_size, distance_between):
                track = rendering.Line((0, i), (screen_width, i))
                track.set_color(0, 0, 0)
                self.viewer.add_geom(track)
                track = rendering.Line((i, 0), (i, screen_width))
                track.set_color(0, 0, 0)
                self.viewer.add_geom(track)

            # Traps (red dots)
            for x in self.traps:
                for y in self.traps:
                    trap = rendering.make_circle(500 / (self.highest + 1))
                    self.traptrans = rendering.Transform()
                    trap.add_attr(self.traptrans)
                    self.traptrans.set_translation(distance_start + x * distance_between,
                                                   distance_start + y * distance_between)
                    trap.set_color(1, 0, 0)
                    self.viewer.add_geom(trap)

            # Exits (green dots)
            for x in [self.lowest, self.highest]:
                for y in [self.lowest, self.highest]:
                    trap = rendering.make_circle(500 / (self.highest + 1))
                    self.traptrans = rendering.Transform()
                    trap.add_attr(self.traptrans)
                    self.traptrans.set_translation(distance_start + x * distance_between,
                                                   distance_start + y * distance_between)
                    trap.set_color(0, 1, 0)
                    self.viewer.add_geom(trap)

            # Player (blue dot)
            player = rendering.make_circle(500 / (self.highest + 1))
            self.playertrans = rendering.Transform()
            player.add_attr(self.playertrans)
            player.set_color(0, 0, 1)
            self.viewer.add_geom(player)
            print("JA")

            # Rotatetest
            bar = rendering.PolyLine([(0, 50), (50, 600)], 0)
            bar.linewidth = rendering.LineWidth(5)
            bar.set_color(1, 1, 0)
            # self.linewidth = rendering.LineWidth(30)
            # bar.add_attr(self.linewidth)
            self.viewer.add_geom(bar)
            print(bar.linewidth.stroke)

        if self.state is None: return None

        x = self.state
        self.playertrans.set_translation(distance_start + self.state[0] * distance_between,
                                         distance_start + self.state[1] * distance_between)

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