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);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…