├── README.md ├── car.png ├── car.py ├── map.png ├── map02.png ├── map03.png ├── map2.png ├── map3.png ├── map4.png ├── map5.png ├── temp.py ├── track.jpg └── track.png /README.md: -------------------------------------------------------------------------------- 1 | # Transformative-AI-Simulation-for-Autonomous-Vehicle-Innovation -------------------------------------------------------------------------------- /car.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sibhisaran/Transformative-AI-Simulation-for-Autonomous-Vehicle-Innovation/e6ead6e43d3bb2c825d351965e7fb09f54c7070d/car.png -------------------------------------------------------------------------------- /car.py: -------------------------------------------------------------------------------- 1 | import math 2 | import random 3 | import sys 4 | import os 5 | import neat 6 | import pygame 7 | WIDTH = 1920 8 | HEIGHT = 1080 9 | CAR_SIZE_X = 60 10 | CAR_SIZE_Y = 60 11 | BORDER_COLOR = (255, 255, 255, 255) 12 | current_generation = 0 13 | class Car: 14 | 15 | def __init__(self): 16 | self.sprite = pygame.image.load('car.png').convert() 17 | self.sprite = pygame.transform.scale(self.sprite, (CAR_SIZE_X, CAR_SIZE_Y)) 18 | self.rotated_sprite = self.sprite 19 | self.position = [830, 920] 20 | self.angle = 0 21 | self.speed = 0 22 | 23 | self.speed_set = False 24 | 25 | self.center = [self.position[0] + CAR_SIZE_X / 2, self.position[1] + CAR_SIZE_Y / 2] 26 | 27 | self.radars = [] 28 | self.drawing_radars = [] 29 | 30 | self.alive = True 31 | 32 | self.distance = 0 33 | self.time = 0 34 | 35 | def draw(self, screen): 36 | screen.blit(self.rotated_sprite, self.position) 37 | self.draw_radar(screen) 38 | 39 | def draw_radar(self, screen): 40 | # Optionally Draw All Sensors / Radars 41 | for radar in self.radars: 42 | position = radar[0] 43 | pygame.draw.line(screen, (0, 255, 0), self.center, position, 1) 44 | pygame.draw.circle(screen, (0, 255, 0), position, 5) 45 | 46 | def check_collision(self, game_map): 47 | self.alive = True 48 | for point in self.corners: 49 | # If Any Corner Touches Border Color -> Crash 50 | # Assumes Rectangle 51 | if game_map.get_at((int(point[0]), int(point[1]))) == BORDER_COLOR: 52 | self.alive = False 53 | break 54 | 55 | def check_radar(self, degree, game_map): 56 | length = 0 57 | x = int(self.center[0] + math.cos(math.radians(360 - (self.angle + degree))) * length) 58 | y = int(self.center[1] + math.sin(math.radians(360 - (self.angle + degree))) * length) 59 | 60 | # While We Don't Hit BORDER_COLOR AND length < 300 (just a max) -> go further and further 61 | while not game_map.get_at((x, y)) == BORDER_COLOR and length < 300: 62 | length = length + 1 63 | x = int(self.center[0] + math.cos(math.radians(360 - (self.angle + degree))) * length) 64 | y = int(self.center[1] + math.sin(math.radians(360 - (self.angle + degree))) * length) 65 | 66 | # Calculate Distance To Border And Append To Radars List 67 | dist = int(math.sqrt(math.pow(x - self.center[0], 2) + math.pow(y - self.center[1], 2))) 68 | self.radars.append([(x, y), dist]) 69 | 70 | def update(self, game_map): 71 | if not self.speed_set: 72 | self.speed = 20 73 | self.speed_set = True 74 | self.rotated_sprite = self.rotate_center(self.sprite, self.angle) 75 | self.position[0] += math.cos(math.radians(360 - self.angle)) * self.speed 76 | self.position[0] = max(self.position[0], 20) 77 | self.position[0] = min(self.position[0], WIDTH - 120) 78 | self.distance += self.speed 79 | self.time += 1 80 | self.position[1] += math.sin(math.radians(360 - self.angle)) * self.speed 81 | self.position[1] = max(self.position[1], 20) 82 | self.position[1] = min(self.position[1], WIDTH - 120) 83 | self.center = [int(self.position[0]) + CAR_SIZE_X / 2, int(self.position[1]) + CAR_SIZE_Y / 2] 84 | length = 0.5 * CAR_SIZE_X 85 | left_top = [self.center[0] + math.cos(math.radians(360 - (self.angle + 30))) * length, self.center[1] + math.sin(math.radians(360 - (self.angle + 30))) * length] 86 | right_top = [self.center[0] + math.cos(math.radians(360 - (self.angle + 150))) * length, self.center[1] + math.sin(math.radians(360 - (self.angle + 150))) * length] 87 | left_bottom = [self.center[0] + math.cos(math.radians(360 - (self.angle + 210))) * length, self.center[1] + math.sin(math.radians(360 - (self.angle + 210))) * length] 88 | right_bottom = [self.center[0] + math.cos(math.radians(360 - (self.angle + 330))) * length, self.center[1] + math.sin(math.radians(360 - (self.angle + 330))) * length] 89 | self.corners = [left_top, right_top, left_bottom, right_bottom] 90 | self.check_collision(game_map) 91 | self.radars.clear() 92 | for d in range(-90, 120, 45): 93 | self.check_radar(d, game_map) 94 | 95 | def get_data(self): 96 | radars = self.radars 97 | return_values = [0, 0, 0, 0, 0] 98 | for i, radar in enumerate(radars): 99 | return_values[i] = int(radar[1] / 30) 100 | 101 | return return_values 102 | 103 | def is_alive(self): 104 | return self.alive 105 | 106 | def get_reward(self): 107 | return self.distance / (CAR_SIZE_X / 2) 108 | 109 | def rotate_center(self, image, angle): 110 | # Rotate The Rectangle 111 | rectangle = image.get_rect() 112 | rotated_image = pygame.transform.rotate(image, angle) 113 | rotated_rectangle = rectangle.copy() 114 | rotated_rectangle.center = rotated_image.get_rect().center 115 | rotated_image = rotated_image.subsurface(rotated_rectangle).copy() 116 | return rotated_image 117 | 118 | 119 | def run_simulation(genomes, config): 120 | 121 | nets = [] 122 | cars = [] 123 | pygame.init() 124 | screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN) 125 | for i, g in genomes: 126 | net = neat.nn.FeedForwardNetwork.create(g, config) 127 | nets.append(net) 128 | g.fitness = 0 129 | 130 | cars.append(Car()) 131 | clock = pygame.time.Clock() 132 | generation_font = pygame.font.SysFont("Arial", 30) 133 | alive_font = pygame.font.SysFont("Arial", 20) 134 | game_map = pygame.image.load('map3.png').convert() 135 | global current_generation 136 | current_generation += 1 137 | counter = 0 138 | 139 | while True: 140 | for event in pygame.event.get(): 141 | if event.type == pygame.QUIT: 142 | sys.exit(0) 143 | for i, car in enumerate(cars): 144 | output = nets[i].activate(car.get_data()) 145 | choice = output.index(max(output)) 146 | if choice == 0: 147 | car.angle += 10 148 | elif choice == 1: 149 | car.angle -= 10 150 | elif choice == 2: 151 | if(car.speed - 2 >= 12): 152 | car.speed -= 2 153 | else: 154 | car.speed += 2 155 | still_alive = 0 156 | for i, car in enumerate(cars): 157 | if car.is_alive(): 158 | still_alive += 1 159 | car.update(game_map) 160 | genomes[i][1].fitness += car.get_reward() 161 | 162 | if still_alive == 0: 163 | break 164 | 165 | counter += 1 166 | if counter == 30 * 40: 167 | break 168 | screen.blit(game_map, (0, 0)) 169 | for car in cars: 170 | if car.is_alive(): 171 | car.draw(screen) 172 | text = generation_font.render("Generation: " + str(current_generation), True, (0,0,0)) 173 | text_rect = text.get_rect() 174 | text_rect.center = (1000, 450) 175 | screen.blit(text, text_rect) 176 | 177 | text = alive_font.render("Still Alive: " + str(still_alive), True, (0, 0, 0)) 178 | text_rect = text.get_rect() 179 | text_rect.center = (900, 490) 180 | screen.blit(text, text_rect) 181 | 182 | pygame.display.flip() 183 | clock.tick(60) # 60 FPS 184 | 185 | if __name__ == "__main__": 186 | config_path = "./config.txt" 187 | config = neat.config.Config(neat.DefaultGenome, 188 | neat.DefaultReproduction, 189 | neat.DefaultSpeciesSet, 190 | neat.DefaultStagnation, 191 | config_path) 192 | 193 | population = neat.Population(config) 194 | population.add_reporter(neat.StdOutReporter(True)) 195 | stats = neat.StatisticsReporter() 196 | population.add_reporter(stats) 197 | population.run(run_simulation, 1000) 198 | -------------------------------------------------------------------------------- /map.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sibhisaran/Transformative-AI-Simulation-for-Autonomous-Vehicle-Innovation/e6ead6e43d3bb2c825d351965e7fb09f54c7070d/map.png -------------------------------------------------------------------------------- /map02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sibhisaran/Transformative-AI-Simulation-for-Autonomous-Vehicle-Innovation/e6ead6e43d3bb2c825d351965e7fb09f54c7070d/map02.png -------------------------------------------------------------------------------- /map03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sibhisaran/Transformative-AI-Simulation-for-Autonomous-Vehicle-Innovation/e6ead6e43d3bb2c825d351965e7fb09f54c7070d/map03.png -------------------------------------------------------------------------------- /map2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sibhisaran/Transformative-AI-Simulation-for-Autonomous-Vehicle-Innovation/e6ead6e43d3bb2c825d351965e7fb09f54c7070d/map2.png -------------------------------------------------------------------------------- /map3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sibhisaran/Transformative-AI-Simulation-for-Autonomous-Vehicle-Innovation/e6ead6e43d3bb2c825d351965e7fb09f54c7070d/map3.png -------------------------------------------------------------------------------- /map4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sibhisaran/Transformative-AI-Simulation-for-Autonomous-Vehicle-Innovation/e6ead6e43d3bb2c825d351965e7fb09f54c7070d/map4.png -------------------------------------------------------------------------------- /map5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sibhisaran/Transformative-AI-Simulation-for-Autonomous-Vehicle-Innovation/e6ead6e43d3bb2c825d351965e7fb09f54c7070d/map5.png -------------------------------------------------------------------------------- /temp.py: -------------------------------------------------------------------------------- 1 | import math 2 | import random 3 | import sys 4 | import os 5 | import neat 6 | import pygame 7 | WIDTH = 1920 8 | HEIGHT = 1080 9 | CAR_SIZE_X = 60 10 | CAR_SIZE_Y = 60 11 | BORDER_COLOR = (255, 255, 255, 255) 12 | current_generation = 0 13 | class Car: 14 | def __init__(self): 15 | self.sprite = pygame.image.load('car.png').convert() 16 | self.sprite = pygame.transform.scale(self.sprite, (CAR_SIZE_X, CAR_SIZE_Y)) 17 | self.rotated_sprite = self.sprite 18 | self.position = [830, 920] 19 | self.angle = 0 20 | self.speed = 0 21 | self.speed_set = False 22 | self.center = [self.position[0] + CAR_SIZE_X / 2, self.position[1] + CAR_SIZE_Y / 2] 23 | self.radars = [] 24 | self.drawing_radars = [] 25 | self.alive = True 26 | self.distance = 0 27 | self.time = 0 28 | 29 | def draw(self, screen): 30 | screen.blit(self.rotated_sprite, self.position) 31 | self.draw_radar(screen) 32 | 33 | def draw_radar(self, screen): 34 | for radar in self.radars: 35 | position = radar[0] 36 | pygame.draw.line(screen, (0, 255, 0), self.center, position, 1) 37 | pygame.draw.circle(screen, (0, 255, 0), position, 5) 38 | 39 | def check_collision(self, game_map): 40 | self.alive = True 41 | for point in self.corners: 42 | if game_map.get_at((int(point[0]), int(point[1]))) == BORDER_COLOR: 43 | self.alive = False 44 | break 45 | 46 | def check_radar(self, degree, game_map): 47 | length = 0 48 | x = int(self.center[0] + math.cos(math.radians(360 - (self.angle + degree))) * length) 49 | y = int(self.center[1] + math.sin(math.radians(360 - (self.angle + degree))) * length) 50 | while not game_map.get_at((x, y)) == BORDER_COLOR and length < 300: 51 | length = length + 1 52 | x = int(self.center[0] + math.cos(math.radians(360 - (self.angle + degree))) * length) 53 | y = int(self.center[1] + math.sin(math.radians(360 - (self.angle + degree))) * length) 54 | dist = int(math.sqrt(math.pow(x - self.center[0], 2) + math.pow(y - self.center[1], 2))) 55 | self.radars.append([(x, y), dist]) 56 | 57 | def update(self, game_map): 58 | if not self.speed_set: 59 | self.speed = 20 60 | self.speed_set = True 61 | self.rotated_sprite = self.rotate_center(self.sprite, self.angle) 62 | self.position[0] += math.cos(math.radians(360 - self.angle)) * self.speed 63 | self.position[0] = max(self.position[0], 20) 64 | self.position[0] = min(self.position[0], WIDTH - 120) 65 | self.distance += self.speed 66 | self.time += 1 67 | self.position[1] += math.sin(math.radians(360 - self.angle)) * self.speed 68 | self.position[1] = max(self.position[1], 20) 69 | self.position[1] = min(self.position[1], WIDTH - 120) 70 | self.center = [int(self.position[0]) + CAR_SIZE_X / 2, int(self.position[1]) + CAR_SIZE_Y / 2] 71 | length = 0.5 * CAR_SIZE_X 72 | left_top = [self.center[0] + math.cos(math.radians(360 - (self.angle + 30))) * length, self.center[1] + math.sin(math.radians(360 - (self.angle + 30))) * length] 73 | right_top = [self.center[0] + math.cos(math.radians(360 - (self.angle + 150))) * length, self.center[1] + math.sin(math.radians(360 - (self.angle + 150))) * length] 74 | left_bottom = [self.center[0] + math.cos(math.radians(360 - (self.angle + 210))) * length, self.center[1] + math.sin(math.radians(360 - (self.angle + 210))) * length] 75 | right_bottom = [self.center[0] + math.cos(math.radians(360 - (self.angle + 330))) * length, self.center[1] + math.sin(math.radians(360 - (self.angle + 330))) * length] 76 | self.corners = [left_top, right_top, left_bottom, right_bottom] 77 | self.check_collision(game_map) 78 | self.radars.clear() 79 | for d in range(-90, 120, 45): 80 | self.check_radar(d, game_map) 81 | 82 | def get_data(self): 83 | radars = self.radars 84 | return_values = [0, 0, 0, 0, 0] 85 | for i, radar in enumerate(radars): 86 | return_values[i] = int(radar[1] / 30) 87 | 88 | return return_values 89 | 90 | def is_alive(self): 91 | return self.alive 92 | 93 | def get_reward(self): 94 | return self.distance / (CAR_SIZE_X / 2) 95 | 96 | def rotate_center(self, image, angle): 97 | rectangle = image.get_rect() 98 | rotated_image = pygame.transform.rotate(image, angle) 99 | rotated_rectangle = rectangle.copy() 100 | rotated_rectangle.center = rotated_image.get_rect().center 101 | rotated_image = rotated_image.subsurface(rotated_rectangle).copy() 102 | return rotated_image 103 | 104 | def run_simulation(genomes, config): 105 | nets = [] 106 | cars = [] 107 | pygame.init() 108 | screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN) 109 | display_map = pygame.image.load('map02.png').convert() 110 | game_map = pygame.image.load('map2.png').convert() 111 | for i, g in genomes: 112 | net = neat.nn.FeedForwardNetwork.create(g, config) 113 | nets.append(net) 114 | g.fitness = 0 115 | cars.append(Car()) 116 | 117 | clock = pygame.time.Clock() 118 | generation_font = pygame.font.SysFont("Arial", 30) 119 | alive_font = pygame.font.SysFont("Arial", 20) 120 | 121 | global current_generation 122 | current_generation += 1 123 | counter = 0 124 | 125 | while True: 126 | for event in pygame.event.get(): 127 | if event.type == pygame.QUIT: 128 | sys.exit(0) 129 | for i, car in enumerate(cars): 130 | output = nets[i].activate(car.get_data()) 131 | choice = output.index(max(output)) 132 | if choice == 0: 133 | car.angle += 10 134 | elif choice == 1: 135 | car.angle -= 10 136 | elif choice == 2: 137 | if car.speed - 2 >= 12: 138 | car.speed -= 2 139 | else: 140 | car.speed += 2 141 | still_alive = 0 142 | for i, car in enumerate(cars): 143 | if car.is_alive(): 144 | still_alive += 1 145 | car.update(game_map) 146 | genomes[i][1].fitness += car.get_reward() 147 | 148 | if still_alive == 0: 149 | break 150 | 151 | counter += 1 152 | if counter == 30 * 40: 153 | break 154 | screen.blit(display_map, (0, 0)) 155 | for car in cars: 156 | if car.is_alive(): 157 | car.draw(screen) 158 | text = generation_font.render("Generation: " + str(current_generation), True, (0, 0, 0)) 159 | text_rect = text.get_rect() 160 | text_rect.center = (1000, 450) 161 | screen.blit(text, text_rect) 162 | text = alive_font.render("Alive: " + str(still_alive), True, (0, 0, 0)) 163 | text_rect = text.get_rect() 164 | text_rect.center = (1000, 490) 165 | screen.blit(text, text_rect) 166 | pygame.display.flip() 167 | clock.tick(60) 168 | if __name__ == "__main__": 169 | config_path = "./config.txt" 170 | config = neat.config.Config(neat.DefaultGenome, 171 | neat.DefaultReproduction, 172 | neat.DefaultSpeciesSet, 173 | neat.DefaultStagnation, 174 | config_path) 175 | 176 | population = neat.Population(config) 177 | population.add_reporter(neat.StdOutReporter(True)) 178 | stats = neat.StatisticsReporter() 179 | population.add_reporter(stats) 180 | population.run(run_simulation, 1000) 181 | -------------------------------------------------------------------------------- /track.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sibhisaran/Transformative-AI-Simulation-for-Autonomous-Vehicle-Innovation/e6ead6e43d3bb2c825d351965e7fb09f54c7070d/track.jpg -------------------------------------------------------------------------------- /track.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sibhisaran/Transformative-AI-Simulation-for-Autonomous-Vehicle-Innovation/e6ead6e43d3bb2c825d351965e7fb09f54c7070d/track.png --------------------------------------------------------------------------------