Example #1
0
    def visualize(self, scale, step):
        """
        Рисует картинку. Этот и все "приватные" (начинающиеся с _) методы необязательны для разбора.
        """
        for i, agent in enumerate(self.agents):
            state = self.agent_states[agent]
            collisions = self.collisions[agent]
            surface = self._agent_surfaces[i]
            rays_lengths = self.vision_for(agent)[-agent.rays:]
            self._agent_images[i] = [self._draw_ladar(rays_lengths, state, scale),
                                     self._get_agent_image(surface, state, scale)]

        if len(self.agents) == 1:
            a = self.agents[0]
            draw_text("Step: %i" % step, self._info_surface, scale, self.size,
                      text_color=white, bg_color=black, tlpoint=(self._info_surface.get_width() - 100, self._info_surface.get_height() - 30))
            draw_text("Reward: %.3f" % a.reward_history[-1], self._info_surface, scale, self.size,
                      text_color=white, bg_color=black)
            steer, acc = a.chosen_actions_history[-1]
            state = self.agent_states[a]
            draw_text("Action: steer.: %.2f, accel: %.2f" % (steer, acc), self._info_surface, scale,
                      self.size, text_color=white, bg_color=black, tlpoint=(self._info_surface.get_width()/2-200, self._info_surface.get_height()/2-50))
            draw_text("Inputs: |v|=%.2f, sin(angle): %.2f" % (
                abs(state.velocity), np.sin(angle(-state.position, state.heading))),
                      self._info_surface, scale,
                      self.size, text_color=white, bg_color=black, tlpoint=(self._info_surface.get_width()/2-200, self._info_surface.get_height()/2))
            draw_text("Circle: %.2f, collisions: %i" % (self.circles[a]/2/pi, collisions), self._info_surface, scale,
                      self.size, text_color=white, bg_color=black,
                      tlpoint=(self._info_surface.get_width() / 2 - 200, self._info_surface.get_height() / 2 + 50))
Example #2
0
    def visualize(self, scale, step):
        """
        Рисует картинку. Этот и все "приватные" (начинающиеся с _) методы необязательны для разбора.
        """
        for i, agent in enumerate(self.agents):
            state = self.agent_states[agent]
            collisions = self.collisions[agent]
            surface = self._agent_surfaces[i]
            rays_lengths = self.vision_for(agent)[-agent.rays:]
            self._agent_images[i] = [
                self._draw_ladar(rays_lengths, state, scale),
                self._get_agent_image(surface, state, scale)
            ]

        if len(self.agents) == 1:
            a = self.agents[0]
            draw_text("Step: %i" % step,
                      self._info_surface,
                      scale,
                      self.size,
                      text_color=white,
                      bg_color=black,
                      tlpoint=(self._info_surface.get_width() - 100,
                               self._info_surface.get_height() - 30))
            draw_text("Reward: %.3f" % a.reward_history[-1],
                      self._info_surface,
                      scale,
                      self.size,
                      text_color=white,
                      bg_color=black)
            steer, acc = a.chosen_actions_history[-1]
            state = self.agent_states[a]
            draw_text("Action: steer.: %.2f, accel: %.2f" % (steer, acc),
                      self._info_surface,
                      scale,
                      self.size,
                      text_color=white,
                      bg_color=black,
                      tlpoint=(self._info_surface.get_width() / 2 - 200,
                               self._info_surface.get_height() / 2 - 50))
            draw_text("Inputs: |v|=%.2f, sin(angle): %.2f" %
                      (abs(state.velocity),
                       np.sin(angle(-state.position, state.heading))),
                      self._info_surface,
                      scale,
                      self.size,
                      text_color=white,
                      bg_color=black,
                      tlpoint=(self._info_surface.get_width() / 2 - 200,
                               self._info_surface.get_height() / 2))
            draw_text("Circle: %.2f, collisions: %i" %
                      (self.circles[a] / 2 / pi, collisions),
                      self._info_surface,
                      scale,
                      self.size,
                      text_color=white,
                      bg_color=black,
                      tlpoint=(self._info_surface.get_width() / 2 - 200,
                               self._info_surface.get_height() / 2 + 50))
Example #3
0
    def _draw_ladar(self, sensors, state, scale, show_labels=False):
        surface = pygame.display.get_surface().copy()
        surface.fill(white)
        surface.set_colorkey(white)
        start_pos = to_px(state.position, scale, surface.get_size())
        delta = pi / (len(sensors) - 1)
        ray = phase(state.heading) - pi / 2
        for s in sensors:
            end_pos = to_px(
                rect(s, ray) + state.position, scale, surface.get_size())
            pygame.draw.line(surface, (0, 255, 0), start_pos, end_pos, 2)
            if show_labels:
                draw_text(str(round(s, 2)),
                          surface,
                          1.0, (100, 50),
                          tlpoint=(end_pos, end_pos))
            ray += delta

        rectangle = surface.get_rect()
        rectangle.topleft = (0, 0)
        return surface, rectangle
    def visualize(self, scale):
        """
        Рисует картинку. Этот и все "приватные" (начинающиеся с _) методы необязательны для разбора.
        """
        for i, agent in enumerate(self.agents):
            state = self.agent_states[agent]
            surface = self._agent_surfaces[i]
            rays_lengths = self.vision_for(agent)[-agent.rays:]
            self._agent_images[i] = [self._draw_ladar(rays_lengths, state, scale),
                                     self._get_agent_image(surface, state, scale)]

        if len(self.agents) == 1:
            a = self.agents[0]
            draw_text("Reward: %.3f, Q Value: %.3f" % (self.learner.last_reward, self.learner.last_qvalue), self._info_surface, scale, self.size,
                      text_color=white, bg_color=black)
            try:
                steer, acc = self.learner.history[-1].action
            except IndexError:
                steer = 0
                acc = 0
            state = self.agent_states[a]
            draw_text("Action: steer.: %.2f, accel: %.2f" % (steer, acc), self._info_surface, scale,
                      self.size, text_color=white, bg_color=black, tlpoint=(self._info_surface.get_width() - 500, 10))
            draw_text("Inputs: |v|=%.2f, sin(angle): %.2f, circle: %.2f" % (
                abs(state.velocity), np.sin(angle(-state.position, state.heading)), self.circles[a]),
                      self._info_surface, scale,
                      self.size, text_color=white, bg_color=black, tlpoint=(self._info_surface.get_width() - 500, 50))