Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/main' into kmt_create_HubHandlin…
Browse files Browse the repository at this point in the history
…gEvents

# Conflicts:
#	src/main/java/lsp/resourceImplementations/transshipmentHub/TransshipmentHubTourEndEventHandler.java
  • Loading branch information
kt86 committed Sep 26, 2023
2 parents a0118f0 + 0442c00 commit caa4727
Show file tree
Hide file tree
Showing 11 changed files with 478 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public EventBasedScoring() {
}

@Override public void handleEvent(Event event) {
log.warn(event.toString());
log.debug(event.toString());
if (event instanceof CarrierTourStartEvent freightTourStartEvent) {
handleEvent(freightTourStartEvent);
} else if (event instanceof CarrierTourEndEvent freightTourEndEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* @author Kai Martins-Turner (kturner)
*/
class MyEventBasedCarrierScorer implements CarrierScoringFunctionFactory {
class EventBasedCarrierScorer_MultipleChains implements CarrierScoringFunctionFactory {

@Inject
private Network network;
Expand Down Expand Up @@ -74,7 +74,7 @@ public EventBasedScoring() {
}

@Override public void handleEvent(Event event) {
log.warn(event.toString());
log.debug(event.toString());
if (event instanceof CarrierTourStartEvent freightTourStartEvent) {
handleEvent(freightTourStartEvent);
} else if (event instanceof CarrierTourEndEvent freightTourEndEvent) {
Expand Down Expand Up @@ -124,7 +124,8 @@ class LinkBasedTollScoring implements SumScoringFunction.ArbitraryEventScoring {
private final double toll;
private double score;

private final List<String> vehicleTypesToBeTolled = Arrays.asList("large50");
// private final List<String> vehicleTypesToBeTolled = Arrays.asList("large50");
private final List<String> vehicleTypesToBeTolled = List.of("heavy40t");

private final List<Id<Vehicle>> tolledVehicles = new ArrayList<>();

Expand All @@ -145,9 +146,24 @@ public LinkBasedTollScoring(double toll) {
}
}

// private void handleEvent(LinkEnterEvent event) {
// List<String> tolledLinkList = Arrays.asList("i(3,4)", "i(3,6)", "i(7,5)R", "i(7,7)R", "j(4,8)R", "j(6,8)R", "j(3,4)", "j(5,4)");
//
// final Id<VehicleType> vehicleTypeId = (VehicleUtils.findVehicle(event.getVehicleId(), scenario)).getType().getId();
//
// //toll a vehicle only once.
// if (!tolledVehicles.contains(event.getVehicleId()))
// if (vehicleTypesToBeTolled.contains(vehicleTypeId.toString())) {
// if (tolledLinkList.contains(event.getLinkId().toString())) {
// log.info("Tolling caused by event: " + event);
// tolledVehicles.add(event.getVehicleId());
// score = score - toll;
// }
// }
// }

private void handleEvent(LinkEnterEvent event) {
// List<String> tolledLinkList = Arrays.asList("i(5,5)R");
List<String> tolledLinkList = Arrays.asList("i(3,4)", "i(3,6)", "i(7,5)R", "i(7,7)R", "j(4,8)R", "j(6,8)R", "j(3,4)", "j(5,4)");
List<String> tolledLinkList = Arrays.asList("70831","14691","49319","70830","17284","65008","65007","62413","17283","144164","144165","4606","118311","4607","15423","53820","15422","138286","69167","138287","17057","74648","74647","113641","10307","10306","51775","155051","51776","150042","150043","150164","90583","96329","19320","132511","19321","64851","144180","34042","124770","34041","74891","144184","124769","35018","35017","77379","35256","108717","113640","157261","142799","157262","52995","934","52996","935","95587","95588","17150","147460","147461","54024","54023","152801","144506","145715","144505","156464","17125","17126","114545","114546","140792","17127","17248","17128","17249","156458","35463","159609","35462","159608","22046","154715","22047","144373","154716","155927","155926","144372","96330","61139","98190","144126","144127","61011","61010","156463","63682","47555","73006","94867","138930","94866","133488","138931","47554","73005","58893","116395","116394","144136","1158","1157","58894","61269","79237","144137","732","149702","733","77854","4785","55946","77855","4786","55945","90018","61264","61263","86201","77738","120646","77739","26507","108414","108415","17115","66841","26506","78255","78254","118561","35447","147535","17116","118560","61270","102480","51917","62494","72973","51918","72972","72050","72051","147027","33258","61169","18419","102479","20863","61170","43048","43049","69459","73037","18420","69458","3255","3254","73036","27017","76094","41429","74241","76095","149583","74240","35426","81688","81689","12686","25848","25849","64459","115416","149592","74374","115417","81474","81475","36983","36984","36985","36986","52917","52918","64460","40311","108695","40310","79385","119212","155909","119213","119334","119335","112023","48277","48278","106946","91853","91854","102288","69129","102287","13607","2985","64482","156612","8983","156613","67517","28548","28549","83543","145734","83542","149536","149537","151175","151174","18159","8994","93250","147370","53001","5918","24153","79875","147369","36147","53002","138543","138542","104212","137699","137698","41960","104211","18160","41723","41724","3505","123744","81389","104205","104206","112065","49320","84772","37107","142803");

final Id<VehicleType> vehicleTypeId = (VehicleUtils.findVehicle(event.getVehicleId(), scenario)).getType().getId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lsp.*;
import lsp.resourceImplementations.ResourceImplementationUtils;
import lsp.resourceImplementations.distributionCarrier.DistributionCarrierUtils;
import lsp.resourceImplementations.mainRunCarrier.MainRunCarrierUtils;
import lsp.resourceImplementations.transshipmentHub.TranshipmentHubUtils;
import lsp.shipment.LSPShipment;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -16,12 +18,14 @@
import org.matsim.contrib.freight.controler.FreightUtils;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.groups.PlanCalcScoreConfigGroup;
import org.matsim.core.config.groups.VspExperimentalConfigGroup;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.replanning.GenericPlanStrategyImpl;
import org.matsim.core.replanning.selectors.BestPlanSelector;
import org.matsim.core.replanning.selectors.ExpBetaPlanSelector;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.vehicles.VehicleType;

Expand All @@ -34,6 +38,9 @@
public class ExampleGroceryDeliveryMultipleChains {

private static final Logger log = LogManager.getLogger(ExampleGroceryDeliveryMultipleChains.class);
static double HUBCOSTS_FIX = 100;
private static final Id<Link> HUB_LINK_ID = Id.createLinkId("91085");
private static final double TOLL_VALUE = 1000;

private ExampleGroceryDeliveryMultipleChains() {}

Expand All @@ -56,7 +63,8 @@ public void install() {
controler.addOverridingModule(new AbstractModule() {
@Override
public void install() {
final MyEventBasedCarrierScorer carrierScorer = new MyEventBasedCarrierScorer();
final EventBasedCarrierScorer_MultipleChains carrierScorer = new EventBasedCarrierScorer_MultipleChains();
carrierScorer.setToll(TOLL_VALUE);
bind(CarrierScoringFunctionFactory.class).toInstance(carrierScorer);
bind(LSPScorerFactory.class).toInstance(MyLSPScorer::new);
bind(CarrierStrategyManager.class).toProvider(() -> {
Expand All @@ -66,7 +74,12 @@ public void install() {
});
bind(LSPStrategyManager.class).toProvider(() -> {
LSPStrategyManager strategyManager = new LSPStrategyManagerImpl();
strategyManager.addStrategy(new GenericPlanStrategyImpl<>(new BestPlanSelector<>()), null, 1);
strategyManager.addStrategy(new GenericPlanStrategyImpl<>(new ExpBetaPlanSelector<>(new PlanCalcScoreConfigGroup())), null, 1);
// strategyManager.addStrategy(new RebalancingShipmentsStrategyFactory().createStrategy(), null, 2);
// strategyManager.addStrategy(new RandomShiftingStrategyFactory().createStrategy(), null, 1);
// strategyManager.addStrategy(new ProximityStrategyFactory(scenario.getNetwork()).createStrategy(), null, 1);
// strategyManager.setMaxPlansPerAgent(5);
strategyManager.setPlanSelectorForRemoval(new WorstPlanForRemovalSelector());
return strategyManager;
});
}
Expand All @@ -91,11 +104,10 @@ private static Config prepareConfig(String[] args) {
ConfigUtils.applyCommandline(config,args);
} else {
config.controler().setOutputDirectory("output/groceryDelivery");
config.controler().setLastIteration(0);
config.controler().setLastIteration(5);
}

// config.network().setInputFile("/Users/niclasrichter/Documents/Studium/Master/Thesis/groceryDeliveryScenario/berlin_v5.5-network.xml.gz");
config.network().setInputFile("https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/berlin/berlin-v5.5-10pct/input/berlin_v5.5-network.xml.gz");
config.network().setInputFile("https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/berlin/berlin-v5.5-10pct/input/berlin-v5.5-network.xml.gz");
config.controler().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
config.controler().setWriteEventsInterval(1);

Expand All @@ -115,10 +127,8 @@ private static Scenario prepareScenario(Config config) {
}

private static LSP createLSP(Scenario scenario) {

//TODO vielleicht kann man carrier und vehicleTypes auch eleganter direkt von der Website laden
String carrierPlanFile = "/Users/niclasrichter/Documents/Studium/Master/Thesis/groceryDeliveryScenario/CarrierLEH_v2_withFleet_Shipment_OneTW_PickupTime_ICEV.xml";
String vehicleTypeFile = "/Users/niclasrichter/Documents/Studium/Master/Thesis/groceryDeliveryScenario/vehicleTypesBVWP100_DC_noTax.xml";
String carrierPlanFile = "https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/berlin/projects/freight/foodRetailing_wo_rangeConstraint/input/CarrierLEH_v2_withFleet_Shipment_OneTW_PickupTime_ICEVandBEV.xml";
String vehicleTypeFile = "https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/berlin/projects/freight/foodRetailing_wo_rangeConstraint/input/vehicleTypesBVWP100_DC_noTax.xml";

CarrierVehicleTypes vehicleTypes = new CarrierVehicleTypes();
CarrierVehicleTypeReader vehicleTypeReader = new CarrierVehicleTypeReader(vehicleTypes);
Expand All @@ -129,43 +139,97 @@ private static LSP createLSP(Scenario scenario) {
carrierReader.readFile(carrierPlanFile);

Carrier carrier = carriers.getCarriers().get(Id.create("kaufland_VERBRAUCHERMARKT_TROCKEN", CarrierImpl.class));
// Depotlink vielleicht lieber von den Fahrzeugen nehmen, ist logischer
Id<Link> depotLink = carrier.getShipments().values().iterator().next().getFrom();
// Id<Link> depotLinkFromShipments = carrier.getShipments().values().iterator().next().getFrom();
Id<Link> depotLinkFromVehicles = carrier.getCarrierCapabilities().getCarrierVehicles().values().iterator().next().getLinkId();

log.info("create LSP");

// Create a lsp plan with direct delivery
LSPPlan singleOneEchelonChainPlan;
LSPPlan multipleMixedEchelonChainsPlan;
{
LogisticChain singleChain;
LogisticChain directChain;
{
Carrier singleCarrier = CarrierUtils.createCarrier(Id.create("singleCarrier", Carrier.class));
singleCarrier.getCarrierCapabilities().setFleetSize(CarrierCapabilities.FleetSize.INFINITE);

CarrierUtils.addCarrierVehicle(singleCarrier, CarrierVehicle.newInstance(Id.createVehicleId("singleCarrier"), depotLink, vehicleTypes.getVehicleTypes().get(Id.create("heavy40t", VehicleType.class))));
CarrierUtils.addCarrierVehicle(singleCarrier, CarrierVehicle.newInstance(Id.createVehicleId("singleCarrier"), depotLinkFromVehicles, vehicleTypes.getVehicleTypes().get(Id.create("heavy40t", VehicleType.class))));
LSPResource singleCarrierResource = DistributionCarrierUtils.DistributionCarrierResourceBuilder.newInstance(singleCarrier, scenario.getNetwork())
.setDistributionScheduler(DistributionCarrierUtils.createDefaultDistributionCarrierScheduler())
.build();

LogisticChainElement singleCarrierElement = LSPUtils.LogisticChainElementBuilder.newInstance(Id.create("singleCarrierElement", LogisticChainElement.class))
LogisticChainElement singleCarrierElement = LSPUtils.LogisticChainElementBuilder.newInstance(Id.create("directCarrierElement", LogisticChainElement.class))
.setResource(singleCarrierResource)
.build();

singleChain = LSPUtils.LogisticChainBuilder.newInstance(Id.create("singleChain", LogisticChain.class))
directChain = LSPUtils.LogisticChainBuilder.newInstance(Id.create("directChain", LogisticChain.class))
.addLogisticChainElement(singleCarrierElement)
.build();
}

singleOneEchelonChainPlan = LSPUtils.createLSPPlan()
.addLogisticChain(singleChain)
.setAssigner(MultipleChainsUtils.createPrimaryLogisticChainShipmentAssigner());
LogisticChain hubChain;
{
Carrier mainCarrier = CarrierUtils.createCarrier(Id.create("mainCarrier", Carrier.class));
mainCarrier.getCarrierCapabilities().setFleetSize(CarrierCapabilities.FleetSize.INFINITE);

CarrierUtils.addCarrierVehicle(mainCarrier, CarrierVehicle.newInstance(Id.createVehicleId("mainTruck"), depotLinkFromVehicles, vehicleTypes.getVehicleTypes().get(Id.create("heavy40t", VehicleType.class))));
LSPResource mainCarrierResource = MainRunCarrierUtils.MainRunCarrierResourceBuilder.newInstance(mainCarrier, scenario.getNetwork())
.setFromLinkId(depotLinkFromVehicles)
.setMainRunCarrierScheduler(MainRunCarrierUtils.createDefaultMainRunCarrierScheduler())
.setToLinkId(HUB_LINK_ID)
.setVehicleReturn(ResourceImplementationUtils.VehicleReturn.returnToFromLink)
.build();

LogisticChainElement mainCarrierElement = LSPUtils.LogisticChainElementBuilder.newInstance(Id.create("mainCarrierElement", LogisticChainElement.class))
.setResource(mainCarrierResource)
.build();

LSPResourceScheduler hubScheduler = TranshipmentHubUtils.TranshipmentHubSchedulerBuilder.newInstance()
.setCapacityNeedFixed(10)
.setCapacityNeedLinear(1)
.build();

LSPResource hubResource = TranshipmentHubUtils.TransshipmentHubBuilder.newInstance(Id.create("Hub", LSPResource.class), HUB_LINK_ID, scenario)
.setTransshipmentHubScheduler(hubScheduler)
.build();

LSPUtils.setFixedCost(hubResource, HUBCOSTS_FIX);

LogisticChainElement hubElement = LSPUtils.LogisticChainElementBuilder.newInstance(Id.create("HubElement", LogisticChainElement.class))
.setResource(hubResource)
.build();

Carrier distributionCarrier = CarrierUtils.createCarrier(Id.create("distributionCarrier", Carrier.class));
distributionCarrier.getCarrierCapabilities().setFleetSize(CarrierCapabilities.FleetSize.INFINITE);

CarrierUtils.addCarrierVehicle(distributionCarrier, CarrierVehicle.newInstance(Id.createVehicleId("distributionTruck"), HUB_LINK_ID, vehicleTypes.getVehicleTypes().get(Id.create("heavy40t_electro", VehicleType.class))));
LSPResource distributionCarrierResource = DistributionCarrierUtils.DistributionCarrierResourceBuilder.newInstance(distributionCarrier, scenario.getNetwork())
.setDistributionScheduler(DistributionCarrierUtils.createDefaultDistributionCarrierScheduler())
.build();

LogisticChainElement distributionCarrierElement = LSPUtils.LogisticChainElementBuilder.newInstance(Id.create("distributionCarrierElement", LogisticChainElement.class))
.setResource(distributionCarrierResource)
.build();

mainCarrierElement.connectWithNextElement(hubElement);
hubElement.connectWithNextElement(distributionCarrierElement);

hubChain = LSPUtils.LogisticChainBuilder.newInstance(Id.create("hubChain", LogisticChain.class))
.addLogisticChainElement(mainCarrierElement)
.addLogisticChainElement(hubElement)
.addLogisticChainElement(distributionCarrierElement)
.build();
}

multipleMixedEchelonChainsPlan = LSPUtils.createLSPPlan()
.addLogisticChain(directChain)
.addLogisticChain(hubChain)
.setAssigner(MultipleChainsUtils.createRandomLogisticChainShipmentAssigner());
}

List<LSPPlan> lspPlans = new ArrayList<>();
lspPlans.add(singleOneEchelonChainPlan);
lspPlans.add(multipleMixedEchelonChainsPlan);

LSP lsp = LSPUtils.LSPBuilder.getInstance(Id.create("myLSP", LSP.class))
.setInitialPlan(singleOneEchelonChainPlan)
.setInitialPlan(multipleMixedEchelonChainsPlan)
.setLogisticChainScheduler(ResourceImplementationUtils.createDefaultSimpleForwardLogisticChainScheduler(createResourcesListFromLSPPlans(lspPlans)))
.build();

Expand All @@ -178,7 +242,6 @@ private static LSP createLSP(Scenario scenario) {
log.info("schedule the LSP with the shipments and according to the scheduler of the Resource");
lsp.scheduleLogisticChains();


return lsp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ExampleMultipleMixedEchelonChains {

private static final Logger log = LogManager.getLogger(ExampleMultipleMixedEchelonChains.class);

private static final AssignerSetting assignerSetting = AssignerSetting.roundRobin;
private static final AssignerSetting assignerSetting = AssignerSetting.primary;
enum AssignerSetting {primary, roundRobin}

private static final double TOLL_VALUE = 1000;
Expand Down Expand Up @@ -86,7 +86,7 @@ public void install() {
controler.addOverridingModule(new AbstractModule() {
@Override
public void install() {
final MyEventBasedCarrierScorer carrierScorer = new MyEventBasedCarrierScorer();
final EventBasedCarrierScorer_MultipleChains carrierScorer = new EventBasedCarrierScorer_MultipleChains();
bind(CarrierScoringFunctionFactory.class).toInstance(carrierScorer);
carrierScorer.setToll(TOLL_VALUE);
bind(LSPScorerFactory.class).toInstance( () -> new MyLSPScorer());
Expand Down Expand Up @@ -120,8 +120,8 @@ private static Config prepareConfig(String[] args) {
}
ConfigUtils.applyCommandline(config,args);
} else {
config.controler().setOutputDirectory("output/multipleMixedEchelonChains_" + "_" + assignerSetting);
config.controler().setLastIteration(2);
config.controler().setOutputDirectory("output/multipleMixedEchelonChains_" + assignerSetting);
config.controler().setLastIteration(0);
}
config.network().setInputFile(String.valueOf(IOUtils.extendUrl(ExamplesUtils.getTestScenarioURL("freight-chessboard-9x9"), "grid9x9.xml")));
config.controler().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void install() {
controler.addOverridingModule(new AbstractModule() {
@Override
public void install() {
final MyEventBasedCarrierScorer carrierScorer = new MyEventBasedCarrierScorer();
final EventBasedCarrierScorer_MultipleChains carrierScorer = new EventBasedCarrierScorer_MultipleChains();
bind(CarrierScoringFunctionFactory.class).toInstance(carrierScorer);
bind(LSPScorerFactory.class).toInstance(MyLSPScorer::new);
bind(CarrierStrategyManager.class).toProvider(() -> {
Expand Down
Loading

0 comments on commit caa4727

Please sign in to comment.