Thank you Stefan.
I did that : here is my class code:
public class VehicleRoutingTransportCostsMatrixCustom extends AbstractForwardVehicleRoutingTransportCosts {
private Map<RelationKey, Double> distances;
private Map<RelationKey, Double> times;
private boolean isSymmetric;
private boolean timesSet;
private boolean distancesSet;
private final Double MAX_VALUE = 9999999999.0D;
private VehicleRoutingTransportCostsMatrixCustom(Builder builder) {
this.distances = new HashMap<>();
this.times = new HashMap<>();
this.isSymmetric = builder.isSymmetric;
this.distances.putAll(builder.distances);
this.times.putAll(builder.times);
this.timesSet = builder.timesSet;
this.distancesSet = builder.distancesSet;
LOGGER.info("TIME SET " + this.timesSet);
LOGGER.info("DISTANCE SET " + this.distancesSet);
}
public double getTransportTime(Location from, Location to, double departureTime, Driver driver, Vehicle vehicle) {
return getTime(from.getId(), to.getId());
}
private double getTime(String fromId, String toId) {
if(fromId.equals(toId)) {
return 0.0D;
} else if(!timesSet) {
return 0.0D;
} else {
RelationKey key = RelationKey.newKey(fromId, toId);
if(!this.isSymmetric) {
if(this.times.containsKey(key)) {
return this.times.get(key);
} else {
return MAX_VALUE;
}
} else {
Double time = this.times.get(key);
if(time == null) {
time = this.times.get(RelationKey.newKey(toId, fromId));
}
if(time != null) {
return time;
} else {
return MAX_VALUE;
}
}
}
}
public double getDistance(String fromId, String toId) {
if(fromId.equals(toId)) {
return 0.0D;
} else if(!distancesSet) {
return 0.0D;
} else {
RelationKey key = RelationKey.newKey(fromId, toId);
if(!isSymmetric) {
if(distances.containsKey(key)) {
return distances.get(key);
} else {
return MAX_VALUE;
}
} else {
Double time = distances.get(key);
if(time == null) {
time = distances.get(RelationKey.newKey(toId, fromId));
}
if(time != null) {
return time;
} else {
return MAX_VALUE;
}
}
}
}
public double getTransportCost(Location from, Location to, double departureTime, Driver driver, Vehicle vehicle) {
if(vehicle == null) {
return this.getDistance(from.getId(), to.getId());
} else {
VehicleTypeImpl.VehicleCostParams costParams = vehicle.getType().getVehicleCostParams();
return costParams.perDistanceUnit * getDistance(from.getId(), to.getId()) + costParams.perTransportTimeUnit * getTime(from.getId(), to.getId());
}
}
public static class Builder {
private static Logger log = LogManager.getLogger(Builder.class);
private boolean isSymmetric;
private Map<RelationKey, Double> distances = new HashMap<>();
private Map<RelationKey, Double> times = new HashMap<>();
private boolean distancesSet = false;
private boolean timesSet = false;
public static Builder newInstance(boolean isSymmetric) {
return new Builder(isSymmetric);
}
private Builder(boolean isSymmetric) {
this.isSymmetric = isSymmetric;
}
public Builder addTransportDistance(String from, String to, double distance) {
RelationKey key = RelationKey.newKey(from, to);
if(!distancesSet) {
distancesSet = true;
}
if(distances.containsKey(key)) {
log.warn("distance from " + from + " to " + to + " already exists. This overrides distance.");
}
distances.put(key, distance);
if(isSymmetric) {
RelationKey revKey = RelationKey.newKey(to, from);
if(distances.containsKey(revKey)) {
distances.put(revKey, distance);
}
}
return this;
}
public Builder addTransportTime(String from, String to, double time) {
RelationKey key = RelationKey.newKey(from, to);
if(!timesSet) {
timesSet = true;
}
if(this.times.containsKey(key)) {
log.warn("transport-time from " + from + " to " + to + " already exists. This overrides times.");
}
this.times.put(key, time);
if(this.isSymmetric) {
RelationKey revKey = RelationKey.newKey(to, from);
if(times.containsKey(revKey)) {
times.put(revKey, time);
}
}
return this;
}
public VehicleRoutingTransportCostsMatrixCustom build() {
return new VehicleRoutingTransportCostsMatrixCustom(this);
}
}
static class RelationKey {
final String from;
final String to;
static RelationKey newKey(String from, String to) {
return new RelationKey(from, to);
}
public RelationKey(String from, String to) {
this.from = from;
this.to = to;
}
public int hashCode() {
boolean prime = true;
byte result = 1;
int result1 = 31 * result + (this.from == null?0:this.from.hashCode());
result1 = 31 * result1 + (this.to == null?0:this.to.hashCode());
return result1;
}
public boolean equals(Object obj) {
if(this == obj) {
return true;
} else if(obj == null) {
return false;
} else if(this.getClass() != obj.getClass()) {
return false;
} else {
RelationKey other = (RelationKey)obj;
if(this.from == null) {
if(other.from != null) {
return false;
}
} else if(!this.from.equals(other.from)) {
return false;
}
if(this.to == null) {
if(other.to != null) {
return false;
}
} else if(!this.to.equals(other.to)) {
return false;
}
return true;
}
}
}
but even I return 9999999999.0 the result looks like this:
when I created full cost matrix (results from google maps api) it generates routes right. You ever had issue like this? or maybe my code is wrong?
thanks again for help