Graphhopper Map Setup In Vehicle Routing Problem Using OptaPlanner

Posted By : Mansi Malik | 24-Apr-2023

Optaplanner

Loading...

What Is The Vehicle Routing Problem?

Many organizations deal with a vehicle routing problem on a daily basis: identifying the best sequence to deliver a number of items to a number of places with a fleet of vehicles. This kind of optimization problem is known as the vehicle routing problem (VRP), and it has several variations.

OptaPlanner can solve many of these vehicle routing variations and gives solution examples.

Graphhopper Map Setup In Vehicle Routing Problem

We have added the graphhopper dependency to the configuration of the graphhopper map. I included the dependency of the most recent version of Graphhopper. I'm using the most recent versions of OptaPlanner 8.35.0.Final and Graphhopper 7.0.

Add the following dependency to graphhopper-core in your pom.xml

<!-- https://mvnrepository.com/artifact/com.graphhopper/graphhopper-core -->
<dependency>
<groupId>com.graphhopper</groupId>
<artifactId>graphhopper-core</artifactId>
<version>7.0</version>
</dependency>

In Main Class, create a hashmap to store graphhopper data based on the vehicle's value.I've created the Vehicles enum, in which I define the vehicle and the vehicle's value.

The map file name and path URL are defined in the application.properties file.

I have created a class, ApplicationPropertyValue, in which I define the objects and the object value I get from application.properties files.

I have now added the startUp method for downloading and importing the map file to a new class called AppEvents. I'm using the EventListener on this method, so it will be called automatically when I run the application. It will check whether our map file is already downloaded or not; if not, the downloadOsmFile method will be called and the map will be downloaded, or if yes, it will call the loadGraphhopperFile method and simply import and load the graphhopper map.

downloadOsmFile Method code

loadGraphhopperFile Method

When you are calculating the distance, you have to set the profile. Otherwise, it will throw an exception that the profile is not set.

Closing Thoughts

I hope this blog helps you set up the map in VRP so that you can quickly set up the map in our project. I provide all the necessary setup information for setting up a map file. If you're using an older version of Graphhoper, you'll have to change some configurations.

Learn more about our OptaPlanner development services. Contact us at [email protected] for project-related queries.