diff --git a/jsprit-core/src/main/java/jsprit/core/problem/solution/route/state/StateFactory.java b/jsprit-core/src/main/java/jsprit/core/problem/solution/route/state/StateFactory.java index 8ef20167..803f18a1 100644 --- a/jsprit-core/src/main/java/jsprit/core/problem/solution/route/state/StateFactory.java +++ b/jsprit-core/src/main/java/jsprit/core/problem/solution/route/state/StateFactory.java @@ -23,9 +23,7 @@ package jsprit.core.problem.solution.route.state; import jsprit.core.problem.HasIndex; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class StateFactory { @@ -33,105 +31,53 @@ public class StateFactory { public interface StateId extends HasIndex{ - } - - public interface State { - double toDouble(); - } - - - public interface States { - - State getState(StateId key); - - void putState(StateId id, State state); - - } - - - static class StateImpl implements State{ - double state; - - public StateImpl(double state) { - super(); - this.state = state; - } - - @Override - public double toDouble() { - return state; - } - - } - - static class StatesImpl implements States{ - - private Map states = new HashMap(); - - public void putState(StateId key, State state) { - states.put(key, state); - } - - @Override - public State getState(StateId key) { - return states.get(key); - } - - } - - public final static StateId MAXLOAD = new StateIdImpl("maxload", 0); + public final static StateId MAXLOAD = new StateIdImpl("max_load", 0); public final static StateId LOAD = new StateIdImpl("load", 1); public final static StateId COSTS = new StateIdImpl("costs", 2); - public final static StateId LOAD_AT_BEGINNING = new StateIdImpl("loadAtBeginning", 3); + public final static StateId LOAD_AT_BEGINNING = new StateIdImpl("load_at_beginning", 3); - public final static StateId LOAD_AT_END = new StateIdImpl("loadAtEnd", 4); + public final static StateId LOAD_AT_END = new StateIdImpl("load_at_end", 4); public final static StateId DURATION = new StateIdImpl("duration", 5); - public final static StateId LATEST_OPERATION_START_TIME = new StateIdImpl("latestOST", 6); + public final static StateId LATEST_OPERATION_START_TIME = new StateIdImpl("latest_ost", 6); - public final static StateId EARLIEST_OPERATION_START_TIME = new StateIdImpl("earliestOST", 7); + public final static StateId EARLIEST_OPERATION_START_TIME = new StateIdImpl("earliest_ost", 7); - public final static StateId FUTURE_MAXLOAD = new StateIdImpl("futureMaxload", 8); + public final static StateId FUTURE_MAXLOAD = new StateIdImpl("future_max_load", 8); - public final static StateId PAST_MAXLOAD = new StateIdImpl("pastMaxload", 9); + public final static StateId PAST_MAXLOAD = new StateIdImpl("past_max_load", 9); - final static List reservedIds = Arrays.asList("maxload","load","costs","loadAtBeginning","loadAtEnd","duration","latestOST","earliestOST" - ,"futureMaxload","pastMaxload"); + final static List reservedIds = Arrays.asList("max_load","load","costs","load_at_beginning","load_at_end","duration","latest_ost","earliest_ost" + ,"future_max_load","past_max_load"); - - public static States createStates(){ - return new StatesImpl(); - } - + + + @Deprecated public static StateId createId(String name){ if(reservedIds.contains(name)){ throwReservedIdException(name); } return new StateIdImpl(name, -1); } + @Deprecated public static StateId createId(String name, int index){ if(reservedIds.contains(name)) throwReservedIdException(name); if(index < 10) throwReservedIdException(name); return new StateIdImpl(name, index); } - - public static State createState(double value){ - return new StateImpl(value); - } + public static boolean isReservedId(String stateId){ - if(reservedIds.contains(stateId)) return true; - return false; + return reservedIds.contains(stateId); } public static boolean isReservedId(StateId stateId){ - if(reservedIds.contains(stateId.toString())) return true; - return false; + return reservedIds.contains(stateId.toString()); } public static void throwReservedIdException(String name) { diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/state/GenericsTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/state/GenericsTest.java deleted file mode 100644 index 15580a51..00000000 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/state/GenericsTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/******************************************************************************* - * 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 . - * - * Contributors: - * Stefan Schroeder - initial API and implementation - ******************************************************************************/ -package jsprit.core.algorithm.state; - -import java.util.HashMap; -import java.util.Map; - -import jsprit.core.problem.Capacity; -import jsprit.core.problem.job.Job; -import jsprit.core.problem.job.Service; -import jsprit.core.problem.solution.route.state.StateFactory; -import jsprit.core.problem.solution.route.state.StateFactory.State; - -public class GenericsTest { - - - -// static class State { -// Class type; -// T state; -// public State(Class type, T state) { -// super(); -// this.type = type; -// this.state = state; -// } -// -// } - - static class States { - - private Map states = new HashMap(); - - public void putState(String id, Class type, T state){ - states.put(id, type.cast(state)); - } - - public T getState(String id, Class type){ - T s = type.cast(states.get(id)); - return s; - } - - } - - public static void main(String[] args) { - States states = new States(); - states.putState("load", Double.class, 0.1); - states.putState("state", String.class, "foo"); - states.putState("job", Job.class, Service.Builder.newInstance("foo").setLocationId("loc").build()); - states.putState("cap", Capacity.class, Capacity.Builder.newInstance().addDimension(0, 1).build()); - - Double load = states.getState("load", Double.class); - String state = states.getState("state", String.class); - Job service = states.getState("job", Job.class); - Capacity capacity = states.getState("cap", Capacity.class); - - states.putState("st", State.class, StateFactory.createState(10.)); - - System.out.println(load); - System.out.println(state); - System.out.println(service); - System.out.println(capacity); - System.out.println(states.getState("st", State.class).toDouble()); - - } - - -} diff --git a/jsprit-core/src/test/java/jsprit/core/algorithm/state/StateManagerTest.java b/jsprit-core/src/test/java/jsprit/core/algorithm/state/StateManagerTest.java index 3ae760e4..2f90d48a 100644 --- a/jsprit-core/src/test/java/jsprit/core/algorithm/state/StateManagerTest.java +++ b/jsprit-core/src/test/java/jsprit/core/algorithm/state/StateManagerTest.java @@ -24,7 +24,6 @@ import jsprit.core.problem.job.Service; import jsprit.core.problem.solution.route.VehicleRoute; import jsprit.core.problem.solution.route.activity.TourActivity; import jsprit.core.problem.solution.route.state.StateFactory; -import jsprit.core.problem.solution.route.state.StateFactory.State; import jsprit.core.problem.solution.route.state.StateFactory.StateId; import jsprit.core.problem.vehicle.Vehicle; import jsprit.core.problem.vehicle.VehicleImpl; @@ -34,20 +33,9 @@ import org.junit.Test; import static org.junit.Assert.*; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; public class StateManagerTest { - @Test - public void whenRouteStateIsSetWithGenericMethod_itMustBeSetCorrectly(){ - VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().build(); - VehicleRoute route = getRoute(mock(Vehicle.class)); - StateManager stateManager = new StateManager(vrp); - StateId id = stateManager.createStateId("myState"); - State state = StateFactory.createState(1.); - stateManager.putRouteState(route, id, state); - assertEquals(1.,stateManager.getRouteState(route, id, State.class).toDouble(),0.01); - } private VehicleRoute getRoute(Vehicle vehicle) { return VehicleRoute.Builder.newInstance(vehicle).addService(Service.Builder.newInstance("s").setLocationId("loc").build()).build(); @@ -84,16 +72,7 @@ public class StateManagerTest { assertEquals(500, getCap.get(0)); } - @Test - public void whenActivityStateIsSetWithGenericMethod_itMustBeSetCorrectly(){ - TourActivity activity = mock(TourActivity.class); - when(activity.getIndex()).thenReturn(0); - StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class)); - StateId id = stateManager.createStateId("myState"); - State state = StateFactory.createState(1.); - stateManager.putActivityState(activity, id, state); - assertEquals(1.,stateManager.getActivityState(activity, id, State.class).toDouble(),0.01); - } + @Test public void whenActivityStateIsSetWithGenericMethodAndBoolean_itMustBeSetCorrectly(){ diff --git a/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratch.java b/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratch.java index 4b550691..233d702b 100644 --- a/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratch.java +++ b/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratch.java @@ -18,15 +18,8 @@ ******************************************************************************/ package jsprit.examples; -import java.util.Collection; - import jsprit.analysis.toolbox.SolutionPrinter; -import jsprit.core.algorithm.InsertionInitialSolutionFactory; -import jsprit.core.algorithm.RemoveEmptyVehicles; -import jsprit.core.algorithm.SearchStrategy; -import jsprit.core.algorithm.SearchStrategyManager; -import jsprit.core.algorithm.VariablePlusFixedSolutionCostCalculatorFactory; -import jsprit.core.algorithm.VehicleRoutingAlgorithm; +import jsprit.core.algorithm.*; import jsprit.core.algorithm.acceptor.GreedyAcceptance; import jsprit.core.algorithm.module.RuinAndRecreateModule; import jsprit.core.algorithm.recreate.BestInsertionBuilder; @@ -49,6 +42,8 @@ import jsprit.core.util.Solutions; import jsprit.instance.reader.SolomonReader; import jsprit.util.Examples; +import java.util.Collection; + public class BuildAlgorithmFromScratch { /** @@ -99,7 +94,7 @@ public class BuildAlgorithmFromScratch { /* * manages route and activity states. */ - StateManager stateManager = new StateManager(vrp.getTransportCosts()); + StateManager stateManager = new StateManager(vrp); /* * tells stateManager to update load states */ diff --git a/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratchWithHardAndSoftConstraints.java b/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratchWithHardAndSoftConstraints.java index 1fa2a432..e513b1d3 100644 --- a/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratchWithHardAndSoftConstraints.java +++ b/jsprit-examples/src/main/java/jsprit/examples/BuildAlgorithmFromScratchWithHardAndSoftConstraints.java @@ -18,15 +18,8 @@ ******************************************************************************/ package jsprit.examples; -import java.util.Collection; - import jsprit.analysis.toolbox.SolutionPrinter; -import jsprit.core.algorithm.InsertionInitialSolutionFactory; -import jsprit.core.algorithm.RemoveEmptyVehicles; -import jsprit.core.algorithm.SearchStrategy; -import jsprit.core.algorithm.SearchStrategyManager; -import jsprit.core.algorithm.VariablePlusFixedSolutionCostCalculatorFactory; -import jsprit.core.algorithm.VehicleRoutingAlgorithm; +import jsprit.core.algorithm.*; import jsprit.core.algorithm.acceptor.GreedyAcceptance; import jsprit.core.algorithm.module.RuinAndRecreateModule; import jsprit.core.algorithm.recreate.BestInsertionBuilder; @@ -49,6 +42,8 @@ import jsprit.core.util.Solutions; import jsprit.instance.reader.SolomonReader; import jsprit.util.Examples; +import java.util.Collection; + public class BuildAlgorithmFromScratchWithHardAndSoftConstraints { /** @@ -99,7 +94,7 @@ public class BuildAlgorithmFromScratchWithHardAndSoftConstraints { /* * manages route and activity states. */ - StateManager stateManager = new StateManager(vrp.getTransportCosts()); + StateManager stateManager = new StateManager(vrp); /* * tells stateManager to update load states */ diff --git a/jsprit-examples/src/main/java/jsprit/examples/TransportOfDisabledPeople.java b/jsprit-examples/src/main/java/jsprit/examples/TransportOfDisabledPeople.java index 65576ac1..687f4c2f 100644 --- a/jsprit-examples/src/main/java/jsprit/examples/TransportOfDisabledPeople.java +++ b/jsprit-examples/src/main/java/jsprit/examples/TransportOfDisabledPeople.java @@ -18,8 +18,6 @@ ******************************************************************************/ package jsprit.examples; -import java.util.Collection; - import jsprit.analysis.toolbox.GraphStreamViewer; import jsprit.analysis.toolbox.GraphStreamViewer.Label; import jsprit.analysis.toolbox.Plotter; @@ -45,6 +43,8 @@ import jsprit.core.util.Coordinate; import jsprit.core.util.Solutions; import jsprit.util.Examples; +import java.util.Collection; + public class TransportOfDisabledPeople { static int WHEELCHAIRSPACE_INDEX = 0; @@ -166,7 +166,7 @@ public class TransportOfDisabledPeople { //build the problem VehicleRoutingProblem problem = vrpBuilder.build(); - StateManager stateManager = new StateManager(problem.getTransportCosts()); + StateManager stateManager = new StateManager(problem); ConstraintManager constraintManager = new ConstraintManager(problem, stateManager); constraintManager.addConstraint(wheelchair_bus_passenger_pickup_constraint);