Documentation on modifying cost function

Hi everybody,
Is there any guide or documentation on how to use a specific cost function for the routing calculation?
My cost function should look like something like this:
for edge e linking e1 to e2
cost(edge e) = param1 * some_heuristic_distance(e2, fixed_point B) + param2 * some_probability(edge) + param3 * length(edge e)
Then I would like to route based on minimizing this specific cost.
I’m really lost in all the Java classes and can’t find a guide or documentation on what specifically should be modified to change the cost function. Any help would be highly appreciated. Thank you!

You can modify the code in CustomWeighting.calcEdgeWeight to do that and inject the new class with your own WeightingFactory or create a new class that implements the Weighting interface. See the SpeedWeighting for a very simple implementation.

Thank you very much for the reply.
I am still very lost. Please bear with me as I am new to Graphopper and Java dev too.
So I tried a top-down approach to see what needs to be changed.
In this simple example of route calculation:

class AnotherExample {

    public static void main(String[] args) {
        String relDir = args.length == 1 ? args[0] : "";
        GraphHopper hopper = createGraphHopperInstance("src/main/resources/berlin-latest.osm.pbf");
        routing(hopper);

        // release resources to properly shutdown or start a new instance
        hopper.close();
    }

    static GraphHopper createGraphHopperInstance(String ghLoc) {
        GraphHopper hopper = new GraphHopper();
        hopper.setOSMFile(ghLoc);
        // specify where to store graphhopper files
        hopper.setGraphHopperLocation("target/routing-graph-cache");

        // see docs/core/profiles.md to learn more about profiles
        hopper.setProfiles(new Profile("car"));

        // now this can take minutes if it imports or a few seconds for loading of course this is dependent on the area you import
        hopper.importOrLoad();
        return hopper;
    }

    public static void routing(GraphHopper hopper) {
        // simple configuration of the request object
        GHRequest req = new GHRequest(52.54824850398229, 13.385630954186816, 52.51316306397109, 13.391887774811742).
                // note that we have to specify which profile we are using even when there is only one like here
                        setProfile("car").
                // define the language for the turn instructions
                        setLocale(Locale.US);
        GHResponse rsp = hopper.route(req);

        // ...
    }

So I followed route() method, which uses createRouter(), which uses createWeightingFactory(). This method uses DefaultWeightingFactory. If I understand correctly, this can only create successfully a weighting if Profile weighting has the same name as customWeighting which is “custom”:

///....
String weightingStr = toLowerCase(profile.getWeighting());
///.....
        Weighting weighting = null;
        if (CustomWeighting.NAME.equalsIgnoreCase(weightingStr)) {
        //....
         weighting = CustomModelParser.createWeighting(encodingManager, turnCostProvider, mergedCustomModel);

And here I am really lost. I got a couple of questions:

  • First of all, is my understanding correct?
  • Does this mean that customWeighting class is always the one providing/dictating how edge weight are calculated?
  • As you said in your reply:

You can modify the code in CustomWeighting.calcEdgeWeight to do that and inject the new class with your own WeightingFactory

How can I do this? Should I create a new class that extends CustomWeighting ( e.g. ModifiedCustomWeighting) and then override calcEdgeWeighting method with a new method where I get to write the cost function that I want?
And if that’s the case, how can I make sure that when I call route(request), it’s my ModifiedCustomWeighting that is used and not the normal CustomWeighting?

I would highly appreciate any help/guidance as I did not find any clear guide on how to do this.
Thank you very much in advance!