diff --git a/CHANGELOG.md b/CHANGELOG.md index d62cb2f7d4..4bef377439 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,9 +11,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) ### Fixed - Fixed fallback to dynamic routing methods if bearings parameter set ([#702](https://github.com/GIScience/openrouteservice/issues/702)) - Enable elevation interpolation for bridges and tunnels ([#685](https://github.com/GIScience/openrouteservice/issues/685)) +- Fixed erroneous duration computation of soft weightings such as green and quiet weightings - Enable recommended weighting for hgv profile and robustify the matching of routing algorithm to the request ([#755](https://github.com/GIScience/openrouteservice/issues/755)) ### Changed - Improve recommended weighting for cycling and walking profiles ([#665](https://github.com/GIScience/openrouteservice/issues/665)) +- Restructure AdditionWeighting ### Deprecated ## [6.1.1] - 2020-06-02 diff --git a/openrouteservice-api-tests/src/test/java/org/heigit/ors/v2/services/routing/ResultTest.java b/openrouteservice-api-tests/src/test/java/org/heigit/ors/v2/services/routing/ResultTest.java index 51ece27e47..623fac7912 100644 --- a/openrouteservice-api-tests/src/test/java/org/heigit/ors/v2/services/routing/ResultTest.java +++ b/openrouteservice-api-tests/src/test/java/org/heigit/ors/v2/services/routing/ResultTest.java @@ -2502,7 +2502,7 @@ public void testPreferGreen() { .body("any { it.key == 'routes' }", is(true)) .body("routes[0].containsKey('summary')", is(true)) .body("routes[0].summary.distance", is(2308.3f)) - .body("routes[0].summary.duration", is(3323.9f)) + .body("routes[0].summary.duration", is(1662.0f)) .statusCode(200); } @@ -2546,7 +2546,7 @@ public void testPreferQuiet() { .body("any { it.key == 'routes' }", is(true)) .body("routes[0].containsKey('summary')", is(true)) .body("routes[0].summary.distance", is(2878.7f)) - .body("routes[0].summary.duration", is(4145.2f)) + .body("routes[0].summary.duration", is(2072.6f)) .statusCode(200); } diff --git a/openrouteservice/src/main/java/org/heigit/ors/routing/graphhopper/extensions/weighting/AdditionWeighting.java b/openrouteservice/src/main/java/org/heigit/ors/routing/graphhopper/extensions/weighting/AdditionWeighting.java index 2e721f1d35..64a0f22555 100644 --- a/openrouteservice/src/main/java/org/heigit/ors/routing/graphhopper/extensions/weighting/AdditionWeighting.java +++ b/openrouteservice/src/main/java/org/heigit/ors/routing/graphhopper/extensions/weighting/AdditionWeighting.java @@ -20,146 +20,21 @@ public class AdditionWeighting extends AbstractWeighting { private Weighting superWeighting; - private WeightCalc weightCalc; + private Weighting[] weightings; public AdditionWeighting(Weighting[] weightings, Weighting superWeighting, FlagEncoder encoder) { super(encoder); this.superWeighting = superWeighting; - - int count = weightings.length; - if (count == 1) - weightCalc = new OneWeightCalc(weightings); - else if (count == 2) - weightCalc = new TwoWeightCalc(weightings); - else if (count == 3) - weightCalc = new ThreeWeightCalc(weightings); - else if (count == 4) - weightCalc = new FourWeightCalc(weightings); - else if (count == 5) - weightCalc = new FiveWeightCalc(weightings); + this.weightings = weightings.clone(); } - public interface WeightCalc { - double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId); - long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId); - } - - public static class OneWeightCalc implements WeightCalc - { - private Weighting weighting; - - public OneWeightCalc(Weighting[] weightings) - { - weighting = weightings[0]; - } - - public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) - { - return weighting.calcWeight(edgeState, reverse, prevOrNextEdgeId); - } - - public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) - { - return weighting.calcMillis(edgeState, reverse, prevOrNextEdgeId); - } - } - - public class TwoWeightCalc extends OneWeightCalc - { - private Weighting weighting; - - public TwoWeightCalc(Weighting[] weightings) - { - super(weightings); - weighting = weightings[1]; - } - - @Override - public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) - { - return super.calcWeight(edgeState, reverse, prevOrNextEdgeId) + weighting.calcWeight(edgeState, reverse, prevOrNextEdgeId); - } - - @Override - public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) - { - return super.calcMillis(edgeState, reverse, prevOrNextEdgeId) + weighting.calcMillis(edgeState, reverse, prevOrNextEdgeId); - } - } - - public class ThreeWeightCalc extends TwoWeightCalc - { - private Weighting weighting; - - public ThreeWeightCalc(Weighting[] weightings) - { - super(weightings); - weighting = weightings[2]; - } - - @Override - public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) - { - return super.calcWeight(edgeState, reverse, prevOrNextEdgeId) + weighting.calcWeight(edgeState, reverse, prevOrNextEdgeId); - } - - @Override - public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) - { - return super.calcMillis(edgeState, reverse, prevOrNextEdgeId) + weighting.calcMillis(edgeState, reverse, prevOrNextEdgeId); - } - } - - public class FourWeightCalc extends ThreeWeightCalc - { - private Weighting weighting; - - public FourWeightCalc(Weighting[] weightings) - { - super(weightings); - weighting = weightings[3]; - } - - @Override - public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) - { - return super.calcWeight(edgeState, reverse, prevOrNextEdgeId) + weighting.calcWeight(edgeState, reverse, prevOrNextEdgeId); - } - - @Override - public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) - { - return super.calcMillis(edgeState, reverse, prevOrNextEdgeId) + weighting.calcMillis(edgeState, reverse, prevOrNextEdgeId); - } - } - - public class FiveWeightCalc extends FourWeightCalc - { - private Weighting weighting; - - public FiveWeightCalc(Weighting[] weightings) - { - super(weightings); - weighting = weightings[4]; - } - - @Override - public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) - { - return super.calcWeight(edgeState, reverse, prevOrNextEdgeId) + weighting.calcWeight(edgeState, reverse, prevOrNextEdgeId); - } - - @Override - public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) - { - return super.calcMillis(edgeState, reverse, prevOrNextEdgeId) + weighting.calcMillis(edgeState, reverse, prevOrNextEdgeId); - } - } - @Override public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) { - - return superWeighting.calcWeight(edgeState, reverse, prevOrNextEdgeId) * weightCalc.calcWeight(edgeState, reverse, prevOrNextEdgeId); + double sumOfWeights = 0; + for (Weighting w:weightings) { + sumOfWeights += w.calcWeight(edgeState, reverse, prevOrNextEdgeId); + } + return superWeighting.calcWeight(edgeState, reverse, prevOrNextEdgeId) * sumOfWeights; } @Override @@ -169,7 +44,7 @@ public double getMinWeight(double distance) { @Override public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) { - return superWeighting.calcMillis(edgeState, reverse, prevOrNextEdgeId) + weightCalc.calcMillis(edgeState, reverse, prevOrNextEdgeId); + return superWeighting.calcMillis(edgeState, reverse, prevOrNextEdgeId); } @Override diff --git a/openrouteservice/src/main/java/org/heigit/ors/routing/graphhopper/extensions/weighting/ConstantWeighting.java b/openrouteservice/src/main/java/org/heigit/ors/routing/graphhopper/extensions/weighting/ConstantWeighting.java new file mode 100644 index 0000000000..d20a9446d9 --- /dev/null +++ b/openrouteservice/src/main/java/org/heigit/ors/routing/graphhopper/extensions/weighting/ConstantWeighting.java @@ -0,0 +1,46 @@ +package org.heigit.ors.routing.graphhopper.extensions.weighting; + +import com.graphhopper.routing.util.FlagEncoder; +import com.graphhopper.routing.util.HintsMap; +import com.graphhopper.routing.weighting.Weighting; +import com.graphhopper.util.EdgeIteratorState; + +public class ConstantWeighting implements Weighting { + private final double weight; + private final long millis; + + public ConstantWeighting(double weight, long millis) { + this.weight = weight; + this. millis = millis; + } + + @Override + public double getMinWeight(double distance) { + return weight; + } + + @Override + public double calcWeight(EdgeIteratorState edgeIteratorState, boolean reverse, int prevOrNextEdgeId) { + return weight; + } + + @Override + public long calcMillis(EdgeIteratorState edgeIteratorState, boolean reverse, int prevOrNextEdgeId) { + return millis; + } + + @Override + public FlagEncoder getFlagEncoder() { + return null; + } + + @Override + public String getName() { + return "constant(" + weight + ")"; + } + + @Override + public boolean matches(HintsMap hintsMap) { + return false; + } +} diff --git a/openrouteservice/src/test/java/org/heigit/ors/routing/graphhopper/extensions/weightings/AdditionWeightingTest.java b/openrouteservice/src/test/java/org/heigit/ors/routing/graphhopper/extensions/weightings/AdditionWeightingTest.java new file mode 100644 index 0000000000..cb992bbcb0 --- /dev/null +++ b/openrouteservice/src/test/java/org/heigit/ors/routing/graphhopper/extensions/weightings/AdditionWeightingTest.java @@ -0,0 +1,34 @@ +package org.heigit.ors.routing.graphhopper.extensions.weightings; + +import com.graphhopper.routing.util.EncodingManager; +import org.heigit.ors.routing.graphhopper.extensions.ORSDefaultFlagEncoderFactory; +import org.heigit.ors.routing.graphhopper.extensions.flagencoders.CarFlagEncoder; +import org.heigit.ors.routing.graphhopper.extensions.flagencoders.FlagEncoderNames; +import org.heigit.ors.routing.graphhopper.extensions.weighting.AdditionWeighting; +import org.heigit.ors.routing.graphhopper.extensions.weighting.ConstantWeighting; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class AdditionWeightingTest { + private final EncodingManager encodingManager; + private final CarFlagEncoder flagEncoder; + + public AdditionWeightingTest() { + encodingManager = EncodingManager.create(new ORSDefaultFlagEncoderFactory(), FlagEncoderNames.CAR_ORS, 4); + flagEncoder = (CarFlagEncoder)encodingManager.getEncoder(FlagEncoderNames.CAR_ORS); + } + + @Test + public void sumOfConstants () { + ConstantWeighting const1 = new ConstantWeighting(1, 10); + ConstantWeighting const2 = new ConstantWeighting(2, 20); + ConstantWeighting const3 = new ConstantWeighting(3, 30); + ConstantWeighting superWeighting = new ConstantWeighting(10, 100); + + ConstantWeighting[] weightings = {const1, const2, const3}; + AdditionWeighting additionWeighting = new AdditionWeighting(weightings, superWeighting, flagEncoder); + assertEquals(60, additionWeighting.calcWeight(null, false, 0), 0.0001); + assertEquals(100, additionWeighting.calcMillis(null, false, 0), 0.0001); + } +}