Example #1
0
    def display(self):
        """
            Display the road and vehicles on a pygame window.
        """
        if not self.enabled:
            return

        self.sim_surface.move_display_window_to(self.window_position())
        RoadGraphics.display(self.env.road, self.sim_surface)
        if self.vehicle_trajectory:
            VehicleGraphics.display_trajectory(self.vehicle_trajectory,
                                               self.sim_surface)
        RoadGraphics.display_traffic(self.env.road, self.sim_surface)

        if self.agent_display:
            self.agent_display(self.agent_surface, self.sim_surface)
            if self.env.config["screen_width"] > self.env.config[
                    "screen_height"]:
                self.screen.blit(self.agent_surface,
                                 (0, self.env.config["screen_height"]))
            else:
                self.screen.blit(self.agent_surface,
                                 (self.env.config["screen_width"], 0))

        self.screen.blit(self.sim_surface, (0, 0))
        self.clock.tick(self.env.SIMULATION_FREQUENCY)
        pygame.display.flip()

        if self.SAVE_IMAGES:
            pygame.image.save(self.screen,
                              "highway-env_{}.png".format(self.frame))
            self.frame += 1
Example #2
0
    def display_traffic_bigmap(cls, env, road, surface):
        """
            Display the road vehicles on a surface.

        :param road: the road to be displayed
        :param surface: the pygame surface
        """
        commands = {}
        ego_car = env.vehicle
        # dt = 1.5 / env.SIMULATION_FREQUENCY
        # ego_car_vx = ego_car.velocity * math.cos(ego_car.heading * 180 / np.pi)
        # ego_car_vy = ego_car.velocity * math.sin(ego_car.heading * 180 / np.pi)
        # vehicle_dx = ego_car_vx * dt
        # vehicle_dy = ego_car_vy * dt
        for v in road.vehicles:
            # v.position -= [vehicle_dx,vehicle_dy]
            # if np.linalg.norm(v.position - ego_car.position) < 75:
            #     if v.lane_index in ego_car.lanes_around or hasattr(v,"state"):
            VehicleGraphics.display(v, surface, command_dict=commands)
            # else:
            #     if not hasattr(v, "state"):
            #         env.road.vehicles.remove(v)
        # print(commands)
        # m.send_message(commands)
        return commands
Example #3
0
    def display_traffic(cls, road, surface):
        """
            Display the road vehicles on a surface.

        :param road: the road to be displayed
        :param surface: the pygame surface
        """
        for v in road.vehicles:
            VehicleGraphics.display(v, surface)
 def handle_events(self):
     """
         Handle pygame events by forwarding them to the display and environment vehicle.
     """
     for event in pygame.event.get():
         if event.type == pygame.QUIT:
             self.env.close()
         self.sim_surface.handle_event(event)
         if self.env.vehicle:
             VehicleGraphics.handle_event(self.env.vehicle, event)
Example #5
0
    def display_traffic(cls, road, surface, offscreen=False):
        """
            Display the road vehicles on a surface.

        :param road: the road to be displayed
        :param surface: the pygame surface
        """
        print("[INFO] in display_traffic, offscreen={}".format(offscreen))
        for v in road.vehicles:
            VehicleGraphics.display(v, surface, offscreen=offscreen)
Example #6
0
    def display_obstacles(road, surface, offscreen=False):
        """
            Display the obstacles on a surface.

        :param road: the road to be displayed
        :param surface: the pygame surface
        :param offscreen: whether the rendering should be done offscreen or not
        """
        for o in road.obstacles:
            VehicleGraphics.display(o, surface, offscreen=offscreen)
    def display_traffic(cls, road, surface, simulation_frequency=15, offscreen=False):
        """
            Display the road vehicles on a surface.

        :param road: the road to be displayed
        :param surface: the pygame surface
        :param simulation_frequency: simulation frequency
        """
        if road.record_history:
            for v in road.vehicles:
                VehicleGraphics.display_history(v, surface, simulation=simulation_frequency, offscreen=offscreen)
        for v in road.vehicles:
            VehicleGraphics.display(v, surface, offscreen=offscreen)
Example #8
0
    def display_traffic(cls, road, surface):
        """
            Display the road vehicles on a surface.

        :param road: the road to be displayed
        :param surface: the pygame surface
        """
        commands = {}
        for v in road.vehicles:
            VehicleGraphics.display(v, surface, command_dict=commands)
        # print(commands)
        # m.send_message(commands)
        return commands
Example #9
0
    def display(self) -> None:
        """Display the road and vehicles on a pygame window."""
        if not self.enabled:
            return
        self.sim_surface.move_display_window_to(self.window_position())
        RoadGraphics.display(self.env.road, self.sim_surface)
        if self.vehicle_trajectory:
            VehicleGraphics.display_trajectory(self.vehicle_trajectory,
                                               self.sim_surface,
                                               offscreen=self.offscreen)

        RoadGraphics.display_road_objects(self.env.road,
                                          self.sim_surface,
                                          offscreen=self.offscreen)

        if self.agent_display:
            self.agent_display(self.agent_surface, self.sim_surface)
            if not self.offscreen:
                if self.config["screen_width"] > self.config["screen_height"]:
                    self.screen.blit(self.agent_surface,
                                     (0, self.config["screen_height"]))
                else:
                    self.screen.blit(self.agent_surface,
                                     (self.config["screen_width"], 0))

        RoadGraphics.display_traffic(
            self.env.road,
            self.sim_surface,
            simulation_frequency=self.env.config["simulation_frequency"],
            offscreen=self.offscreen)

        VehicleGraphics.display(self.env.vehicle,
                                self.sim_surface,
                                offscreen=self.offscreen,
                                ego=True)

        # ObservationGraphics.display(self.env.observation_type, self.sim_surface)

        if not self.offscreen:
            self.screen.blit(self.sim_surface, (0, 0))
            if self.env.config["real_time_rendering"]:
                self.clock.tick(self.env.config["simulation_frequency"])
            pygame.display.flip()

        if self.SAVE_IMAGES and self.directory:
            pygame.image.save(
                self.sim_surface,
                str(self.directory / "highway-env_{}.png".format(self.frame)))
            self.frame += 1
Example #10
0
    def display_traffic(road: Road, surface: WorldSurface, simulation_frequency: int = 15, offscreen: bool = False) \
            -> None:
        """
        Display the road vehicles on a surface.

        :param road: the road to be displayed
        :param surface: the pygame surface
        :param simulation_frequency: simulation frequency
        :param offscreen: render without displaying on a screen
        """
        if road.record_history:
            for v in road.vehicles:
                VehicleGraphics.display_history(v, surface, simulation=simulation_frequency, offscreen=offscreen)
        for v in road.vehicles:
            VehicleGraphics.display(v, surface, offscreen=offscreen)
    def handle_events(self):
        """
            Handle pygame events by forwarding them to the display and environment vehicle.
        """
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.env.close()
            self.sim_surface.handle_event(event)
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    pause = True
                    while pause == True:
                        for event in pygame.event.get():
                            if event.type == pygame.KEYDOWN:
                                if event.key == pygame.K_SPACE:
                                    pause = False

            if self.env.vehicle:
                VehicleGraphics.handle_event(self.env.vehicle, event)
Example #12
0
    def display(self) -> None:
        """Display the road and vehicles on a pygame window."""
        if not self.enabled:
            return
        if self.start:
            model_config = {
                "type": "EgoAttentionNetwork",
                "feature_size": 64,
                "embedding_layer": {
                    "type": "MultiLayerPerceptron",
                    "layers": [64, 64],
                    "reshape": False,
                    "in": 7
                },
                "others_embedding_layer": {
                    "type": "MultiLayerPerceptron",
                    "layers": [64, 64],
                    "reshape": False,
                    "in": 7
                },
                "self_attention_layer": {
                    "type": "SelfAttention",
                    "feature_size": 64,
                    "heads": 2
                },
                "attention_layer": {
                    "type": "EgoAttention",
                    "feature_size": 64,
                    "heads": 2
                },
                "output_layer": {
                    "type": "MultiLayerPerceptron",
                    "layers": [64, 64],
                    "reshape": False,
                },
                # "heads": 2,
                # "dropout_factor": 0
                "out": 3
            }
            self.q_network = models_eshagh.EgoAttentionNetwork(
                model_config).to('cpu')
            self.target_network = models_eshagh.EgoAttentionNetwork(
                model_config).to('cpu')
            self.target_network.load_state_dict(self.q_network.state_dict())
            torch.save(
                self.q_network.state_dict(),
                "/u/05/aznarr1/unix/Documents/Projectintersection/highway-env-master/model"
                + "/q_network_1_3_2.pt")
            self.start = False

        self.sim_surface.move_display_window_to(self.window_position())
        RoadGraphics.display(self.env.road, self.sim_surface)

        if self.vehicle_trajectory:
            VehicleGraphics.display_trajectory(self.vehicle_trajectory,
                                               self.sim_surface,
                                               offscreen=self.offscreen)

        RoadGraphics.display_road_objects(self.env.road,
                                          self.sim_surface,
                                          offscreen=self.offscreen)
        """load_path = "/u/05/aznarr1/unix/Documents/Projectintersection/highway-env-master/model"
        self.q_network.load_state_dict(torch.load(load_path + "/q_network_1_3_2.pt"))


        observat = KinematicObservation(self.env).observe()
        logits, aaaamatrix, selfattmatrixs = self.q_network.forward(observat.reshape((1,)+observat.shape))
        action = torch.argmax(logits, dim=1).tolist()[0]
        #print(logits)
        #print('graph',aaaamatrix)
        aaaamatrix = torch.squeeze(aaaamatrix)

        aamatrix = aaaamatrix.cpu().detach().numpy()
        selfamatrix = torch.squeeze(selfattmatrixs)
        smatrix = selfamatrix.cpu().detach().numpy()
        #print(smatrix.shape)
        #print('FOR TEST',observat)
        #print(aamatrix.shape)
        #xxx = dqn_self_attention.attmatrix(observat)
        #print(xxx)
        """ """ position = [*self.sim_surface.pos2pix(observat[1][1]*100, observat[1][2]*100)]
        if observat[2][0]==1:
            position2 = [*self.sim_surface.pos2pix(observat[2][1]*100, observat[2][2]*100)]
        else:
            if observat[2][0]==1:
                position2 = [*self.sim_surface.pos2pix(observat[2][1]*100, observat[2][2]*100)]
            else:
                if observat[3][0]==1:
                    position2 = [*self.sim_surface.pos2pix(observat[3][1]*100, observat[3][2]*100)]
                else:
                    if observat[4][0]==1:
                        position2 = [*self.sim_surface.pos2pix(observat[4][1]*100, observat[4][2]*100)]
                    else:
                        if observat[5][0]==1:
                            position2 = [*self.sim_surface.pos2pix(observat[5][1]*100, observat[5][2]*100)]
                        else:
                            position2 = [*self.sim_surface.pos2pix(observat[6][1]*100, observat[6][2]*100)]
        print(position)
        print(position2)
        #pygame.draw.rect(self.sim_surface, (50, 200, 0), position)
        pygame.draw.line(self.sim_surface, (50, 200, 0), position,position2, 10) """ """
        #print(x)
        #print()
        originx=observat[0][1]*100
        originy=observat[0][2]*100
        #origgiinn=PositionType([originx, originy])
        self.sim_surface.move_display_window_to2(originx,originy)
        i=0
        j=0
        print(aamatrix)
        print(smatrix)
        while i < 15:
            j=0
            while j < 15:
                if observat[i][0]>0 and observat[j][0]>0:
                    if i==j:
                        position = self.sim_surface.pos2pix(observat[i][1]*100, observat[i][2]*100)
                        position2 = self.sim_surface.pos2pix(observat[j][1]*100, observat[j][2]*100)
                        pygame.draw.circle(self.sim_surface, (50, 200, 0), position, int(smatrix[0][i][j]*20))
                        pygame.draw.circle(self.sim_surface, (50, 0, 200), position, int(smatrix[1][i][j]*20))
                    position = self.sim_surface.pos2pix(observat[i][1]*100, observat[i][2]*100)
                    position2 = self.sim_surface.pos2pix(observat[j][1]*100, observat[j][2]*100)
                    pygame.draw.line(self.sim_surface, (50, 200, 0), position, position2, int(smatrix[0][i][j]*10))
                    pygame.draw.line(self.sim_surface, (50, 00, 200), position, position2, int(smatrix[1][i][j]*10))
                j=j+1
            i=i+1
        k=0
        while k < 15:
            position = self.sim_surface.pos2pix(observat[0][1]*100, observat[0][2]*100)
            position2 = self.sim_surface.pos2pix(observat[k][1]*100, observat[k][2]*100)
            pygame.draw.line(self.sim_surface, (200, 200, 0), position, position2, int(smatrix[0][0][k]*10))
            pygame.draw.line(self.sim_surface, (200, 200, 200), position, position2, int(smatrix[1][0][k]*10))
            k=k+1

        position = self.sim_surface.pos2pix(observat[0][1]*100, observat[0][2]*100)
        pygame.draw.circle(self.sim_surface, (200, 200, 0), position, int(smatrix[0][0][0]*20))
        pygame.draw.circle(self.sim_surface, (200, 200, 200), position, int(smatrix[1][0][0]*20))

        k=0
        while k < 15:
            position = self.sim_surface.pos2pix(observat[0][1]*100, observat[0][2]*100)
            position2 = self.sim_surface.pos2pix(observat[k][1]*100, observat[k][2]*100)
            pygame.draw.line(self.sim_surface, (200, 0, 0), position, position2, int(aamatrix[0][k]*10))
            pygame.draw.line(self.sim_surface, (250, 150, 30), position, position2, int(aamatrix[1][k]*10))
            k=k+1

        position = self.sim_surface.pos2pix(observat[0][1]*100, observat[0][2]*100)
        pygame.draw.circle(self.sim_surface, (200, 0, 0), position, int(aamatrix[0][0]*20))
        pygame.draw.circle(self.sim_surface, (250, 150, 30), position, int(aamatrix[1][0]*20))"""
        """position3 = self.sim_surface.pos2pix(observat[2][1]*100, observat[2][2]*100)
                position4 = self.sim_surface.pos2pix(observat[3][1]*100, observat[3][2]*100)
                position5 = self.sim_surface.pos2pix(observat[4][1]*100, observat[4][2]*100)
                position6 = self.sim_surface.pos2pix(observat[5][1]*100, observat[5][2]*100)
                position7 = self.sim_surface.pos2pix(observat[6][1]*100, observat[6][2]*100)
                position8 = self.sim_surface.pos2pix(observat[7][1]*100, observat[7][2]*100)
                position9 = self.sim_surface.pos2pix(observat[8][1]*100, observat[8][2]*100)
                position10 = self.sim_surface.pos2pix(observat[9][1]*100, observat[9][2]*100)


                pygame.draw.line(self.sim_surface, (50, 200, 0), position, position3, int(aamatrix[0][2]*10))
                pygame.draw.line(self.sim_surface, (50, 200, 0), position, position4, int(aamatrix[0][3]*10))
                pygame.draw.line(self.sim_surface, (50, 200, 0), position, position5, int(aamatrix[0][4]*10))
                pygame.draw.line(self.sim_surface, (50, 200, 0), position, position6, int(aamatrix[0][5]*10))
                pygame.draw.line(self.sim_surface, (50, 200, 0), position, position7, int(aamatrix[0][6]*10))
                pygame.draw.line(self.sim_surface, (50, 200, 0), position, position8, int(aamatrix[0][7]*10))
                pygame.draw.line(self.sim_surface, (50, 200, 0), position, position9, int(aamatrix[0][8]*10))
                pygame.draw.line(self.sim_surface, (50, 200, 200), position, position2, int(aamatrix[1][1]*10))
                pygame.draw.line(self.sim_surface, (50, 200, 200), position, position3, int(aamatrix[1][2]*10))
                pygame.draw.line(self.sim_surface, (50, 200, 200), position, position4, int(aamatrix[1][3]*10))
                pygame.draw.line(self.sim_surface, (50, 200, 200), position, position5, int(aamatrix[1][4]*10))
                pygame.draw.line(self.sim_surface, (50, 200, 200), position, position6, int(aamatrix[1][5]*10))
                pygame.draw.line(self.sim_surface, (50, 200, 200), position, position7, int(aamatrix[1][6]*10))"""
        self.sim_surface.move_display_window_to(self.window_position())
        #print(position)
        if self.agent_display:
            self.agent_display(self.agent_surface, self.sim_surface)
            if not self.offscreen:
                if self.env.config["screen_width"] > self.env.config[
                        "screen_height"]:
                    self.screen.blit(self.agent_surface,
                                     (0, self.env.config["screen_height"]))
                else:
                    self.screen.blit(self.agent_surface,
                                     (self.env.config["screen_width"], 0))

        RoadGraphics.display_traffic(
            self.env.road,
            self.sim_surface,
            simulation_frequency=self.env.config["simulation_frequency"],
            offscreen=self.offscreen)

        if not self.offscreen:
            self.screen.blit(self.sim_surface, (0, 0))
            self.clock.tick(self.env.config["simulation_frequency"])
            pygame.display.flip()

        if self.SAVE_IMAGES and self.directory:
            pygame.image.save(
                self.sim_surface,
                str(self.directory / "highway-env_{}.png".format(self.frame)))
            self.frame += 1