Repository navigation
Expand file tree
/
Copy pathnoiser.py
More file actions
194 lines (149 loc) · 6.77 KB
/
Copy pathnoiser.py
File metadata and controls
194 lines (149 loc) · 6.77 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import time
import random
import copy
class Noiser(object):
# define frequency into noise events per minute
# define the amount_of_time of setting the noise
def __init__(self, noise_type, frequency=15, intensity=10, min_noise_time_amount=2.0):
# self.variable_type = variable
self.noise_type = noise_type
self.frequency = frequency
self.noise_being_set = False
self.noise_start_time = time.time()
self.noise_end_time = time.time() + 1
self.min_noise_time_amount = min_noise_time_amount
self.noise_time_amount = min_noise_time_amount + float(random.randint(50, 200) / 100.0)
self.second_counter = time.time()
self.steer_noise_time = 0
self.intensity = intensity + random.randint(-2, 2)
self.remove_noise = False
self.current_noise_mean = 0
def set_noise(self):
if self.noise_type == 'Spike' or self.noise_type == 'Throttle':
# flip between positive and negative
coin = random.randint(0, 1)
if coin == 0: # negative
self.current_noise_mean = 0.001 # -random.gauss(0.05,0.02)
else: # positive
self.current_noise_mean = -0.001 # random.gauss(0.05,0.02)
def get_noise(self):
if self.noise_type == 'Spike' or self.noise_type == 'Throttle':
if self.current_noise_mean > 0:
return min(0.55,
self.current_noise_mean + (
time.time() - self.noise_start_time) * 0.03 * self.intensity)
else:
return max(-0.55,
self.current_noise_mean - (
time.time() - self.noise_start_time) * 0.03 * self.intensity)
def get_noise_removing(self):
# print 'REMOVING'
added_noise = (self.noise_end_time - self.noise_start_time) * 0.02 * self.intensity
# print added_noise
if self.noise_type == 'Spike' or self.noise_type == 'Throttle':
if self.current_noise_mean > 0:
added_noise = min(0.55, added_noise + self.current_noise_mean)
return added_noise - (time.time() - self.noise_end_time) * 0.03 * self.intensity
else:
added_noise = max(-0.55, self.current_noise_mean - added_noise)
return added_noise + (time.time() - self.noise_end_time) * 0.03 * self.intensity
def is_time_for_noise(self, steer):
# Count Seconds
second_passed = False
if time.time() - self.second_counter >= 1.0:
second_passed = True
self.second_counter = time.time()
if time.time() - self.noise_start_time >= self.noise_time_amount and not self.remove_noise and self.noise_being_set:
self.noise_being_set = False
self.remove_noise = True
self.noise_end_time = time.time()
if self.noise_being_set:
return True
if self.remove_noise:
# print "TIME REMOVING ",(time.time()-self.noise_end_time)
if (time.time() - self.noise_end_time) > (
self.noise_time_amount): # if half the amount put passed
self.remove_noise = False
self.noise_time_amount = self.min_noise_time_amount + float(
random.randint(50, 200) / 100.0)
return False
else:
return True
if second_passed and not self.noise_being_set:
# Ok, if noise is not being set but a second passed... we may start puting more
seed = random.randint(0, 60)
if seed < self.frequency:
if not self.noise_being_set:
self.noise_being_set = True
self.set_noise()
self.steer_noise_time = steer
self.noise_start_time = time.time()
return True
else:
return False
else:
return False
def set_noise_exist(self, noise_exist):
self.noise_being_set = noise_exist
def compute_noise(self, action, speed):
# noisy_action = action
if self.noise_type == 'None':
return action, False, False
if self.noise_type == 'Spike':
if self.is_time_for_noise(action.steer):
steer = action.steer
if self.remove_noise:
steer_noisy = max(
min(steer + self.get_noise_removing() * (25 / (2.3 * speed + 5)), 1), -1)
else:
steer_noisy = max(min(steer + self.get_noise() * (25 / (2.3 * speed + 5)), 1),
-1)
noisy_action = copy.deepcopy(action)
noisy_action.steer = steer_noisy
return noisy_action, False, not self.remove_noise
else:
return action, False, False
if self.noise_type == 'Throttle':
if self.is_time_for_noise(action.throttle):
throttle_noisy = action.throttle
brake_noisy = action.brake
if self.remove_noise:
print(" Throttle noise removing", self.get_noise_removing())
noise = self.get_noise_removing()
if noise > 0:
throttle_noisy = max(min(throttle_noisy + noise, 1), 0)
else:
brake_noisy = max(min(brake_noisy + -noise, 1), 0)
else:
print(" Throttle noise ", self.get_noise())
noise = self.get_noise()
if noise > 0:
throttle_noisy = max(min(throttle_noisy + noise, 1), 0)
else:
brake_noisy = max(min(brake_noisy + -noise, 1), 0)
noisy_action = copy.deepcopy(action)
noisy_action.throttle = throttle_noisy
noisy_action.brake = brake_noisy
# print 'timefornosie'
return noisy_action, False, not self.remove_noise
else:
return action, False, False
if __name__ == '__main__':
import matplotlib.pyplot as plt
# Noise testing
noise_input = []
human_input = []
noiser = Noiser('Spike')
for i in range(500):
human_action = Control()
human_action.steer = 0.0
human_action.gas = 0.0
human_action.brake = 0.0
human_action.hand_brake = 0.0
human_action.reverse = 0.0
human_input.append(human_action.steer)
noisy_action, _, _ = noiser.compute_noise(human_action)
time.sleep(0.01)
noise_input.append(noisy_action.steer)
plt.plot(range(500), human_input, 'g', range(500), noise_input, 'r')
plt.show()