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

Fix scoring with toll #301

Merged
merged 6 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
<parent>
<groupId>org.matsim</groupId>
<artifactId>matsim-all</artifactId>
<!-- <version>2025.0-PR3390</version>-->
<version>2025.0-2024w36</version>
<version>2025.0-PR3443</version>
<!-- <version>2025.0-PR3390</version>-->
<!-- <version>2025.0-2024w36</version>-->
<!-- <version>2025.0-SNAPSHOT</version>-->
<relativePath/>
</parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.events.*;
import org.matsim.api.core.v01.events.handler.PersonMoneyEventHandler;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.population.Person;
import org.matsim.core.scoring.ScoringFunction;
Expand All @@ -37,12 +36,11 @@
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.CarrierServiceEndEvent;
import org.matsim.freight.carriers.events.CarrierServiceStartEvent;
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 @@ -60,8 +58,6 @@ public ScoringFunction createScoringFunction(Carrier carrier) {
this.carrierId = carrier.getId();
SumScoringFunction sf = new SumScoringFunction();
sf.addScoringFunction(new EventBasedScoring());
sf.addScoringFunction(new FindOtherEventsForDebuggingOnly_NO_Scoring());
sf.addScoringFunction(new TollMoneyScoringForDebuggingOnly_NO_Scoring());
return sf;
}

Expand Down Expand Up @@ -116,8 +112,7 @@ private void handleEvent(CarrierTourStartEvent event) {
private void handleEvent(CarrierTourEndEvent event) {
v2c.handleEvent(event);
// Fix costs for vehicle usage
final VehicleType vehicleType =
(VehicleUtils.findVehicle(event.getVehicleId(), scenario)).getType();
final VehicleType vehicleType = (VehicleUtils.findVehicle(event.getVehicleId(), scenario)).getType();

double tourDuration = event.getTime() - tourStartTime.get(event.getTourId());

Expand Down Expand Up @@ -147,84 +142,20 @@ private void handleEvent(PersonMoneyEvent event) {
double tollValue = 0;

if (event.getPurpose().equals("toll")) {
if (carrierId.equals(v2c.getCarrierOfVehicle(d2v.getVehicleOfDriver(event.getPersonId())))) {
Id<Vehicle> vehicleId = d2v.getVehicleOfDriver(event.getPersonId());
if (vehicleId != null) {
Id<Carrier> carrierIdOfVehicle = v2c.getCarrierOfVehicle(vehicleId);
if (carrierId.equals(carrierIdOfVehicle)) {
// toll a person only once.
if (!tolledPersons.contains(event.getPersonId())) {
log.info("Tolling caused by event: {}", event);
tolledPersons.add(event.getPersonId());
tollValue = event.getAmount();
if (!tolledPersons.contains(event.getPersonId())) {
log.info("Tolling caused by event: {}", event);
tolledPersons.add(event.getPersonId());
tollValue = event.getAmount();
}
}
score = score - tollValue;
}
}
score = score - tollValue;
}
}

/**
* Versuche nur mit dem Debugger die PersonMoneyEvents zu entdecken, die eigentlich da sein sollten.
*/
private class FindOtherEventsForDebuggingOnly_NO_Scoring implements ArbitraryEventScoring {

final Logger log = LogManager.getLogger(FindOtherEventsForDebuggingOnly_NO_Scoring.class);


public FindOtherEventsForDebuggingOnly_NO_Scoring() {
super();
}

@Override
public void finish() {}

@Override
public double getScore() {
return Double.MIN_VALUE;
}

@Override
public void handleEvent(Event event) {
log.debug(event.toString());

switch (event) {
case CarrierTourStartEvent carrierTourStartEvent -> {}
// case CarrierTourEndEvent carrierTourEndEvent -> {}
case CarrierServiceStartEvent carrierServiceStartEvent -> {}
case CarrierServiceEndEvent carrierServiceEndEvent -> {}
case LinkEnterEvent linkEnterEvent ->{}
case LinkLeaveEvent linkLeaveEvent ->{}
// case PersonMoneyEvent personMoneyEvent -> handleEvent(personMoneyEvent);
default -> {handleOtherEvent(event);}
}
}

private void handleOtherEvent(Event event) {
log.info("Found another event: {}", event.toString());
}

}


/**
* Versuche nur mit dem Debugger die Einträge aus den PersonMoneyEvents zu entdecken, die eigentlich da sein sollten.
*/
private static class TollMoneyScoringForDebuggingOnly_NO_Scoring implements SumScoringFunction.MoneyScoring, PersonMoneyEventHandler {

private double score = 0.0;
@Override
public void addMoney(double amount) {
// TODO Auto-generated method stub
}

@Override
public void finish() { }

@Override
public double getScore() {
return this.score;
}

@Override
public void handleEvent(PersonMoneyEvent event) {
// Todo: implement
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,12 @@ public static void main(String[] args) {
log.info("Add LSP(s) to the scenario");
Collection<LSP> lsps = new LinkedList<>();
lsps.add(createLspWithTwoChains(scenario, "Edeka", MultipleChainsUtils.createLSPShipmentsFromCarrierShipments(carrierEdeka), getDepotLinkFromVehicle(carrierEdeka), HUB_LINK_ID_NEUKOELLN, vehicleTypes, vehicleTypes, vehicleTypes));
lsps.add(createLspWithTwoChains(scenario, "Kaufland", MultipleChainsUtils.createLSPShipmentsFromCarrierShipments(carrierKaufland), getDepotLinkFromVehicle(carrierKaufland), HUB_LINK_ID_NEUKOELLN, vehicleTypes, vehicleTypes, vehicleTypes));
lsps.add(createLspWithDirectChain(scenario, "Edeka_DIRECT", MultipleChainsUtils.createLSPShipmentsFromCarrierShipments(carrierEdeka), getDepotLinkFromVehicle(carrierEdeka), vehicleTypes));
lsps.add(createLspWithDirectChain(scenario, "Kaufland_DIRECT", MultipleChainsUtils.createLSPShipmentsFromCarrierShipments(carrierKaufland), getDepotLinkFromVehicle(carrierKaufland), vehicleTypes));
// lsps.add(createLspWithTwoChains(scenario, "Kaufland", MultipleChainsUtils.createLSPShipmentsFromCarrierShipments(carrierKaufland), getDepotLinkFromVehicle(carrierKaufland), HUB_LINK_ID_NEUKOELLN, vehicleTypes, vehicleTypes, vehicleTypes));
// lsps.add(createLspWithDirectChain(scenario, "Edeka_DIRECT", MultipleChainsUtils.createLSPShipmentsFromCarrierShipments(carrierEdeka), getDepotLinkFromVehicle(carrierEdeka), vehicleTypes));
// lsps.add(createLspWithDirectChain(scenario, "Kaufland_DIRECT", MultipleChainsUtils.createLSPShipmentsFromCarrierShipments(carrierKaufland), getDepotLinkFromVehicle(carrierKaufland), vehicleTypes));
LSPUtils.addLSPs(scenario, new LSPs(lsps));




log.info("Prepare controler");
Controler controler = new Controler(scenario);
controler.addOverridingModule(
Expand Down Expand Up @@ -217,7 +215,7 @@ private static Config prepareConfig(String[] args) {
ConfigUtils.applyCommandline(config, args);
} else {
config.controller().setOutputDirectory(OUTPUT_DIRECTORY);
config.controller().setLastIteration(1);
config.controller().setLastIteration(0);
}

config.network().setInputFile(
Expand Down