1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

rem penalty vehicle stuff

This commit is contained in:
oblonski 2014-12-12 08:13:10 +01:00
parent 69b23e9da4
commit 9938a6a123
6 changed files with 53 additions and 281 deletions

View file

@ -290,17 +290,13 @@ public class JobInsertionCostsCalculatorBuilder {
ServiceInsertionCalculator serviceInsertion = new ServiceInsertionCalculator(vrp.getTransportCosts(), actInsertionCalc, constraintManager);
serviceInsertion.setJobActivityFactory(activityFactory);
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
JobCalculatorSwitcher switcher = new JobCalculatorSwitcher();
switcher.put(Shipment.class, shipmentInsertion);
switcher.put(Service.class, serviceInsertion);
switcher.put(Pickup.class, serviceInsertion);
switcher.put(Delivery.class, serviceInsertion);
PenalyzeInsertionCostsWithPenaltyVehicle penalyzeInsertionCosts = new PenalyzeInsertionCostsWithPenaltyVehicle(switcher);
CalculatorPlusListeners calcPlusListeners = new CalculatorPlusListeners(penalyzeInsertionCosts);
return calcPlusListeners;
return new CalculatorPlusListeners(switcher);
}
private CalculatorPlusListeners createCalculatorConsideringFixedCosts(VehicleRoutingProblem vrp, JobInsertionCostsCalculator baseCalculator, RouteAndActivityStateGetter activityStates2, double weightOfFixedCosts){
@ -344,11 +340,7 @@ public class JobInsertionCostsCalculatorBuilder {
return vrp.copyAndGetActivities(job);
}
});
PenalyzeInsertionCostsWithPenaltyVehicle penalyzeInsertionCosts = new PenalyzeInsertionCostsWithPenaltyVehicle(jobInsertionCalculator);
CalculatorPlusListeners calcPlusListener = new CalculatorPlusListeners(penalyzeInsertionCosts);
return calcPlusListener;
return new CalculatorPlusListeners(jobInsertionCalculator);
}
private JobInsertionCostsCalculator createFinalInsertion(VehicleFleetManager fleetManager, JobInsertionCostsCalculator baseCalc, RouteAndActivityStateGetter activityStates2){

View file

@ -26,7 +26,10 @@ import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.DefaultShipmentActivityFactory;
import jsprit.core.problem.solution.route.activity.DefaultTourActivityFactory;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.*;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleType;
import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.problem.vehicle.VehicleTypeKey;
import jsprit.core.util.Coordinate;
import jsprit.core.util.CrowFlyCosts;
import jsprit.core.util.Locations;
@ -119,12 +122,6 @@ public class VehicleRoutingProblem {
};
private boolean addPenaltyVehicles = false;
private double penaltyFactor = 1.0;
private Double penaltyFixedCosts = null;
private int jobIndexCounter = 1;
private int vehicleIndexCounter = 1;
@ -141,25 +138,6 @@ public class VehicleRoutingProblem {
private final DefaultTourActivityFactory serviceActivityFactory = new DefaultTourActivityFactory();
/**
* Create a location (i.e. coordinate) and returns the key of the location which is Coordinate.toString().
*
* @param x x-coordinate of location
* @param y y-coordinate of location
* @return locationId
* @see Coordinate
*/
@SuppressWarnings("UnusedDeclaration")
@Deprecated
public String createLocation(double x, double y){
Coordinate coordinate = new Coordinate(x, y);
String id = coordinate.toString();
if(!tentative_coordinates.containsKey(id)){
tentative_coordinates.put(id, coordinate);
}
return id;
}
private void incJobIndexCounter(){
jobIndexCounter++;
}
@ -426,56 +404,14 @@ public class VehicleRoutingProblem {
logger.warn("set routing costs crowFlyDistance.");
transportCosts = new CrowFlyCosts(getLocations());
}
if(addPenaltyVehicles){
if(fleetSize.equals(FleetSize.INFINITE)){
logger.warn("penaltyType and FleetSize.INFINITE does not make sense. thus no penalty-types are added.");
}
else{
addPenaltyVehicles();
}
}
for(Job job : tentativeJobs.values())
for(Job job : tentativeJobs.values())
if (!jobsInInitialRoutes.contains(job.getId())) {
addJobToFinalJobMapAndCreateActivities(job);
}
return new VehicleRoutingProblem(this);
}
private void addPenaltyVehicles() {
Set<VehicleTypeKey> vehicleTypeKeys = new HashSet<VehicleTypeKey>();
List<Vehicle> uniqueVehicles = new ArrayList<Vehicle>();
for(Vehicle v : this.uniqueVehicles){
VehicleTypeKey key = new VehicleTypeKey(v.getType().getTypeId(),v.getStartLocationId(),v.getEndLocationId(),v.getEarliestDeparture(),v.getLatestArrival(), v.getSkills());
if(!vehicleTypeKeys.contains(key)){
uniqueVehicles.add(v);
vehicleTypeKeys.add(key);
}
}
for(Vehicle v : uniqueVehicles){
double fixed = v.getType().getVehicleCostParams().fix * penaltyFactor;
if(penaltyFixedCosts!=null){
fixed = penaltyFixedCosts;
}
VehicleTypeImpl t = VehicleTypeImpl.Builder.newInstance(v.getType().getTypeId())
.setCostPerDistance(penaltyFactor*v.getType().getVehicleCostParams().perDistanceUnit)
.setCostPerTime(penaltyFactor*v.getType().getVehicleCostParams().perTimeUnit)
.setFixedCost(fixed)
.setCapacityDimensions(v.getType().getCapacityDimensions())
.build();
PenaltyVehicleType penType = new PenaltyVehicleType(t,penaltyFactor);
String vehicleId = v.getId();
VehicleImpl penVehicle = VehicleImpl.Builder.newInstance(vehicleId).setEarliestStart(v.getEarliestDeparture())
.setLatestArrival(v.getLatestArrival()).setStartLocationCoordinate(v.getStartLocationCoordinate()).setStartLocationId(v.getStartLocationId())
.setEndLocationId(v.getEndLocationId()).setEndLocationCoordinate(v.getEndLocationCoordinate())
.addSkills(v.getSkills())
.setReturnToDepot(v.isReturnToDepot()).setType(penType).build();
addVehicle(penVehicle);
}
}
@SuppressWarnings("UnusedDeclaration")
@SuppressWarnings("UnusedDeclaration")
public Builder addLocation(String locationId, Coordinate coordinate) {
tentative_coordinates.put(locationId, coordinate);
return this;
@ -527,46 +463,7 @@ public class VehicleRoutingProblem {
return Collections.unmodifiableCollection(vehicleTypes);
}
/**
* Adds penaltyVehicles, i.e. for every unique vehicle-location and type combination a penalty-vehicle is constructed having penaltyFactor times higher fixed and variable costs
* (see .addPenaltyVehicles(double penaltyFactor, double penaltyFixedCosts) if fixed costs = 0.0).
*
* <p>This only makes sense for FleetSize.FINITE. Thus, penaltyVehicles are only added if is FleetSize.FINITE.
* <p>The id of penaltyVehicles is constructed as follows vehicleId = "penaltyVehicle" + "_" + {locationId} + "_" + {typeId}.
* <p>By default: no penalty-vehicles are added
*
* @param penaltyFactor penaltyFactor of penaltyVehicle
* @return this builder
* @deprecated since 1.3.2-SNAPSHOT bad job list replaces penalty vehicles
*/
@Deprecated
public Builder addPenaltyVehicles(double penaltyFactor){
this.addPenaltyVehicles = true;
this.penaltyFactor = penaltyFactor;
return this;
}
/**
* Adds penaltyVehicles, i.e. for every unique vehicle-location and type combination a penalty-vehicle is constructed having penaltyFactor times higher fixed and variable costs.
* <p>This method takes penaltyFixedCosts as absolute value in contrary to the method without penaltyFixedCosts where fixedCosts is the product of penaltyFactor and typeFixedCosts.
* <p>This only makes sense for FleetSize.FINITE. Thus, penaltyVehicles are only added if is FleetSize.FINITE.
* <p>The id of penaltyVehicles is constructed as follows vehicleId = "penaltyVehicle" + "_" + {locationId} + "_" + {typeId}.
* <p>By default: no penalty-vehicles are added
*
* @param penaltyFactor the penaltyFactor of penaltyVehicle
* @param penaltyFixedCosts which is an absolute penaltyValue (in contrary to penaltyFactor)
* @return this builder
* @deprecated since 1.3.2-SNAPSHOT bad job list replaces penalty vehicles
*/
@Deprecated
public Builder addPenaltyVehicles(double penaltyFactor, double penaltyFixedCosts){
this.addPenaltyVehicles = true;
this.penaltyFactor = penaltyFactor;
this.penaltyFixedCosts = penaltyFixedCosts;
return this;
}
/**
/**
* Returns an unmodifiable collection of already added jobs.
*
* @return collection of jobs

View file

@ -1,3 +1,20 @@
/*******************************************************************************
* 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
* 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/>.
******************************************************************************/
package jsprit.core.algorithm.state;
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
@ -57,7 +74,6 @@ public class SolomonSkills_IT {
else skillServiceBuilder.addRequiredSkill("skill1");
skillProblemBuilder.addJob(skillServiceBuilder.build());
}
skillProblemBuilder.addPenaltyVehicles(3.);
skillProblemBuilder.setFleetSize(VehicleRoutingProblem.FleetSize.FINITE);
VehicleRoutingProblem skillProblem = skillProblemBuilder.build();

View file

@ -27,7 +27,10 @@ import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.solution.route.VehicleRoute;
import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.vehicle.*;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType;
import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.util.Coordinate;
import org.junit.Test;
@ -317,153 +320,6 @@ public class VehicleRoutingProblemTest {
}
@Test
public void whenSettingAddPenaltyVehicleOptions_itShouldAddPenaltyVehicle(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build();
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build();
builder.addVehicle(vehicle);
builder.setFleetSize(FleetSize.FINITE);
builder.addPenaltyVehicles(3.0);
VehicleRoutingProblem vrp = builder.build();
assertEquals(2,vrp.getVehicles().size());
boolean penaltyVehicleInCollection = false;
for(Vehicle v : vrp.getVehicles()){
if(v.getType() instanceof PenaltyVehicleType) penaltyVehicleInCollection = true;
}
assertTrue(penaltyVehicleInCollection);
}
@Test
public void whenSettingAddPenaltyVehicleOptionsAndFleetSizeIsInfinite_noPenaltyVehicleIsAdded(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build();
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build();
builder.addVehicle(vehicle);
builder.addPenaltyVehicles(3.0);
VehicleRoutingProblem vrp = builder.build();
assertEquals(1,vrp.getVehicles().size());
boolean penaltyVehicleInCollection = false;
for(Vehicle v : vrp.getVehicles()){
if(v.getType() instanceof PenaltyVehicleType) penaltyVehicleInCollection = true;
}
assertFalse(penaltyVehicleInCollection);
}
@Test
public void whenSettingAddPenaltyVehicleOptionsAndTwoVehiclesWithSameLocationAndType_onlyOnePenaltyVehicleIsAdded(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build();
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build();
VehicleImpl vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("loc").setType(type).build();
builder.addVehicle(vehicle);
builder.addVehicle(vehicle2);
builder.setFleetSize(FleetSize.FINITE);
builder.addPenaltyVehicles(3.0);
VehicleRoutingProblem vrp = builder.build();
assertEquals(3,vrp.getVehicles().size());
boolean penaltyVehicleInCollection = false;
for(Vehicle v : vrp.getVehicles()){
if(v.getType() instanceof PenaltyVehicleType) penaltyVehicleInCollection = true;
}
assertTrue(penaltyVehicleInCollection);
}
@Test
public void whenSettingAddPenaltyVehicleOptionsWithAbsoluteFixedCostsAndTwoVehiclesWithSameLocationAndType_onePenaltyVehicleIsAddedWithTheCorrectPenaltyFixedCosts(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build();
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build();
VehicleImpl vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("loc").setType(type).build();
builder.addVehicle(vehicle);
builder.addVehicle(vehicle2);
builder.setFleetSize(FleetSize.FINITE);
builder.addPenaltyVehicles(3.0,10000);
VehicleRoutingProblem vrp = builder.build();
assertEquals(3,vrp.getVehicles().size());
double fix = 0.0;
for(Vehicle v : vrp.getVehicles()){
if(v.getType() instanceof PenaltyVehicleType) {
fix = v.getType().getVehicleCostParams().fix;
}
}
assertEquals(10000,fix,0.01);
}
@Test
public void whenSettingAddPenaltyVehicleOptionsAndTwoVehiclesWithDiffLocationAndType_twoPenaltyVehicleIsAdded(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build();
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build();
VehicleImpl vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("loc2").setType(type).build();
builder.addVehicle(vehicle);
builder.addVehicle(vehicle2);
builder.setFleetSize(FleetSize.FINITE);
builder.addPenaltyVehicles(3.0);
VehicleRoutingProblem vrp = builder.build();
assertEquals(4,vrp.getVehicles().size());
int countPenaltyVehicles = 0;
for(Vehicle v : vrp.getVehicles()){
if(v.getType() instanceof PenaltyVehicleType) {
countPenaltyVehicles++;
}
}
assertEquals(2,countPenaltyVehicles);
}
@Test
public void whenSettingAddPenaltyVehicleOptionsAndTwoVehiclesWithSameLocationButDiffType_twoPenaltyVehicleIsAdded(){
VehicleRoutingProblem.Builder builder = VehicleRoutingProblem.Builder.newInstance();
VehicleType type = VehicleTypeImpl.Builder.newInstance("type").build();
VehicleType type2 = VehicleTypeImpl.Builder.newInstance("type2").build();
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("v").setStartLocationId("loc").setType(type).build();
VehicleImpl vehicle2 = VehicleImpl.Builder.newInstance("v2").setStartLocationId("loc").setType(type2).build();
builder.addVehicle(vehicle);
builder.addVehicle(vehicle2);
builder.setFleetSize(FleetSize.FINITE);
builder.addPenaltyVehicles(3.0);
VehicleRoutingProblem vrp = builder.build();
assertEquals(4,vrp.getVehicles().size());
int countPenaltyVehicles = 0;
for(Vehicle v : vrp.getVehicles()){
if(v.getType() instanceof PenaltyVehicleType) {
countPenaltyVehicles++;
}
}
assertEquals(2,countPenaltyVehicles);
}
@Test
public void whenAddingVehicleWithDiffStartAndEnd_startLocationMustBeRegisteredInLocationMap(){

View file

@ -38,7 +38,7 @@ import java.util.Arrays;
import java.util.Collection;
public class MultipleDepotExampleWithPenaltyVehicles {
public class MultipleDepotExample2 {
public static void main(String[] args) {

View file

@ -1,10 +1,21 @@
package jsprit.instance.reader;
/*******************************************************************************
* 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
* 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/>.
******************************************************************************/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
package jsprit.instance.reader;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.VehicleRoutingProblem.Builder;
@ -16,6 +27,8 @@ import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.util.Coordinate;
import java.io.*;
/**
* Reads modified files from Taillard's website
* http://mistic.heig-vd.ch/taillard/problemes.dir/vrp.dir/vrp.html. You can find the modified version here:
@ -108,7 +121,6 @@ public class VrphGoldenReader {
typeBuilder.setCostPerDistance(Double.parseDouble(tokens[4]));
nuOfVehicles = Integer.parseInt(tokens[5]);
vrpBuilder.setFleetSize(FleetSize.FINITE);
vrpBuilder.addPenaltyVehicles(5.0, 5000);
}
else throw new IllegalStateException("option " + vrphType + " cannot be applied with this instance");
}
@ -118,7 +130,6 @@ public class VrphGoldenReader {
typeBuilder.setCostPerDistance(Double.parseDouble(tokens[4]));
nuOfVehicles = Integer.parseInt(tokens[5]);
vrpBuilder.setFleetSize(FleetSize.FINITE);
vrpBuilder.addPenaltyVehicles(5.0, 5000);
}
else throw new IllegalStateException("option " + vrphType + " cannot be applied with this instance");
}