-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils3.py
733 lines (672 loc) · 35.6 KB
/
utils3.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from settings import PROJECT_ROOT
from math import *
from apps.point.models import Route, Station, Transport, Onestation
from django.db.models import Max
import os
import datetime
from utyls.speed_matrix.speed_matrix124 import speed_matrix124
from utyls.new_route_speed_matrix.new_route_speed_matrix124 import new_route_speed_matrix124
from utyls.points_list3 import points_list_3
from utyls.all_x import all_x
from utyls.route_zazor124 import route_zazor124
from utyls.route_stat124 import route_stat124
from utyls.points_list_for_sort124 import points_list_for_sort124
from utyls.metastation_sort124 import metastation_sort124
# функция нахождения cумы
def sum(seq):
def add(x, y):
return x+y
return reduce(add, seq, 0)
# функция нахождения растояния по шаровым координатам
def len_witput_points(start_point, end_point):
R = 6376 # радиус земли
sp1 = start_point[1]*pi/180
ep1 = end_point[1]*pi/180
sp0 = start_point[0]*pi/180
ep0 = end_point[0]*pi/180
lenth = acos(sin(sp1)*sin(ep1) + cos(sp1)*cos(ep1)*cos(ep0-sp0))*R
return lenth
#ф-ия нахождения х(иксов) всех станций "с кешем"
def get_all_x():
all_station_x = all_x()
return all_station_x
# нахождение точек в радиусе старта
def get_points_in_radius_start(start_x, start_y, all_station_x, Transport1, Transport2, Transport3, Transport4):
KoeRad = 0.01
ninee = [1, 2]
while ninee:
sx1 = start_x + KoeRad
sx2 = start_x - KoeRad
sy1 = start_y + KoeRad
sy2 = start_y - KoeRad
radius_x_start, points_in_radius_start = list(), list()
for station_x in all_station_x:
if sx2 < station_x < sx1:
radius_x_start += [station_x]
for x_start in radius_x_start:
station_for_y = Station.objects.filter(coordinate_x=x_start, notstations=True).values_list('coordinate_y', 'matrix_index')
for station_y in station_for_y:
if sy2 < station_y[0] < sy1:
transport = Station.objects.get(matrix_index=station_y[1]).route.transport_type_id
if Transport1 == 0 and transport == 1:
points_in_radius_start += [station_y[1]]
if Transport2 == 0 and transport == 2:
points_in_radius_start += [station_y[1]]
if Transport3 == 0 and transport == 3:
points_in_radius_start += [station_y[1]]
if Transport4 == 0 and transport == 4:
points_in_radius_start += [station_y[1]]
if points_in_radius_start == []:
KoeRad = KoeRad * 2
else:
break
return list(set(points_in_radius_start))
# нахождение точек в радиусе финиша
def get_points_in_radius_finish(finish_x, finish_y, all_station_x, Transport1, Transport2, Transport3, Transport4):
KoeRad = 0.01
ninee = [1, 2]
while ninee:
fx1 = finish_x + KoeRad
fx2 = finish_x - KoeRad
fy1 = finish_y + KoeRad
fy2 = finish_y - KoeRad
radius_x_finish, points_in_radius_finish = list(), list()
for station_x in all_station_x:
if fx2 < station_x < fx1:
radius_x_finish += [station_x]
for x_finish in radius_x_finish:
station_for_y = Station.objects.filter(coordinate_x=x_finish, notstations=True).values_list('coordinate_y', 'matrix_index')
for station_y in station_for_y:
if fy2 < station_y[0] < fy1:
transport = Station.objects.get(matrix_index=station_y[1]).route.transport_type_id
if Transport1 == 0 and transport == 1:
points_in_radius_finish += [station_y[1]]
if Transport2 == 0 and transport == 2:
points_in_radius_finish += [station_y[1]]
if Transport3 == 0 and transport == 3:
points_in_radius_finish += [station_y[1]]
if Transport4 == 0 and transport == 4:
points_in_radius_finish += [station_y[1]]
if points_in_radius_finish == []:
KoeRad = KoeRad * 2
else:
break
return list(set(points_in_radius_finish))
# ф-ия создания списка маршрутов с остановками внутри маршрута
def points_list3():
points_list_item = points_list_3()
return points_list_item
# ф-ия создания словоря маршрутов с остановками внутри маршрута
def points_dict():
routes_dict = dict()
for route_item in Route.objects.all():
routes_dict[route_item.id] = list(route_item.station_set.filter(notstations=True).values_list('matrix_index', flat=True).order_by('matrix_index'))
return routes_dict
#ф-ии расчётов матриц переходов от станции ко всем остальным станциям внутри маршрута
def new_route_speed_matrix(Transport1, Transport2, Transport3, Transport4):
para_dict = new_route_speed_matrix124()
return para_dict
#ф-ии расчётов матриц переходов от станции ко всем остальным станциям внутри маршрута
def get_route_speed_matrix(Transport1, Transport2, Transport3, Transport4):
order_dict, dict_route_matrix = dict(), dict()
from_index_list, para_dict = list(), list()
speed_matrix = get_speed_matrix(Transport1, Transport2, Transport3, Transport4)
points_dict_item = points_dict()
for route_id in points_dict_item:
item = points_dict_item[route_id]
list_speed_matrix = list()
len_item = len(item)
route_matrix = [[0] * len_item for i in range(len_item)]
for item_index in range(len_item):
next_item_index = item_index + 1
if next_item_index == len_item:
break
from_matrix_index = item[item_index]
to_matrix_index = item[next_item_index]
list_speed_matrix += [speed_matrix[to_matrix_index][from_matrix_index]]
order_dict[from_matrix_index] = item_index
order_dict[to_matrix_index] = next_item_index
for item_index in range(len_item):
next_item_index = item_index + 1
if next_item_index == len_item:
break
for item_index2 in range(len_item):
next_item_index2 = item_index2 + 1
if next_item_index2 == len_item:
break
if item_index < next_item_index2:
route_matrix[item_index][next_item_index2] = sum(list_speed_matrix[item_index:next_item_index2])
if item_index2 < next_item_index:
route_matrix[next_item_index][item_index2] = sum(list_speed_matrix[item_index2:next_item_index])
dict_route_matrix[route_id] = route_matrix
para_dict = [dict_route_matrix, order_dict]
return para_dict
# ф-ия создания словаря с открытыми, закрытыми маршрутами
def points_dict_open():
routes_dict = dict()
for route_item in Route.objects.all():
routes_dict[route_item.id] = 1
return routes_dict
# ф-ия нахождения времени в радиусе старта и финиша
def len_start_finish(start_x, start_y, finish_x, finish_y, points_in_radius_start, points_in_radius_finish):
start = [start_x, start_y]
finish = [finish_x, finish_y]
len_list_start_finish = []
points_list = points_list_for_sort124()#Station.objects.filter(notstations=True).values_list('coordinate_x', 'coordinate_y').order_by('matrix_index')
list_lenths1 = list()
for station in points_in_radius_start:
lenth = len_witput_points(start, points_list[station]) / 3
list_lenths1 += [lenth]
len_list_start_finish += [list_lenths1]
list_lenths2 = list()
for station in points_in_radius_finish:
lenth = len_witput_points(finish, points_list[station]) / 3
list_lenths2 += [lenth]
len_list_start_finish += [list_lenths2]
return len_list_start_finish
# функция создания speed_matrix "с кешем"
def get_speed_matrix(Transport1, Transport2, Transport3, Transport4):
speed_matrix = speed_matrix124()
# s_m__txt = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix.txt')
# s_m__txt1 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix1.txt')
# s_m__txt2 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix2.txt')
# s_m__txt3 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix3.txt')
# s_m__txt4 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix4.txt')
# s_m__txt12 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix12.txt')
# s_m__txt13 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix13.txt')
# s_m__txt14 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix14.txt')
# s_m__txt23 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix23.txt')
# s_m__txt24 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix24.txt')
# s_m__txt34 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix34.txt')
# s_m__txt123 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix123.txt')
# #s_m__txt124 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix124.txt')
# s_m__txt134 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix134.txt')
# s_m__txt234 = os.path.join(PROJECT_ROOT, 'kesh2/speed_matrix234.txt')
# if Transport1 == 0 and Transport2 == 0 and Transport3 == 0 and Transport4 == 0:
# fp = open(s_m__txt, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
# if Transport1 == 0 and Transport2 == 1 and Transport3 == 1 and Transport4 == 1:
# fp = open(s_m__txt1, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
# if Transport1 == 1 and Transport2 == 0 and Transport3 == 1 and Transport4 == 1:
# fp = open(s_m__txt2, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
# if Transport1 == 1 and Transport2 == 1 and Transport3 == 0 and Transport4 == 1:
# fp = open(s_m__txt3, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
# if Transport1 == 1 and Transport2 == 1 and Transport3 == 1 and Transport4 == 0:
# fp = open(s_m__txt4, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
# if Transport1 == 0 and Transport2 == 0 and Transport3 == 1 and Transport4 == 1:
# fp = open(s_m__txt12, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
# if Transport1 == 0 and Transport2 == 1 and Transport3 == 0 and Transport4 == 1:
# fp = open(s_m__txt13, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
# if Transport1 == 0 and Transport2 == 1 and Transport3 == 1 and Transport4 == 0:
# fp = open(s_m__txt14, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
# if Transport1 == 1 and Transport2 == 0 and Transport3 == 0 and Transport4 == 1:
# fp = open(s_m__txt23, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
# if Transport1 == 1 and Transport2 == 0 and Transport3 == 1 and Transport4 == 0:
# fp = open(s_m__txt24, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
# if Transport1 == 1 and Transport2 == 1 and Transport3 == 0 and Transport4 == 0:
# fp = open(s_m__txt34, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
# if Transport1 == 0 and Transport2 == 0 and Transport3 == 0 and Transport4 == 1:
# fp = open(s_m__txt123, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
# if Transport1 == 0 and Transport2 == 0 and Transport3 == 1 and Transport4 == 0:
# speed_matrix = speed_matrix124()
# if Transport1 == 0 and Transport2 == 1 and Transport3 == 0 and Transport4 == 0:
# fp = open(s_m__txt134, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
# if Transport1 == 1 and Transport2 == 0 and Transport3 == 0 and Transport4 == 0:
# fp = open(s_m__txt234, 'r')
# read_file = fp.read()
# speed_matrix = eval(read_file)
# fp.close()
return speed_matrix
def new_Metastation(Transport1, Transport2, Transport3, Transport4):
metastation_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation.txt')
metastation1_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation1.txt')
metastation2_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation2.txt')
metastation3_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation3.txt')
metastation4_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation4.txt')
metastation124_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation124.txt')
metastation134_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation134.txt')
metastation234_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation234.txt')
metastation123_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation123.txt')
metastation12_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation12.txt')
metastation13_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation13.txt')
metastation14_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation14.txt')
metastation23_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation23.txt')
metastation24_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation24.txt')
metastation34_txt = os.path.join(PROJECT_ROOT, 'kesh2/metastation34.txt')
if Transport1 == 1 and Transport2 == 0 and Transport3 == 0 and Transport4 == 0:
fp = open(metastation234_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 0 and Transport2 == 1 and Transport3 == 0 and Transport4 == 0:
fp = open(metastation134_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 0 and Transport2 == 0 and Transport3 == 1 and Transport4 == 0:
fp = open(metastation124_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 0 and Transport2 == 0 and Transport3 == 0 and Transport4 == 1:
fp = open(metastation123_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 1 and Transport2 == 1 and Transport3 == 0 and Transport4 == 0:
fp = open(metastation34_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 1 and Transport2 == 0 and Transport3 == 1 and Transport4 == 0:
fp = open(metastation24_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 1 and Transport2 == 0 and Transport3 == 0 and Transport4 == 1:
fp = open(metastation23_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 0 and Transport2 == 1 and Transport3 == 1 and Transport4 == 0:
fp = open(metastation14_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 0 and Transport2 == 1 and Transport3 == 0 and Transport4 == 1:
fp = open(metastation13_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 0 and Transport2 == 0 and Transport3 == 1 and Transport4 == 1:
fp = open(metastation12_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 1 and Transport2 == 1 and Transport3 == 1 and Transport4 == 0:
fp = open(metastation4_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 1 and Transport2 == 1 and Transport3 == 0 and Transport4 == 1:
fp = open(metastation3_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 1 and Transport2 == 0 and Transport3 == 1 and Transport4 == 1:
fp = open(metastation2_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 0 and Transport2 == 1 and Transport3 == 1 and Transport4 == 1:
fp = open(metastation1_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
if Transport1 == 0 and Transport2 == 0 and Transport3 == 0 and Transport4 == 0:
fp = open(metastation_txt, 'r')
read_file = fp.read()
Metastat = eval(read_file)
fp.close()
return Metastat
# Запустили считать волновой алгоритм
def volna(points_in_radius_finish, points_in_radius_start, len_list_start_finish, all_station_x, route_speed_matrix, speed_matrix, metastation_sort, points_list, Transport1, Transport2, Transport3, Transport4):
point_dict1 = points_dict()
route_dict = points_dict_open()
len_dinamic = len(all_station_x)
len_dinamic_list = len_dinamic + 2
dinamic_list = [[100] * len_dinamic_list][0]
next_points_list = points_in_radius_start
mass_next_points_list = len_list_start_finish[0]
route_d_st = route_stations(Transport1, Transport2, Transport3, Transport4)
test_min1 = 0
test_min2 = 0
test_min3 = 0
test_min4 = 0
test_min5 = 0
test_min6 = 0
test_min7 = 0
while next_points_list:
# Находим минимальную остановку среди открытых маршрутов
len_in = min(mass_next_points_list)
index_start_point = mass_next_points_list.index(len_in)
point_in = next_points_list[index_start_point]
route_key = route_d_st[point_in]#Station.objects.get(matrix_index=point_in).route_id
test_min1 += 1
if route_dict[route_key] == 1:
test_min2 += 1
# Берём соответствующий минимальной остановке маршрут и переносим его в словарь всех остановок в
# соответствующиие места увеличив значение на значение до минимальной остановки.
route_matrix = route_speed_matrix[0][route_key]
order_in_matrix = route_speed_matrix[1][point_in]
list_station_in_route = route_matrix[order_in_matrix]
len_list_station_in_route = len(list_station_in_route)
zero_order = point_in - order_in_matrix
slys_order = zero_order + len_list_station_in_route - 1
# Приращение маршрута на минимальное
for station_in in range(len_list_station_in_route):
test_min3 += 1
list_station_in_route[station_in] += len_in
if dinamic_list[zero_order:slys_order + 1][station_in] < list_station_in_route[station_in]:
list_station_in_route[station_in] = dinamic_list[zero_order:slys_order + 1][station_in]
dinamic_list[zero_order:slys_order + 1] = list_station_in_route
list_index = range(zero_order, slys_order + 1)
for ob_element in list_index:
test_min4 += 1
# Если попалась остановка из списка радиуса finish мы её записываем в список с ключём finish.
if ob_element in points_in_radius_finish and dinamic_list[-1] > dinamic_list[zero_order:slys_order + 1][list_index.index(ob_element)] + len_list_start_finish[1][points_in_radius_finish.index(ob_element)]:
dinamic_list[-1] = dinamic_list[zero_order:slys_order + 1][list_index.index(ob_element)] + len_list_start_finish[1][points_in_radius_finish.index(ob_element)]
dinamic_list[-2] = ob_element
# Считаем все переходы записываем соответствующие значения в словарь и закрываем маршрут.
for para in metastation_sort[ob_element]:
test_min5 += 1
if dinamic_list[para] > dinamic_list[ob_element] + speed_matrix[ob_element][para] and para not in list_index and (para not in next_points_list or mass_next_points_list[next_points_list.index(para)] > dinamic_list[ob_element] + speed_matrix[ob_element][para]) and dinamic_list[-1] > dinamic_list[ob_element] + speed_matrix[ob_element][para]:
dinamic_for = dinamic_list[ob_element] + speed_matrix[ob_element][para]
route_key2 = route_d_st[para]#Station.objects.get(matrix_index=para).route_id
if route_dict[route_key2] == 1 and para not in next_points_list:
next_points_list += [para]
mass_next_points_list += [dinamic_for]
if route_dict[route_key2] == 0 or route_dict[route_key2] == 2:
route_dict[route_key2] = 2
if para in next_points_list and mass_next_points_list[next_points_list.index(para)] > dinamic_list[ob_element] + speed_matrix[ob_element][para]:
index_next_points_list = next_points_list.index(para)
mass_next_points_list[index_next_points_list] = dinamic_for
dinamic_list[para] = dinamic_for
# Закрываем маршрут
route_dict[route_key] = 0
# Двойная волна с начала маршрута и конца
www = route_zazor(Transport1, Transport2, Transport3, Transport4)
for key_in_dict in route_dict:
if route_dict[key_in_dict] == 2:
dinamic1 = point_dict1[key_in_dict][0]
dinamic2 = point_dict1[key_in_dict][-1]
dinamic_slys = dinamic_list[dinamic1:dinamic2 + 1]
for index_in_dinamic_slys in range(len(dinamic_slys)):
next_index_in_dinamic_slys = index_in_dinamic_slys + 1
if next_index_in_dinamic_slys == len(dinamic_slys):
break
if dinamic_slys[next_index_in_dinamic_slys] > dinamic_slys[index_in_dinamic_slys] + www[key_in_dict][1][index_in_dinamic_slys]:
dinamic_slys[next_index_in_dinamic_slys] = dinamic_slys[index_in_dinamic_slys] + www[key_in_dict][1][index_in_dinamic_slys]
dinamic_slys.reverse()
for index_in_dinamic_slys in range(len(dinamic_slys)):
next_index_in_dinamic_slys = index_in_dinamic_slys + 1
if next_index_in_dinamic_slys == len(dinamic_slys):
break
if dinamic_slys[next_index_in_dinamic_slys] > dinamic_slys[index_in_dinamic_slys] + www[key_in_dict][0][index_in_dinamic_slys]:
dinamic_slys[next_index_in_dinamic_slys] = dinamic_slys[index_in_dinamic_slys] + www[key_in_dict][0][index_in_dinamic_slys]
dinamic_slys.reverse()
dinamic_list[dinamic1:dinamic2 + 1] = dinamic_slys
route_dict[key_in_dict] = 0
# сравниваем с точкой finish если минимальная меньше идём дальше иначе выходим из цикла
if min(mass_next_points_list) > dinamic_list[-1]:
break
next_points_list.remove(point_in)
mass_next_points_list.remove(len_in)
if next_points_list == []:
break
print test_min1, test_min2, test_min3, test_min4, test_min5, test_min6, test_min7
return dinamic_list
# Минимум в маршруте
def min_in_route(list_p, dinamic_list, list_in, list_of_excluded):
minimal = list_p
for index_p in range(len(list_p)):
next_index_p = index_p + 1
prev_index_p = index_p - 1
if next_index_p == len(list_p):
break
p_i_p = dinamic_list[list_p[prev_index_p]]
i_p = dinamic_list[list_p[index_p]]
n_i_p = dinamic_list[list_p[next_index_p]]
value_list_in0= dinamic_list[list_in[0]]
if p_i_p > i_p and n_i_p > i_p:
if index_p < list_p.index(list_in[0]):
z = list_p[index_p:list_p.index(list_in[0]) + 1]
print z, 'z2'
if index_p > list_p.index(list_in[0]):
z = list_p[list_p.index(list_in[0]):index_p + 1]
if len(minimal) > len(z) and list_p[index_p] != list_in[0] and list_p[index_p] not in list_of_excluded and dinamic_list[list_p[index_p]] < value_list_in0:
minimal = z
print z, 'z3'
if dinamic_list[list_p[-1]] < p_i_p and list_p[-1] != list_in[0] and list_p[-1] not in list_of_excluded and dinamic_list[list_p[-1]] < value_list_in0:
z = list_p[list_p.index(list_in[0]):]
print z, 'z4'
if len(minimal) > len(z):
minimal = z
if dinamic_list[list_p[0]] < n_i_p and list_p[0] != list_in[0] and list_p[0] not in list_of_excluded and dinamic_list[list_p[0]] < value_list_in0:
z = list_p[0:list_p.index(list_in[0]) + 1]
print z, 'z1'
if len(minimal) > len(z):
minimal = z
return minimal
# Запустили считать алгоритм обратной волны
def revers_volna(points_list, dinamic_list, speed_matrix):
ind_ex_list2 = list()
print dinamic_list[-2], min(dinamic_list[:866]), dinamic_list.index(min(dinamic_list[:866])), dinamic_list[:866].count(min(dinamic_list[:866]))
min_dinamic_list866 = min(dinamic_list[:866])
list_in = [dinamic_list[-2]]
list_of_excluded = []
while list_in:
for list_p in points_list:
i = 0
if list_in[0] in list_p:
minimal = min_in_route(list_p, dinamic_list, list_in, list_of_excluded)
if list_in[0] == minimal[0]:
list_of_excluded += [minimal[-1]]
if list_in[0] == minimal[-1]:
list_of_excluded += [minimal[0]]
min_dinamic = min(dinamic_list[minimal[0]:minimal[-1] + 1])
index_min_dinamic = dinamic_list[minimal[0]:minimal[-1] + 1].index(min_dinamic)
index_station_finish = dinamic_list[minimal[0]:minimal[-1] + 1].index(dinamic_list[list_in[0]])
index_in_dinamic_list_min = minimal[0] + index_min_dinamic
dinamic_slyse = list()
if index_min_dinamic < index_station_finish:
dinamic_slyse = range(minimal[0] + index_min_dinamic, minimal[0] + index_station_finish + 1)
if index_min_dinamic > index_station_finish:
dinamic_slyse = range(minimal[0] + index_station_finish, minimal[0] + index_min_dinamic + 1)
if dinamic_slyse != []:
if dinamic_slyse[-1] == list_in[0]:
dinamic_slyse.reverse()
ind_ex_list2 += dinamic_slyse
for ind_ex in dinamic_list[:866]:
if ind_ex < min_dinamic and round(speed_matrix[dinamic_list.index(ind_ex)][index_in_dinamic_list_min], 6) == round(dinamic_list[index_in_dinamic_list_min] - dinamic_list[dinamic_list.index(ind_ex)], 6) and min_dinamic != list_in[0]:
list_in += [dinamic_list.index(ind_ex)]
list_in.remove(list_in[0])
i += 1
print ind_ex_list2, list_in, list_of_excluded
break
if list_in[0] == dinamic_list.index(min(dinamic_list[:866])):
ind_ex_list2 +=[list_in[0]]
break
if i == 0:
min_dinamic_list866 = min_dinamic
break
if min_dinamic == min_dinamic_list866 or list_in[0] == dinamic_list.index(min_dinamic_list866):
break
return ind_ex_list2
# Ф-ия адаптации результатов вывода
def result_adapt(dinamic_list, dinamic_list_min, start_x, start_y, finish_x, finish_y):
dinamic_list_min.reverse()
final_views = [{'x': start_x, 'y': start_y, 'idRoute':"-1", 'transportName':"", 'stopName':"Start", 't':'0', 'TransportsType':'', 'routeName':''}]
i = 0
q_list = list()
for q in dinamic_list_min:
q_list += [q]
item_dict = {}
point = Station.objects.get(matrix_index=q)
item_dict['stopName'] = point.name
item_dict['idRoute'] = point.route_id
item_dict['routeName'] = Route.objects.get(id=point.route_id).route
item_dict['route__transport_type'] = point.route.transport_type_id
transport_id = item_dict['transportName'] = item_dict['route__transport_type']
item_dict['TransportsType'] = Transport.objects.get(id=transport_id).name
item_dict['x'] = point.coordinate_x
item_dict['y'] = point.coordinate_y
item_dict['t'] = round(dinamic_list[q]*60, 2)
final_views += [item_dict]
orde = point.order
long_route = len(Station.objects.filter(route=point.route_id).values_list('order', flat=True)) - 1
next_orde = orde + 1
prev_orde = orde - 1
prev_q = q - 1
next_q = q + 1
if long_route >= next_orde and Station.objects.get(order=next_orde, route=point.route_id).matrix_index == -1 and next_q not in q_list:
end_orde = range(Station.objects.get(matrix_index=next_q).order)
order_list = end_orde[next_orde:]
for element in order_list:
item_dict = {}
point2 = Station.objects.get(order=element, route=point.route_id)
item_dict['route__transport_type'] = 10
item_dict['idRoute'] = point.route_id
item_dict['x'] = point2.coordinate_x
item_dict['y'] = point2.coordinate_y
item_dict['stopName'] = element
item_dict['t'] = round(dinamic_list[q]*60, 2)
final_views += [item_dict]
elif prev_orde >= 1 and Station.objects.get(order=prev_orde, route=point.route_id).matrix_index == -1 and prev_q not in q_list:
end_orde = range(Station.objects.get(matrix_index=q).order)
end_orde2 = len(range(Station.objects.get(matrix_index=prev_q).order + 1))
order_list = end_orde[end_orde2:orde]
order_list.reverse()
for element in order_list:
item_dict = {}
point2 = Station.objects.get(order=element, route=point.route_id)
item_dict['route__transport_type'] = 10
item_dict['idRoute'] = point.route_id
item_dict['x'] = point2.coordinate_x
item_dict['y'] = point2.coordinate_y
item_dict['stopName'] = element
item_dict['t'] = round(dinamic_list[q]*60, 2)
final_views += [item_dict]
i += 1
final_views.append({'x': finish_x, 'y': finish_y, 'idRoute': "-1", 'transportName': "", 'stopName': "Finish", 't': round(dinamic_list[-1]*60, 2), 'TransportsType': '', 'routeName': ''})
final_views.reverse()
return final_views
def Metastation_sort(Transport1, Transport2, Transport3, Transport4):
# Metastation_sort124_txt = os.path.join(PROJECT_ROOT, 'kesh2/Metastation_sort124.txt')
# max_station_timestamp = Station.objects.all().aggregate(Max('timestamp'))
# max_route_timestamp = Route.objects.all().aggregate(Max('timestamp'))
# max_transport_timestamp = Transport.objects.all().aggregate(Max('timestamp'))
# max_onestation_timestamp = Onestation.objects.all().aggregate(Max('timestamp'))
# max_timestamp = max(max_onestation_timestamp, max_station_timestamp, max_route_timestamp, max_transport_timestamp)
# max_timestamp = max_timestamp['timestamp__max']
# if os.path.isfile(Metastation_sort124_txt) == False:
# open(Metastation_sort124_txt, 'w')
# sm_file = os.path.getmtime(Metastation_sort124_txt)
# stat = os.stat(Metastation_sort124_txt)
# file_size = stat.st_size
# timestamp = datetime.datetime.fromtimestamp(sm_file)
#
# if timestamp < max_timestamp or file_size == 0:
# list_in_raduus = [[]] * 866
# metastations_stations_list = new_Metastation(Transport1, Transport2, Transport3, Transport4)
# metastations_stations_list.sort()
# for element in range(866):
# one_list = list()
# for para in metastations_stations_list:
# if element == para[0]:
# one_list += [para[1]]
# list_in_raduus[element] = one_list
#
# fp = open(Metastation_sort124_txt, "w")
# fp.write(repr(list_in_raduus))
# fp.close()
# else:
# fp = open(Metastation_sort124_txt, 'r')
# read_file = fp.read()
# list_in_raduus = eval(read_file)
# fp.close()
list_in_raduus = metastation_sort124()
return list_in_raduus
def route_zazor(Transport1, Transport2, Transport3, Transport4):
speed_matrix_short = route_zazor124()
return speed_matrix_short
# def route_zazor(Transport1, Transport2, Transport3, Transport4):
# q = points_dict()
# speed_matrix_short = dict()
# speed_matrix = get_speed_matrix(Transport1, Transport2, Transport3, Transport4)
# for item in q:
# go_in, go_out, all_list = [], [], []
# z = q[item]
# for index in range(len(z)):
# next_index = index + 1
# if next_index == len(z):
# break
# go_in += [speed_matrix[index + z[0]][next_index + z[0]]]
# go_out += [speed_matrix[next_index + z[0]][index + z[0]]]
# go_out.reverse()
# all_list += [go_out]
# all_list += [go_in]
# speed_matrix_short[item] = all_list
#
# return speed_matrix_short
def route_stations(Transport1, Transport2, Transport3, Transport4):
stash = route_stat124()
return stash
# def points_list_for_sort():
# points_list124_txt = os.path.join(PROJECT_ROOT, 'kesh2/points_list_for_sort124.txt')
# max_station_timestamp = Station.objects.all().aggregate(Max('timestamp'))
# max_route_timestamp = Route.objects.all().aggregate(Max('timestamp'))
# max_transport_timestamp = Transport.objects.all().aggregate(Max('timestamp'))
# max_onestation_timestamp = Onestation.objects.all().aggregate(Max('timestamp'))
# max_timestamp = max(max_onestation_timestamp, max_station_timestamp, max_route_timestamp, max_transport_timestamp)
# max_timestamp = max_timestamp['timestamp__max']
# if os.path.isfile(points_list124_txt) == False:
# open(points_list124_txt, 'w')
# sm_file = os.path.getmtime(points_list124_txt)
# stat = os.stat(points_list124_txt)
# file_size = stat.st_size
# timestamp = datetime.datetime.fromtimestamp(sm_file)
#
# if timestamp < max_timestamp or file_size == 0:
# points_list = list(Station.objects.filter(notstations=True).values_list('coordinate_x', 'coordinate_y').order_by('matrix_index'))
#
# fp = open(points_list124_txt, "w")
# fp.write(repr(points_list))
# fp.close()
# else:
# fp = open(points_list124_txt, 'r')
# read_file = fp.read()
# points_list = eval(read_file)
# fp.close()
#
# return points_list