1
+ /*
2
+ * Licensed to GraphHopper GmbH under one or more contributor
3
+ * license agreements. See the NOTICE file distributed with this work for
4
+ * additional information regarding copyright ownership.
5
+ *
6
+ * GraphHopper GmbH licenses this file to you under the Apache License,
7
+ * Version 2.0 (the "License"); you may not use this file except in
8
+ * compliance with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ package com .graphhopper .routing .ch ;
20
+
21
+ import com .graphhopper .routing .Dijkstra ;
22
+ import com .graphhopper .routing .util .CarFlagEncoder ;
23
+ import com .graphhopper .routing .util .EncodingManager ;
24
+ import com .graphhopper .routing .util .TraversalMode ;
25
+ import com .graphhopper .routing .weighting .ShortestWeighting ;
26
+ import com .graphhopper .routing .weighting .Weighting ;
27
+ import com .graphhopper .storage .CHConfig ;
28
+ import com .graphhopper .storage .CHGraph ;
29
+ import com .graphhopper .storage .GraphBuilder ;
30
+ import com .graphhopper .storage .GraphHopperStorage ;
31
+ import org .junit .jupiter .api .Test ;
32
+
33
+ import static org .junit .jupiter .api .Assertions .*;
34
+
35
+ class NodeBasedWitnessPathSearcherTest {
36
+
37
+ private final CarFlagEncoder encoder = new CarFlagEncoder ();
38
+ private final EncodingManager encodingManager = EncodingManager .create (encoder );
39
+ private final Weighting weighting = new ShortestWeighting (encoder );
40
+ private final GraphHopperStorage graph = new GraphBuilder (encodingManager ).setCHConfigs (CHConfig .nodeBased ("profile" , weighting )).create ();
41
+ private final CHGraph lg = graph .getCHGraph ();
42
+ private final PrepareCHGraph pg = PrepareCHGraph .nodeBased (lg , weighting );
43
+
44
+ private void createExampleGraph () {
45
+ //5-1-----2
46
+ // \ __/|
47
+ // 0 |
48
+ // / |
49
+ // 4-----3
50
+ //
51
+ graph .edge (0 , 1 , 1 , true );
52
+ graph .edge (0 , 2 , 1 , true );
53
+ graph .edge (0 , 4 , 3 , true );
54
+ graph .edge (1 , 2 , 3 , true );
55
+ graph .edge (2 , 3 , 1 , true );
56
+ graph .edge (4 , 3 , 2 , true );
57
+ graph .edge (5 , 1 , 2 , true );
58
+ graph .freeze ();
59
+ }
60
+
61
+ @ Test
62
+ public void testShortestPathSkipNode () {
63
+ createExampleGraph ();
64
+ final double normalDist = new Dijkstra (graph , weighting , TraversalMode .NODE_BASED ).calcPath (4 , 2 ).getDistance ();
65
+ NodeBasedWitnessPathSearcher algo = new NodeBasedWitnessPathSearcher (pg );
66
+
67
+ setMaxLevelOnAllNodes ();
68
+
69
+ algo .ignoreNode (3 );
70
+ algo .setWeightLimit (100 );
71
+ int nodeEntry = algo .findEndNode (4 , 2 );
72
+ assertTrue (algo .getWeight (nodeEntry ) > normalDist );
73
+
74
+ algo .clear ();
75
+ algo .setMaxVisitedNodes (1 );
76
+ nodeEntry = algo .findEndNode (4 , 2 );
77
+ assertEquals (-1 , nodeEntry );
78
+ }
79
+
80
+ @ Test
81
+ public void testShortestPathSkipNode2 () {
82
+ createExampleGraph ();
83
+ final double normalDist = new Dijkstra (graph , weighting , TraversalMode .NODE_BASED ).calcPath (4 , 2 ).getDistance ();
84
+ assertEquals (3 , normalDist , 1e-5 );
85
+ NodeBasedWitnessPathSearcher algo = new NodeBasedWitnessPathSearcher (pg );
86
+
87
+ setMaxLevelOnAllNodes ();
88
+
89
+ algo .ignoreNode (3 );
90
+ algo .setWeightLimit (10 );
91
+ int nodeEntry = algo .findEndNode (4 , 2 );
92
+ assertEquals (4 , algo .getWeight (nodeEntry ), 1e-5 );
93
+
94
+ nodeEntry = algo .findEndNode (4 , 1 );
95
+ assertEquals (4 , algo .getWeight (nodeEntry ), 1e-5 );
96
+ }
97
+
98
+ @ Test
99
+ public void testShortestPathLimit () {
100
+ createExampleGraph ();
101
+ NodeBasedWitnessPathSearcher algo = new NodeBasedWitnessPathSearcher (pg );
102
+
103
+ setMaxLevelOnAllNodes ();
104
+
105
+ algo .ignoreNode (0 );
106
+ algo .setWeightLimit (2 );
107
+ int endNode = algo .findEndNode (4 , 1 );
108
+ // did not reach endNode
109
+ assertNotEquals (1 , endNode );
110
+ }
111
+
112
+ private void setMaxLevelOnAllNodes () {
113
+ int nodes = pg .getNodes ();
114
+ for (int node = 0 ; node < nodes ; node ++) {
115
+ pg .setLevel (node , nodes );
116
+ }
117
+ }
118
+
119
+ }
0 commit comments