如何解决最大限度地减少所有路线的车辆乘客时间
我想尽量减少乘客在车上的时间。
如果在 LEFT
和 CENTER
有上客车并且都要去 BottOM
,那么车辆应该先上车 LEFT
,因为 {{1 }} 正在前往 CENTER
的路上。
我如何告诉 jsprit 优先处理这个问题?
我尝试使用这个简单的例子来实现一个 RouteConstraint,它惩罚车辆中的总时间,从 BottOM
到 pickupShipment
:
deliverShipment
这给出了下面的结果,在离开
import com.graphhopper.jsprit.core.algorithm.VehicleRoutingalgorithm;
import com.graphhopper.jsprit.core.algorithm.Box.jsprit;
import com.graphhopper.jsprit.core.algorithm.selector.SelectBest;
import com.graphhopper.jsprit.core.algorithm.state.StateManager;
import com.graphhopper.jsprit.core.problem.Location;
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem;
import com.graphhopper.jsprit.core.problem.constraint.ConstraintManager;
import com.graphhopper.jsprit.core.problem.constraint.softRouteConstraint;
import com.graphhopper.jsprit.core.problem.job.Shipment;
import com.graphhopper.jsprit.core.problem.misc.JobInsertionContext;
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TimeWindow;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TourActivity;
import com.graphhopper.jsprit.core.problem.vehicle.VehicleImpl;
import com.graphhopper.jsprit.core.reporting.solutionPrinter;
import junit.framework.TestCase;
import java.util.LinkedList;
public class PenalizeExtraTimeInVehicleTest extends TestCase {
final Location LEFT = Location.newInstance(10.0,50.0);
final Location CENTER = Location.newInstance(50.0,50.0);
final Location BottOM = Location.newInstance(50.0,90.0);
public void testPickUpWithoutWastingTime() {
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance()
.setFleetSize(VehicleRoutingProblem.FleetSize.FINITE)
.addVehicle(VehicleImpl.Builder.newInstance("V-1")
.setStartLocation(BottOM)
.build())
.addJob(Shipment.Builder.newInstance("Center_to_Bot")
.setPickupTimeWindow(TimeWindow.newInstance(100.0,200.0))
.setDeliveryTimeWindow(TimeWindow.newInstance(200.0,300.0))
.setPickupLocation(CENTER)
.setDeliveryLocation(BottOM)
.build())
.addJob(Shipment.Builder.newInstance("Left_to_Bot1") // if this doesn't end with "1" it will be picked up first
.setPickupTimeWindow(TimeWindow.newInstance(100.0,300.0))
.setPickupLocation(LEFT)
.setDeliveryLocation(BottOM)
.build())
.build();
final StateManager stateManager = new StateManager(vrp);
final ConstraintManager constraintManager = new ConstraintManager(vrp,stateManager);
constraintManager.addConstraint(new SoftRouteConstraint() {
/**
* Penalize for total time in vehicle for all passengers in a route.
*/
@Override
public double getCosts( JobInsertionContext insertionContext ) {
LinkedList<Double> stack = new LinkedList<>();
double timeInVehicle = 0.0;
for ( TourActivity eachAct : insertionContext.getRoute().getActivities() ) {
if ( "pickupShipment".equals(eachAct.getName())) {
stack.push(eachAct.getEndTime());
} else if ("deliverShipment".equals(eachAct.getName())) {
final Double popped = stack.pop();
timeInVehicle += eachAct.getArrTime() - popped;
}
}
return timeInVehicle;
}
});
final VehicleRoutingalgorithm algorithm = jsprit.Builder.newInstance(vrp)
.setStateAndConstraintManager(stateManager,constraintManager)
.buildAlgorithm();
VehicleRoutingProblemSolution solution = new SelectBest().selectSolution(algorithm.searchSolutions());
SolutionPrinter.print(vrp,solution,SolutionPrinter.Print.VERBOSE);
final TourActivity firstActivity = solution.getRoutes().iterator().next().getActivities().get(0);
assertEquals("Left_to_Bot",((TourActivity.JobActivity) firstActivity).getJob().getId());
}
}
之前顽固地选择了 center
。
另外值得注意的是,left
会影响结果。如果我将 Job.id
重命名为 Left_to_Bot1
,它将首先被拾取。
Left_to_Bot
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。