Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions config-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ graphhopper:
# To enable finite u-turn costs use something like fastest|u_turn_costs=30, where 30 are the u-turn costs in seconds
# (given as integer). Note that since the u-turn costs are given in seconds the weighting you use should also
# calculate the weight in seconds. The u-turn costs will only be applied for edge_based, see below.
prepare.ch.weightings: fastest
# prepare.ch.weightings: fastest

# To enable turn-costs in speed mode (contraction hierarchies) edge-based graph traversal and a more elaborate
# pre-processing is required. Using this option you can either turn off the edge-based pre-processing (choose 'off'),
# use edge-based pre-processing for all encoders/vehicles with turn_costs=true (choose 'edge_or_node') or use node-based
# pre-processing for all encoders/vehicles and additional edge-based pre-processing for all encoders/vehicles with
# turn_costs=true (choose 'edge_and_node').
prepare.ch.edge_based: off
# prepare.ch.edge_based: off


# Disable the speed mode. Should be used only with routing.max_visited_nodes or when the hybrid mode is enabled instead
Expand All @@ -65,10 +65,12 @@ graphhopper:


# The hybrid mode can be enabled with
# prepare.lm.weightings: fastest
prepare.lm.weightings: fastest
# should i also include 'FastestDynamic' to use that?
# prepare.lm.weightings: fastestDynamic

# To tune the performance vs. memory usage for the hybrid mode use
# prepare.lm.landmarks: 16
prepare.lm.landmarks: 16

# Make landmark preparation parallel if you have enough RAM. Change this only if you know what you are doing and if the default worked for you.
# prepare.lm.threads: 1
Expand All @@ -91,7 +93,7 @@ graphhopper:
# If enabled, allows a user to run flexibility requests even if speed mode is enabled. Every request then has to include a hint ch.disable=true.
# Attention, non-CH route calculations take way more time and resources, compared to CH routing.
# A possible attacker might exploit this to slow down your service. Only enable it if you need it and with routing.maxVisitedNodes
# routing.ch.disabling_allowed: true
routing.ch.disabling_allowed: true


# If enabled, allows a user to run flexible mode requests even if the hybrid mode is enabled. Every such request then has to include a hint routing.lm.disable=true.
Expand Down
11 changes: 6 additions & 5 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ graphhopper:
# To enable finite u-turn costs use something like fastest|u_turn_costs=30, where 30 are the u-turn costs in seconds
# (given as integer). Note that since the u-turn costs are given in seconds the weighting you use should also
# calculate the weight in seconds. The u-turn costs will only be applied for edge_based, see below.
prepare.ch.weightings: fastest
# prepare.ch.weightings: fastest

# To enable turn-costs in speed mode (contraction hierarchies) edge-based graph traversal and a more elaborate
# pre-processing is required. Using this option you can either turn off the edge-based pre-processing (choose 'off'),
Expand All @@ -56,7 +56,8 @@ graphhopper:


# Disable the speed mode. Should be used only with routing.max_visited_nodes or when the hybrid mode is enabled instead
# prepare.ch.weightings: no
prepare.ch.weightings: no
# prepare.lm.weightings: fastestDynamic


# To make CH preparation faster for multiple flagEncoders you can increase the default threads if you have enough RAM.
Expand All @@ -65,10 +66,10 @@ graphhopper:


# The hybrid mode can be enabled with
# prepare.lm.weightings: fastest
prepare.lm.weightings: fastest

# To tune the performance vs. memory usage for the hybrid mode use
# prepare.lm.landmarks: 16
prepare.lm.landmarks: 16

# Make landmark preparation parallel if you have enough RAM. Change this only if you know what you are doing and if the default worked for you.
# prepare.lm.threads: 1
Expand All @@ -91,7 +92,7 @@ graphhopper:
# If enabled, allows a user to run flexibility requests even if speed mode is enabled. Every request then has to include a hint ch.disable=true.
# Attention, non-CH route calculations take way more time and resources, compared to CH routing.
# A possible attacker might exploit this to slow down your service. Only enable it if you need it and with routing.maxVisitedNodes
# routing.ch.disabling_allowed: true
routing.ch.disabling_allowed: true


# If enabled, allows a user to run flexible mode requests even if the hybrid mode is enabled. Every such request then has to include a hint routing.lm.disable=true.
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/com/graphhopper/GraphHopper.java
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ public boolean load(String graphHopperFolder) {

GHLock lock = null;
try {
// create locks only if writes are allowed, if they are not allowed a lock cannot be created
// create locks only if writes are allowed, if they are not allowed a lock cannot be created
// (e.g. on a read only filesystem locks would fail)
if (ghStorage.getDirectory().getDefaultType().isStoring() && isAllowWrites()) {
lockFactory.setLockDir(new File(ghLocation));
Expand Down Expand Up @@ -906,6 +906,8 @@ public Weighting createWeighting(HintsMap hintsMap, FlagEncoder encoder, Graph g
weighting = new PriorityWeighting(encoder, hintsMap);
else
weighting = new FastestWeighting(encoder, hintsMap);
} else if ("fastestDynamic".equalsIgnoreCase(weightingStr)) {
weighting = new FastestDynamicWeighting(encoder, hintsMap);
} else if ("curvature".equalsIgnoreCase(weightingStr)) {
if (encoder.supports(CurvatureWeighting.class))
weighting = new CurvatureWeighting(encoder, hintsMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@ public final BooleanEncodedValue getAccessEnc() {
*
* @Deprecated
*/
protected void setSpeed(boolean reverse, IntsRef edgeFlags, double speed) {
// protected void setSpeed(boolean reverse, IntsRef edgeFlags, double speed) {
public void setSpeed(boolean reverse, IntsRef edgeFlags, double speed) {
if (speed < 0 || Double.isNaN(speed))
throw new IllegalArgumentException("Speed cannot be negative or NaN: " + speed + ", flags:" + BitUtil.LITTLE.toBitString(edgeFlags));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public class CarExpFlagEncoder extends AbstractFlagEncoder {
*/
protected final Map<String, Integer> defaultSpeedMap = new HashMap<>();

// blockedEdgesTop10 is the list of edges of concern, blockedEdgesTop10LF key-value pair where key=osmId, value=how many cars took that edge.
public long[] blockedEdgesTop10;
public HashMap<Long, Double> blockedEdgesTop10LF;

public CarExpFlagEncoder() {
this(5, 5, 0);
}
Expand Down Expand Up @@ -102,7 +106,7 @@ public CarExpFlagEncoder(int speedBits, double speedFactor, int maxTurnCosts) {

trackTypeSpeedMap.put("grade1", 20); // paved
trackTypeSpeedMap.put("grade2", 15); // now unpaved - gravel mixed with ...
trackTypeSpeedMap.put("grade3", 10); // ... hard and soft materials
trackTypeSpeedMap.put("grade3", 10); // ... hard and soft materials

badSurfaceSpeedMap.add("cobblestone");
badSurfaceSpeedMap.add("grass_paver");
Expand Down Expand Up @@ -155,6 +159,9 @@ public int getVersion() {
return 2;
}

public void setBlockedEdgesTop10LF(HashMap<Long, Double> blockedEdgesTop10LF){this.blockedEdgesTop10LF = blockedEdgesTop10LF;}
public HashMap<Long, Double> getBlockedEdgesTop10LF(){ return blockedEdgesTop10LF; }

/**
* Define the place of the speedBits in the edge flags for car.
*/
Expand Down Expand Up @@ -248,6 +255,8 @@ public long handleRelationTags(long oldRelationFlags, ReaderRelation relation) {

@Override
public IntsRef handleWayTags(IntsRef edgeFlags, ReaderWay way, EncodingManager.Access accept, long relationFlags) {
long wayID= way.getId();

if (accept.canSkip())
return edgeFlags;

Expand All @@ -273,13 +282,42 @@ public IntsRef handleWayTags(IntsRef edgeFlags, ReaderWay way, EncodingManager.A
accessEnc.setBool(true, edgeFlags, true);
}

} else {
if (blockedEdgesTop10LF.containsKey(wayID)) {
double edgeVal = blockedEdgesTop10LF.get(wayID);
// say for every time the edge is traversed speed is reduced by .001. for example: if an edge has 10,000 car the speed reduced by 10.
double newSpeed = speed - (edgeVal * .001);
if (newSpeed >= speed) {
setSpeed(false, edgeFlags, 0);
if (speedTwoDirections)
setSpeed(true, edgeFlags, 0);
} else {
setSpeed(false, edgeFlags, newSpeed);
if (speedTwoDirections)
setSpeed(true, edgeFlags, newSpeed);
}
}
}
else {
double ferrySpeed = getFerrySpeed(way);
accessEnc.setBool(false, edgeFlags, true);
accessEnc.setBool(true, edgeFlags, true);
setSpeed(false, edgeFlags, ferrySpeed);
if (speedTwoDirections)
setSpeed(true, edgeFlags, ferrySpeed);

if (blockedEdgesTop10LF.containsKey(wayID)) {
double edgeVal = blockedEdgesTop10LF.get(wayID);
double newSpeed = ferrySpeed - (edgeVal * .001);
if (newSpeed >= ferrySpeed) {
setSpeed(false, edgeFlags, 0);
if (speedTwoDirections)
setSpeed(true, edgeFlags, 0);
} else {
setSpeed(false, edgeFlags, newSpeed);
if (speedTwoDirections)
setSpeed(true, edgeFlags, newSpeed);
}
}
}

for (String restriction : restrictions) {
Expand Down Expand Up @@ -362,6 +400,6 @@ protected double applyBadSurfaceSpeed(ReaderWay way, double speed) {

@Override
public String toString() {
return "car";
return "car_exp";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper.routing.weighting;

import com.carrotsearch.hppc.IntSet;
import com.graphhopper.coll.GHIntHashSet;
import com.graphhopper.util.EdgeIteratorState;

import java.util.Collection;

/**
* Rates already used Paths worse.
*
* @author Tunaggina
*/

public class AvoidEdgesWeightingSIGMA extends AbstractAdjustedWeighting {
// contains the edge IDs of the already visited edges
// protected final IntSet avoidedEdges = new GHIntHashSet();
private IntSet avoidedEdges = new GHIntHashSet();

private double edgePenaltyFactor = 5.0;

public AvoidEdgesWeightingSIGMA(Weighting superWeighting) {
super(superWeighting);
}

public AvoidEdgesWeightingSIGMA setEdgePenaltyFactor(double edgePenaltyFactor) {
this.edgePenaltyFactor = edgePenaltyFactor;
return this;
}

/**
* This method adds the specified path to this weighting which should be penalized in the
* calcWeight method.
*/
// when visitedEdges is protected....

// public void addEdges(Collection<EdgeIteratorState> edges) {
// for (EdgeIteratorState edge : edges) {
// visitedEdges.add(edge.getEdge());
// }
// }

public void setAvoidedEdges(IntSet avoidedEdges) {
this.avoidedEdges = avoidedEdges;
}

@Override
public double getMinWeight(double distance) {
return superWeighting.getMinWeight(distance);
}

@Override
public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
double weight = superWeighting.calcWeight(edgeState, reverse, prevOrNextEdgeId);
if (avoidedEdges.contains(edgeState.getEdge()))
return weight * edgePenaltyFactor;
return weight;
}

@Override
public String getName() {
return "avoid_edges_Sigma";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.graphhopper.routing.weighting;

import com.carrotsearch.hppc.IntFloatHashMap;
import com.graphhopper.routing.util.FlagEncoder;
import com.graphhopper.routing.util.HintsMap;
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.util.PMap;
import com.graphhopper.util.Parameters.Routing;
import java.util.List;

/**
* Calculates the fastest route with the specified vehicle (VehicleEncoder). Calculates the weight
* in seconds. Updates the weight for MyImport_Sigma.java
* <p>
*
* @author Tunaggina
*/


public class FastestDynamicWeighting extends AbstractWeighting {

protected final static double SPEED_CONV = 3.6;
private final double headingPenalty;
private final long headingPenaltyMillis;
private final double maxSpeed;
private double dynamicEdgePenaltyFactor = 50.0 ; //0.01

// need to correspond "myWeighedEdges" to "count10" in "MyImport_Sigma"
// list of the edges that have been traversed.
private List<Integer> myWeighedEdges = new ArrayList();

// private List<Integer> getCountLog(){
// return null;
// }

// I am trying to get the values directly..however, as 'Weighting' class does not have any parameter for that,
// it will not take this value. To do this, I think we need to add another parameter "customWeight" along with FlagEncoder
// and PMap. If that parameter is empty= it won't impact anything. if the parameter has the arraylist of edges= then it consider.
public FastestDynamicWeighting(FlagEncoder encoder, PMap map) {
super(encoder);
headingPenalty = map.getDouble(Routing.HEADING_PENALTY, Routing.DEFAULT_HEADING_PENALTY);
headingPenaltyMillis = Math.round(headingPenalty * 1000);
maxSpeed = encoder.getMaxSpeed() / SPEED_CONV;
// this.myWeighedEdges = getCountLog();
this.myWeighedEdges = getMyWeighedEdges();
}

public FastestDynamicWeighting(FlagEncoder encoder) {
this(encoder, new HintsMap(0));
}

private List<Integer> getMyWeighedEdges(){
return myWeighedEdges;
}

public void setMyWeighedEdges(List<Integer> myWeighedEdges) {
this.myWeighedEdges = myWeighedEdges;
}

@Override
public double getMinWeight(double distance) {
return distance / maxSpeed;
}

@Override
public double calcWeight(EdgeIteratorState edge, boolean reverse, int prevOrNextEdgeId) {
double speed = reverse ? edge.getReverse(avSpeedEnc) : edge.get(avSpeedEnc);
if (speed == 0)
return Double.POSITIVE_INFINITY;

double time = edge.getDistance() / speed * SPEED_CONV;

// add direction penalties at start/stop/via points
boolean unfavoredEdge = edge.get(EdgeIteratorState.UNFAVORED_EDGE);
if (unfavoredEdge)
time += headingPenalty;

// get the value of the edge
if (myWeighedEdges.contains(edge.getEdge()))
time= time + time * dynamicEdgePenaltyFactor ;

return time ;
}


@Override
public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
// TODO move this to AbstractWeighting? see #485
long time = 0;
boolean unfavoredEdge = edgeState.get(EdgeIteratorState.UNFAVORED_EDGE);
if (unfavoredEdge)
time += headingPenaltyMillis;

return time + super.calcMillis(edgeState, reverse, prevOrNextEdgeId);
}

@Override
public String getName() {
return "fastestDynamic";
}

}

/*
// i also tried to make it public (like AvoiEdgesWeighting class) but the weighting class does not allow

public void setMyWeighedEdges(List<Integer> avoidedEdges) { //just the edge_ids that needed to be avoided
this.myWeighedEdges = myWeighedEdges;
}
*/
Loading