我通过使用<代码>J Sprit.setObjectiveFunction()而不是习惯限制来解决这一问题。 执行起来要简单得多。
public double getCosts( VehicleRoutingProblemSolution solution ) {
double costPerPassengerSecond = 0.001;
double costs = 0.0;
int passengersInVehicle = 0;
for ( VehicleRoute route : solution.getRoutes() ) {
costs += route.getVehicle().getType().getVehicleCostParams().fix;
TourActivity prevAct = route.getStart();
for ( TourActivity act : route.getActivities() ) {
costs += vrp.getTransportCosts().getTransportCost(prevAct.getLocation(), act.getLocation(), prevAct.getEndTime(), route.getDriver(), route.getVehicle());
//costs += vrp.getActivityCosts().getActivityCost(act, act.getArrTime(), route.getDriver(), route.getVehicle()); // there are no activity costs, because CostPerServiceTime is zero
costs += costPerPassengerSecond * passengersInVehicle * vrp.getTransportCosts().getTransportTime(prevAct.getLocation(), act.getLocation(), prevAct.getEndTime(), route.getDriver(), route.getVehicle());
passengersInVehicle += act.getSize().get(0);
}
costs += vrp.getTransportCosts().getTransportCost(prevAct.getLocation(), route.getEnd().getLocation(), prevAct.getEndTime(), route.getDriver(), route.getVehicle());
//if ( route.getVehicle().getBreak() != null && !hasBreak && route.getEnd().getArrTime() > route.getVehicle().getBreak().getTimeWindow().getEnd() ) {
// costs += 4.0 * (maxCosts * 2.0 + route.getVehicle().getBreak().getServiceDuration() * route.getVehicle().getType().getVehicleCostParams().perServiceTimeUnit);
//}
}
return costs;
}