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

relax api

This commit is contained in:
Stefan Schroeder 2013-10-22 15:31:02 +02:00
parent ee94ac2e21
commit 1e520f3269
42 changed files with 382 additions and 948 deletions

View file

@ -63,7 +63,7 @@ public class BuildCVRPAlgoFromScratchTest {
VehicleFleetManager fleetManager = new InfiniteVehicles(vrp.getVehicles());
JobInsertionCalculator finalServiceInsertion = new CalculatesVehTypeDepServiceInsertion(fleetManager, serviceInsertion);
JobInsertionCalculator finalServiceInsertion = new VehicleTypeDependentJobInsertionCalculator(fleetManager, serviceInsertion);
BestInsertion bestInsertion = new BestInsertion(finalServiceInsertion);
@ -76,7 +76,7 @@ public class BuildCVRPAlgoFromScratchTest {
public void calculateCosts(VehicleRoutingProblemSolution solution) {
double costs = 0.0;
for(VehicleRoute route : solution.getRoutes()){
costs += stateManager.getRouteState(route, StateIdFactory.COSTS).toDouble();
costs += stateManager.getRouteState(route, StateFactory.COSTS).toDouble();
}
solution.setCost(costs);
}

View file

@ -67,7 +67,7 @@ public class BuildPDVRPAlgoFromScratchTest {
// CalculatesServiceInsertion serviceInsertion = new CalculatesServiceInsertion(vrp.getTransportCosts(), marginalCalculus, new HardConstraints.HardLoadConstraint(stateManager));
VehicleFleetManager fleetManager = new InfiniteVehicles(vrp.getVehicles());
JobInsertionCalculator finalServiceInsertion = new CalculatesVehTypeDepServiceInsertion(fleetManager, serviceInsertion);
JobInsertionCalculator finalServiceInsertion = new VehicleTypeDependentJobInsertionCalculator(fleetManager, serviceInsertion);
BestInsertion bestInsertion = new BestInsertion(finalServiceInsertion);
@ -80,7 +80,7 @@ public class BuildPDVRPAlgoFromScratchTest {
public void calculateCosts(VehicleRoutingProblemSolution solution) {
double costs = 0.0;
for(VehicleRoute route : solution.getRoutes()){
costs += stateManager.getRouteState(route, StateIdFactory.COSTS).toDouble();
costs += stateManager.getRouteState(route, StateFactory.COSTS).toDouble();
}
solution.setCost(costs);
}
@ -131,8 +131,8 @@ public class BuildPDVRPAlgoFromScratchTest {
loadAtEnd += j.getCapacityDemand();
}
}
stateManager.putRouteState(route, StateIdFactory.LOAD_AT_BEGINNING, new StateImpl(loadAtDepot));
stateManager.putRouteState(route, StateIdFactory.LOAD, new StateImpl(loadAtEnd));
stateManager.putRouteState(route, StateFactory.LOAD_AT_BEGINNING, new StateImpl(loadAtDepot));
stateManager.putRouteState(route, StateFactory.LOAD, new StateImpl(loadAtEnd));
iterateForward.visit(route);
iterateBackward.visit(route);
}
@ -149,14 +149,14 @@ public class BuildPDVRPAlgoFromScratchTest {
// log.info("insert job " + job2insert.getClass().toString() + " job " + job2insert + "" + job2insert.getCapacityDemand() + " in route " + inRoute.getTourActivities());
if(job2insert instanceof Delivery){
int loadAtDepot = (int) stateManager.getRouteState(inRoute, StateIdFactory.LOAD_AT_BEGINNING).toDouble();
int loadAtDepot = (int) stateManager.getRouteState(inRoute, StateFactory.LOAD_AT_BEGINNING).toDouble();
// log.info("loadAtDepot="+loadAtDepot);
stateManager.putRouteState(inRoute, StateIdFactory.LOAD_AT_BEGINNING, new StateImpl(loadAtDepot + job2insert.getCapacityDemand()));
stateManager.putRouteState(inRoute, StateFactory.LOAD_AT_BEGINNING, StateFactory.createState(loadAtDepot + job2insert.getCapacityDemand()));
}
if(job2insert instanceof Pickup){
int loadAtEnd = (int) stateManager.getRouteState(inRoute, StateIdFactory.LOAD).toDouble();
int loadAtEnd = (int) stateManager.getRouteState(inRoute, StateFactory.LOAD_AT_END).toDouble();
// log.info("loadAtEnd="+loadAtEnd);
stateManager.putRouteState(inRoute, StateIdFactory.LOAD, new StateImpl(loadAtEnd + job2insert.getCapacityDemand()));
stateManager.putRouteState(inRoute, StateFactory.LOAD_AT_END, StateFactory.createState(loadAtEnd + job2insert.getCapacityDemand()));
}
iterateForward.visit(inRoute);
iterateBackward.visit(inRoute);

View file

@ -77,7 +77,7 @@ public class CalcVehicleTypeDependentServiceInsertionTest {
when(calc.calculate(vehicleRoute, service, veh1, veh1.getEarliestDeparture(), null, Double.MAX_VALUE)).thenReturn(iDataVeh1);
when(calc.calculate(vehicleRoute, service, veh2, veh2.getEarliestDeparture(), null, Double.MAX_VALUE)).thenReturn(iDataVeh2);
when(calc.calculate(vehicleRoute, service, veh2, veh2.getEarliestDeparture(), null, 10.0)).thenReturn(iDataVeh2);
CalculatesVehTypeDepServiceInsertion insertion = new CalculatesVehTypeDepServiceInsertion(fleetManager,calc);
VehicleTypeDependentJobInsertionCalculator insertion = new VehicleTypeDependentJobInsertionCalculator(fleetManager,calc);
InsertionData iData = insertion.calculate(vehicleRoute, service, null, 0.0, null, Double.MAX_VALUE);
assertThat(iData.getSelectedVehicle(), is(veh1));
@ -91,7 +91,7 @@ public class CalcVehicleTypeDependentServiceInsertionTest {
when(calc.calculate(vehicleRoute, service, veh1, veh1.getEarliestDeparture(), null, Double.MAX_VALUE)).thenReturn(iDataVeh1);
when(calc.calculate(vehicleRoute, service, veh2, veh2.getEarliestDeparture(), null, Double.MAX_VALUE)).thenReturn(iDataVeh2);
when(calc.calculate(vehicleRoute, service, veh2, veh2.getEarliestDeparture(), null, 20.0)).thenReturn(iDataVeh2);
CalculatesVehTypeDepServiceInsertion insertion = new CalculatesVehTypeDepServiceInsertion(fleetManager,calc);
VehicleTypeDependentJobInsertionCalculator insertion = new VehicleTypeDependentJobInsertionCalculator(fleetManager,calc);
InsertionData iData = insertion.calculate(vehicleRoute, service, null, 0.0, null, Double.MAX_VALUE);
assertThat(iData.getSelectedVehicle(), is(veh2));

View file

@ -29,7 +29,6 @@ import org.junit.Test;
import util.Coordinate;
import util.ManhattanDistanceCalculator;
import util.RouteUtils;
import algorithms.StateUpdates.UpdateStates;
import basics.Job;
import basics.Service;
import basics.VehicleRoutingProblem;
@ -151,10 +150,10 @@ public class GendreauPostOptTest {
ServiceInsertionCalculator standardServiceInsertion = new ServiceInsertionCalculator(cost, new LocalActivityInsertionCostsCalculator(cost, activityCosts), new HardLoadConstraint(states), new HardTimeWindowActivityLevelConstraint(states, cost));
CalculatesServiceInsertionConsideringFixCost withFixCost = new CalculatesServiceInsertionConsideringFixCost(standardServiceInsertion, states);
JobInsertionConsideringFixCostsCalculator withFixCost = new JobInsertionConsideringFixCostsCalculator(standardServiceInsertion, states);
withFixCost.setWeightOfFixCost(1.2);
insertionCalc = new CalculatesVehTypeDepServiceInsertion(fleetManager, withFixCost);
insertionCalc = new VehicleTypeDependentJobInsertionCalculator(fleetManager, withFixCost);
// updater = new TourStateUpdater(states, cost, activityCosts);
@ -185,14 +184,14 @@ public class GendreauPostOptTest {
// routes.add(new VehicleRoute(getEmptyTour(),getDriver(),getNoVehicle()));
VehicleRoutingProblemSolution sol = new VehicleRoutingProblemSolution(routes, states.getRouteState(route, StateIdFactory.COSTS).toDouble() + getFixedCosts(routes));
VehicleRoutingProblemSolution sol = new VehicleRoutingProblemSolution(routes, states.getRouteState(route, StateFactory.COSTS).toDouble() + getFixedCosts(routes));
assertEquals(110.0, sol.getCost(), 0.5);
RuinRadial radialRuin = new RuinRadial(vrp, 0.2, new JobDistanceAvgCosts(vrp.getTransportCosts()));
radialRuin.addListener(stateUpdater);
// radialRuin.addListener(stateUpdater);
InsertionStrategy insertionStrategy = new BestInsertion(insertionCalc);
insertionStrategy.addListener(stateUpdater);
@ -218,7 +217,7 @@ public class GendreauPostOptTest {
double c = 0.0;
for(VehicleRoute r : newSolution.getRoutes()){
c += states.getRouteState(r, StateIdFactory.COSTS).toDouble() + r.getVehicle().getType().getVehicleCostParams().fix;
c += states.getRouteState(r, StateFactory.COSTS).toDouble() + r.getVehicle().getType().getVehicleCostParams().fix;
}
return c;

View file

@ -0,0 +1,78 @@
/*******************************************************************************
* Copyright (C) 2013 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 algorithms;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import basics.Job;
import basics.algo.InsertionStartsListener;
import basics.algo.JobInsertedListener;
import basics.costs.VehicleRoutingActivityCosts;
import basics.costs.VehicleRoutingTransportCosts;
import basics.route.VehicleRoute;
class UpdateStates implements JobInsertedListener, InsertionStartsListener{
private RouteActivityVisitor routeActivityVisitor;
private ReverseRouteActivityVisitor revRouteActivityVisitor;
private InsertionListeners insertionListeners = new InsertionListeners();
public UpdateStates(StateManager states, VehicleRoutingTransportCosts routingCosts, VehicleRoutingActivityCosts activityCosts) {
routeActivityVisitor = new RouteActivityVisitor();
routeActivityVisitor.addActivityVisitor(new UpdateActivityTimes(routingCosts));
routeActivityVisitor.addActivityVisitor(new UpdateCostsAtAllLevels(activityCosts, routingCosts, states));
routeActivityVisitor.addActivityVisitor(new UpdateLoadAtAllLevels(states));
routeActivityVisitor.addActivityVisitor(new UpdateMaxLoad(states));
revRouteActivityVisitor = new ReverseRouteActivityVisitor();
revRouteActivityVisitor.addActivityVisitor(new UpdateLatestOperationStartTimeAtActLocations(states, routingCosts));
insertionListeners.addListener(new UpdateLoadsAtStartAndEndOfRouteWhenInsertionStarts(states));
insertionListeners.addListener(new UpdateLoadsAtStartAndEndOfRouteWhenJobHasBeenInserted(states));
}
public void update(VehicleRoute route){
List<VehicleRoute> routes = Arrays.asList(route);
insertionListeners.informInsertionStarts(routes, Collections.EMPTY_LIST);
routeActivityVisitor.visit(route);
revRouteActivityVisitor.visit(route);
}
@Override
public void informJobInserted(Job job2insert, VehicleRoute inRoute, double additionalCosts, double additionalTime) {
insertionListeners.informJobInserted(job2insert, inRoute, additionalCosts, additionalTime);
routeActivityVisitor.visit(inRoute);
revRouteActivityVisitor.visit(inRoute);
}
@Override
public void informInsertionStarts(Collection<VehicleRoute> vehicleRoutes,Collection<Job> unassignedJobs) {
insertionListeners.informInsertionStarts(vehicleRoutes, unassignedJobs);
for(VehicleRoute route : vehicleRoutes) {
routeActivityVisitor.visit(route);
revRouteActivityVisitor.visit(route);
}
}
}

View file

@ -28,7 +28,6 @@ import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import algorithms.StateUpdates.UpdateStates;
import basics.Job;
import basics.Service;
import basics.costs.VehicleRoutingTransportCosts;

View file

@ -30,7 +30,6 @@ import org.junit.Test;
import util.Coordinate;
import util.ManhattanDistanceCalculator;
import algorithms.StateUpdates.UpdateStates;
import basics.Job;
import basics.Service;
import basics.costs.VehicleRoutingTransportCosts;

View file

@ -148,8 +148,8 @@ public class TestIterateRouteForwardInTime {
forwardInTime.addActivityVisitor(new UpdateLoadAtAllLevels(stateManager));
forwardInTime.visit(vehicleRoute);
assertEquals(5.0, stateManager.getActivityState(firstAct,StateIdFactory.LOAD).toDouble(), 0.01);
assertEquals(10.0, stateManager.getActivityState(secondAct,StateIdFactory.LOAD).toDouble(), 0.01);
assertEquals(5.0, stateManager.getActivityState(firstAct,StateFactory.LOAD).toDouble(), 0.01);
assertEquals(10.0, stateManager.getActivityState(secondAct,StateFactory.LOAD).toDouble(), 0.01);
}
@ -173,9 +173,9 @@ public class TestIterateRouteForwardInTime {
forwardInTime.addActivityVisitor(new UpdateCostsAtAllLevels(new DefaultVehicleRoutingActivityCosts(), cost, stateManager));
forwardInTime.visit(vehicleRoute);
assertEquals(10.0, stateManager.getActivityState(firstAct, StateIdFactory.COSTS).toDouble(),0.05);
assertEquals(5.0, stateManager.getActivityState(firstAct, StateIdFactory.LOAD).toDouble(),0.05);
assertEquals(10.0, stateManager.getActivityState(firstAct, StateIdFactory.EARLIEST_OPERATION_START_TIME).toDouble(),0.05);
assertEquals(10.0, stateManager.getActivityState(firstAct, StateFactory.COSTS).toDouble(),0.05);
assertEquals(5.0, stateManager.getActivityState(firstAct, StateFactory.LOAD).toDouble(),0.05);
assertEquals(10.0, stateManager.getActivityState(firstAct, StateFactory.EARLIEST_OPERATION_START_TIME).toDouble(),0.05);
// assertEquals(20.0, states.getState(tour.getActivities().get(0)).getLatestOperationStart(),0.05);
}
@ -188,9 +188,9 @@ public class TestIterateRouteForwardInTime {
forwardInTime.addActivityVisitor(new UpdateCostsAtAllLevels(new DefaultVehicleRoutingActivityCosts(), cost, stateManager));
forwardInTime.visit(vehicleRoute);
assertEquals(30.0, stateManager.getActivityState(secondAct, StateIdFactory.COSTS).toDouble(),0.05);
assertEquals(10.0, stateManager.getActivityState(secondAct, StateIdFactory.LOAD).toDouble(),0.05);
assertEquals(30.0, stateManager.getActivityState(secondAct, StateIdFactory.EARLIEST_OPERATION_START_TIME).toDouble(),0.05);
assertEquals(30.0, stateManager.getActivityState(secondAct, StateFactory.COSTS).toDouble(),0.05);
assertEquals(10.0, stateManager.getActivityState(secondAct, StateFactory.LOAD).toDouble(),0.05);
assertEquals(30.0, stateManager.getActivityState(secondAct, StateFactory.EARLIEST_OPERATION_START_TIME).toDouble(),0.05);
// assertEquals(40.0, states.getState(tour.getActivities().get(1)).getLatestOperationStart(),0.05);
}
@ -202,7 +202,7 @@ public class TestIterateRouteForwardInTime {
forwardInTime.addActivityVisitor(new UpdateCostsAtAllLevels(new DefaultVehicleRoutingActivityCosts(), cost, stateManager));
forwardInTime.visit(vehicleRoute);
assertEquals(40.0, stateManager.getRouteState(vehicleRoute,StateIdFactory.COSTS).toDouble(), 0.05);
assertEquals(40.0, stateManager.getRouteState(vehicleRoute,StateFactory.COSTS).toDouble(), 0.05);
assertEquals(40.0, vehicleRoute.getEnd().getArrTime(),0.05);
assertEquals(50.0, vehicleRoute.getEnd().getTheoreticalLatestOperationStartTime(),0.05);
}

View file

@ -26,7 +26,6 @@ import org.junit.Test;
import util.Coordinate;
import util.ManhattanDistanceCalculator;
import algorithms.StateUpdates.UpdateStates;
import basics.Job;
import basics.Service;
import basics.costs.VehicleRoutingTransportCosts;
@ -102,7 +101,7 @@ public class TestTourStateUpdaterWithService {
states = new StateManager();
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("test", 0).build();
VehicleTypeImpl type = VehicleTypeImpl.Builder.newInstance("test", 10).build();
vehicle = VehicleImpl.Builder.newInstance("testvehicle").setType(type).setLocationId("0,0")
.setEarliestStart(0.0).setLatestArrival(50.0).build();
@ -119,8 +118,8 @@ public class TestTourStateUpdaterWithService {
@Test
public void testCalculatedCost() {
updateStates.update(vehicleRoute);
assertEquals(40.0, states.getRouteState(vehicleRoute,StateIdFactory.COSTS).toDouble(), 0.05);
assertEquals(10, states.getRouteState(vehicleRoute, StateIdFactory.LOAD).toDouble(), 0.05);
assertEquals(40.0, states.getRouteState(vehicleRoute,StateFactory.COSTS).toDouble(), 0.05);
assertEquals(10, states.getRouteState(vehicleRoute, StateFactory.LOAD).toDouble(), 0.05);
}
@Test
@ -136,27 +135,27 @@ public class TestTourStateUpdaterWithService {
@Test
public void testStatesOfAct1(){
updateStates.update(vehicleRoute);
assertEquals(10.0, states.getActivityState(tour.getActivities().get(0), StateIdFactory.COSTS).toDouble(),0.05);
assertEquals(5.0, states.getActivityState(tour.getActivities().get(0), StateIdFactory.LOAD).toDouble(),0.05);
assertEquals(10.0, states.getActivityState(tour.getActivities().get(0), StateFactory.COSTS).toDouble(),0.05);
assertEquals(5.0, states.getActivityState(tour.getActivities().get(0), StateFactory.LOAD).toDouble(),0.05);
// assertEquals(10.0, states.getActivityState(tour.getActivities().get(0), StateTypes.EARLIEST_OPERATION_START_TIME).toDouble(),0.05);
assertEquals(20.0, states.getActivityState(tour.getActivities().get(0), StateIdFactory.LATEST_OPERATION_START_TIME).toDouble(),0.05);
assertEquals(20.0, states.getActivityState(tour.getActivities().get(0), StateFactory.LATEST_OPERATION_START_TIME).toDouble(),0.05);
}
@Test
public void testStatesOfAct2(){
updateStates.update(vehicleRoute);
assertEquals(30.0, states.getActivityState(tour.getActivities().get(1), StateIdFactory.COSTS).toDouble(),0.05);
assertEquals(10.0, states.getActivityState(tour.getActivities().get(1), StateIdFactory.LOAD).toDouble(),0.05);
assertEquals(30.0, states.getActivityState(tour.getActivities().get(1), StateFactory.COSTS).toDouble(),0.05);
assertEquals(10.0, states.getActivityState(tour.getActivities().get(1), StateFactory.LOAD).toDouble(),0.05);
// assertEquals(10.0, states.getActivityState(tour.getActivities().get(0), StateTypes.EARLIEST_OPERATION_START_TIME).toDouble(),0.05);
assertEquals(40.0, states.getActivityState(tour.getActivities().get(1), StateIdFactory.LATEST_OPERATION_START_TIME).toDouble(),0.05);
assertEquals(40.0, states.getActivityState(tour.getActivities().get(1), StateFactory.LATEST_OPERATION_START_TIME).toDouble(),0.05);
}
@Test
public void testStatesOfAct3(){
updateStates.update(vehicleRoute);
assertEquals(40.0, states.getRouteState(vehicleRoute, StateIdFactory.COSTS).toDouble(), 0.05);
assertEquals(40.0, states.getRouteState(vehicleRoute, StateFactory.COSTS).toDouble(), 0.05);
assertEquals(40.0, vehicleRoute.getEnd().getArrTime(),0.05);
assertEquals(50.0, vehicleRoute.getEnd().getTheoreticalLatestOperationStartTime(),0.05);
}