-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveCars.py
273 lines (241 loc) · 8.3 KB
/
MoveCars.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# Prerequisites Chromosomes, Chromosomes_Fitness
# Outputs Fitness(standing vector: an element for each car)
# Initializations
carLocations = env.start_points # Car Initial Location[X, Y] in [Meters]
carHeadings = env.start_headings # Car Initial Heading Counter Clock Wise[Degrees]
steerAngles = env.start_steerAngles # [Degrees] Counter Clock Wise(Same for all cars)
env.lines = GetEnvLines(env) # [x1 y1 x2 y2; ....]
cameraVisibleRange = cameraVisibleRange / 2
if (display_option)
fig = figure(1)
timesteps = 1
Old_Locations = cell(1, env.nbrOfCars)
for car_id = 1: env.nbrOfCars
Old_Locations{car_id} = zeros(nbrOfTimeStepsToTimeout - 1, 2);
Generation_ids = zeros(1, env.nbrOfCars)
Chromosome_ids = ones(1, env.nbrOfCars)
LifeTimes = zeros(1, env.nbrOfCars) # In number of draw steps(multiple of GA.dt)
timeStepsDone = 0
prev_carLines = cell(1, env.nbrOfCars)
BestFitnessChromoID = ones(1, env.nbrOfCars)
Car_Finished_Pool = zeros(1, env.nbrOfCars)
nbrOfParentsToKeep = ceil(GA.PercentBestParentsToKeep * GA.populationSize / 100)
All_Chromosomes = zeros(env.nbrOfCars * GA.populationSize, GA.chromosomeLength)
All_Chromosomes_Fitness = zeros(env.nbrOfCars * GA.populationSize, GA.chromosomeLength)
aviobj = avifile('video.avi', 'fps', 10, 'quality', 95)
# Iterating Generations
while (1)
# Move Car and Draw Environment - Get Sensor Readings and Collision State
if (display_option)
clf(fig)
hold on
# if (timeStepsDone >= 200 / dt)
# return;
# end
if (timeStepsDone >= timeToStartDraw / dt | | all(Car_Finished_Pool))
display_option = 2;
else
display_option = 0;
[newCenters sensor.readings prev_carLines collision_bools] = MoveCarsTimestep(carLocations, carHeadings, prev_carLines,
steerAngles, car, sensor, env,
display_option);
if (display_option == 1 | | display_option == 2)
axis
equal;
axis([0 num 0 num]);
if (camera_mode == 0)
axis([newCenters(1) - cameraVisibleRange newCenters(1) + cameraVisibleRange newCenters(2) - cameraVisibleRange
newCenters(2) + cameraVisibleRange]);
end
xlabel(['Number of Collisions = ' num2str(length(find(collision_bools))) '. Time = ' num2str(timeStepsDone * dt)
' seconds.']);
aviobj = addframe(aviobj, gcf);
drawnow;
end
timeStepsDone = timeStepsDone + 1;
string = [];
% string = ['Time ' num2str(timeStepsDone * dt) ' seconds '];
for car_id = 1: length(Generation_ids)
% string = [string 'Car' num2str(car_id) ' Gen#/Chrom#=' num2str(Generation_ids(car_id)) '/'
num2str(Chromosome_ids(car_id))...
% ' BestChrom#:F=' num2str(BestFitnessChromoID(car_id)) ':' num2str(Chromosomes_Fitness
{car_id}(BestFitnessChromoID(car_id))) ' - '];
string = [string num2str(Generation_ids(car_id)) '/' num2str(Chromosome_ids(car_id))...
'(' num2str(BestFitnessChromoID(car_id)) ':' num2str(Chromosomes_Fitness
{car_id}(BestFitnessChromoID(car_id))) ') - '];
end
disp(string);
if (save_option)
saveas(gcf, sprintf('Results//fig%i_%i.png', Generation, timesteps), 'png');
end
% Increase
lifetimes
by
1
LifeTimes = LifeTimes + 1;
% Iterate
Cars in that
timestep
for car_id = 1:length(collision_bools)
% Update
Fitness
% Fitness = -sqrt((carLocation(1) - env.destination(1)) ^ 2 + (carLocation(2) - env.destination(2)) ^ 2);
% Fitness = sqrt((carLocation(1) - carLocation_initial(1)) ^ 2 + (carLocation(2) - carLocation_initial(2)) ^ 2);
Fitness = LifeTimes(car_id);
% If
car is almost in same
place
after
nbrOfTimeStepsToTimeout
has
passed, set
rotating_around_my_self_bool
rotating_around_my_self_bool = 0;
if (LifeTimes(car_id) >= nbrOfTimeStepsToTimeout)
Old_Locations
{car_id} = [Old_Locations{car_id}(2:end,:); carLocations(car_id,:)];
mean_x = mean(Old_Locations
{car_id}(:, 1));
mean_y = mean(Old_Locations
{car_id}(:, 2));
var_x = mean((Old_Locations{car_id}(:, 1) - mean_x). ^ 2);
var_y = mean((Old_Locations{car_id}(:, 2) - mean_y). ^ 2);
if (var_x <= smallXYVariance & & var_y <= smallXYVariance)
rotating_around_my_self_bool = 1;
end
else
Old_Locations
{car_id}(LifeTimes(car_id),:) = carLocations(car_id,:);
end
if (collision_bools(car_id))
if (Fitness > max(Chromosomes_Fitness{car_id}))
BestFitnessChromoID(car_id) = Chromosome_ids(car_id); % Save
Best
Fitness
end
Chromosomes_Fitness
{car_id}(Chromosome_ids(car_id)) = Fitness;
if (Fitness >= GA.goodFitness)
Car_Finished_Pool(car_id) = 1;
BestFitnessChromoID(car_id) = Chromosome_ids(car_id);
end
ResetCarAndLifeTime;
if (~Car_Finished_Pool(car_id))
Chromosome_ids(car_id) = Chromosome_ids(car_id) + 1;
end
elseif(rotating_around_my_self_bool)
Chromosomes_Fitness
{car_id}(Chromosome_ids(car_id)) = 0; % TODO
Is
this
good ?
ResetCarAndLifeTime;
if (~Car_Finished_Pool(car_id))
Chromosome_ids(car_id) = Chromosome_ids(car_id) + 1;
end
rotating_around_my_self_bool = 0;
end
% Jump
to
car
next
Generation if necessary
if (Chromosome_ids(car_id) > GA.populationSize & & ~Car_Finished_Pool(car_id))
if (Generation_ids(car_id) >= GA.nbrOfGenerations_max)
Car_Finished_Pool(car_id) = 1;
Chromosome_ids(car_id) = BestFitnessChromoID(car_id);
else
% Replacement
TODO: I
always
replace
all
with childs
% if (GA.replacement_option == 0)
% if (GA.keptParentsAreGolobal_option) % (TODO) Commented for faster run for now
for i=1:env.nbrOfCars
All_Chromosomes((i - 1) * GA.populationSize + 1: i * GA.populationSize,:) = Chromosomes
{i};
All_Chromosomes_Fitness((i - 1) * GA.populationSize + 1: i * GA.populationSize) = Chromosomes_Fitness
{i};
end
[tmp idx] = sort(All_Chromosomes_Fitness, 'descend');
idx2 = idx(1:nbrOfParentsToKeep);
ParentsToKeep = All_Chromosomes(idx2,:);
[tmp idx] = sort(Chromosomes_Fitness
{car_id}, 'descend');
idx2 = idx(1:end - nbrOfParentsToKeep);
Current_Chromosomes = Chromosomes
{car_id}(idx2,:);
Current_Fitness = Chromosomes_Fitness
{car_id}(idx2);
% else % TODO
% end
Chromosomes_Childs = ApplyGA(GA, Current_Chromosomes, Current_Fitness);
Chromosomes
{car_id} = [ParentsToKeep;
Chromosomes_Childs];
% elseif(GA.replacement_option == 2)
% Chromosomes_Childs = ApplyGA(GA, Chromosomes
{car_id}, Chromosomes_Fitness
{car_id});
% T = round(rand(GA.populationSize, GA.tournament_size) * (GA.populationSize - 1) + 1); % Tournaments(Random
from
1
to
GA.populationSize)
% [temp idx] = max(Chromosomes_Fitness(T), [], 2); % Index
to
determine
the
winners
% WinnersIdx = T(sub2ind(size(T), (1:GA.populationSize)
',idx)); % Winners Indeces
% keyboard
%
% % Chromosomes_Fitness
Chromosomes_Childs
% % Chromosomes
{car_id}
% end
Chromosome_ids(car_id) = 1;
Generation_ids(car_id) = Generation_ids(car_id) + 1;
Chromosomes_Fitness
{car_id} = 0 * Chromosomes_Fitness
{car_id};
BestFitnessChromoID(car_id) = 1;
end
end
current_chromosome = Chromosomes
{car_id}(Chromosome_ids(car_id),:);
% Apply
sensor
reading
to
ANN
to
calculate
steerAngle
outputs = Feedforward(sensor.readings(car_id,:), current_chromosome, Network_Arch, unipolarBipolarSelector);
steerAngles(car_id) = pi / 2 * (outputs(2) - outputs(1)); % From - 90
to
90
degrees
% sensor.readings
% [outputs steerAngles(car_id) * 180 / pi]
% keyboard
% 2
D
car
steering
physics(Calculate
carLocation and carHeading)
frontWheel = carLocations(car_id,:) + car.wheelBase / 2 * [cos(carHeadings(car_id)) sin(carHeadings(car_id))];
backWheel = carLocations(car_id,:) - car.wheelBase / 2 * [cos(carHeadings(car_id)) sin(carHeadings(car_id))];
backWheel = backWheel + carSpeed * dt * [cos(carHeadings(car_id)) sin(carHeadings(car_id))];
frontWheel = frontWheel + carSpeed * dt * [cos(carHeadings(car_id) + steerAngles(car_id))
sin(carHeadings(car_id) + steerAngles(car_id))];
carLocations(car_id,:) = (frontWheel + backWheel) / 2;
carHeadings(car_id) = atan2(frontWheel(2) - backWheel(2), frontWheel(1) - backWheel(1));
end
end