Using real distances between points in optaplanner

1k views Asked by At


Hello

I am new to optaplanner. I am trying to use the vrp (tw) example.

I'll like to set real distances (route distances) in order to get a real solution.

I have real distances between all points in a double NXN matrix (distance(a,b)<>distance(b,a)), so, how can I use the matrix in the .xml (.vrp) input file to solve vrp problem ?

Note : my matrix is about from 2X10X10 to 2X100X100.

Thanks in advance.

opp

2

There are 2 answers

1
user3227547 On

Here is the way I went. There might be much better solutions than this - I have to admit that I just started to play around with Optaplanner. Any suggestions on improvement are welcomed.

Hope this helps. Brgds, Paul

My matrix is in the form "From Customer" "To Customer" "Distance" and I created a class Distance. *In the Importer I build a DistanceMap for each Location:*

 readConstantLine("CustFrom CustTo Distance");
            long locationId = -1;
            line = bufferedReader.readLine(); 
            distanceMap = new HashMap<Long, Double>(locationListSize);
            while (line != null && !line.trim().isEmpty()) {
                 String[] lineTokens = splitBySpacesOrTabs(line.trim(), 3);
                 if (locationId != Long.parseLong(lineTokens[0])){
                        if (distanceMap.isEmpty() == false){
                             Location location = new Location();
                             location = locationList.get((int) locationId);
                             distance.setDistanceMap(distanceMap);
                             location.setDistance(distance);
                             locationList.set((int) locationId, location);

                         }
                        locationId = Long.parseLong(lineTokens[0]);
                        distance = new Distance();
                        distanceMap = new HashMap<Long, Double>(locationListSize);

                 }
                 distanceMap.put( Long.parseLong(lineTokens[1]), Double.parseDouble(lineTokens[2]));         
                 line = bufferedReader.readLine();       
                }
            if (distanceMap.isEmpty() == false){
             Location location = new Location();
             location = locationList.get((int) locationId);
             distance.setDistanceMap(distanceMap);
             location.setDistance(distance);
             locationList.set((int) locationId, location);

In the location class I use the following method to get the Distance:

 public int getMilliDistanceDistanceMap(Location location) {
        // Implementation distanceMap
     return distance.getDistance(location, this)  ; 

And the distance class method looks like this:

public int getDistance(Location fromLocation,Location toLocation )
{
    double distance = toLocation.getDistance().distanceMap.get(fromLocation.getId());
    return  (int) (distance * 1000);
}
0
Geoffrey De Smet On

As of 2019, start from optaweb-vehicle-routing