-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutefinder.php
143 lines (106 loc) · 3.43 KB
/
routefinder.php
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
<?php
declare(strict_types = 1);
function getTime(string $from, string $to, array $matrix)
{
$toParts = explode("##", $to);
$to = ["lat" => $toParts[0], 'lng' => $toParts[1]];
foreach ($matrix[$from] As $possibleRoute)
{
if(($possibleRoute['b']['lat'] == $to['lat']) && ($possibleRoute['b']['lng'] == $to['lng']))
{
return $possibleRoute['route']['time'];
}
}
throw new Exception("If we end here something is fucked up with the paths");
}
function findOptimalPath(array $from, array $to, array &$way_matrix, array $path, int $depth = 5, int $maxJourneyTime , int $totalJourneyTime = 0) : array
{
$maxTime = null;
print implode(" ", $path) . "\n";
if($depth == 0)
{
return [];
}
foreach ($way_matrix[$from['lat'] . '##' . $from['lng']] As $possibleRoute)
{
if(($possibleRoute['b']['lat'] == $to['lat']) && ($possibleRoute['b']['lng'] == $to['lng']))
{
$maxTime = $possibleRoute['route']['time'];
# do something if maxTime < 30 minutes!
if($maxTime < 600000)
{
$path[] = $possibleRoute['b']['lat'] . '##' . $possibleRoute['b']['lng'];
return [$path];
}
break;
}
}
if(($totalJourneyTime + $maxTime) > $maxJourneyTime)
{
return [];
}
$results = [];
foreach ($way_matrix[$from['lat'] . '##' . $from['lng']] As $possibleRoute)
{
if(in_array($possibleRoute['b']['lat'] . '##' . $possibleRoute['b']['lng'], $path)) {
continue;
}
if(!isset($possibleRoute['route']) || ($possibleRoute['route']['time'] >= $maxTime)) {
continue;
}
# get route from current node
$currentpath = $path;
$currentpath[] = $possibleRoute['b']['lat'] . '##' . $possibleRoute['b']['lng'];
$result = findOptimalPath($possibleRoute['b'], $to, $way_matrix, $currentpath, $depth - 1, $maxJourneyTime, $totalJourneyTime + $possibleRoute['route']['time']);
$results = array_merge($results, $result);
}
return array_filter($results);
}
/*
* Execution
*/
$jsonString = file_get_contents(__DIR__ . "/way_matrix.json");
$way_matrix = json_decode($jsonString, true);
$sorted_matrix = [];
foreach($way_matrix As $key => $relation)
{
unset($relation['route']['instructions']);
unset($relation['route']['points']);
$sorted_matrix[$relation['a']['lat'] . '##' . $relation['a']['lng']][] = $relation;
}
unset($way_matrix);
//"'2950 Hugo-Kirchberg-Straße/Tesa'",
$from = ["lat" => "53.654290000000000", 'lng' => "9.985280000000000"];
//"'2441 Rahlstedter Weg/Berner Heerweg'",
//$to = ["lat" => "53.606246000000000", "lng" => "10.120210000000000"];
//'2040 Ohnhorststraße/Klein Flottbek'
$to = ["lat" => "53.558587000000000", "lng" => "9.862160000000000"];
$output = findOptimalPath($from, $to, $sorted_matrix, [$from["lat"]. "##" . $from["lng"]], 5, 1200000, 0);
$sorted = [];
foreach($output As $route)
{
$routeCount = count($route) - 1;
$totalTime = 0;
for($i = 0 ; $i < $routeCount; $i++)
{
$totalTime += getTime($route[$i], $route[$i+1], $sorted_matrix);
}
$changes = count($route);
$sorted[$changes][$totalTime] = $route;
}
$counter = 0;
ksort($sorted);
foreach($sorted As $changes => $routesPerChanges)
{
ksort($routesPerChanges);
foreach ($routesPerChanges As $time => $route)
{
$i++;
print "Route 1: " . ($changes - 2) . " Umstiege" . " " . ((int) ($time / 1000 / 60) * 3) . " Minuten werden benötigt\n";
if($i == 10)
{
break 2;
}
}
}
$i = 0;