Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove FlagEncoder #2611

Merged
merged 34 commits into from Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7bd6bf7
Remove FlagEncoder from FastestWeighting and subclasses
easbar Jun 22, 2022
cceb167
Start removing flag encoders in tests
easbar Jun 22, 2022
e108544
Remove more flag encoders from tests
easbar Jun 22, 2022
64ac653
Remove even more flag encoders from tests
easbar Jun 22, 2022
8e4a642
Remove more FlagEncoders, 162 to go...
easbar Jun 23, 2022
f5cd02f
Merge branch 'master' into remove_flag_encoder
easbar Jun 23, 2022
67101df
a few more, 159
easbar Jun 23, 2022
f4b98f3
Another
easbar Jun 23, 2022
1e0de74
107
easbar Jun 25, 2022
81b758a
90
easbar Jun 25, 2022
fa739ae
68
easbar Jun 25, 2022
750ecd3
51, most tests green
easbar Jun 25, 2022
69fdf0b
48, Remove EncodingManager#hasEncoder and EncodingManager#getEncoder
easbar Jun 25, 2022
3bc3cc9
28, FlagEncoders no longer 'stored' in EncodingManager
easbar Jun 25, 2022
d11fdbd
0, we still use VehicleEncodedValues to collectively add EVs to the E…
easbar Jun 25, 2022
67e5c0c
Move private/destination factors to weighting factory
easbar Jun 25, 2022
34c5632
Merge branch 'master' into remove_flag_encoder
easbar Jun 26, 2022
51f8f62
Fix a test
easbar Jun 26, 2022
0be010b
Merge branch 'master' into remove_flag_encoder
easbar Jun 29, 2022
b0d4df5
Merge branch 'master' into remove_flag_encoder
easbar Jun 30, 2022
6524be0
fix
easbar Jun 30, 2022
2807bac
Fix a few more tests
easbar Jun 30, 2022
97ca231
default weighting factory determines which vehicles get private/desti…
easbar Jun 30, 2022
0dfec45
remove max speed from custom weighting create weighting
easbar Jun 30, 2022
036645a
vehicle access/speed/priority
easbar Jun 30, 2022
e6238da
tmp
easbar Jun 30, 2022
c78a1b0
use keys
easbar Jun 30, 2022
56a598d
Merge branch 'master' into remove_flag_encoder
easbar Jun 30, 2022
b42f992
Remove car/bike_access evs from DefaultEncodedValueFactory
easbar Jul 1, 2022
4d5c538
Merge branch 'master' into remove_flag_encoder
easbar Jul 3, 2022
4bad6b2
fix
easbar Jul 3, 2022
a0e4e95
minor
easbar Jul 4, 2022
f68bd4b
use access enc from super class (was the same)
easbar Jul 4, 2022
421b56f
update changelog
easbar Jul 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions benchmark/benchmark.sh
Expand Up @@ -53,7 +53,8 @@ measurement.weighting=fastest \
measurement.ch.node=true \
measurement.ch.edge=true \
measurement.lm=false \
"graph.flag_encoders=car|turn_costs=true" \
measurement.vehicle=car \
measurement.turn_costs=true \
graph.location=${TMP_DIR}measurement-small-gh \
prepare.min_network_size=10000 \
measurement.json=true \
Expand All @@ -80,7 +81,8 @@ measurement.ch.edge=false \
measurement.lm=true \
"measurement.lm.active_counts=[4,8,12]" \
measurement.lm.edge_based=true \
"graph.flag_encoders=car|turn_costs=true" \
measurement.vehicle=car \
measurement.turn_costs=true \
graph.location=${TMP_DIR}measurement-big-gh \
prepare.min_network_size=10000 \
measurement.json=true \
Expand Down Expand Up @@ -110,7 +112,8 @@ measurement.ch.edge=false \
measurement.lm=true \
"measurement.lm.active_counts=[8]" \
measurement.lm.edge_based=false \
"graph.flag_encoders=car|turn_costs=true" \
measurement.vehicle=car \
measurement.turn_costs=true \
graph.location=${TMP_DIR}measurement-big-little-custom-gh \
prepare.min_network_size=10000 \
measurement.json=true \
Expand Down Expand Up @@ -140,7 +143,8 @@ measurement.ch.edge=false \
measurement.lm=true \
"measurement.lm.active_counts=[8]" \
measurement.lm.edge_based=false \
"graph.flag_encoders=car|turn_costs=true" \
measurement.vehicle=car \
measurement.turn_costs=true \
graph.location=${TMP_DIR}measurement-big-very-custom-gh \
prepare.min_network_size=10000 \
measurement.json=true \
Expand All @@ -167,7 +171,8 @@ measurement.ch.edge=false \
measurement.lm=true \
"measurement.lm.active_counts=[4,8,12]" \
measurement.lm.edge_based=false \
"graph.flag_encoders=foot" \
measurement.vehicle=foot \
measurement.turn_costs=false \
graph.location=${TMP_DIR}measurement-big-outdoor-gh \
prepare.min_network_size=10000 \
measurement.json=true \
Expand Down
40 changes: 14 additions & 26 deletions core/src/main/java/com/graphhopper/GraphHopper.java
Expand Up @@ -66,7 +66,6 @@
import java.text.DateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.graphhopper.util.GHUtility.readCountries;
import static com.graphhopper.util.Helper.*;
Expand Down Expand Up @@ -121,7 +120,7 @@ public class GraphHopper {
// for data reader
private String osmFile;
private ElevationProvider eleProvider = ElevationProvider.NOOP;
private FlagEncoderFactory flagEncoderFactory = new DefaultFlagEncoderFactory();
private VehicleEncodedValuesFactory vehicleEncodedValuesFactory = new DefaultVehicleEncodedValuesFactory();
private VehicleTagParserFactory vehicleTagParserFactory = new DefaultVehicleTagParserFactory();
private EncodedValueFactory encodedValueFactory = new DefaultEncodedValueFactory();
private TagParserFactory tagParserFactory = new DefaultTagParserFactory();
Expand Down Expand Up @@ -390,8 +389,8 @@ public TranslationMap getTranslationMap() {
return trMap;
}

public GraphHopper setFlagEncoderFactory(FlagEncoderFactory factory) {
this.flagEncoderFactory = factory;
public GraphHopper setVehicleEncodedValuesFactory(VehicleEncodedValuesFactory factory) {
this.vehicleEncodedValuesFactory = factory;
return this;
}

Expand Down Expand Up @@ -580,7 +579,7 @@ private void buildEncodingManagerAndOSMParsers(String flagEncodersStr, String en
.collect(Collectors.toList());

EncodingManager.Builder emBuilder = new EncodingManager.Builder();
flagEncodersMap.forEach((name, encoderStr) -> emBuilder.add(flagEncoderFactory.createFlagEncoder(name, new PMap(encoderStr))));
flagEncodersMap.forEach((name, encoderStr) -> emBuilder.add(vehicleEncodedValuesFactory.createVehicleEncodedValues(name, new PMap(encoderStr))));
profiles.forEach(profile -> emBuilder.add(Subnetwork.create(profile.getName())));
encodedValueStrings.forEach(s -> emBuilder.add(encodedValueFactory.create(s)));
encodingManager = emBuilder.build();
Expand Down Expand Up @@ -822,7 +821,6 @@ protected void createBaseGraphAndProperties() {
properties.put("graph.em.edge_config", encodingManager.toEdgeConfigAsString());
properties.put("graph.em.turn_cost_config", encodingManager.toTurnCostConfigAsString());
properties.put("graph.encoded_values", encodingManager.toEncodedValuesAsString());
properties.put("graph.flag_encoders", encodingManager.toFlagEncodersAsString());
}

private List<CustomArea> readCustomAreas() {
Expand Down Expand Up @@ -947,23 +945,9 @@ private void loadEncodingManagerFromProperties(StorableProperties properties) {
throw new IllegalStateException("Duplicate encoded value name: " + encodedValue.getName() + " in: graph.encoded_values=" + encodedValueStr);
});

String flagEncodersStr = properties.get("graph.flag_encoders");
LinkedHashMap<String, VehicleEncodedValues> flagEncoders = Stream.of(flagEncodersStr.split(","))
.map(str -> flagEncoderFactory.deserializeFlagEncoder(str, name -> {
EncodedValue ev = encodedValues.get(name);
if (ev == null)
throw new IllegalStateException("FlagEncoder " + str + " uses unknown encoded value: " + name);
return ev;
}))
.collect(Collectors.toMap(FlagEncoder::getName, f -> (VehicleEncodedValues) f,
(f1, f2) -> {
throw new IllegalStateException("Duplicate flag encoder: " + f1.getName() + " in: " + flagEncodersStr);
},
LinkedHashMap::new));

EncodedValue.InitializerConfig edgeConfig = EncodedValueSerializer.deserializeInitializerConfig(properties.get("graph.em.edge_config"));
EncodedValue.InitializerConfig turnCostConfig = EncodedValueSerializer.deserializeInitializerConfig(properties.get("graph.em.turn_cost_config"));
encodingManager = new EncodingManager(encodedValues, flagEncoders, edgeConfig, turnCostConfig);
encodingManager = new EncodingManager(encodedValues, edgeConfig, turnCostConfig);
}

private ArrayNode deserializeEncodedValueList(String encodedValueStr) {
Expand All @@ -983,11 +967,15 @@ private void checkProfilesConsistency() {
throw new IllegalArgumentException("There has to be at least one profile");
EncodingManager encodingManager = getEncodingManager();
for (Profile profile : profilesByName.values()) {
if (!encodingManager.hasEncoder(profile.getVehicle())) {
throw new IllegalArgumentException("Unknown vehicle '" + profile.getVehicle() + "' in profile: " + profile + ". Make sure all vehicles used in 'profiles' exist in 'graph.flag_encoders'");
}
FlagEncoder encoder = encodingManager.getEncoder(profile.getVehicle());
if (profile.isTurnCosts() && !encoder.supportsTurnCosts()) {
String accessEncName = EncodingManager.getKey(profile.getVehicle(), "access");
String speedEncName = EncodingManager.getKey(profile.getVehicle(), "average_speed");
if (!encodingManager.hasEncodedValue(accessEncName) || !encodingManager.hasEncodedValue(speedEncName))
throw new IllegalArgumentException("Unknown vehicle '" + profile.getVehicle() + "' in profile: " + profile + ". " +
"Encoded values " + accessEncName + " and " + speedEncName + " are required");
DecimalEncodedValue turnCostEnc = encodingManager.hasEncodedValue(TurnCost.key(profile.getVehicle()))
? encodingManager.getDecimalEncodedValue(TurnCost.key(profile.getVehicle()))
: null;
if (profile.isTurnCosts() && turnCostEnc == null) {
throw new IllegalArgumentException("The profile '" + profile.getName() + "' was configured with " +
"'turn_costs=true', but the corresponding vehicle '" + profile.getVehicle() + "' does not support turn costs." +
"\nYou need to add `|turn_costs=true` to the vehicle in `graph.flag_encoders`");
Expand Down
Expand Up @@ -19,8 +19,8 @@
package com.graphhopper.routing;

import com.graphhopper.config.Profile;
import com.graphhopper.routing.ev.*;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.routing.util.FlagEncoder;
import com.graphhopper.routing.weighting.*;
import com.graphhopper.routing.weighting.custom.CustomModelParser;
import com.graphhopper.routing.weighting.custom.CustomProfile;
Expand Down Expand Up @@ -54,13 +54,14 @@ public Weighting createWeighting(Profile profile, PMap requestHints, boolean dis
hints.putAll(profile.getHints());
hints.putAll(requestHints);

FlagEncoder encoder = encodingManager.getEncoder(profile.getVehicle());
final String vehicle = profile.getVehicle();
TurnCostProvider turnCostProvider;
if (profile.isTurnCosts() && !disableTurnCosts) {
if (!encoder.supportsTurnCosts())
throw new IllegalArgumentException("Encoder " + encoder + " does not support turn costs");
DecimalEncodedValue turnCostEnc = encodingManager.getDecimalEncodedValue(TurnCost.key(vehicle));
if (turnCostEnc == null)
throw new IllegalArgumentException("Vehicle " + vehicle + " does not support turn costs");
int uTurnCosts = hints.getInt(Parameters.Routing.U_TURN_COSTS, INFINITE_U_TURN_COSTS);
turnCostProvider = new DefaultTurnCostProvider(encoder.getTurnCostEnc(), graph.getTurnCostStorage(), uTurnCosts);
turnCostProvider = new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage(), uTurnCosts);
} else {
turnCostProvider = NO_TURN_COST_PROVIDER;
}
Expand All @@ -70,26 +71,46 @@ public Weighting createWeighting(Profile profile, PMap requestHints, boolean dis
throw new IllegalArgumentException("You have to specify a weighting");

Weighting weighting = null;
BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(EncodingManager.getKey(vehicle, "access"));
DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey(vehicle, "average_speed"));
DecimalEncodedValue priorityEnc = encodingManager.hasEncodedValue(EncodingManager.getKey(vehicle, "priority"))
? encodingManager.getDecimalEncodedValue(EncodingManager.getKey(vehicle, "priority"))
: null;
if (CustomWeighting.NAME.equalsIgnoreCase(weightingStr)) {
if (!(profile instanceof CustomProfile))
throw new IllegalArgumentException("custom weighting requires a CustomProfile but was profile=" + profile.getName());
CustomModel queryCustomModel = requestHints.getObject(CustomModel.KEY, null);
CustomProfile customProfile = (CustomProfile) profile;
queryCustomModel = CustomModel.merge(customProfile.getCustomModel(), queryCustomModel);
weighting = CustomModelParser.createWeighting(encoder, encodingManager, turnCostProvider, queryCustomModel);
// todonow: this used to be encoder.getMaxSpeed()!
double maxSpeed = speedEnc.getMaxDecimal();
easbar marked this conversation as resolved.
Show resolved Hide resolved
weighting = CustomModelParser.createWeighting(accessEnc, speedEnc,
priorityEnc, maxSpeed, encodingManager, turnCostProvider, queryCustomModel);
} else if ("shortest".equalsIgnoreCase(weightingStr)) {
weighting = new ShortestWeighting(encoder.getAccessEnc(), encoder.getAverageSpeedEnc(), turnCostProvider);
weighting = new ShortestWeighting(accessEnc, speedEnc, turnCostProvider);
} else if ("fastest".equalsIgnoreCase(weightingStr)) {
if (encoder.getPriorityEnc() != null)
weighting = new PriorityWeighting(encoder, hints, turnCostProvider);
if (!encodingManager.hasEncodedValue(RoadAccess.KEY))
throw new IllegalArgumentException("fastest weighting requires road_access");
EnumEncodedValue<RoadAccess> roadAccessEnc = encodingManager.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class);
if (priorityEnc != null)
weighting = new PriorityWeighting(accessEnc, speedEnc, priorityEnc, roadAccessEnc, hints, turnCostProvider);
else
weighting = new FastestWeighting(encoder, hints, turnCostProvider);
weighting = new FastestWeighting(accessEnc, speedEnc, roadAccessEnc, hints, turnCostProvider);
} else if ("curvature".equalsIgnoreCase(weightingStr)) {
if (encoder.getCurvatureEnc() != null)
weighting = new CurvatureWeighting(encoder, hints, turnCostProvider);

DecimalEncodedValue curvatureEnc = encodingManager.hasEncodedValue(EncodingManager.getKey(vehicle, "curvature"))
? encodingManager.getDecimalEncodedValue(EncodingManager.getKey(vehicle, "curvature"))
: null;
if (curvatureEnc == null || priorityEnc == null)
throw new IllegalArgumentException("curvature weighting requires curvature and priority, but not found for " + vehicle);
if (!encodingManager.hasEncodedValue(RoadAccess.KEY))
throw new IllegalArgumentException("curvature weighting requires road_access");
EnumEncodedValue<RoadAccess> roadAccessEnc = encodingManager.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class);
weighting = new CurvatureWeighting(accessEnc, speedEnc, priorityEnc, curvatureEnc, roadAccessEnc, hints, turnCostProvider);
} else if ("short_fastest".equalsIgnoreCase(weightingStr)) {
weighting = new ShortFastestWeighting(encoder, hints, turnCostProvider);
if (!encodingManager.hasEncodedValue(RoadAccess.KEY))
throw new IllegalArgumentException("curvature weighting requires road_access");
EnumEncodedValue<RoadAccess> roadAccessEnc = encodingManager.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class);
weighting = new ShortFastestWeighting(accessEnc, speedEnc, roadAccessEnc, hints, turnCostProvider);
}

if (weighting == null)
Expand Down
13 changes: 8 additions & 5 deletions core/src/main/java/com/graphhopper/routing/ProfileResolver.java
Expand Up @@ -79,11 +79,14 @@ public Profile resolveProfile(PMap hints) {
boolean disableLM = hints.getBool(Parameters.Landmark.DISABLE, false);

String vehicle = hints.getString("vehicle", "").toLowerCase();
if (!vehicle.isEmpty() && !encodingManager.hasEncoder(vehicle))
throw new IllegalArgumentException("Vehicle not supported: `" + vehicle + "`. Supported are: `" + encodingManager.toString() +
"`\nYou should consider using the `profile` parameter instead of specifying a vehicle." +
"\nAvailable profiles: " + getProfileNames() +
"\nTo learn more about profiles, see: docs/core/profiles.md");
if (!vehicle.isEmpty()) {
List<String> availableVehicles = encodingManager.getVehicles();
if (!availableVehicles.contains(vehicle))
throw new IllegalArgumentException("Vehicle not supported: `" + vehicle + "`. Supported are: `" + availableVehicles +
"`\nYou should consider using the `profile` parameter instead of specifying a vehicle." +
"\nAvailable profiles: " + getProfileNames() +
"\nTo learn more about profiles, see: docs/core/profiles.md");
}

// we select the profile based on the given request hints and the available profiles
if (!chProfiles.isEmpty() && !disableCH) {
Expand Down

This file was deleted.