-
Notifications
You must be signed in to change notification settings - Fork 1
/
environment.py
156 lines (114 loc) · 5.7 KB
/
environment.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from environment.car import Car
import pygame
from pygame.math import Vector2
import numpy as np
from environment.config import Args, configure
import time
import cv2
class Environment:
def __init__(self, configs:Args):
self.exit = False
pygame.init()
pygame.font.init()
self.myfont = pygame.font.SysFont(configs.font, 15, bold=True)
self.clock = pygame.time.Clock()
self.index = 0
self.config = configs
self.screen = pygame.display.set_mode((configs.cameraHeight, configs.cameraHeight))
self.trackImage = pygame.image.load(configs.trackPath)
self.cars = pygame.sprite.Group()
self.cameraPosition = Vector2(configs.startingPositionX - configs.cameraHeight//2,configs.startingPositionY - configs.cameraHeight//2)
self.cameraPositions = []
self.cameraPositions.append((self.cameraPosition.x, self.cameraPosition.y))
pygame.event.set_allowed([pygame.QUIT])
self.image = np.zeros((self.config.width,self.config.height, 3)).astype(np.uint8)
for i in range(self.config.numberOfCars):
self.cars.add(Car(configs.startingPositionX , configs.startingPositionY - i//2, index = i, configs = self.config, trackImage= self.trackImage))
self.draw()
def step(self, action, data = None, render = True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
state = np.zeros((self.config.numberOfCars, self.config.valueStackSize*(self.config.numberOfLasers + 1)))
rewards = np.zeros((self.config.numberOfCars,))
dead = np.zeros((self.config.numberOfCars,))
# pressed = pygame.key.get_pressed()
# if pressed[pygame.K_UP]:
# action[:, 1] = 1.0
# if pressed[pygame.K_LEFT]:
# action[:, 0] = 1.0
# if pressed[pygame.K_RIGHT]:
# action[:, 0] = -1.0
# if pressed[pygame.K_SPACE]:
# action[:, 2] = 1.0
self.index += 1
i = 0
bestCarDistance = 0
bestCarIndex = 0
for car in self.cars:
state[i], dead[i], rewards[i], carDistance = car.update(action[i])
if (dead[i] != 1.0) and carDistance > bestCarDistance:
bestCarDistance = carDistance
bestCarIndex = i
self.cameraPosition.x = car.position.x - self.config.cameraHeight//2
self.cameraPosition.y = car.position.y - self.config.cameraHeight//2
if car.angle > 0:
self.cameraPosition.y -= self.config.cameraOffset
elif car.angle < 0:
self.cameraPosition.y += self.config.cameraOffset
i += 1
if len(self.cameraPositions) > 20:
self.cameraPositions.pop(0)
self.cameraPositions.append((self.cameraPosition.x, self.cameraPosition.y))
mean = np.mean(self.cameraPositions, 0)
self.cameraPosition =Vector2(int(mean[0]), int(mean[1]))
if render:
self.draw(data)
if self.config.test:
self.clock.tick(30)
return state, dead, rewards, bestCarIndex
def reset(self):
self.cars = pygame.sprite.Group()
self.cameraPosition = Vector2(self.config.startingPositionX - self.config.cameraHeight//2,self.config.startingPositionY - self.config.cameraHeight//2)
self.cameraPositions = []
self.cameraPositions.append((self.cameraPosition.x, self.cameraPosition.y))
self.image = np.zeros((self.config.width,self.config.height, 3)).astype(np.uint8)
for i in range(self.config.numberOfCars):
self.cars.add(Car(self.config.startingPositionX , self.config.startingPositionY, index = i, configs = self.config, trackImage = self.trackImage))
self.draw()
def saveImage(self):
cv2.imwrite(self.config.saveLocation + str(self.config.checkpoint) + ' - IMAGE.png', self.image)
def draw(self, data = None):
self.screen.fill(self.config.bgColor)
cropRect = (self.cameraPosition.x, self.cameraPosition.y, self.config.cameraHeight, self.config.cameraHeight)
self.screen.blit(self.trackImage,(0,0),cropRect)
for car in self.cars:
if self.config.test:
point = (int(car.genTrackPoint[1]), int(car.genTrackPoint[0]))
self.image[point] = car.color
for point in car.trackPoints:
point = (int(point[1]), int(point[0]))
self.image[point] = np.array([255, 255, 255])
car.draw(self.screen, self.cameraPosition)
if self.config.test:
dilation = cv2.dilate(self.image,np.ones((5,5),np.uint8),iterations = 1)
cv2.imshow('Track', cv2.resize(dilation, (500, 500)))
if not data is None:
s = pygame.Surface(((len(data.keys())*20 + 35),len(data.keys())*20 + 5), pygame.SRCALPHA) # per-pixel alpha
s.fill((255,255,255,128))
self.screen.blit(s, (0,0))
i = 0
for key in data.keys():
renderedText = self.myfont.render(str(key)+ " : " + str(data[key]), False, (0, 0, 0))
self.screen.blit(renderedText,(5,5 + 20*i))
i += 1
pygame.display.flip()
if __name__ == '__main__':
config, useCuda, device = configure()
env = Environment(config)
while True:
action = np.zeros((config.numberOfCars, 3))
state, dead, rewards = env.step(action, render = config.render)
if 0.0 not in dead:
time.sleep(2)
env.reset()