From 4c5ba8d4b2fffbaf4119c3ea10be50f032cf43c4 Mon Sep 17 00:00:00 2001 From: Kai Martins-Turner Date: Mon, 22 Jan 2024 12:43:24 +0100 Subject: [PATCH 1/6] Try to use generic WorstPlan... --- .../LSPWorstPlanForRemovalSelector.java | 99 ++++++++++--------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/LSPWorstPlanForRemovalSelector.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/LSPWorstPlanForRemovalSelector.java index e0af254b..ebd2f33c 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/LSPWorstPlanForRemovalSelector.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/LSPWorstPlanForRemovalSelector.java @@ -3,58 +3,59 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.matsim.api.core.v01.population.HasPlansAndId; +import org.matsim.core.replanning.selectors.GenericWorstPlanForRemovalSelector; import org.matsim.core.replanning.selectors.PlanSelector; import org.matsim.freight.logistics.LSP; import org.matsim.freight.logistics.LSPPlan; -class LSPWorstPlanForRemovalSelector implements PlanSelector { +class LSPWorstPlanForRemovalSelector extends GenericWorstPlanForRemovalSelector {//implements PlanSelector { - private static final String UNDEFINED_TYPE = "undefined"; - - @Override - public LSPPlan selectPlan(HasPlansAndId lsp) { - - Map typeCounts = new ConcurrentHashMap(); - - // count how many plans per type an agent has: - for (LSPPlan plan : lsp.getPlans()) { - String type = plan.getType(); - if (type == null) { - type = UNDEFINED_TYPE; - } - typeCounts.merge(type, 1, (a, b) -> a + b); - } - - LSPPlan worst = null; - double worstScore = Double.POSITIVE_INFINITY; - for (LSPPlan plan : lsp.getPlans()) { - String type = plan.getType(); - if (type == null) { - type = UNDEFINED_TYPE; - } - if (typeCounts.get(type) > 1) { - if (plan.getScore() == null || plan.getScore().isNaN()) { - worst = plan; - worstScore = Double.NEGATIVE_INFINITY; - } else if (plan.getScore() < worstScore) { - worst = plan; - worstScore = plan.getScore(); - } - } - } - if (worst == null) { - // there is exactly one plan, or we have of each plan-type exactly one. - // select the one with worst score globally, or the first one with score=null - for (LSPPlan plan : lsp.getPlans()) { - if (plan.getScore() == null || plan.getScore().isNaN()) { - return plan; - } - if (plan.getScore() < worstScore) { - worst = plan; - worstScore = plan.getScore(); - } - } - } - return worst; - } +// private static final String UNDEFINED_TYPE = "undefined"; +// +// @Override +// public LSPPlan selectPlan(HasPlansAndId lsp) { +// +// Map typeCounts = new ConcurrentHashMap(); +// +// // count how many plans per type an agent has: +// for (LSPPlan plan : lsp.getPlans()) { +// String type = plan.getType(); +// if (type == null) { +// type = UNDEFINED_TYPE; +// } +// typeCounts.merge(type, 1, (a, b) -> a + b); +// } +// +// LSPPlan worst = null; +// double worstScore = Double.POSITIVE_INFINITY; +// for (LSPPlan plan : lsp.getPlans()) { +// String type = plan.getType(); +// if (type == null) { +// type = UNDEFINED_TYPE; +// } +// if (typeCounts.get(type) > 1) { +// if (plan.getScore() == null || plan.getScore().isNaN()) { +// worst = plan; +// worstScore = Double.NEGATIVE_INFINITY; +// } else if (plan.getScore() < worstScore) { +// worst = plan; +// worstScore = plan.getScore(); +// } +// } +// } +// if (worst == null) { +// // there is exactly one plan, or we have of each plan-type exactly one. +// // select the one with worst score globally, or the first one with score=null +// for (LSPPlan plan : lsp.getPlans()) { +// if (plan.getScore() == null || plan.getScore().isNaN()) { +// return plan; +// } +// if (plan.getScore() < worstScore) { +// worst = plan; +// worstScore = plan.getScore(); +// } +// } +// } +// return worst; +// } } From 12fa85dfa2741cc47bd7bad9e66b6c420234d483 Mon Sep 17 00:00:00 2001 From: Kai Martins-Turner Date: Tue, 23 Jan 2024 15:52:34 +0100 Subject: [PATCH 2/6] use MATSim-Snapshot version for developing. --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 8b4f4a81..181add1c 100644 --- a/pom.xml +++ b/pom.xml @@ -14,10 +14,10 @@ org.matsim matsim-all - 16.0-2024w04 + - + 16.0-SNAPSHOT From 5d8f96a55e78caed3b8ffab8fd8dc3dfdf932bbb Mon Sep 17 00:00:00 2001 From: Kai Nagel Date: Mon, 25 Mar 2024 11:12:30 +0100 Subject: [PATCH 3/6] reformatting; add a (design) comment --- ...pleMultipleTwoEchelonChainsReplanning.java | 31 ++++++------------- .../ProximityStrategyFactory.java | 11 ++++--- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleTwoEchelonChainsReplanning.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleTwoEchelonChainsReplanning.java index cd793cd6..2484a501 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleTwoEchelonChainsReplanning.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleTwoEchelonChainsReplanning.java @@ -80,41 +80,30 @@ public void install() { } }); + // @formatter:off controler.addOverridingModule( new AbstractModule() { @Override public void install() { - final EventBasedCarrierScorer_MultipleChains carrierScorer = - new EventBasedCarrierScorer_MultipleChains(); + final EventBasedCarrierScorer_MultipleChains carrierScorer = new EventBasedCarrierScorer_MultipleChains(); bind(CarrierScoringFunctionFactory.class).toInstance(carrierScorer); bind(LSPScorerFactory.class).toInstance(MyLSPScorer::new); - bind(CarrierStrategyManager.class) - .toProvider( - () -> { - CarrierStrategyManager strategyManager = - CarrierControlerUtils.createDefaultCarrierStrategyManager(); - strategyManager.addStrategy( - new GenericPlanStrategyImpl<>(new BestPlanSelector<>()), null, 1); + bind(CarrierStrategyManager.class).toProvider( () -> { + CarrierStrategyManager strategyManager = CarrierControlerUtils.createDefaultCarrierStrategyManager(); + strategyManager.addStrategy( new GenericPlanStrategyImpl<>(new BestPlanSelector<>()), null, 1); return strategyManager; }); - bind(LSPStrategyManager.class) - .toProvider( - () -> { + bind(LSPStrategyManager.class).toProvider( () -> { LSPStrategyManager strategyManager = new LSPStrategyManagerImpl(); - strategyManager.addStrategy( - new GenericPlanStrategyImpl<>( - new ExpBetaPlanSelector<>(new ScoringConfigGroup())), - null, - 1); - strategyManager.addStrategy( - ProximityStrategyFactory.createStrategy(scenario.getNetwork()), null, 1); + strategyManager.addStrategy( new GenericPlanStrategyImpl<>( new ExpBetaPlanSelector<>(new ScoringConfigGroup())), null, 1); + strategyManager.addStrategy( ProximityStrategyFactory.createStrategy(scenario.getNetwork()), null, 1); strategyManager.setMaxPlansPerAgent(5); - strategyManager.setPlanSelectorForRemoval( - new LSPWorstPlanForRemovalSelector()); + strategyManager.setPlanSelectorForRemoval( new LSPWorstPlanForRemovalSelector()); return strategyManager; }); } }); + // @formatter:on log.info("Run MATSim"); diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ProximityStrategyFactory.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ProximityStrategyFactory.java index 85a1ee0a..2c89956a 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ProximityStrategyFactory.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ProximityStrategyFactory.java @@ -19,14 +19,17 @@ final class ProximityStrategyFactory { + // yyyy This factory class contains a long anonymous class. It seems that it should be the other way round: The anonymous class should be a proper + // class, and the factory method (or maybe just normal constructor) should be contained in the class. At some point, try to exchange. kmt & kai, mar'24 + + // @formatter:off + private ProximityStrategyFactory() {} // class contains only static methods; do not instantiate static GenericPlanStrategy createStrategy(Network network) { - GenericPlanStrategyImpl strategy = - new GenericPlanStrategyImpl<>(new ExpBetaPlanSelector<>(new ScoringConfigGroup())); - GenericPlanStrategyModule randomModule = - new GenericPlanStrategyModule<>() { + GenericPlanStrategyImpl strategy = new GenericPlanStrategyImpl<>(new ExpBetaPlanSelector<>(new ScoringConfigGroup())); + GenericPlanStrategyModule randomModule = new GenericPlanStrategyModule<>() { @Override public void prepareReplanning(ReplanningContext replanningContext) {} From 51beff4f084ff21d815e09da8edef82516ed979f Mon Sep 17 00:00:00 2001 From: Kai Martins-Turner Date: Mon, 25 Mar 2024 15:53:30 +0100 Subject: [PATCH 4/6] update MATSim Version: PR 3184 includes the updated GenericWorstPlanForRemovalSelector --- pom.xml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 181add1c..b29a3264 100644 --- a/pom.xml +++ b/pom.xml @@ -15,9 +15,8 @@ org.matsim matsim-all - - - 16.0-SNAPSHOT + 16.0-PR3184 + From 6e4d8d1c697c1b62ae83fd7d9fcbac5f7c194ba3 Mon Sep 17 00:00:00 2001 From: Kai Martins-Turner Date: Mon, 25 Mar 2024 15:59:36 +0100 Subject: [PATCH 5/6] Replace the LSPWorstPlanForRemovalSelector by using the GenericWorstPlanForRemovalSelector directly. --- .../ExampleGroceryDeliveryMultipleChains.java | 3 +- ...pleMultipleOneEchelonChainsReplanning.java | 3 +- ...pleMultipleTwoEchelonChainsReplanning.java | 3 +- .../LSPWorstPlanForRemovalSelector.java | 61 ------------------- .../multipleChains/WorstPlanSelectorTest.java | 5 +- 5 files changed, 9 insertions(+), 66 deletions(-) delete mode 100644 src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/LSPWorstPlanForRemovalSelector.java diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleGroceryDeliveryMultipleChains.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleGroceryDeliveryMultipleChains.java index 03009d9d..53e28111 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleGroceryDeliveryMultipleChains.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleGroceryDeliveryMultipleChains.java @@ -20,6 +20,7 @@ import org.matsim.core.replanning.GenericPlanStrategyImpl; import org.matsim.core.replanning.selectors.BestPlanSelector; import org.matsim.core.replanning.selectors.ExpBetaPlanSelector; +import org.matsim.core.replanning.selectors.GenericWorstPlanForRemovalSelector; import org.matsim.core.scenario.ScenarioUtils; import org.matsim.freight.carriers.*; import org.matsim.freight.carriers.controler.CarrierControlerUtils; @@ -93,7 +94,7 @@ public void install() { // ProximityStrategyFactory(scenario.getNetwork()).createStrategy(), null, 1); // strategyManager.setMaxPlansPerAgent(5); strategyManager.setPlanSelectorForRemoval( - new LSPWorstPlanForRemovalSelector()); + new GenericWorstPlanForRemovalSelector<>()); return strategyManager; }); } diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleOneEchelonChainsReplanning.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleOneEchelonChainsReplanning.java index cc371850..c570ab79 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleOneEchelonChainsReplanning.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleOneEchelonChainsReplanning.java @@ -20,6 +20,7 @@ import org.matsim.core.replanning.GenericPlanStrategyImpl; import org.matsim.core.replanning.selectors.BestPlanSelector; import org.matsim.core.replanning.selectors.ExpBetaPlanSelector; +import org.matsim.core.replanning.selectors.GenericWorstPlanForRemovalSelector; import org.matsim.core.scenario.ScenarioUtils; import org.matsim.core.utils.io.IOUtils; import org.matsim.examples.ExamplesUtils; @@ -116,7 +117,7 @@ public void install() { // strategyManager.addStrategy(ProximityStrategyFactory.createStrategy(scenario.getNetwork()), null, 1); strategyManager.setMaxPlansPerAgent(5); strategyManager.setPlanSelectorForRemoval( - new LSPWorstPlanForRemovalSelector()); + new GenericWorstPlanForRemovalSelector<>()); return strategyManager; }); } diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleTwoEchelonChainsReplanning.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleTwoEchelonChainsReplanning.java index 2484a501..13bfaac6 100644 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleTwoEchelonChainsReplanning.java +++ b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleMultipleTwoEchelonChainsReplanning.java @@ -20,6 +20,7 @@ import org.matsim.core.replanning.GenericPlanStrategyImpl; import org.matsim.core.replanning.selectors.BestPlanSelector; import org.matsim.core.replanning.selectors.ExpBetaPlanSelector; +import org.matsim.core.replanning.selectors.GenericWorstPlanForRemovalSelector; import org.matsim.core.scenario.ScenarioUtils; import org.matsim.core.utils.io.IOUtils; import org.matsim.examples.ExamplesUtils; @@ -98,7 +99,7 @@ public void install() { strategyManager.addStrategy( new GenericPlanStrategyImpl<>( new ExpBetaPlanSelector<>(new ScoringConfigGroup())), null, 1); strategyManager.addStrategy( ProximityStrategyFactory.createStrategy(scenario.getNetwork()), null, 1); strategyManager.setMaxPlansPerAgent(5); - strategyManager.setPlanSelectorForRemoval( new LSPWorstPlanForRemovalSelector()); + strategyManager.setPlanSelectorForRemoval( new GenericWorstPlanForRemovalSelector<>()); return strategyManager; }); } diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/LSPWorstPlanForRemovalSelector.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/LSPWorstPlanForRemovalSelector.java deleted file mode 100644 index ebd2f33c..00000000 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/LSPWorstPlanForRemovalSelector.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.matsim.freight.logistics.example.lsp.multipleChains; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import org.matsim.api.core.v01.population.HasPlansAndId; -import org.matsim.core.replanning.selectors.GenericWorstPlanForRemovalSelector; -import org.matsim.core.replanning.selectors.PlanSelector; -import org.matsim.freight.logistics.LSP; -import org.matsim.freight.logistics.LSPPlan; - -class LSPWorstPlanForRemovalSelector extends GenericWorstPlanForRemovalSelector {//implements PlanSelector { - -// private static final String UNDEFINED_TYPE = "undefined"; -// -// @Override -// public LSPPlan selectPlan(HasPlansAndId lsp) { -// -// Map typeCounts = new ConcurrentHashMap(); -// -// // count how many plans per type an agent has: -// for (LSPPlan plan : lsp.getPlans()) { -// String type = plan.getType(); -// if (type == null) { -// type = UNDEFINED_TYPE; -// } -// typeCounts.merge(type, 1, (a, b) -> a + b); -// } -// -// LSPPlan worst = null; -// double worstScore = Double.POSITIVE_INFINITY; -// for (LSPPlan plan : lsp.getPlans()) { -// String type = plan.getType(); -// if (type == null) { -// type = UNDEFINED_TYPE; -// } -// if (typeCounts.get(type) > 1) { -// if (plan.getScore() == null || plan.getScore().isNaN()) { -// worst = plan; -// worstScore = Double.NEGATIVE_INFINITY; -// } else if (plan.getScore() < worstScore) { -// worst = plan; -// worstScore = plan.getScore(); -// } -// } -// } -// if (worst == null) { -// // there is exactly one plan, or we have of each plan-type exactly one. -// // select the one with worst score globally, or the first one with score=null -// for (LSPPlan plan : lsp.getPlans()) { -// if (plan.getScore() == null || plan.getScore().isNaN()) { -// return plan; -// } -// if (plan.getScore() < worstScore) { -// worst = plan; -// worstScore = plan.getScore(); -// } -// } -// } -// return worst; -// } -} diff --git a/src/test/java/org/matsim/freight/logistics/example/lsp/multipleChains/WorstPlanSelectorTest.java b/src/test/java/org/matsim/freight/logistics/example/lsp/multipleChains/WorstPlanSelectorTest.java index 6c326596..e6766568 100644 --- a/src/test/java/org/matsim/freight/logistics/example/lsp/multipleChains/WorstPlanSelectorTest.java +++ b/src/test/java/org/matsim/freight/logistics/example/lsp/multipleChains/WorstPlanSelectorTest.java @@ -4,8 +4,8 @@ import java.util.*; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.Scenario; import org.matsim.api.core.v01.network.Link; @@ -21,6 +21,7 @@ import org.matsim.core.replanning.GenericPlanStrategyImpl; import org.matsim.core.replanning.selectors.BestPlanSelector; import org.matsim.core.replanning.selectors.ExpBetaPlanSelector; +import org.matsim.core.replanning.selectors.GenericWorstPlanForRemovalSelector; import org.matsim.core.scenario.ScenarioUtils; import org.matsim.core.utils.io.IOUtils; import org.matsim.examples.ExamplesUtils; @@ -249,7 +250,7 @@ public void install() { strategyManager.addStrategy(new GenericPlanStrategyImpl<>(new ExpBetaPlanSelector<>(new ScoringConfigGroup())), null, 1); strategyManager.addStrategy( RandomDistributionAllShipmentsStrategyFactory.createStrategy(), null, 1); strategyManager.setMaxPlansPerAgent(2); - strategyManager.setPlanSelectorForRemoval(new LSPWorstPlanForRemovalSelector() ); + strategyManager.setPlanSelectorForRemoval(new GenericWorstPlanForRemovalSelector<>() ); return strategyManager; }); } From 6b208813673f797725c56fab7dec6252c8211c5a Mon Sep 17 00:00:00 2001 From: Kai Martins-Turner Date: Mon, 25 Mar 2024 15:59:51 +0100 Subject: [PATCH 6/6] remove empty java file --- .../logistics/example/lsp/multipleChains/ExampleThreeChains.java | 1 - 1 file changed, 1 deletion(-) delete mode 100644 src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleThreeChains.java diff --git a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleThreeChains.java b/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleThreeChains.java deleted file mode 100644 index 8b137891..00000000 --- a/src/main/java/org/matsim/freight/logistics/example/lsp/multipleChains/ExampleThreeChains.java +++ /dev/null @@ -1 +0,0 @@ -