Skip to content

Commit

Permalink
Merge pull request #760 from GIScience/refactor/AdditionWeighting
Browse files Browse the repository at this point in the history
Refactor/addition weighting
  • Loading branch information
takb authored Jul 3, 2020
2 parents 35d16ec + 5c2288d commit f844c41
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 135 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit f844c41

Please sign in to comment.