-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneat_car.py
95 lines (77 loc) · 2.75 KB
/
neat_car.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pygame
import math
import os
import sys
import neat
import pickle
# Add this at the top of the file
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parent.parent))
from car import Car
CAR_SIZE_X = 30
CAR_SIZE_Y = 30
BORDER_COLOR = (255, 255, 255, 255) # Color To Crash on Hit
class NeatCar(Car):
def __init__(self, net=None, position=None):
super().__init__(position=position, angle=0)
self.net = net
self.sprite = pygame.image.load("./neat_/car.png").convert_alpha()
self.sprite = pygame.transform.scale(self.sprite, (CAR_SIZE_X, CAR_SIZE_Y))
self.rotated_sprite = self.sprite
self.name = "NEAT"
def load_net(self):
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, "config.txt")
config = neat.Config(
neat.DefaultGenome,
neat.DefaultReproduction,
neat.DefaultSpeciesSet,
neat.DefaultStagnation,
config_path,
)
with open("./neat_/checkpoints/2024-08-25/best_genome.pickle", "rb") as f:
genome = pickle.load(f)
self.net = neat.nn.FeedForwardNetwork.create(genome, config)
def get_reward(self):
# Calculate reward based on distance and velocity
distance_reward = self.distance / (CAR_SIZE_X / 2)
velocity_reward = self.speed / 20 # Assuming max speed is 20, adjust as needed
# Combine the rewards (you can adjust the weights)
total_reward = 0.7 * distance_reward + 0.3 * velocity_reward
return total_reward
# return self.distance / (CAR_SIZE_X / 2)
def action(self):
input = self.get_data()
output = self.net.activate(input)
choice = output.index(max(output))
if choice == 0:
self.angle += 10 # Left
self.n_drifts_left += 1
self.n_drifts_right = 0
elif choice == 1:
self.angle -= 10 # Right
self.n_drifts_left = 0
self.n_drifts_right += 1
elif choice == 2:
if self.speed - 2 >= 6:
self.speed -= 2 # Slow Down
self.n_drifts_right = 0
self.n_drifts_left = 0
else:
self.n_drifts_right = 0
self.n_drifts_left = 0
self.speed += 2 # Speed Up
def action_train(self):
input = self.get_data()
output = self.net.activate(input)
choice = output.index(max(output))
if choice == 0:
self.angle += 10 # Left
elif choice == 1:
self.angle -= 10 # Right
elif choice == 2:
if self.speed - 2 >= 6:
self.speed -= 2 # Slow Down
else:
self.speed += 2 # Speed Up