I now have a HashMap, the key is an object of edgeId, nodeId, and the value is the number of vehicles. When the route arrives, the more vehicles, the higher the weight:
for (EdgeParameter key : edgeParameters) {
//创建一个 EncodeValue
BooleanEncodedValue bool = new SimpleBooleanEncodedValue(“congestion_” + key.getAdjEdge(), false);
bool.init(config);
BaseGraph baseGraph = hopper.getBaseGraph();
EdgeIteratorState edgeIteratorState = baseGraph.getEdgeIteratorState(key.getAdjEdge(), key.getAdjNode());
edgeIteratorState.set(bool, true);
}
CustomModel customModel = new CustomModel();
for (EdgeParameter key : edgeParameters) {
Integer number = carNumber.get(key);
Double X = Double.valueOf(number) / 10;
Double Y = 1 - X;
customModel.addToPriority(
Statement.If("congestion_" + key.getAdjEdge(), Statement.Op.MULTIPLY, Y + "")
);
}
Thank you very much for the official reply. I am a beginner programmer from China, and my English proficiency is poor, so I have to use translation software to communicate with you. This may cause inconvenience to your reading, and I apologize. Now let me describe the function I want to achieve: avoiding congested road sections, assuming 0-5 vehicles’ priority: multiple '_ By: 0.9 ‘, 6-10 vehicles’ priority: multiple_ By: 0.5 and so on. Because there will be different vehicles on each side at different times, I would like to customize the 'EncodedValue' and set it to the corresponding side, rather than the official ones such as' road '_ Class, road_ Environment..., My imagined ‘EncodedValue’: Assuming there is an edge edgeId=1, there are 5 cars, and the time is 2030_ 06_ 01_ 10: At 37:35, then I will attach the following ‘EncodedValue’ to the edge of edgeId=1: ‘Congestion_1=2030_06_001:10:37:35’. Therefore, the ‘CustomimModel’ during routing is:“
{
Priority:[{
If “:” confliction_1==2030_06_01_10:37:35 ",
Multiply_by: ‘0.9’
}]
}“
The above is my case, which has caused you trouble. I look forward to receiving your help. Thank you!
This sounds like you want to change the priority based on the congestion of the road, i.e. the number of vehicles on the road at a certain time. First thing you should note is that the GraphHopper routing graph (including the EncodedValues) is basically read-only after it is created during the initial import. I’m not sure if this fits your need when representing road congestion.
Anyway, I think you should try to use an IntEncodedValue to represent the number vehicles (or the level of congestion).
For example:
int bits = 8; // this will allow for 2^8=256 different congestion levels
boolean bothDirections = true; // this will allow setting different congestions for the two directions of each road
IntEncodedValueImpl congestionEnc = new IntEncodedValueImpl("congestion", bits, bothDirections);
which you could use like this in the custom model:
before create the graph? Is it before GraphHopper. importOrLoad()?
And, ‘GraphHopper. getEncodingManager()’, I cannot find the ‘create/set’ method in this’ EncodingManager ‘object. It only has’ get’, and I cannot add the created IntEncodedValue to the ‘EncodingManager’`
Yes, before importOrLoad(). Add your EncodedValue to DefaultEncodedValueFactory like in the PR I linked above. Then you can activate it like this: hopper.setEncodedValuesString("congestion").