Search in sources :

Example 6 with CustomModel

use of com.graphhopper.util.CustomModel in project graphhopper by graphhopper.

the class CustomModelParserTest method testSpeedFactorAndPriorityAndMaxSpeed.

@Test
public void testSpeedFactorAndPriorityAndMaxSpeed() {
    EdgeIteratorState primary = graph.edge(0, 1).setDistance(10).set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 80).set(encoder.getAccessEnc(), true, true);
    EdgeIteratorState secondary = graph.edge(1, 2).setDistance(10).set(roadClassEnc, SECONDARY).set(avgSpeedEnc, 70).set(encoder.getAccessEnc(), true, true);
    CustomModel customModel = new CustomModel();
    customModel.addToPriority(If("road_class == PRIMARY", MULTIPLY, 0.9));
    customModel.addToSpeed(If("road_class == PRIMARY", MULTIPLY, 0.8));
    CustomWeighting.Parameters parameters = CustomModelParser.createWeightingParameters(customModel, encodingManager, avgSpeedEnc, encoder.getMaxSpeed(), null);
    assertEquals(0.9, parameters.getEdgeToPriorityMapping().get(primary, false), 0.01);
    assertEquals(64, parameters.getEdgeToSpeedMapping().get(primary, false), 0.01);
    assertEquals(1, parameters.getEdgeToPriorityMapping().get(secondary, false), 0.01);
    assertEquals(70, parameters.getEdgeToSpeedMapping().get(secondary, false), 0.01);
    customModel.addToSpeed(If("road_class != PRIMARY", LIMIT, 50));
    CustomWeighting.EdgeToDoubleMapping speedMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager, avgSpeedEnc, encoder.getMaxSpeed(), null).getEdgeToSpeedMapping();
    assertEquals(64, speedMapping.get(primary, false), 0.01);
    assertEquals(50, speedMapping.get(secondary, false), 0.01);
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) CustomModel(com.graphhopper.util.CustomModel) Test(org.junit.jupiter.api.Test)

Example 7 with CustomModel

use of com.graphhopper.util.CustomModel in project graphhopper by graphhopper.

the class CustomModelParserTest method testIllegalOrder.

@Test
void testIllegalOrder() {
    CustomModel customModel = new CustomModel();
    customModel.addToPriority(Else(MULTIPLY, 0.9));
    customModel.addToPriority(If("road_environment != FERRY", MULTIPLY, 0.8));
    assertThrows(IllegalArgumentException.class, () -> CustomModelParser.createWeightingParameters(customModel, encodingManager, avgSpeedEnc, encoder.getMaxSpeed(), null));
    CustomModel customModel2 = new CustomModel();
    customModel2.addToPriority(ElseIf("road_environment != FERRY", MULTIPLY, 0.9));
    customModel2.addToPriority(If("road_class != PRIMARY", MULTIPLY, 0.8));
    assertThrows(IllegalArgumentException.class, () -> CustomModelParser.createWeightingParameters(customModel2, encodingManager, avgSpeedEnc, encoder.getMaxSpeed(), null));
}
Also used : CustomModel(com.graphhopper.util.CustomModel) Test(org.junit.jupiter.api.Test)

Example 8 with CustomModel

use of com.graphhopper.util.CustomModel in project graphhopper by graphhopper.

the class CustomModelParserTest method testPriority.

@Test
void testPriority() {
    EdgeIteratorState primary = graph.edge(0, 1).setDistance(10).set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 80).set(encoder.getAccessEnc(), true, true);
    EdgeIteratorState secondary = graph.edge(1, 2).setDistance(10).set(roadClassEnc, SECONDARY).set(avgSpeedEnc, 70).set(encoder.getAccessEnc(), true, true);
    EdgeIteratorState tertiary = graph.edge(1, 2).setDistance(10).set(roadClassEnc, TERTIARY).set(avgSpeedEnc, 70).set(encoder.getAccessEnc(), true, true);
    CustomModel customModel = new CustomModel();
    customModel.addToPriority(If("road_class == PRIMARY", MULTIPLY, 0.5));
    customModel.addToPriority(ElseIf("road_class == SECONDARY", MULTIPLY, 0.7));
    customModel.addToPriority(Else(MULTIPLY, 0.9));
    customModel.addToPriority(If("road_environment != FERRY", MULTIPLY, 0.8));
    CustomWeighting.EdgeToDoubleMapping priorityMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager, avgSpeedEnc, encoder.getMaxSpeed(), null).getEdgeToPriorityMapping();
    assertEquals(0.5 * 0.8, priorityMapping.get(primary, false), 0.01);
    assertEquals(0.7 * 0.8, priorityMapping.get(secondary, false), 0.01);
    assertEquals(0.9 * 0.8, priorityMapping.get(tertiary, false), 0.01);
    // force integer value
    customModel = new CustomModel();
    customModel.addToPriority(If("road_class == PRIMARY", MULTIPLY, 1));
    customModel.addToPriority(If("road_class == SECONDARY", MULTIPLY, 0.9));
    priorityMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager, avgSpeedEnc, encoder.getMaxSpeed(), null).getEdgeToPriorityMapping();
    assertEquals(1, priorityMapping.get(primary, false), 0.01);
    assertEquals(0.9, priorityMapping.get(secondary, false), 0.01);
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) CustomModel(com.graphhopper.util.CustomModel) Test(org.junit.jupiter.api.Test)

Example 9 with CustomModel

use of com.graphhopper.util.CustomModel in project graphhopper by graphhopper.

the class CustomModelParserTest method testString.

@Test
public void testString() {
    EdgeIteratorState deu = graph.edge(0, 1).setDistance(10).set(countryEnc, "DEU").set(avgSpeedEnc, 80).set(encoder.getAccessEnc(), true, true);
    EdgeIteratorState blup = graph.edge(1, 2).setDistance(10).set(countryEnc, "blup").set(avgSpeedEnc, 70).set(encoder.getAccessEnc(), true, true);
    CustomModel customModel = new CustomModel();
    customModel.addToPriority(If("country == \"DEU\"", MULTIPLY, 0.9));
    customModel.addToPriority(ElseIf("country == \"blup\"", MULTIPLY, 0.7));
    customModel.addToPriority(Else(MULTIPLY, 0.5));
    CustomWeighting.EdgeToDoubleMapping priorityMapping = CustomModelParser.createWeightingParameters(customModel, encodingManager, avgSpeedEnc, encoder.getMaxSpeed(), null).getEdgeToPriorityMapping();
    assertEquals(0.9, priorityMapping.get(deu, false), 0.01);
    assertEquals(0.7, priorityMapping.get(blup, false), 0.01);
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) CustomModel(com.graphhopper.util.CustomModel) Test(org.junit.jupiter.api.Test)

Example 10 with CustomModel

use of com.graphhopper.util.CustomModel in project graphhopper by graphhopper.

the class CustomModelParserTest method testBrackets.

@Test
public void testBrackets() {
    EdgeIteratorState primary = graph.edge(0, 1).setDistance(10).set(encoder.getAccessEnc(), true, true).set(roadClassEnc, PRIMARY).set(avgSpeedEnc, 80);
    EdgeIteratorState secondary = graph.edge(0, 1).setDistance(10).set(encoder.getAccessEnc(), true, true).set(roadClassEnc, SECONDARY).set(avgSpeedEnc, 40);
    CustomModel customModel = new CustomModel();
    customModel.addToPriority(If("(road_class == PRIMARY || car$access == true) && car$average_speed > 50", MULTIPLY, 0.9));
    CustomWeighting.Parameters parameters = CustomModelParser.createWeightingParameters(customModel, encodingManager, avgSpeedEnc, encoder.getMaxSpeed(), null);
    assertEquals(0.9, parameters.getEdgeToPriorityMapping().get(primary, false), 0.01);
    assertEquals(1, parameters.getEdgeToPriorityMapping().get(secondary, false), 0.01);
}
Also used : EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) CustomModel(com.graphhopper.util.CustomModel) Test(org.junit.jupiter.api.Test)

Aggregations

CustomModel (com.graphhopper.util.CustomModel)12 Test (org.junit.jupiter.api.Test)8 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)5 CustomProfile (com.graphhopper.routing.weighting.custom.CustomProfile)4 Profile (com.graphhopper.config.Profile)3 GraphHopperServerConfiguration (com.graphhopper.application.GraphHopperServerConfiguration)2 GraphHopperServerTestConfiguration (com.graphhopper.application.util.GraphHopperServerTestConfiguration)2 CustomWeighting (com.graphhopper.routing.weighting.custom.CustomWeighting)2 GraphBuilder (com.graphhopper.storage.GraphBuilder)2 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)2 PMap (com.graphhopper.util.PMap)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 CHProfile (com.graphhopper.config.CHProfile)1 LMProfile (com.graphhopper.config.LMProfile)1 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)1 PriorityWeighting (com.graphhopper.routing.weighting.PriorityWeighting)1 NodeAccess (com.graphhopper.storage.NodeAccess)1 JsonFeature (com.graphhopper.util.JsonFeature)1 File (java.io.File)1