Beispiel #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
Beispiel #2
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)
        RoadGraphics.display_traffic(self.env.road, self.sim_surface)

        if self.agent_display:
            self.agent_display(self.agent_surface, self.sim_surface)
            if self.SCREEN_WIDTH > self.SCREEN_HEIGHT:
                self.screen.blit(self.agent_surface, (0, self.SCREEN_HEIGHT))
            else:
                self.screen.blit(self.agent_surface, (self.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
Beispiel #3
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
Beispiel #4
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