Custom weight based on shortest weight

Hi all,

I am trying to create a Custom Weighting called ShortestEasyWeight that extends ShortestWeighting.

The custom weight should be very similar to ShortestWeighting, except with the addition of priority to the edge weight calculation. I would to include the priority of each road edge based on a routing_priority tag that I have. I am trying to replicate what @njanakiev have done by adding my routing_priority tag (https://github.com/njanakiev/minimal-graphhopper/blob/master/src/main/java/com/janakiev/minimal/routing/MinimalWeighting.java). This is what my code looks like at the moment:

package com.graphhopper.routing;
import com.graphhopper.routing.util.FlagEncoder;
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.routing.weighting.ShortestWeighting;

public class ShortestEasyWeighting extends ShortestWeighting {
protected double maxSpeed;
protected double routingPriority;

public ShortestEasyWeighting(FlagEncoder encoder) {

    this.flagEncoder = encoder;
    maxSpeed = encoder.getMaxSpeed();
    routingPriority = encoder.getRoutingPriority();

}

@Override
public double getMinWeight(double currDistToGoal) {
    return currDistToGoal;
}

//@Override
public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
    
    return edgeState.getDistance / (maxSpeed * routingPriority);
}

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

}

These are the errors I am getting:

[ERROR] /home/t4a/Downloads/graphhopper-4.x/core/src/main/java/com/graphhopper/routing/weighting/ShortestEasyWeighting.java:[39,54] error: no suitable constructor found for ShortestWeighting(no arguments)
[ERROR] constructor ShortestWeighting.ShortestWeighting(FlagEncoder) is not applicable
[ERROR] (actual and formal argument lists differ in length)
[ERROR] constructor ShortestWeighting.ShortestWeighting(FlagEncoder,TurnCostProvider) is not applicable
[ERROR] (actual and formal argument lists differ in length)
[ERROR] /home/t4a/Downloads/graphhopper-4.x/core/src/main/java/com/graphhopper/routing/weighting/ShortestEasyWeighting.java:[41,12] error: cannot assign a value to final variable flagEncoder
[ERROR] /home/t4a/Downloads/graphhopper-4.x/core/src/main/java/com/graphhopper/routing/weighting/ShortestEasyWeighting.java:[43,33] error: cannot find symbol
[ERROR] symbol: method getRoutingPriority()
[ERROR] location: variable encoder of type FlagEncoder
[ERROR] /home/t4a/Downloads/graphhopper-4.x/core/src/main/java/com/graphhopper/routing/weighting/ShortestEasyWeighting.java:[55,24] error: cannot find symbol
[ERROR] symbol: variable getDistance

Any suggestions how I can achieve this would be greatly appreciated.

I have changed the code to this and I am not getting any errors when compiling:

package com.graphhopper.routing;
import com.graphhopper.routing.util.FlagEncoder;
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.routing.weighting.ShortestWeighting;

//import static com.graphhopper.routing.weighting.TurnCostProvider.NO_TURN_COST_PROVIDER;

public class ShortestEasyWeighting extends ShortestWeighting {
protected double maxSpeed;
protected double routingPriority;

public ShortestEasyWeighting(FlagEncoder flagEncoder) {

    super(flagEncoder);
  //  maxSpeed = encoder.getMaxSpeed();
  //  routingPriority = encoder.getRoutingPriority();

}

@Override
public double getMinWeight(double currDistToGoal) {
    return currDistToGoal;
}

//@Override
public double calcWeight(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
    //return edgeState.getDistance();
    return edgeState.getDistance() / (maxSpeed * routingPriority);
}

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

}

However, I get this error when executing a GraphHopper instance:

2021-11-30 15:35:47.799 [main] ERROR io.dropwizard.cli.ServerCommand - Unable to start server, shutting down
java.lang.IllegalArgumentException: Could not create weighting for profile: ‘my_custom_car’.
Profile: name=my_custom_car|vehicle=car|weighting=short_easy|turnCosts=false|hints={distance_factor=0}
Error: Weighting ‘short_easy’ not supported
at com.graphhopper.GraphHopper.checkProfilesConsistency(GraphHopper.java:845)
at com.graphhopper.GraphHopper.load(GraphHopper.java:787)
at com.graphhopper.GraphHopper.importOrLoad(GraphHopper.java:633)
at com.graphhopper.http.GraphHopperManaged.start(GraphHopperManaged.java:103)
at io.dropwizard.lifecycle.JettyManaged.doStart(JettyManaged.java:27)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:73)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:169)
at org.eclipse.jetty.server.Server.start(Server.java:423)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:117)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:97)
at org.eclipse.jetty.server.Server.doStart(Server.java:387)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:73)
at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:60)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:98)
at io.dropwizard.cli.Cli.run(Cli.java:78)
at io.dropwizard.Application.run(Application.java:94)
at com.graphhopper.http.GraphHopperApplication.main(GraphHopperApplication.java:35)
java.lang.IllegalArgumentException: Could not create weighting for profile: ‘my_custom_car’.
Profile: name=my_custom_car|vehicle=car|weighting=short_easy|turnCosts=false|hints={distance_factor=0}
Error: Weighting ‘short_easy’ not supported
at com.graphhopper.GraphHopper.checkProfilesConsistency(GraphHopper.java:845)
at com.graphhopper.GraphHopper.load(GraphHopper.java:787)
at com.graphhopper.GraphHopper.importOrLoad(GraphHopper.java:633)
at com.graphhopper.http.GraphHopperManaged.start(GraphHopperManaged.java:103)
at io.dropwizard.lifecycle.JettyManaged.doStart(JettyManaged.java:27)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:73)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:169)
at org.eclipse.jetty.server.Server.start(Server.java:423)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:117)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:97)
at org.eclipse.jetty.server.Server.doStart(Server.java:387)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:73)
at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:60)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:98)
at io.dropwizard.cli.Cli.run(Cli.java:78)
at io.dropwizard.Application.run(Application.java:94)
at com.graphhopper.http.GraphHopperApplication.main(GraphHopperApplication.java:35)

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