Skip to content

Commit c92ab78

Browse files
karusselleasbar
andauthored
Remove shortest weighting from DefaultWeightingFactory class (graphhopper#2865)
* heading_penalty parameter should be picked and properly merged for custom weighting too * remove shortest weighting * keep ShortestWeighting in jar * keep default constructor * fix imports * fix imports * comments from review * changelog --------- Co-authored-by: easbar <[email protected]>
1 parent e439a45 commit c92ab78

23 files changed

+264
-268
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 8.0 [not yet released]
22

3+
- remove shortest weighting for public usage, use a high distance_influence instead, see #2865
34
- removed duration:seconds as intermediate tag
45
- /info endpoint does no longer return the vehicle used per profile and won't return encoded value of vehicles like car_average_speed
56
- Country rules no longer contain maxspeed handling, enable a much better alternative via `max_speed_calculator.enabled: true`. On the client side use `max_speed_estimated` to determine if max_speed is from OSM or an estimation. See #2810

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ Here is a list of the more detailed features:
232232
* OpenStreetMap integration: stores and considers road type, speed limit, the surface, barriers, access restrictions, ferries, [conditional access restrictions](https://github.com/graphhopper/graphhopper/pull/621), ...
233233
* GraphHopper is fast. And with the so called "Contraction Hierarchies" it can be even faster (enabled by default).
234234
* Memory efficient data structures, algorithms and [the low and high level API](./docs/core/low-level-api.md) is tuned towards ease of use and efficiency
235-
* Multiple weightings (fastest/shortest/custom/...) and pre-built routing profiles: car, bike, racing bike, mountain bike, foot, hike, motorcycle, wheelchair, ...
235+
* Pre-built routing profiles: car, bike, racing bike, mountain bike, foot, hike, motorcycle, wheelchair, ...
236236
* [Customization of these profiles](./docs/core/profiles.md#custom-profiles) are possible and e.g. get truck routing or support for cargo bikes and [many other changes](https://www.graphhopper.com/blog/2020/05/31/examples-for-customizable-routing/)
237237
* Provides a powerful [web API](./docs/web/api-doc.md) that exposes the data from OpenStreetMap and allows customizing the vehicle profiles per request. With JavaScript and Java clients.
238238
* Does [map matching](./map-matching)

config-example.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ graphhopper:
1515
# In general a profile consists of the following
1616
# - name (required): a unique string identifier for the profile
1717
# - vehicle (required): refers to the `graph.vehicles` used for this profile
18-
# - weighting (required): the weighting used for this profile like custom,fastest,shortest or short_fastest
18+
# - weighting (required): the weighting used for this profile like custom
1919
# - turn_costs (true/false, default: false): whether or not turn restrictions should be applied for this profile.
2020
#
2121
# Depending on the above fields there are other properties that can be used, e.g.
2222
# - distance_factor: 0.1 (can be used to fine tune the time/distance trade-off of short_fastest weighting)
2323
# - u_turn_costs: 60 (time-penalty for doing a u-turn in seconds (only possible when `turn_costs: true`)).
24-
# Note that since the u-turn costs are given in seconds the weighting you use should also calculate the weight
25-
# in seconds, so for example it does not work with shortest weighting.
2624
# - custom_model_files: when you specified "weighting: custom" you need to set one or more json files which are searched in
2725
# custom_models.directory or the working directory that defines the custom_model. If you want an empty model you can
2826
# set "custom_model_files: []

core/src/main/java/com/graphhopper/GraphHopper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ public GraphHopper setStoreOnFlush(boolean storeOnFlush) {
248248
* <pre>
249249
* {@code
250250
* hopper.setProfiles(
251-
* new Profile("my_car").setVehicle("car").setWeighting("shortest"),
252-
* new Profile("your_bike").setVehicle("bike").setWeighting("fastest")
251+
* new CustomProfile("my_car").setVehicle("car"),
252+
* new CustomProfile("your_bike").setVehicle("bike")
253253
* );
254254
* hopper.getCHPreparationHandler().setCHProfiles(
255255
* new CHProfile("my_car"),

core/src/main/java/com/graphhopper/routing/DefaultWeightingFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public Weighting createWeighting(Profile profile, PMap requestHints, boolean dis
9595
weighting = CustomModelParser.createWeighting(accessEnc, speedEnc,
9696
priorityEnc, encodingManager, turnCostProvider, mergedCustomModel);
9797
} else if ("shortest".equalsIgnoreCase(weightingStr)) {
98-
weighting = new ShortestWeighting(accessEnc, speedEnc, turnCostProvider);
98+
throw new IllegalArgumentException("Instead of weighting=shortest use weighting=custom with a high distance_influence");
9999
} else if ("fastest".equalsIgnoreCase(weightingStr)) {
100100
if (!encodingManager.hasEncodedValue(RoadAccess.KEY))
101101
throw new IllegalArgumentException("The fastest weighting requires road_access");

core/src/main/java/com/graphhopper/routing/weighting/AbstractWeighting.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static boolean isValidName(String name) {
9191

9292
@Override
9393
public String toString() {
94-
return getName() + "|" + speedEnc.getName().split("$")[0];
94+
return getName() + "|" + speedEnc.getName();
9595
}
9696

9797
}

core/src/main/java/com/graphhopper/routing/weighting/ShortestWeighting.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,61 @@
2121
import com.graphhopper.routing.ev.DecimalEncodedValue;
2222
import com.graphhopper.util.EdgeIteratorState;
2323

24-
import static com.graphhopper.routing.weighting.TurnCostProvider.NO_TURN_COST_PROVIDER;
25-
2624
/**
2725
* Calculates the shortest route - independent of a vehicle as the calculation is based on the
2826
* distance only.
29-
* <p>
3027
*
3128
* @author Peter Karich
3229
*/
33-
public class ShortestWeighting extends AbstractWeighting {
30+
public class ShortestWeighting implements Weighting {
31+
32+
final BooleanEncodedValue accessEnc;
33+
final DecimalEncodedValue speedEnc;
34+
final TurnCostProvider tcProvider;
35+
3436
public ShortestWeighting(BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc) {
35-
this(accessEnc, speedEnc, NO_TURN_COST_PROVIDER);
37+
this(accessEnc, speedEnc, TurnCostProvider.NO_TURN_COST_PROVIDER);
3638
}
3739

38-
public ShortestWeighting(BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, TurnCostProvider turnCostProvider) {
39-
super(accessEnc, speedEnc, turnCostProvider);
40+
public ShortestWeighting(BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, TurnCostProvider tcProvider) {
41+
this.accessEnc = accessEnc;
42+
this.speedEnc = speedEnc;
43+
this.tcProvider = tcProvider;
4044
}
4145

4246
@Override
43-
public double getMinWeight(double currDistToGoal) {
44-
return currDistToGoal;
47+
public double getMinWeight(double distance) {
48+
return distance;
4549
}
4650

4751
@Override
4852
public double calcEdgeWeight(EdgeIteratorState edgeState, boolean reverse) {
49-
if (edgeHasNoAccess(edgeState, reverse))
53+
if (reverse ? !edgeState.getReverse(accessEnc) : !edgeState.get(accessEnc))
5054
return Double.POSITIVE_INFINITY;
5155
return edgeState.getDistance();
5256
}
5357

58+
@Override
59+
public long calcEdgeMillis(EdgeIteratorState edgeState, boolean reverse) {
60+
double speed = reverse ? edgeState.getReverse(speedEnc) : edgeState.get(speedEnc);
61+
return Math.round(edgeState.getDistance() / speed * 3.6 * 1000);
62+
}
63+
64+
@Override
65+
public double calcTurnWeight(int inEdge, int viaNode, int outEdge) {
66+
return tcProvider.calcTurnWeight(inEdge, viaNode, outEdge);
67+
}
68+
69+
@Override
70+
public long calcTurnMillis(int inEdge, int viaNode, int outEdge) {
71+
return tcProvider.calcTurnMillis(inEdge, viaNode, outEdge);
72+
}
73+
74+
@Override
75+
public boolean hasTurnCosts() {
76+
return tcProvider != TurnCostProvider.NO_TURN_COST_PROVIDER;
77+
}
78+
5479
@Override
5580
public String getName() {
5681
return "shortest";

core/src/main/java/com/graphhopper/routing/weighting/Weighting.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
import com.graphhopper.util.EdgeIteratorState;
2121

2222
/**
23-
* Specifies how the best route is calculated. E.g. the fastest or shortest route.
24-
* <p>
23+
* Specifies how the best route is calculated.
2524
*
2625
* @author Peter Karich
2726
*/

0 commit comments

Comments
 (0)