-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #760 from GIScience/refactor/AdditionWeighting
Refactor/addition weighting
- Loading branch information
Showing
5 changed files
with
92 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
.../main/java/org/heigit/ors/routing/graphhopper/extensions/weighting/ConstantWeighting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
.../java/org/heigit/ors/routing/graphhopper/extensions/weightings/AdditionWeightingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |