How to use Java code to create custom EncodedValue and use

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 + "")
        );
    }

But I can’t route out the route

It is very hard to tell what you are trying to achieve, but you can use this PR as a blueprint to understand how custom encoded values can be added:

1 Like

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!

笔记

I integrated into the project through the Jar package, using GraphHopper-7.0 version and using Java code for development,

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:

priority:
   - if: congestion < 5
     multiply_by: 0.9
   - else_if: congestion < 10
     multiply_by: 0.8
   - else: ''
     multiply_by: 0.7



这是我的Java代码,但是还是不能实现路由,getBast(),is []

This is my Java code, but it still cannot implement routing, ‘getBast()’, is’ []`

the config is this:
EncodedValue.InitializerConfig config = new EncodedValue.InitializerConfig();

You can check for errors using res.getErrors().
Also you need add the congestion EncodedValue to the EncodingManager before you create the graph.

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").

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.