How to fix - Error: custom weighting requires a CustomProfile but was profile?

Hi!
I’m trying to add new FlagEncoder and custom profiles.
Here is my code:

Flagencoder

@Component
public class SmallTruckEncoder extends CarFlagEncoder {

    public SmallTruckEncoder() {
        this(new PMap());
    }

    public SmallTruckEncoder(int speedBits, double speedFactor, int maxTurnCosts) {
        this(new PMap().putObject("speed_bits", speedBits).putObject("speed_factor", speedFactor).
                putObject("max_turn_costs", maxTurnCosts));
    }

    public SmallTruckEncoder(PMap properties) {
        super(properties.getInt("speed_bits", 5),
                properties.getDouble("speed_factor", 5),
                properties.getInt("max_turn_costs", properties.getBool("turn_costs", false) ? 1 : 0));

        intendedValues.add("delivery");
        restrictedValues.remove("delivery");

        maxPossibleSpeed = 110;
    }

    @Override
    public int getVersion() {
        return 1;
    }

    @Override
    public TransportationMode getTransportationMode() {
        return TransportationMode.HGV;
    }

    @Override
    public String toString() {
        return VehicleType.SMALL_TRUCK.getValue();
    }

}

config.yml

graph.flag_encoders: small_truck|turn_costs=true
graph.dataaccess: RAM_STORE
graph.encoded_values: max_weight,max_height,max_width
profiles:
    - name: small_truck_short_fastest
      vehicle: small_truck
      turn_costs: true
      weighting: custom
      custom_model_file: ./small_truck.yml

small_truck.yml

priority:
  - if: max_weight < 3.5
    multiply by: 0

I’m getting exception
Caused by: java.lang.IllegalArgumentException: Could not create weighting for profile: ‘small_truck_short_fastest’.
Profile: name=small_truck_short_fastest|vehicle=car|weighting=custom|turnCosts=true|hints={}
Error: custom weighting requires a CustomProfile but was profile=small_truck_short_fastest

How I can fix it?
I used informations from this sources:

Thanks in advance!

You should return HGV here.

How I can fix it?

Can you paste your config in quotes (```) with the correct white space?

1 Like

You should return HGV here.
Many thanks. Updated code

Can you paste your config in quotes (```) with the correct white space?
Sure.

graph.flag_encoders: small_truck|turn_costs=true
graph.dataaccess: RAM_STORE
graph.encoded_values: max_weight,max_height,max_width
profiles:
    - name: small_truck_short_fastest
      vehicle: small_truck
      turn_costs: true
      weighting: custom
      custom_model_file: ./small_truck.yml

small_truck.yml

priority:
  - if: max_weight < 3.5
    multiply by: 0

I tried to reproduce this problem with the latest version but wasn’t able to. You need to make this new FlagEncoder available in DefaultFlagEncoderFactory:

if (name.equals("small_truck")) return new TruckFlagEncoder(configuration);

Ensure that “small_truck” is also used in toString of TruckFlagEncoder.

Then set this factory to the GraphHopper class.

Please make sure you use the latest version 4.0 too. E.g. the function is called multiply_by and no longer multiply by.

btw: you could directly embed the small_truck custom model:

graphhopper:
   ...
   profiles:
      - name: small_truck_short_fastest
        custom_model:
          priority:
           - if: max_weight < 3.5
             multiply_by: 0

In recent version also no path entry is allowed in custom_model_file and you would need:

graphhopper:
  ...
  custom_model_folder: ./custom_models
  profiles:
    - name: small_truck_short_fastest
      vehicle: small_truck
      turn_costs: true
      weighting: custom
      custom_model_file: small_truck.yml

If you still cannot make it working you would need to send a minimal project where this problem occurs so that I can debug it.

1 Like

Hi!
Thank you so much for the quick response.

I tried to reproduce this problem with the latest version but wasn’t able to. You need to make this new FlagEncoder available in DefaultFlagEncoderFactory

Sure. Here is my code:

@Component
public class CustomFlagEncoderFactory extends DefaultFlagEncoderFactory {

    public CustomFlagEncoderFactory() {
        super();
    }

    @Override
    public FlagEncoder createFlagEncoder(String name, PMap configuration) {
        if (name.equals(VehicleType.MOFA.getValue())) {
            return new MofaFlagEncoder(configuration);
        } else if (name.equals(VehicleType.SMALL_TRUCK.getValue())) {
            return new SmallTruckEncoder(configuration);
        } else if (name.equals(VehicleType.TRUCK.getValue())) {
            return new TruckEncoder(configuration);
        }
        return super.createFlagEncoder(name, configuration);
    }
}

Ensure that “small_truck” is also used in toString of TruckFlagEncoder.
Done.

Then set this factory to the GraphHopper class.

hopper.setEncodingManager(EncodingManager.create(smallTruckEncoder));
    hopper.setFlagEncoderFactory(customFlagEncoderFactory);

Please make sure you use the latest version 4.0 too. E.g. the function is called multiply_by and no longer multiply by.
I’m using graphopper-reader-osm 3.0-pre3 (not graphhopper-core). In mvnrepository the latest version of this library is 3.0-pre3, not 4.0.
https://mvnrepository.com/artifact/com.graphhopper/graphhopper-reader-osm
Do I need to build the latest version manually?

Thanks in advance!

You should only use releases like 3.0 or 4.0. Not pre-releases.

Do I need to build the latest version manually?

No. You can use graphhopper-core - see here: Move the reader-osm module into core by easbar · Pull Request #2298 · graphhopper/graphhopper · GitHub

1 Like

So I have updated graphhopper to 4.0. But the exception not gone away.

If you still cannot make it working you would need to send a minimal project where this problem occurs so that I can debug it.

Here is the minimal project:
https://github.com/hasanli-orkhan/gh-demo

Exception from DefaultWeightingFactory

Caused by: java.lang.IllegalArgumentException: Could not create weighting for profile: 'small_truck_short_fastest'.
Profile: name=small_truck_short_fastest|vehicle=small_truck|weighting=custom|turnCosts=true|hints={}
Error: custom weighting requires a CustomProfile but was profile=small_truck_short_fastest

Ah, you didn’t say that you initialize GraphHopper via Java :slight_smile:

You need to create the profiles like so: https://github.com/graphhopper/graphhopper/blob/master/web/src/test/java/com/graphhopper/application/resources/RouteResourceCustomModelTest.java#L68

Or if you need to read stuff from config file it gets a bit more complicated: https://github.com/graphhopper/graphhopper/blob/c643eb7b0dd68e1a3c22ff8cc5e2dbb0a439734b/web-bundle/src/main/java/com/graphhopper/http/GraphHopperManaged.java#L59

1 Like

Thank you! I will try.

Thank you so much! I have resolved this issue by adding CustomProfile in Java configuration.

1 Like

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