Skip to content

Commit

Permalink
Added unit and API tests, weight/share factor parameters, improved er…
Browse files Browse the repository at this point in the history
…ror handling
  • Loading branch information
takb committed Jan 28, 2019
1 parent 6c967b4 commit c1ecee3
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
### Added
- Added support for ISO 3166-1 Alpha-2 / Alpha-3 codes for routing directions option avoid_countries (Issue #195)
- Added support for GH alternative_route algorithm (Issue #377)
### Fixed
### Changed
- Updated rural speed limit in France to be 80km/h (Issue #355)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public ResultTest() {
addParameter("preference", "fastest");
addParameter("bikeProfile", "cycling-regular");
addParameter("carProfile", "driving-car");

// query for testing the alternative routes algorithm
addParameter("coordinatesAR", "8.680401,49.437436|8.746362,49.414191");
}

@Test
Expand Down Expand Up @@ -1475,4 +1478,27 @@ public void testAccessRestrictionsWarnings() {
.body("routes[0].extras.roadaccessrestrictions.values[1][2]", is(32))
.statusCode(200);
}

@Test
public void testAlternativeRoutes() {

given()
.param("coordinates", getParameter("coordinatesAR"))
.param("instructions", "true")
.param("preference", getParameter("preference"))
.param("profile", getParameter("carProfile"))
.param("options", "{\"alternative_routes\": 2}")
.when().log().ifValidationFails()
.get(getEndPointName())
.then()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("routes.size()", is(2))
.body("routes[0].summary.distance", is(8178.2f))
.body("routes[0].summary.duration", is(1087.3f))
.body("routes[1].summary.distance", is(10670.8f))
.body("routes[1].summary.duration", is(1414))
.statusCode(200);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ public RouteResultBuilder()
public RouteResult[] createRouteResults(List<GHResponse> routes, RoutingRequest request, List<RouteExtraInfo> extras) throws Exception {
if (routes.isEmpty())
return new RouteResult[]{new RouteResult(request.getExtraInfo())};

if (routes.size() > 1) {
if (request.getSearchParameters().getAlternativeRoutes() > 1) {
throw new InternalServerException(RoutingErrorCodes.INVALID_PARAMETER_VALUE, "Alternative routes algorithm does not support more than two way points");
}
return new RouteResult[]{createRouteResult(routes, request, extras)};
}
return createRouteResultSet(routes.get(0), request, extras);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ public class RouteSearchParameters {
private int[] _avoidCountries = null;
private BordersExtractor.Avoid _avoidBorders = BordersExtractor.Avoid.NONE;

// TAKB: parameters weight factor and share factor seem to be ignored by the algorithm, further testing required.
private int _alternativeRoutes = -1;
private double _alternativeRoutesWeightFactor = 1.4;
private double _alternativeRoutesShareFactor = 0.6;

private String _options;

Expand Down Expand Up @@ -176,6 +179,22 @@ public void setAlternativeRoutes(int _alternativeRoutes) {
this._alternativeRoutes = _alternativeRoutes;
}

public double getAlternativeRoutesWeightFactor() {
return _alternativeRoutesWeightFactor;
}

public void set_alternativeRoutesWeightFactor(double _alternativeRoutesWeightFactor) {
this._alternativeRoutesWeightFactor = _alternativeRoutesWeightFactor;
}

public double getAlternativeRoutesShareFactor() {
return _alternativeRoutesShareFactor;
}

public void set_alternativeRoutesShareFactor(double _alternativeRoutesShareFactor) {
this._alternativeRoutesShareFactor = _alternativeRoutesShareFactor;
}

public String getOptions() {
return _options;
}
Expand Down Expand Up @@ -414,6 +433,20 @@ else if (jProfileParams.has("maximum_gradient"))
} catch (Exception ex) {
throw new ParameterValueException(RoutingErrorCodes.INVALID_PARAMETER_FORMAT, "alternative_routes", json.getString("alternative_routes"));
}
if (json.has("alternative_routes_weight_factor")) {
try {
_alternativeRoutesWeightFactor = json.getDouble("alternative_routes_weight_factor");
} catch (Exception ex) {
throw new ParameterValueException(RoutingErrorCodes.INVALID_PARAMETER_FORMAT, "alternative_routes_weight_factor", json.getString("alternative_routes_weight_factor"));
}
}
if (json.has("alternative_routes_share_factor")) {
try {
_alternativeRoutesShareFactor = json.getDouble("alternative_routes_share_factor");
} catch (Exception ex) {
throw new ParameterValueException(RoutingErrorCodes.INVALID_PARAMETER_FORMAT, "alternative_routes_share_factor", json.getString("alternative_routes_share_factor"));
}
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -919,9 +919,9 @@ else if (bearings[1] == null)
if (searchParams.getAlternativeRoutes() > 0) {
req.setAlgorithm("alternative_route");
req.getHints().put("alternative_route.max_paths", searchParams.getAlternativeRoutes());
// TAKB: TODO more testing required to find out if we can tweak here to remove irregular alternative paths
// req.getHints().put("alternative_route.max_weight_factor", 1.4);
// req.getHints().put("alternative_route.max_share_factor", 0.6);
req.getHints().put("alternative_route.max_weight_factor", searchParams.getAlternativeRoutesWeightFactor());
req.getHints().put("alternative_route.max_share_factor", searchParams.getAlternativeRoutesShareFactor());
// TAKB: contraction hierarchies have to be disabled for alternative routes until GH pulls https://github.com/graphhopper/graphhopper/pull/1524 and we update our fork.
req.getHints().put("ch.disable", true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ public RouteResult[] computeRoute(RoutingRequest req) throws Exception {
WayPointBearing[] bearings = (req.getContinueStraight() || searchParams.getBearings() != null) ? new WayPointBearing[2] : null;
double[] radiuses = searchParams.getMaximumRadiuses() != null ? new double[2] : null;

if (req.getSearchParameters().getAlternativeRoutes() > 1 && coords.length > 2) {
throw new InternalServerException(RoutingErrorCodes.INVALID_PARAMETER_VALUE, "Alternative routes algorithm does not support more than two way points.");
}

for (int i = 1; i <= nSegments; ++i) {
c1 = coords[i];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,13 @@ public void setBearings() {
routeSearchParameters.setBearings(new WayPointBearing[]{});
Assert.assertArrayEquals(new WayPointBearing[]{}, routeSearchParameters.getBearings());
}
}

@Test
public void alternativeRoutesParams() throws Exception {
RouteSearchParameters routeSearchParameters = new RouteSearchParameters();
routeSearchParameters.setOptions("{\"alternative_routes\": 2, \"alternative_routes_weight_factor\": 3.3, \"alternative_routes_share_factor\": 4.4}}");
Assert.assertEquals(2, routeSearchParameters.getAlternativeRoutes());
Assert.assertEquals(3.3, routeSearchParameters.getAlternativeRoutesWeightFactor(), 0.0);
Assert.assertEquals(4.4, routeSearchParameters.getAlternativeRoutesShareFactor(), 0.0);
}
}

0 comments on commit c1ecee3

Please sign in to comment.