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

Commit

Permalink
Merge pull request #287 from matsim-vsp/kmt_2echelon4Diss
Browse files Browse the repository at this point in the history
More forward using Roadpricing also for scoring
  • Loading branch information
kt86 authored Aug 8, 2024
2 parents d9b2536 + d5fb110 commit 744db00
Show file tree
Hide file tree
Showing 11 changed files with 467 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
*********************************************************************** *
* project: org.matsim.*
* *
* *********************************************************************** *
* *
* copyright : (C) 2024 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* ***********************************************************************
*/

package org.matsim.freight.logistics.analysis;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.events.VehicleEntersTrafficEvent;
import org.matsim.api.core.v01.events.VehicleLeavesTrafficEvent;
import org.matsim.api.core.v01.events.handler.VehicleEntersTrafficEventHandler;
import org.matsim.api.core.v01.events.handler.VehicleLeavesTrafficEventHandler;
import org.matsim.api.core.v01.population.Person;
import org.matsim.vehicles.Vehicle;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Basic event handler that collects the relation between vehicles and drivers.
* Necessary since link enter and leave events do not contain the driver anymore.
* <p>
* This is the vice-versa implementation of {@link org.matsim.core.events.algorithms.Vehicle2DriverEventHandler}.
* <p>
* In a first step only used internally. When needed more often, I have nothing against putting it more central. -> matsim-libs
*
* @author kturner
*/
public class Driver2VehicleEventHandler implements VehicleEntersTrafficEventHandler, VehicleLeavesTrafficEventHandler {

private final Map<Id<Person>, Id<Vehicle>> driversVehicles = new ConcurrentHashMap<>();

@Override
public void reset(int iteration) {
driversVehicles.clear();
}

@Override
public void handleEvent(VehicleEntersTrafficEvent event) {
driversVehicles.put(event.getPersonId(), event.getVehicleId());
}

@Override
public void handleEvent(VehicleLeavesTrafficEvent event) {
driversVehicles.remove(event.getPersonId());
}

/**
* @param personId the unique driver identifier.
* @return vehicle id of the driver's vehicle
*/
public Id<Vehicle> getVehicleOfDriver(Id<Person> personId) {
return driversVehicles.get(personId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
*********************************************************************** *
* project: org.matsim.*
* *
* *********************************************************************** *
* *
* copyright : (C) 2024 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* ***********************************************************************
*/

package org.matsim.freight.logistics.analysis;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.matsim.api.core.v01.Id;
import org.matsim.freight.carriers.Carrier;
import org.matsim.freight.carriers.events.CarrierTourEndEvent;
import org.matsim.freight.carriers.events.CarrierTourStartEvent;
import org.matsim.freight.carriers.events.eventhandler.CarrierTourEndEventHandler;
import org.matsim.freight.carriers.events.eventhandler.CarrierTourStartEventHandler;
import org.matsim.vehicles.Vehicle;

/**
* Basic event handler that collects the relation between vehicles and carrier.
* Necessary since there is no event having all this information together.
* <p>
* This is a modified implementation of {@link org.matsim.core.events.algorithms.Vehicle2DriverEventHandler}.
* <p>
* In a first step only used internally. When needed more often, I have nothing against putting it more central. -> matsim-libs
*
* @author kturner
*/
public class Vehicle2CarrierEventHandler implements CarrierTourStartEventHandler, CarrierTourEndEventHandler {

private final Map<Id<Vehicle>, Id<Carrier>> vehicle2carrier = new ConcurrentHashMap<>();

@Override
public void reset(int iteration) {
vehicle2carrier.clear();
}

@Override
public void handleEvent(CarrierTourStartEvent event) {
vehicle2carrier.put(event.getVehicleId(), event.getCarrierId());
}

@Override
public void handleEvent(CarrierTourEndEvent event) {
vehicle2carrier.remove(event.getVehicleId());
}

/**
* @param vehicleId the unique vehicle Id
* @return id of the vehicle's carrier
*/
public Id<Carrier> getCarrierOfVehicle(Id<Vehicle> vehicleId) {
return vehicle2carrier.get(vehicleId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ private static LSP createInitialLSP(Scenario scenario) {
CarrierVehicle.newInstance(vollectionVehicleId, collectionLinkId, collectionType);

CarrierCapabilities.Builder capabilitiesBuilder = CarrierCapabilities.Builder.newInstance();
capabilitiesBuilder.addType(collectionType);
capabilitiesBuilder.addVehicle(carrierVehicle);
capabilitiesBuilder.setFleetSize(FleetSize.INFINITE);
CarrierCapabilities capabilities = capabilitiesBuilder.build();
Expand Down Expand Up @@ -142,7 +141,6 @@ private static LSP createInitialLSP(Scenario scenario) {

CarrierCapabilities mainRunCapabilities =
CarrierCapabilities.Builder.newInstance()
.addType(mainRunType)
.addVehicle(mainRunCarrierVehicle)
.setFleetSize(FleetSize.INFINITE)
.build();
Expand Down Expand Up @@ -210,7 +208,6 @@ private static LSP createInitialLSP(Scenario scenario) {

CarrierCapabilities.Builder distributionCapabilitiesBuilder =
CarrierCapabilities.Builder.newInstance();
distributionCapabilitiesBuilder.addType(distributionType);
distributionCapabilitiesBuilder.addVehicle(distributionCarrierVehicle);
distributionCapabilitiesBuilder.setFleetSize(FleetSize.INFINITE);
CarrierCapabilities distributionCapabilities = distributionCapabilitiesBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.events.Event;
import org.matsim.api.core.v01.events.LinkEnterEvent;
import org.matsim.api.core.v01.events.*;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.population.Person;
import org.matsim.core.scoring.ScoringFunction;
import org.matsim.core.scoring.SumScoringFunction;
import org.matsim.freight.carriers.Carrier;
import org.matsim.freight.carriers.Tour;
import org.matsim.freight.carriers.controler.CarrierScoringFunctionFactory;
import org.matsim.freight.carriers.events.CarrierTourEndEvent;
import org.matsim.freight.carriers.events.CarrierTourStartEvent;
import org.matsim.freight.logistics.analysis.Driver2VehicleEventHandler;
import org.matsim.freight.logistics.analysis.Vehicle2CarrierEventHandler;
import org.matsim.vehicles.Vehicle;
import org.matsim.vehicles.VehicleType;
import org.matsim.vehicles.VehicleUtils;
Expand All @@ -46,17 +48,14 @@
*/
class EventBasedCarrierScorer4MultipleChains implements CarrierScoringFunctionFactory {



@Inject private Network network;

@Inject private Scenario scenario;


private double toll;
private List<String> tolledVehicleTypes = new ArrayList<>();
private List<String> tolledLinks = new ArrayList<>();


public ScoringFunction createScoringFunction(Carrier carrier) {
SumScoringFunction sf = new SumScoringFunction();
sf.addScoringFunction(new EventBasedScoring());
Expand Down Expand Up @@ -84,9 +83,6 @@ void setTolledLinks(List<String> tolledLinks) {
private class EventBasedScoring implements SumScoringFunction.ArbitraryEventScoring {

final Logger log = LogManager.getLogger(EventBasedScoring.class);
private final double MAX_SHIFT_DURATION = 8 * 3600;
private final Map<VehicleType, Double> vehicleType2TourDuration = new LinkedHashMap<>();
private final Map<VehicleType, Integer> vehicleType2ScoredFixCosts = new LinkedHashMap<>();
private final Map<Id<Tour>, Double> tourStartTime = new LinkedHashMap<>();
private double score;

Expand All @@ -105,21 +101,20 @@ public double getScore() {
@Override
public void handleEvent(Event event) {
log.debug(event.toString());
switch (event) {
case CarrierTourStartEvent freightTourStartEvent -> handleEvent(freightTourStartEvent);
case CarrierTourEndEvent freightTourEndEvent -> handleEvent(freightTourEndEvent);
case LinkEnterEvent linkEnterEvent -> handleEvent(linkEnterEvent);
default -> {
}
}
switch (event) {
case CarrierTourStartEvent carrierTourStartEvent -> handleEvent(carrierTourStartEvent);
case CarrierTourEndEvent carrierTourEndEvent -> handleEvent(carrierTourEndEvent);
case LinkEnterEvent linkEnterEvent -> handleEvent(linkEnterEvent);
default -> {}
}
}

private void handleEvent(CarrierTourStartEvent event) {
// Save time of freight tour start
tourStartTime.put(event.getTourId(), event.getTime());
}

// Fix costs for vehicle usage
// scores fix costs for vehicle usage and variable costs per time
private void handleEvent(CarrierTourEndEvent event) {
// Fix costs for vehicle usage
final VehicleType vehicleType =
Expand All @@ -134,6 +129,7 @@ private void handleEvent(CarrierTourEndEvent event) {
score = score - (tourDuration * vehicleType.getCostInformation().getCostsPerSecond());
}

// scores variable costs per distance
private void handleEvent(LinkEnterEvent event) {
final double distance = network.getLinks().get(event.getLinkId()).getLength();
final double costPerMeter =
Expand Down Expand Up @@ -187,14 +183,15 @@ private void handleEvent(LinkEnterEvent event) {
(VehicleUtils.findVehicle(event.getVehicleId(), scenario)).getType().getId();

// toll a vehicle only once.
if (!tolledVehicles.contains(event.getVehicleId()))
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;
}
}
}
}
}
}
Loading

0 comments on commit 744db00

Please sign in to comment.