-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_fuzzy.py
executable file
·572 lines (408 loc) · 17.4 KB
/
main_fuzzy.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import GA2
import numpy as np
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import matplotlib.pyplot as plt
import argparse
import json
import sys
from datetime import datetime
import threading
import socket
import subprocess
import time
from keras.models import Sequential
from keras.layers import Dense
import shutil
import fuzzy_in_two
import fuzzy_in_four
import ConsuptionModel as cM
import math
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("Environment variable SUMO_HOME not defined")
import traci
mapas_todos = [
"super.net.xml",
]
max_speed_caminhao = 30 # ~ 108km/h
max_dif_altura = 50
extras_mapas = None
caminho_veiculo = ['AA0AB0','AB0AC0','AC0AD0','AD0AE0','AE0AF0','AF0AG0','AG0AH0','AH0AI0','AI0AJ0','AJ0AK0','AK0AL0','AL0AM0','AM0AN0','AN0AO0',
'AO0AP0','AP0AQ0','AQ0AR0','AR0AS0','AS0AT0','AT0AU0','AU0AV0','AV0AW0','AW0AX0','AX0AY0','AY0AZ0','AZ0BA0','BA0BB0','BB0BC0','BC0BD0','BD0BE0',
'BE0BF0','BF0BG0','BG0BH0','BH0BI0','BI0BJ0','BJ0BK0','BK0BL0','BL0BM0','BM0BN0','BN0BO0','BO0BP0','BP0BQ0','BQ0BR0','BR0BS0','BS0BT0','BT0BU0',
'BU0BV0','BV0BW0','BW0BX0','BX0BY0','BY0BZ0','BZ0CA0','CA0CB0','CB0CC0','CC0CD0','CD0CE0','CE0CF0','CF0CG0','CG0CH0','CH0CI0','CI0CJ0','CJ0CK0',
'CK0CL0','CL0CM0','CM0CN0','CN0CO0','CO0CP0','CP0CQ0','CQ0CR0','CR0CS0','CS0CT0','CT0CU0','CU0CV0','CV0CW0','CW0CX0','CX0CY0','CY0CZ0','CZ0DA0',
'DA0DB0','DB0DC0','DC0DD0','DD0DE0','DE0DF0','DF0DG0','DG0DH0','DH0DI0','DI0DJ0','DJ0DK0','DK0DL0','DL0DM0','DM0DN0','DN0DO0','DO0DP0','DP0DQ0',
'DQ0DR0','DR0DS0','DS0DT0','DT0DU0','DU0DV0','DV0DW0','DW0DX0','DX0DY0','DY0DZ0','DZ0EA0','EA0EB0','EB0EC0','EC0ED0',
'ED0EE0','EE0EF0','EF0EG0','EG0EH0','EH0EI0','EI0EJ0','EJ0EK0','EK0EL0','EL0EM0','EM0EN0','EN0EO0','EO0EP0','EP0EQ0',
'EQ0ER0','ER0ES0','ES0ET0','ET0EU0','EU0EV0','EV0EW0','EW0EX0','EX0EY0','EY0EZ0','EZ0FA0','FA0FB0','FB0FC0','FC0FD0',
'FD0FE0','FE0FF0','FF0FG0','FG0FH0','FH0FI0','FI0FJ0','FJ0FK0','FK0FL0','FL0FM0','FM0FN0','FN0FO0','FO0FP0','FP0FQ0',
'FQ0FR0','FR0FS0','FS0FT0','FT0FU0','FU0FV0','FV0FW0','FW0FX0','FX0FY0','FY0FZ0','FZ0GA0','GA0GB0','GB0GC0','GC0GD0','GD0GE0',
'GE0GF0','GF0GG0','GG0GH0','GH0GI0','GI0GJ0','GJ0GK0','GK0GL0','GL0GM0','GM0GN0','GN0GO0','GO0GP0','GP0GQ0','GQ0GR0','GR0GS0',
'GS0GT0','GT0GU0','GU0GV0','GV0GW0','GW0GX0','GX0GY0','GY0GZ0','GZ0HA0','HA0HB0','HB0HC0','HC0HD0','HD0HE0','HE0HF0','HF0HG0',
'HG0HH0','HH0HI0','HI0HJ0','HJ0HK0','HK0HL0','HL0HM0','HM0HN0','HN0HO0','HO0HP0','HP0HQ0','HQ0HR0','HR0HS0','HS0HT0','HT0HU0',
'HU0HV0','HV0HW0','HW0HX0','HX0HY0','HY0HZ0','HZ0IA0','IA0IB0','IB0IC0','IC0ID0','ID0IE0','IE0IF0','IF0IG0','IG0IH0','IH0II0',
'II0IJ0','IJ0IK0','IK0IL0','IL0IM0','IM0IN0','IN0IO0','IO0IP0','IP0IQ0','IQ0IR0','IR0IS0','IS0IT0','IT0IU0','IU0IV0','IV0IW0',
'IW0IX0','IX0IY0','IY0IZ0','IZ0JA0','JA0JB0','JB0JC0','JC0JD0','JD0JE0','JE0JF0','JF0JG0','JG0JH0','JH0JI0','JI0JJ0','JJ0JK0',
'JK0JL0','JL0JM0','JM0JN0','JN0JO0','JO0JP0','JP0JQ0','JQ0JR0','JR0JS0','JS0JT0','JT0JU0','JU0JV0','JV0JW0','JW0JX0','JX0JY0',
'JY0JZ0','JZ0KA0','KA0KB0','KB0KC0','KC0KD0','KD0KE0','KE0KF0','KF0KG0','KG0KH0','KH0KI0','KI0KJ0','KJ0KK0','KK0KL0','KL0KM0',
'KM0KN0','KN0KO0','KO0KP0','KP0KQ0','KQ0KR0','KR0KS0','KS0KT0','KT0KU0','KU0KV0','KV0KW0','KW0KX0','KX0KY0','KY0KZ0','KZ0LA0',
'LA0LB0','LB0LC0','LC0LD0','LD0LE0','LE0LF0','LF0LG0','LG0LH0','LH0LI0','LI0LJ0','LJ0LK0','LK0LL0','LL0LM0','LM0LN0','LN0LO0',
]
def get_model():
model = Sequential()
model.add(Dense(8, activation='sigmoid', input_shape=(6,)))
model.add(Dense(8, activation='sigmoid'))
model.add(Dense(8, activation='sigmoid'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='categorical_crossentropy')
return model
def calculate_real_fuel(speed,accel,slope,instant_fuel):
modelo = cM.ModelConsuption(speed, accel, slope)
consuption = modelo.run()
instant_fuel = consuption
return instant_fuel
def calculate_real_fuel2(speed,accel,slope,instant_fuel):
modelo = cM.ModelConsuption(speed, accel, slope)
consuption = modelo.run()
p = 0.1 # Percentage increase in consumption [0,1]
factor_RPM = ((speed**2)*(p))/(30**2)
instant_fuel = consuption + consuption*factor_RPM
return instant_fuel
def calculate_real_fuel3(speed,accel,slope,instant_fuel):
modelo = cM.ModelConsuption(speed, accel, slope)
consuption = modelo.run()
p = 0.1 # Percentage increase in consumption [0,1]
factor_RPM = (math.exp(speed)*p)/(math.exp(30))
instant_fuel = consuption + consuption*factor_RPM
return instant_fuel
def calculate_real_fuel4(speed,accel,slope,instant_fuel):
modelo = cM.ModelConsuption(speed, accel, slope)
consuption = modelo.run()
p = 0.2 # Percentage increase in consumption [0,1]
maxSlope = 25
maxSpeed = 30
factor_Slope = math.exp(slope)/math.exp(maxSlope)
factor_Speed = math.exp(speed)/math.exp(maxSpeed)
factor_RPM = factor_Speed*factor_Speed*p
instant_fuel = consuption + consuption*factor_RPM
return instant_fuel
def verificaVelocidadeIdeal():
consumo = []
menor_consumo1 = []
velocidade1 = []
angulo = []
for angle in range(-15,15,1):
menor_consumo = float('Infinity')
for speed in range(1,30,1):
consumo = calculate_real_fuel(speed, 0, angle)
if consumo < menor_consumo:
menor_consumo = consumo
velocidade = speed
ang = angle
menor_consumo1.append(menor_consumo)
velocidade1.append(velocidade)
angulo.append(ang)
print("Ângulos", angulo)
print("Melhores velocidades",velocidade1)
print("Consumo",menor_consumo1)
def calculate_new_fuel(speed, max_speed_caminhao, instant_fuel, instant_slope, max_slope, instant_acell, max_accel, step):
total_accel = instant_acell/max_accel
total_slope = instant_slope/max_slope
# print('total_slope', total_slope)
# print('instant_slope', instant_slope)
if total_slope>=0 and total_accel>=0:
instant_fuel = instant_fuel*(1.1+total_accel)
# print('total_accel', total_accel)
print(instant_fuel)
return instant_fuel
def getClosest(arr, n, target):
if type(arr) is not list:
return arr
if (target <= arr[0]["x"]):
return arr[0]
if (target >= arr[n - 1]["x"]):
return arr[n - 1]
# Doing binary search
i = 0; j = n; mid = 0
while (i < j):
mid = (i + j) // 2
if (arr[mid]["x"] == target):
return arr[mid]
# If target is less than array
# element, then search in left
if (target < arr[mid]["x"]) :
# If target is greater than previous
# to mid, return closest of two
if (mid > 0 and target > arr[mid - 1]["x"]):
return getClosest(arr[mid - 1], arr[mid], target)
# Repeat for left half
j = mid
# If target is greater than mid
else :
if (mid < n - 1 and target < arr[mid + 1]["x"]):
return getClosest(arr[mid], arr[mid + 1], target)
# update i
i = mid + 1
# Only single element left after search
return arr[mid]
def get_info_pos(mapa, x):
global extras_mapas
if extras_mapas==None:
extras_mapas = {}
for m in mapas_todos:
f = open("./mapas/"+m.replace(".net.xml",".extras"),"r")
extras_mapas[m] = json.loads(f.read())
f.close()
d = extras_mapas[mapa]
return getClosest(d,len(d),x)
class UnusedPortLock:
lock = threading.Lock()
def __init__(self):
self.acquired = False
def __enter__(self):
self.acquire()
def __exit__(self):
self.release()
def acquire(self):
if not self.acquired:
UnusedPortLock.lock.acquire()
self.acquired = True
def release(self):
if self.acquired:
UnusedPortLock.lock.release()
self.acquired = False
def find_unused_port():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.bind(('127.0.0.1', 0))
sock.listen(socket.SOMAXCONN)
ipaddr, port = sock.getsockname()
sock.close()
return port
def terminate_sumo(sumo):
if sumo.returncode != None:
os.system("taskkill.exe /F /im sumo.exe")
time.sleep(1)
def custom_mutate(wei):
if type(wei)==np.ndarray:
ret = []
for w in wei:
ret.append(custom_mutate(w))
return np.array(ret, dtype=object)
return (wei*np.random.uniform(low=0.9, high=1.1)) + np.random.uniform(low=-0.01, high=0.01)
def run(mapa):
npdf = 0
mean_speed = 0
pdf = np.zeros((110,), dtype=float)
f_2 = fuzzy_in_two.Algorithm()
f_4 = fuzzy_in_four.Algorithm()
step = 1
traci.route.add("trip", caminho_veiculo)
traci.vehicle.add("caminhao", "trip")
traci.vehicle.setParameter("caminhao","carFollowModel","KraussPS")
traci.vehicle.setVehicleClass("caminhao","truck")
traci.vehicle.setShapeClass("caminhao","truck")
traci.vehicle.setEmissionClass("caminhao","HBEFA3/HDV")
traci.vehicle.setMaxSpeed("caminhao",max_speed_caminhao)
r = 1
total_fuel = 0
#verificaVelocidadeIdeal()
try:
while step == 1 or traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep()
try:
speed = traci.vehicle.getSpeed("caminhao")
angle = traci.vehicle.getSlope("caminhao")
x, y, z = traci.vehicle.getPosition3D("caminhao")
inf10 = get_info_pos(mapa, x+10)
inf30 = get_info_pos(mapa, x+30)
inf50 = get_info_pos(mapa, x+50)
inf70 = get_info_pos(mapa, x+70)
normalization = 1 #90
angle /= normalization
inf10 = inf10["angle"]/normalization
inf30 = inf30["angle"]/normalization
inf50 = inf50["angle"]/normalization
inf70 = inf70["angle"]/normalization
entrada = [speed, angle, inf10, inf30, inf50, inf70]
#r = model.predict(np.array([np.array(entrada)]))[0][0] #chamar fuzzy
r = f_2.findSpeed(speed, angle, inf10, inf30, inf50, inf70)
i_pdf = int(r*3.6+0.49)
pdf[i_pdf] += 1
npdf += 1
# print(r, max_speed_caminhao , r*max_speed_caminhao)
traci.vehicle.setSpeed("caminhao",r)
instant_fuel_consuption = traci.vehicle.getFuelConsumption("caminhao")
max_angulo = 60
# angle /= max_angulo
max_acel = traci.vehicle.getAccel("caminhao")
inst_acel = traci.vehicle.getAcceleration("caminhao")
#instant_fuel_consuption2 = calculate_new_fuel(speed, max_speed_caminhao, instant_fuel_consuption, angle, max_angulo, inst_acel, max_acel, step)
instant_fuel_consuption2 = calculate_real_fuel4(speed, inst_acel, angle, instant_fuel_consuption)
total_fuel += instant_fuel_consuption2
except Exception as e:
print("######################---")
print(e)
print("######################---")
pass
step += 1
except Exception as e:
print("######################")
print(e)
print("######################")
raise
for i in range(len(pdf)):
pdf[i] = float(pdf[i]*100)/float(npdf)
if os.path.exists('pdf/results/fuzzy_2.csv'):
os.remove('pdf/results/fuzzy_2.csv')
resultFile = open('pdf/results/fuzzy_2.csv', 'a')
for i in range(len(pdf)):
mean_speed += i*pdf[i]
# print('i: ', i, ' pdf: ', pdf[i], ' i*pdf: ', i*pdf[i])
resultFile.write("{}{}{}{}".format(i,':',pdf[i],'\n'))
# resultFile.write("{}{}{}{}".format('mean_speed',':',mean_speed/100,'\n'))
resultFile.close()
print("Simulation finished")
traci.close()
sys.stdout.flush()
# time.sleep(1)
return total_fuel
def run_pre():
# caminho_veiculo_pre = ['A0B0', 'B0C0', 'C0D0', 'D0E0', 'E0F0', 'F0G0', 'G0H0', 'H0I0', 'I0J0', 'J0K0', 'K0L0', 'L0M0', 'M0N0', 'N0O0', 'O0P0', 'P0Q0', 'Q0R0', 'R0S0', 'S0T0']
traci.route.add("trip", caminho_veiculo)
traci.vehicle.add("path_mapper", "trip")
traci.vehicle.setParameter("path_mapper","carFollowModel","KraussPS")
traci.vehicle.setVehicleClass("path_mapper","passenger")
traci.vehicle.setMaxSpeed("path_mapper",0.3) # 3.6
dados = []
try:
step = 1
while step == 1 or traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep()
try:
traci.vehicle.setSpeed("path_mapper",0.3)
angle = traci.vehicle.getSlope("path_mapper")
x, _, z = traci.vehicle.getPosition3D("path_mapper")
speed = traci.vehicle.getSpeed("path_mapper")
dados.append({"x":x, "z":z, "angle":angle})
except:
pass
step += 1
except Exception as e:
print("######################")
print(e, step)
print("######################")
# time.sleep(1)
print("Pre Simulation finished")
traci.close()
sys.stdout.flush()
# time.sleep(1)
return dados
def start_pre_simulation(sumo, scenario, network):
unused_port_lock = UnusedPortLock()
unused_port_lock.__enter__()
remote_port = find_unused_port()
sumo = subprocess.Popen([sumo, "-c", scenario, "--device.emissions.probability", "1.0", "--remote-port", str(remote_port), "--duration-log.statistics","--log", "logfile.txt"], stdout=sys.stdout, stderr=sys.stderr)
unused_port_lock.release()
try:
traci.init(remote_port)
return run_pre()
except Exception as e:
print(e)
raise
finally:
print("Terminating SUMO")
terminate_sumo(sumo)
unused_port_lock.__exit__()
def start_simulation(sumo, scenario, network, output, mapa):
unused_port_lock = UnusedPortLock()
unused_port_lock.__enter__()
remote_port = find_unused_port()
sumo = subprocess.Popen([sumo, "-c", scenario, "--tripinfo-output", output, "--device.emissions.probability", "1.0", "--remote-port", str(remote_port), "--duration-log.statistics","--log", "logfile.txt"], stdout=sys.stdout, stderr=sys.stderr)
unused_port_lock.release()
try:
traci.init(remote_port)
return run(mapa)
except Exception as e:
print(e)
raise
finally:
print("Terminating SUMO")
terminate_sumo(sumo)
unused_port_lock.__exit__()
def custom_fitness(outputfile="super.net.xml_0_0.out.xml"):
mapas = mapas_todos
folder = "./mapas/"
consumo_total = 0
for m in mapas:
consumo = start_simulation("sumo", (folder+m).replace(".net.xml",".sumo.cfg"), (folder+m), "./output_fuzzy/"+m+outputfile, m)
# sumo-gui
consumo_total += consumo
# if consumo_total==float("Infinity"): # isso faz pular mapas no teste caso a gente ja tenha identificado algum muito lento
# break
f = open("./output_fuzzy/"+m+outputfile,"r")
conteudo = f.read()
f.close()
conteudo = conteudo.split("\n")
conteudo[33] = conteudo[33].split("fuel_abs")
conteudo[33] = conteudo[33][0]+"fuel_abs=\"{:.6f}\" electricity_abs=\"0\"/>".format(consumo_total)
conteudo = "\n".join(conteudo)
f = open("./output_fuzzy/"+m+outputfile,"w")
f.write(conteudo)
f.close()
return 1/consumo_total
def custom_random_genome():
model = get_model()
w = model.get_weights()
return custom_mutate(np.array(w, dtype=object))
def pre_simulation():
mapas = [
"super.net.xml",
]
folder = "./mapas/"
for m in mapas:
dados = start_pre_simulation("sumo", (folder+m).replace(".net.xml",".sumo.cfg"), (folder+m))
f = open((folder+m).replace(".net.xml",".extras"),"w")
f.write(json.dumps(dados))
f.close()
def main():
folder = './output_fuzzy/'
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
custom_fitness()
#pre_simulation()
# population_size = 1
# iteration_limit = 1
# cut_half_pop = True
# replicate_best = 0.1
# name = str(population_size)+"_"+\
# str(iteration_limit)+"_"+\
# str(cut_half_pop)+"_"+\
# str(replicate_best)
# g = GA2.GeneticAlgorithm(custom_random_genome)
# g.set_evaluate(custom_fitness)
# g.set_population_size(population_size)
# g.set_iteration_limit(iteration_limit)
# g.set_stop_criteria_type(1)
# g.set_mutate(custom_mutate)
# g.set_cut_half_population(cut_half_pop)
# g.set_replicate_best(replicate_best)
# g.run()
# infos = {}
# infos["ga_config"] = g.get_config()
# infos["historic"] = g.historic
# f = open("./results/"+name+".json","w")
# f.write(json.dumps(infos, indent=2))
# f.close()
if __name__ == '__main__':
main()