Skip to content

Commit

Permalink
Only Activate if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
braktar committed Jun 2, 2016
1 parent cd64408 commit 08074e7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.graphhopper.jsprit.core.problem.misc.JobInsertionContext;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TourActivity;
import com.graphhopper.jsprit.core.problem.solution.route.state.RouteAndActivityStateGetter;
import com.graphhopper.jsprit.core.problem.vehicle.Vehicle;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -107,7 +109,12 @@ public void addTimeWindowConstraint() {

public void addRouteDurationConstraint() {
if (!routeDurationConstraintsSet) {
addConstraint(new VehicleRouteDurationConstraints(stateManager, vrp.getTransportCosts(), vrp.getActivityCosts()), Priority.HIGH);
boolean useConstraint = false;
for(Vehicle vehc : vrp.getVehicles())
if(vehc.getMaximumRouteDuration() != null)
useConstraint = true;
if(useConstraint)
addConstraint(new VehicleRouteDurationConstraints(stateManager, vrp.getTransportCosts(), vrp.getActivityCosts()), Priority.HIGH);
routeDurationConstraintsSet = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public VehicleRouteDurationConstraints(RouteAndActivityStateGetter states, Vehic

@Override
public ConstraintsStatus fulfilled(JobInsertionContext iFacts, TourActivity prevAct, TourActivity newAct, TourActivity nextAct, double prevActDepTime) {
if(iFacts.getNewVehicle().getMaximumRouteDuration() == null)
return ConstraintsStatus.FULFILLED;

double maximumVehicleDuration = iFacts.getNewVehicle().getMaximumRouteDuration();

double routeDurationIncrease;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ private void writeVehiclesAndTheirTypes(XMLConfiguration xmlConfig) {
}
xmlConfig.setProperty(vehiclePathString + "(" + counter + ").timeSchedule.start", vehicle.getEarliestDeparture());
xmlConfig.setProperty(vehiclePathString + "(" + counter + ").timeSchedule.end", vehicle.getLatestArrival());
xmlConfig.setProperty(vehiclePathString + "(" + counter + ").duration", vehicle.getMaximumRouteDuration());
if (vehicle.getMaximumRouteDuration() != null)
xmlConfig.setProperty(vehiclePathString + "(" + counter + ").duration", vehicle.getMaximumRouteDuration());

if (vehicle.getBreak() != null) {
Collection<TimeWindow> tws = vehicle.getBreak().getTimeWindows();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ public interface Vehicle extends HasId, HasIndex {

public abstract Break getBreak();

public abstract double getMaximumRouteDuration();
public abstract Double getMaximumRouteDuration();
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public Break getBreak() {
}

@Override
public double getMaximumRouteDuration() {
return 0;
public Double getMaximumRouteDuration() {
return null;
}

}
Expand Down Expand Up @@ -134,7 +134,7 @@ public static class Builder {

private Break aBreak;

private double maximumRouteDuration = Double.MAX_VALUE;
private Double maximumRouteDuration = null;

private Builder(String id) {
super();
Expand Down Expand Up @@ -307,7 +307,7 @@ public static NoVehicle createNoVehicle() {

private final Location startLocation;

private final double maximumRouteDuration;
private final Double maximumRouteDuration;

private final Break aBreak;

Expand Down Expand Up @@ -387,7 +387,7 @@ public Break getBreak() {
}

@Override
public double getMaximumRouteDuration() {
public Double getMaximumRouteDuration() {
return maximumRouteDuration;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public class VehicleTypeKey extends AbstractVehicle.AbstractTypeKey {
public final double latestEnd;
public final Skills skills;
public final boolean returnToDepot;
public final double maximumRouteDuration;
public Double maximumRouteDuration = null;

public VehicleTypeKey(String typeId, String startLocationId, String endLocationId, double earliestStart, double latestEnd, Skills skills, boolean returnToDepot, double maximumRouteDuration) {
public VehicleTypeKey(String typeId, String startLocationId, String endLocationId, double earliestStart, double latestEnd, Skills skills, boolean returnToDepot, Double maximumRouteDuration) {
super();
this.type = typeId;
this.startLocationId = startLocationId;
Expand All @@ -65,7 +65,9 @@ public boolean equals(Object o) {
if (!skills.equals(that.skills)) return false;
if (!startLocationId.equals(that.startLocationId)) return false;
if (!type.equals(that.type)) return false;
if (Double.compare(that.maximumRouteDuration, maximumRouteDuration) != 0) return false;
if (that.maximumRouteDuration == null ^ maximumRouteDuration == null) return false;
if (that.maximumRouteDuration != null && maximumRouteDuration != null
&& Double.compare(that.maximumRouteDuration, maximumRouteDuration) != 0) return false;

return true;
}
Expand All @@ -83,7 +85,10 @@ public int hashCode() {
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + skills.hashCode();
result = 31 * result + (returnToDepot ? 1 : 0);
temp = Double.doubleToLongBits(maximumRouteDuration);
if(maximumRouteDuration != null)
temp = Double.doubleToLongBits(maximumRouteDuration);
else
temp = Double.doubleToLongBits(Double.MAX_VALUE);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
Expand All @@ -92,7 +97,9 @@ public int hashCode() {
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(type).append("_").append(startLocationId).append("_").append(endLocationId)
.append("_").append(Double.toString(earliestStart)).append("_").append(Double.toString(latestEnd)).append("_").append(maximumRouteDuration);
.append("_").append(Double.toString(earliestStart)).append("_").append(Double.toString(latestEnd));
if(maximumRouteDuration != null)
stringBuilder.append("_").append(maximumRouteDuration);
return stringBuilder.toString();
}

Expand Down

0 comments on commit 08074e7

Please sign in to comment.