mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
fixes
This commit is contained in:
parent
aaef511b75
commit
dc8adca385
2 changed files with 47 additions and 20 deletions
|
|
@ -26,10 +26,7 @@ import com.graphhopper.jsprit.core.problem.cost.VehicleRoutingActivityCosts;
|
||||||
import com.graphhopper.jsprit.core.problem.job.Job;
|
import com.graphhopper.jsprit.core.problem.job.Job;
|
||||||
import com.graphhopper.jsprit.core.problem.job.Shipment;
|
import com.graphhopper.jsprit.core.problem.job.Shipment;
|
||||||
import com.graphhopper.jsprit.core.problem.misc.JobInsertionContext;
|
import com.graphhopper.jsprit.core.problem.misc.JobInsertionContext;
|
||||||
import com.graphhopper.jsprit.core.problem.solution.route.activity.DeliveryActivity;
|
import com.graphhopper.jsprit.core.problem.solution.route.activity.*;
|
||||||
import com.graphhopper.jsprit.core.problem.solution.route.activity.End;
|
|
||||||
import com.graphhopper.jsprit.core.problem.solution.route.activity.PickupActivity;
|
|
||||||
import com.graphhopper.jsprit.core.problem.solution.route.activity.TourActivity;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -75,6 +72,9 @@ public class MaxTimeInVehicleConstraint implements HardActivityConstraint {
|
||||||
double newActDeparture = newActStart + activityCosts.getActivityDuration(prevAct, newAct, newActArrival, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
double newActDeparture = newActStart + activityCosts.getActivityDuration(prevAct, newAct, newActArrival, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||||
double nextActArrival = newActDeparture + transportTime.getTransportTime(newAct.getLocation(),nextAct.getLocation(),newActDeparture,iFacts.getNewDriver(),iFacts.getNewVehicle());
|
double nextActArrival = newActDeparture + transportTime.getTransportTime(newAct.getLocation(),nextAct.getLocation(),newActDeparture,iFacts.getNewDriver(),iFacts.getNewVehicle());
|
||||||
double nextActStart = Math.max(nextActArrival, nextAct.getTheoreticalEarliestOperationStartTime());
|
double nextActStart = Math.max(nextActArrival, nextAct.getTheoreticalEarliestOperationStartTime());
|
||||||
|
if (nextAct instanceof DeliverService && nextActStart - iFacts.getNewVehicle().getEarliestDeparture() > ((TourActivity.JobActivity)nextAct).getJob().getMaxTimeInVehicle()) {
|
||||||
|
return ConstraintsStatus.NOT_FULFILLED;
|
||||||
|
}
|
||||||
if(newAct instanceof DeliveryActivity){
|
if(newAct instanceof DeliveryActivity){
|
||||||
double pickupEnd;
|
double pickupEnd;
|
||||||
if(iFacts.getAssociatedActivities().size() == 1){
|
if(iFacts.getAssociatedActivities().size() == 1){
|
||||||
|
|
@ -107,10 +107,9 @@ public class MaxTimeInVehicleConstraint implements HardActivityConstraint {
|
||||||
}
|
}
|
||||||
double directArrTimeNextAct = prevActDepTime + transportTime.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), prevActDepTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
double directArrTimeNextAct = prevActDepTime + transportTime.getTransportTime(prevAct.getLocation(), nextAct.getLocation(), prevActDepTime, iFacts.getNewDriver(), iFacts.getNewVehicle());
|
||||||
double directNextActStart = Math.max(directArrTimeNextAct, nextAct.getTheoreticalEarliestOperationStartTime());
|
double directNextActStart = Math.max(directArrTimeNextAct, nextAct.getTheoreticalEarliestOperationStartTime());
|
||||||
double additionalTimeOfNewAct = (nextActStart - prevActDepTime) - (directNextActStart - prevActDepTime);
|
double additionalTimeOfNewAct = nextActStart - directNextActStart;
|
||||||
if (additionalTimeOfNewAct > minSlack) {
|
if (additionalTimeOfNewAct > minSlack) {
|
||||||
if (newActIsPickup) return ConstraintsStatus.NOT_FULFILLED;
|
return ConstraintsStatus.NOT_FULFILLED;
|
||||||
else return ConstraintsStatus.NOT_FULFILLED;
|
|
||||||
}
|
}
|
||||||
if (newActIsDelivery) {
|
if (newActIsDelivery) {
|
||||||
Map<Job, Double> openJobsAtNext;
|
Map<Job, Double> openJobsAtNext;
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,8 @@ import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class MaxTimeInVehicleTest {
|
public class MaxTimeInVehicleTest {
|
||||||
|
|
||||||
|
Random RANDOM = new Random();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testShipment(){
|
public void testShipment(){
|
||||||
Shipment s1 = Shipment.Builder.newInstance("s1").setPickupLocation(Location.newInstance(34.773586,32.079754)).setDeliveryLocation(Location.newInstance(34.781247,38.294571))
|
Shipment s1 = Shipment.Builder.newInstance("s1").setPickupLocation(Location.newInstance(34.773586,32.079754)).setDeliveryLocation(Location.newInstance(34.781247,38.294571))
|
||||||
|
|
@ -73,9 +75,6 @@ public class MaxTimeInVehicleTest {
|
||||||
SolutionPrinter.print(vrp,solution, SolutionPrinter.Print.VERBOSE);
|
SolutionPrinter.print(vrp,solution, SolutionPrinter.Print.VERBOSE);
|
||||||
assertEquals(0,solution.getUnassignedJobs().size());
|
assertEquals(0,solution.getUnassignedJobs().size());
|
||||||
assertEquals(1,solution.getRoutes().size());
|
assertEquals(1,solution.getRoutes().size());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -307,8 +306,6 @@ public class MaxTimeInVehicleTest {
|
||||||
assertEquals(0,solution.getUnassignedJobs().size());
|
assertEquals(0,solution.getUnassignedJobs().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
Random RANDOM = new Random();
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRouteTwoDriversTwoRouts_NotAllAssigned() {
|
public void testRouteTwoDriversTwoRouts_NotAllAssigned() {
|
||||||
int numJobs = Math.abs(RANDOM.nextInt(100)) + 10,
|
int numJobs = Math.abs(RANDOM.nextInt(100)) + 10,
|
||||||
|
|
@ -334,7 +331,7 @@ public class MaxTimeInVehicleTest {
|
||||||
.setLocation(Location.newInstance(RANDOM.nextDouble(), RANDOM.nextDouble())).setServiceTime(serviceTime).build());
|
.setLocation(Location.newInstance(RANDOM.nextDouble(), RANDOM.nextDouble())).setServiceTime(serviceTime).build());
|
||||||
}
|
}
|
||||||
|
|
||||||
VehicleRoutingProblemSolution solution = getVehicleRoutingProblemSolution(builder);
|
VehicleRoutingProblemSolution solution = getVehicleRoutingProblemSolution(builder, true, 4);
|
||||||
|
|
||||||
assertFalse(solution.getRoutes().isEmpty());
|
assertFalse(solution.getRoutes().isEmpty());
|
||||||
final Iterator<VehicleRoute> iterator = solution.getRoutes().iterator();
|
final Iterator<VehicleRoute> iterator = solution.getRoutes().iterator();
|
||||||
|
|
@ -383,7 +380,7 @@ public class MaxTimeInVehicleTest {
|
||||||
.setLocation(Location.newInstance(RANDOM.nextDouble(), RANDOM.nextDouble())).setServiceTime(serviceTime).build());
|
.setLocation(Location.newInstance(RANDOM.nextDouble(), RANDOM.nextDouble())).setServiceTime(serviceTime).build());
|
||||||
}
|
}
|
||||||
|
|
||||||
VehicleRoutingProblemSolution solution = getVehicleRoutingProblemSolution(builder);
|
VehicleRoutingProblemSolution solution = getVehicleRoutingProblemSolution(builder, true, 4);
|
||||||
|
|
||||||
final Iterator<VehicleRoute> iterator = solution.getRoutes().iterator();
|
final Iterator<VehicleRoute> iterator = solution.getRoutes().iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
|
|
@ -396,9 +393,38 @@ public class MaxTimeInVehicleTest {
|
||||||
assertTrue(solution.getUnassignedJobs().isEmpty());
|
assertTrue(solution.getUnassignedJobs().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
private VehicleRoutingProblemSolution getVehicleRoutingProblemSolution(VehicleRoutingProblem.Builder builder) {
|
@Test
|
||||||
VehicleRoutingProblem vrp = builder
|
public void testLowMaxTimeCauseTwoRoutes(){
|
||||||
.setRoutingCost(new VehicleRoutingTransportCosts() {
|
Shipment s1 = Shipment.Builder.newInstance("s1").setPickupLocation(Location.newInstance(8,0))
|
||||||
|
.setDeliveryLocation(Location.newInstance(10,0))
|
||||||
|
.setDeliveryServiceTime(2)
|
||||||
|
.setMaxTimeInVehicle(10)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Delivery d2 = Delivery.Builder.newInstance("d2")
|
||||||
|
.setMaxTimeInVehicle(13)
|
||||||
|
.setLocation(Location.newInstance(10, 5)).setServiceTime(2).build();
|
||||||
|
|
||||||
|
VehicleImpl v1 = VehicleImpl.Builder.newInstance("v1")
|
||||||
|
.setStartLocation(Location.newInstance(8,5)).setReturnToDepot(true).build();
|
||||||
|
|
||||||
|
VehicleImpl v2 = VehicleImpl.Builder.newInstance("v2")
|
||||||
|
.setStartLocation(Location.newInstance(5,0)).setReturnToDepot(true).build();
|
||||||
|
|
||||||
|
|
||||||
|
final VehicleRoutingProblemSolution solution = getVehicleRoutingProblemSolution(VehicleRoutingProblem.Builder.newInstance()
|
||||||
|
.addVehicle(v1)
|
||||||
|
.addVehicle(v2)
|
||||||
|
.addJob(s1)
|
||||||
|
.addJob(d2), false, 4);
|
||||||
|
assertEquals(2, solution.getRoutes().size());
|
||||||
|
assertEquals(0, solution.getUnassignedJobs().size());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private VehicleRoutingProblemSolution getVehicleRoutingProblemSolution(VehicleRoutingProblem.Builder builder, boolean routeCost, int numThreads) {
|
||||||
|
if (routeCost) {
|
||||||
|
builder.setRoutingCost(new VehicleRoutingTransportCosts() {
|
||||||
@Override
|
@Override
|
||||||
public double getBackwardTransportCost(Location location, Location location1, double v, Driver driver, Vehicle vehicle) {
|
public double getBackwardTransportCost(Location location, Location location1, double v, Driver driver, Vehicle vehicle) {
|
||||||
return 1;
|
return 1;
|
||||||
|
|
@ -423,7 +449,9 @@ public class MaxTimeInVehicleTest {
|
||||||
public double getDistance(Location location, Location location1, double v, Vehicle vehicle) {
|
public double getDistance(Location location, Location location1, double v, Vehicle vehicle) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
}
|
||||||
|
VehicleRoutingProblem vrp = builder
|
||||||
.setFleetSize(VehicleRoutingProblem.FleetSize.FINITE)
|
.setFleetSize(VehicleRoutingProblem.FleetSize.FINITE)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|
@ -435,7 +463,7 @@ public class MaxTimeInVehicleTest {
|
||||||
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
|
ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
|
||||||
constraintManager.addConstraint(new MaxTimeInVehicleConstraint(vrp.getTransportCosts(), vrp.getActivityCosts(), id, stateManager, vrp, openJobsId), ConstraintManager.Priority.CRITICAL);
|
constraintManager.addConstraint(new MaxTimeInVehicleConstraint(vrp.getTransportCosts(), vrp.getActivityCosts(), id, stateManager, vrp, openJobsId), ConstraintManager.Priority.CRITICAL);
|
||||||
|
|
||||||
VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setStateAndConstraintManager(stateManager,constraintManager).buildAlgorithm();
|
VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setStateAndConstraintManager(stateManager,constraintManager).setProperty(Jsprit.Parameter.THREADS, String.valueOf(numThreads)).buildAlgorithm();
|
||||||
vra.setMaxIterations(100);
|
vra.setMaxIterations(100);
|
||||||
VehicleRoutingProblemSolution solution = Solutions.bestOf(vra.searchSolutions());
|
VehicleRoutingProblemSolution solution = Solutions.bestOf(vra.searchSolutions());
|
||||||
SolutionPrinter.print(vrp,solution, SolutionPrinter.Print.VERBOSE);
|
SolutionPrinter.print(vrp,solution, SolutionPrinter.Print.VERBOSE);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue