Skip to content

Commit

Permalink
add feature #127
Browse files Browse the repository at this point in the history
  • Loading branch information
oblonski committed Sep 4, 2014
1 parent afe98c1 commit d80f6bc
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*******************************************************************************
* Copyright (C) 2013 Stefan Schroeder
*
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
Expand Down Expand Up @@ -88,20 +88,29 @@ public String toString() {
@Override
public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle newVehicle, double newVehicleDepartureTime, final Driver newDriver, final double bestKnownCosts) {
JobInsertionContext insertionContext = new JobInsertionContext(currentRoute, jobToInsert, newVehicle, newDriver, newVehicleDepartureTime);
if(!hardRouteLevelConstraint.fulfilled(insertionContext)){
Service service = (Service)jobToInsert;
int insertionIndex = InsertionData.NO_INDEX;
TourActivity deliveryAct2Insert = activityFactory.createActivities(service).get(0);
insertionContext.getAssociatedActivities().add(deliveryAct2Insert);

/*
check hard constraints at route level
*/
if(!hardRouteLevelConstraint.fulfilled(insertionContext)){
return InsertionData.createEmptyInsertionData();
}

/*
check soft constraints at route level
*/
double additionalICostsAtRouteLevel = softRouteConstraint.getCosts(insertionContext);

double bestCost = bestKnownCosts;

//from job2insert induced costs at route level
double additionalICostsAtRouteLevel = softRouteConstraint.getCosts(insertionContext);
additionalICostsAtRouteLevel += additionalAccessEgressCalculator.getCosts(insertionContext);

Service service = (Service)jobToInsert;
int insertionIndex = InsertionData.NO_INDEX;

TourActivity deliveryAct2Insert = activityFactory.createActivities(service).get(0);

additionalICostsAtRouteLevel += additionalAccessEgressCalculator.getCosts(insertionContext);

/*
generate new start and end for new vehicle
*/
Start start = Start.newInstance(newVehicle.getStartLocationId(), newVehicle.getEarliestDeparture(), Double.MAX_VALUE);
start.setEndTime(newVehicleDepartureTime);
End end = End.newInstance(newVehicle.getEndLocationId(), 0.0, newVehicle.getLatestArrival());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*******************************************************************************
* Copyright (C) 2013 Stefan Schroeder
*
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
Expand All @@ -29,7 +29,6 @@
import jsprit.core.problem.solution.route.activity.Start;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl.NoVehicle;
import jsprit.core.util.CalculationUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -87,32 +86,37 @@ public String toString() {
*/
@Override
public InsertionData getInsertionData(final VehicleRoute currentRoute, final Job jobToInsert, final Vehicle newVehicle, double newVehicleDepartureTime, final Driver newDriver, final double bestKnownCosts) {
if(jobToInsert == null) throw new IllegalStateException("jobToInsert is missing.");
if(newVehicle == null || newVehicle instanceof NoVehicle) throw new IllegalStateException("newVehicle is missing.");
if(!(jobToInsert instanceof Shipment)) throw new IllegalStateException("jobToInsert should be of type Shipment!");

JobInsertionContext insertionContext = new JobInsertionContext(currentRoute, jobToInsert, newVehicle, newDriver, newVehicleDepartureTime);
if(!hardRouteLevelConstraint.fulfilled(insertionContext)){
JobInsertionContext insertionContext = new JobInsertionContext(currentRoute, jobToInsert, newVehicle, newDriver, newVehicleDepartureTime);
Shipment shipment = (Shipment)jobToInsert;
TourActivity pickupShipment = activityFactory.createActivities(shipment).get(0);
TourActivity deliverShipment = activityFactory.createActivities(shipment).get(1);
insertionContext.getAssociatedActivities().add(pickupShipment);
insertionContext.getAssociatedActivities().add(deliverShipment);

/*
check hard route constraints
*/
if(!hardRouteLevelConstraint.fulfilled(insertionContext)){
return InsertionData.createEmptyInsertionData();
}

double bestCost = bestKnownCosts;

double additionalICostsAtRouteLevel = softRouteConstraint.getCosts(insertionContext);
additionalICostsAtRouteLevel += additionalAccessEgressCalculator.getCosts(insertionContext);

Shipment shipment = (Shipment)jobToInsert;
TourActivity pickupShipment = activityFactory.createActivities(shipment).get(0);
TourActivity deliverShipment = activityFactory.createActivities(shipment).get(1);

int pickupInsertionIndex = InsertionData.NO_INDEX;
/*
check soft route constraints
*/
double additionalICostsAtRouteLevel = softRouteConstraint.getCosts(insertionContext);

double bestCost = bestKnownCosts;
additionalICostsAtRouteLevel += additionalAccessEgressCalculator.getCosts(insertionContext);

int pickupInsertionIndex = InsertionData.NO_INDEX;
int deliveryInsertionIndex = InsertionData.NO_INDEX;

Start start = Start.newInstance(newVehicle.getStartLocationId(), newVehicle.getEarliestDeparture(), newVehicle.getLatestArrival());
start.setEndTime(newVehicleDepartureTime);

End end = End.newInstance(newVehicle.getEndLocationId(), 0.0, newVehicle.getLatestArrival());


JobInsertionContext.ActivityContext pickupContext = new JobInsertionContext.ActivityContext();

TourActivity prevAct = start;
double prevActEndTime = newVehicleDepartureTime;
boolean pickupShipmentLoopBroken = false;
Expand All @@ -135,7 +139,13 @@ else if(pickupShipmentConstraintStatus.equals(ConstraintsStatus.NOT_FULFILLED_BR
TourActivity prevAct_deliveryLoop = pickupShipment;
double shipmentPickupArrTime = prevActEndTime + transportCosts.getTransportTime(prevAct.getLocationId(), pickupShipment.getLocationId(), prevActEndTime, newDriver, newVehicle);
double shipmentPickupEndTime = CalculationUtils.getActivityEndTime(shipmentPickupArrTime, pickupShipment);
double prevActEndTime_deliveryLoop = shipmentPickupEndTime;

pickupContext.setArrivalTime(shipmentPickupArrTime);
pickupContext.setEndTime(shipmentPickupEndTime);
pickupContext.setInsertionIndex(i);
insertionContext.setRelatedActivityContext(pickupContext);

double prevActEndTime_deliveryLoop = shipmentPickupEndTime;
boolean deliverShipmentLoopBroken = false;
//deliverShipmentLoop
for(int j=i;j<activities.size();j++){
Expand Down Expand Up @@ -188,8 +198,13 @@ else if(deliverShipmentConstraintStatus.equals(ConstraintsStatus.NOT_FULFILLED_B
double shipmentPickupArrTime = prevActEndTime + transportCosts.getTransportTime(prevAct.getLocationId(), pickupShipment.getLocationId(), prevActEndTime, newDriver, newVehicle);
double shipmentPickupEndTime = CalculationUtils.getActivityEndTime(shipmentPickupArrTime, pickupShipment);
double prevActEndTime_deliveryLoop = shipmentPickupEndTime;

ConstraintsStatus deliverShipmentConstraintStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct_deliveryLoop, deliverShipment, end, prevActEndTime_deliveryLoop);

pickupContext.setArrivalTime(shipmentPickupArrTime);
pickupContext.setEndTime(shipmentPickupEndTime);
pickupContext.setInsertionIndex(activities.size());
insertionContext.setRelatedActivityContext(pickupContext);

ConstraintsStatus deliverShipmentConstraintStatus = hardActivityLevelConstraint.fulfilled(insertionContext, prevAct_deliveryLoop, deliverShipment, end, prevActEndTime_deliveryLoop);
if(deliverShipmentConstraintStatus.equals(ConstraintsStatus.FULFILLED)){
double additionalDeliveryICosts = softActivityConstraint.getCosts(insertionContext, prevAct_deliveryLoop, deliverShipment, end, prevActEndTime_deliveryLoop);
double deliveryAIC = calculate(insertionContext,prevAct_deliveryLoop,deliverShipment,end,prevActEndTime_deliveryLoop);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*******************************************************************************
* Copyright (C) 2013 Stefan Schroeder
*
* Copyright (C) 2014 Stefan Schroeder
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
Expand All @@ -19,61 +19,149 @@
import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.Vehicle;

import java.util.ArrayList;
import java.util.List;

/**
* Provides context information about a particular job insertion.
*
*/
public class JobInsertionContext {

public static class ActivityContext {

private double arrivalTime;

private double endTime;

private int insertionIndex;

public double getArrivalTime() {
return arrivalTime;
}

public double getEndTime() {
return endTime;
}

public int getInsertionIndex() {
return insertionIndex;
}

public void setArrivalTime(double arrivalTime) {
this.arrivalTime = arrivalTime;
}

public void setEndTime(double endTime) {
this.endTime = endTime;
}

public void setInsertionIndex(int insertionIndex) {
this.insertionIndex = insertionIndex;
}
}

private VehicleRoute route;

private Job job;

private Vehicle newVehicle;

private Driver newDriver;

private double newDepTime;

private List<TourActivity> associatedActivities = new ArrayList<TourActivity>();

private ActivityContext pickupContext;

/**
* Returns the existing route where the .getJob() needs to be inserted in.
*
* @return the route
*/
public VehicleRoute getRoute() {
return route;
}

/**
* Returns the job that needs to be inserted.
*
* @return the job
*/
public Job getJob() {
return job;
}

/**
* Returns the vehicle that should operate the new route, i.e. route <code>this.getRoute()</code> + new job <code>this.getJob()</code>.
*
* @return the newVehicle
*/
public Vehicle getNewVehicle() {
return newVehicle;
}

/**
* Returns the driver that should operate the new route, i.e. route <code>this.getRoute()</code> + new job <code>this.getJob()</code>.
*
* <p>Currently the driver is just a mock, it has no functions</p>
*
* @return the newDriver
*/
public Driver getNewDriver() {
return newDriver;
}

/**
* Returns the new departure time at the new vehicle's start location.
*
* @return the newDepTime
*/
public double getNewDepTime() {
return newDepTime;
}

public JobInsertionContext(VehicleRoute route, Job job, Vehicle newVehicle,
Driver newDriver, double newDepTime) {
/**
* Constructs the context.
*
* @param route the existing route where the job needs to be inserted in
* @param job the job to be inserted
* @param newVehicle the new vehicle that should operate the new route
* @param newDriver the new driver that should operate the new route
* @param newDepTime the new departure time at the new vehicle's start location
*/
public JobInsertionContext(VehicleRoute route, Job job, Vehicle newVehicle, Driver newDriver, double newDepTime) {
super();
this.route = route;
this.job = job;
this.newVehicle = newVehicle;
this.newDriver = newDriver;
this.newDepTime = newDepTime;
}



public List<TourActivity> getAssociatedActivities() {
return associatedActivities;
}

/**
* Sets pickup context.
*
* @param pickupContext pickup context
*/
public void setRelatedActivityContext(ActivityContext pickupContext){
this.pickupContext = pickupContext;
}

/**
* Returns pickup context. If no context available, returns null.
*
* @return pickup context
*/
public ActivityContext getRelatedActivityContext(){
return this.pickupContext;
}
}
Loading

0 comments on commit d80f6bc

Please sign in to comment.