forked from praveenVnktsh/CarRacingv0-PPO-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.py
More file actions
157 lines (119 loc) · 5.28 KB
/
environment.py
File metadata and controls
157 lines (119 loc) · 5.28 KB
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
157
from agentFile import Agent
import gym
import cv2
import numpy as np
from config import Args
class Env():
def __init__(self, configs:Args):
self.env = gym.make('CarRacing-v0')
self.configs = configs
self.env.seed(configs.seed)
self.previousRewards = []
self.rewardThresh = self.env.spec.reward_threshold
self.rewards = []
def reset(self):
self.counter = 0
self.rewards = []
self.die = False
img_rgb = self.env.reset()
distances, _ = self.preprocess(img_rgb)
self.stack = distances * self.configs.valueStackSize + [0., 0., 0.]*self.configs.actionStack # four frames for decision
# print('ENVIRONMENT RESET -- Stack size = ',self.stack )
assert len(self.stack) == self.configs.valueStackSize * self.configs.numberOfLasers + 3*self.configs.actionStack
return np.array(self.stack)
def checkGreen(self, img_rgb):
_, gray = self.preprocess(img_rgb)
temp = gray[66:78, 44:52]
if temp.mean() < 100:
return True
return False
def step(self, action, steps, agent:Agent):
finalReward = 0
death = False
rgbState = None
reason = 'NULL'
for i in range(self.configs.action_repeat):
rgbState, reward, envDeath, _ = self.env.step(self.configs.actionTransformation(action))
self.env.render()
if self.checkGreen(rgbState):
finalReward -= 0.05
jerkPenalty = 10*np.linalg.norm(np.array(agent.buffer['a'][agent.counter-1]) - np.array(agent.buffer['a'][agent.counter-2]))
finalReward -= jerkPenalty
finalReward -= action[2]
finalReward += reward
self.storeRewards(reward)
death = True
if self.checkExtendedPenalty():
reason = 'Greenery'
finalReward -= 10
elif steps > self.configs.deathThreshold:
reason = 'Timesteps exceeded'
else:
death = False
if death:
break
distances, _ = self.preprocess(rgbState)
self.stack = self.stack[self.configs.numberOfLasers:self.configs.valueStackSize * self.configs.numberOfLasers]
self.stack += distances
# self.stack += action.tolist()
assert len(self.stack) == self.configs.valueStackSize * self.configs.numberOfLasers + 3*self.configs.actionStack
return np.array(self.stack), finalReward, death, reason
def render(self, *arg):
self.env.render(*arg)
def checkPixelGreen(self, pixel):
if pixel == 0:
return True
return False
def preprocess(self, rgb):
gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
_, gray = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
gray = gray[0:83, 0:95]
temp = gray.copy()[0:83, 0:95]
temprgb = rgb.copy()[0:83, 0:95]
x = 48
y = 73
locs = [None, None, None, None, None]
for i in range(0, 95):
if None not in locs:
break
chk = (min(max(0, y-i), 82), min(max(x-i, 0), 94) )
if locs[0] == None and self.checkPixelGreen(temp[chk]):
locs[0] = chk
cv2.circle(temprgb, (x - i + 1, y - i), 1, (255, 0, 0), 1) #leftmost
chk = (min(max(0, y-i), 82), max(min(x+i, 94), 0) )
if locs[4] == None and self.checkPixelGreen(temp[chk]): # rightmost
locs[4] = chk
cv2.circle(temprgb, (x + i - 1, y - i), 1, (255, 0, 0), 1)
chk = (min(max(0, y-i), 82), x)
if locs[2] == None and self.checkPixelGreen(temp[chk]): #middle
locs[2] = chk
cv2.circle(temprgb, (x, y - i), 1, (255, 0, 0), 1)
chk = (min(max(0, y-i), 82), max(min(x + i//2, 94), 0))
if locs[3] == None and self.checkPixelGreen(temp[chk]): #midright
locs[3] = chk
cv2.circle(temprgb, (x + i//2 - 1, y - i), 1, (255, 0, 0), 1)
chk = (min(max(0, y-i), 82), min(max(x - i//2, 0), 94))
if locs[1] == None and self.checkPixelGreen(temp[chk]): #midleft
locs[1] = chk
cv2.circle(temprgb, (x - i//2 + 1, y - i), 1, (255, 0, 0), 1)
distances = []
for i in range(len(locs)):
if locs[i] == None:
locs[i] = (self.configs.maxDistance, self.configs.maxDistance)
dist = round(np.linalg.norm(np.array(locs[i]) - np.array((y, x))), 2)
if dist == 0:
dist = self.configs.maxDistance
distances.append(dist)
temprgb = cv2.resize(temprgb, (0,0), fx = 2, fy = 2)
cv2.imshow('img', cv2.resize(temprgb, (300, 300)))
# cv2.waitKey(200)
return distances, gray
def checkExtendedPenalty(self):
temp = np.array(self.rewards)
if temp[temp < 0].size == temp.size:
return True
return False
def storeRewards(self, reward):
if len(self.rewards) > self.configs.deathByGreeneryThreshold:
self.rewards.pop(0)
self.rewards.append(reward)