Skip to content

Commit

Permalink
renamed classes
Browse files Browse the repository at this point in the history
  • Loading branch information
paulheinr committed Feb 13, 2025
1 parent 6771f5a commit ac123b9
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.util.Map;

public class BellochePenaltyFunction implements PenaltyFunction {
public class BellochePenaltyFunction implements ParkingSearchTimeFunction {
private static final Logger log = LogManager.getLogger(BellochePenaltyFunction.class);
private final double alpha;
private final double beta;
Expand All @@ -19,7 +19,7 @@ public BellochePenaltyFunction(double alpha, double beta) {
}

@Override
public double calculatePenalty(Map<Id<Link>, ParkingCount> parkingCount) {
public double calculateParkingSearchTime(Map<Id<Link>, ParkingCount> parkingCount) {
Tuple<Double, Double> weightedOccK = getWeightedOccK(parkingCount);

if (weightedOccK.getSecond() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import java.util.*;
import java.util.stream.Collectors;

import static org.matsim.run.scoring.parking.ParkingObserver.LINK_OFF_STREET_SPOTS;
import static org.matsim.run.scoring.parking.ParkingObserver.LINK_ON_STREET_SPOTS;
import static org.matsim.run.scoring.parking.ParkingTimeEstimator.LINK_OFF_STREET_SPOTS;
import static org.matsim.run.scoring.parking.ParkingTimeEstimator.LINK_ON_STREET_SPOTS;

public class EventBasedParkingCapacityInitializer implements ParkingCapacityInitializer {
@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public void install() {

// bind parking classes
bind(KernelFunction.class).to(ConstantKernelFunction.class);
bind(PenaltyFunction.class).toInstance(new BellochePenaltyFunction(0.4, -6));
bind(ParkingSearchTimeFunction.class).toInstance(new BellochePenaltyFunction(0.4, -6));
bind(ParkingCapacityInitializer.class).to(ZeroParkingCapacityInitializer.class);

bind(ParkingObserver.class).in(Singleton.class);
bind(ParkingTimeEstimator.class).in(Singleton.class);
bind(ParkingEventsHandler.class).in(Singleton.class);
addEventHandlerBinding().to(ParkingEventsHandler.class);
addControlerListenerBinding().to(ParkingObserver.class);
addControlerListenerBinding().to(ParkingTimeEstimator.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

import java.util.Map;

public interface PenaltyFunction {
double calculatePenalty(Map<Id<Link>, ParkingCount> parkingCount);
public interface ParkingSearchTimeFunction {
double calculateParkingSearchTime(Map<Id<Link>, ParkingCount> parkingCount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,31 @@

import java.util.*;

public class ParkingObserver implements AfterMobsimListener {
public class ParkingTimeEstimator implements AfterMobsimListener {
static final String LINK_ON_STREET_SPOTS = "onstreet_spots";
static final String LINK_OFF_STREET_SPOTS = "offstreet_spots";

@Inject
Network network;

@Inject
KernelFunction kernelFunction;

@Inject
PenaltyFunction penaltyFunction;
ParkingSearchTimeFunction parkingSearchTimeFunction;

@Inject
EventsManager eventsManager;

@Inject
ActivityEstimator activityEstimator;

@Inject
LegEstimator legEstimator;

@Inject
ParkingCapacityInitializer parkingCapacityInitializer;

@Inject
ParkingEventsHandler parkingEventsHandler;

Expand All @@ -55,6 +61,30 @@ public void notifyAfterMobsim(AfterMobsimEvent event) {
run();
}

private void initializeParking() {
int counter = 0;
indexByLinkId = new HashMap<>(network.getLinks().size());

for (Id<Link> id : network.getLinks().keySet()) {
indexByLinkId.put(id, counter++);
}

int linkCount = network.getLinks().size();
capacity = new int[linkCount];
parkingCount = new int[linkCount];

Map<Id<Link>, ParkingCapacityInitializer.ParkingInitialCapacity> initialize =
parkingCapacityInitializer.initialize(parkingEventsHandler.getVehicleEntersTrafficEvents(), parkingEventsHandler.getVehicleLeavesTrafficEvents());

for (Link link : network.getLinks().values()) {
ParkingCapacityInitializer.ParkingInitialCapacity parkingInitialCapacity = initialize.get(link.getId());
int index = indexByLinkId.get(link.getId());

capacity[index] = parkingInitialCapacity.capacity();
parkingCount[index] = parkingInitialCapacity.initial();
}
}

private void run() {
List<VehicleLeavesTrafficEvent> vehicleLeavesTrafficEvents = parkingEventsHandler.getVehicleLeavesTrafficEvents();
List<VehicleEntersTrafficEvent> vehicleEntersTrafficEvents = parkingEventsHandler.getVehicleEntersTrafficEvents();
Expand Down Expand Up @@ -92,7 +122,7 @@ private void handleEvent(VehicleLeavesTrafficEvent event) {
Map<Id<Link>, Double> weightedLinks = kernelFunction.calculateKernel(network.getLinks().get(event.getLinkId()), kernelDistance);
Map<Id<Link>, ParkingCount> parkingCounts = applyWeights(weightedLinks);

double penalty = penaltyFunction.calculatePenalty(parkingCounts);
double penalty = parkingSearchTimeFunction.calculateParkingSearchTime(parkingCounts);
applyPenalty(event.getTime(), event.getPersonId(), penalty, event.getLinkId());

parkVehicle(event.getLinkId());
Expand All @@ -115,36 +145,12 @@ private Map<Id<Link>, ParkingCount> applyWeights(Map<Id<Link>, Double> weightedL
return result;
}

private void applyPenalty(double time, Id<Person> personId, double penaltyInSec, Id<Link> linkId) {
private void applyPenalty(double time, Id<Person> personId, double parkingSearchTime, Id<Link> linkId) {
//TODO convert penalty to money

double score = 0.0;

PersonScoreEvent personScoreEvent = new PersonScoreEvent(time, personId, score, "parking");
PersonScoreEvent personScoreEvent = new PersonScoreEvent(time, personId, score, "parking" + parkingSearchTime);
eventsManager.processEvent(personScoreEvent);
}

private void initializeParking() {
int counter = 0;
indexByLinkId = new HashMap<>(network.getLinks().size());

for (Id<Link> id : network.getLinks().keySet()) {
indexByLinkId.put(id, counter++);
}

int linkCount = network.getLinks().size();
capacity = new int[linkCount];
parkingCount = new int[linkCount];

Map<Id<Link>, ParkingCapacityInitializer.ParkingInitialCapacity> initialize =
parkingCapacityInitializer.initialize(parkingEventsHandler.getVehicleEntersTrafficEvents(), parkingEventsHandler.getVehicleLeavesTrafficEvents());

for (Link link : network.getLinks().values()) {
ParkingCapacityInitializer.ParkingInitialCapacity parkingInitialCapacity = initialize.get(link.getId());
int index = indexByLinkId.get(link.getId());

capacity[index] = parkingInitialCapacity.capacity();
parkingCount[index] = parkingInitialCapacity.initial();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.util.Map;
import java.util.Optional;

import static org.matsim.run.scoring.parking.ParkingObserver.LINK_OFF_STREET_SPOTS;
import static org.matsim.run.scoring.parking.ParkingObserver.LINK_ON_STREET_SPOTS;
import static org.matsim.run.scoring.parking.ParkingTimeEstimator.LINK_OFF_STREET_SPOTS;
import static org.matsim.run.scoring.parking.ParkingTimeEstimator.LINK_ON_STREET_SPOTS;

public class ZeroParkingCapacityInitializer implements ParkingCapacityInitializer {
private Network network;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

import java.util.Map;

class BellochePenaltyFunctionTest {
class BellocheParkingSearchTimeFunctionTest {

@Test
void everyLinkFull() {
BellochePenaltyFunction bellochePenaltyFunction = new BellochePenaltyFunction(0.4, -6);
double penalty = bellochePenaltyFunction.calculatePenalty(Map.of(getDummyLinkId(), new ParkingCount(1, 1, 1)));
double penalty = bellochePenaltyFunction.calculateParkingSearchTime(Map.of(getDummyLinkId(), new ParkingCount(1, 1, 1)));
Assertions.assertEquals(0.4 * Math.exp(6), penalty); //161.37
}

@Test
void everyLinkEmpty() {
BellochePenaltyFunction bellochePenaltyFunction = new BellochePenaltyFunction(0.4, -6);
double penalty = bellochePenaltyFunction.calculatePenalty(Map.of(getDummyLinkId(), new ParkingCount(0, 1, 1)));
double penalty = bellochePenaltyFunction.calculateParkingSearchTime(Map.of(getDummyLinkId(), new ParkingCount(0, 1, 1)));
Assertions.assertEquals(0.4, penalty);
}

@Test
void zeroCapacity() {
BellochePenaltyFunction bellochePenaltyFunction = new BellochePenaltyFunction(0.4, -6);
double penalty = bellochePenaltyFunction.calculatePenalty(Map.of(getDummyLinkId(), new ParkingCount(0, 0, 1)));
double penalty = bellochePenaltyFunction.calculateParkingSearchTime(Map.of(getDummyLinkId(), new ParkingCount(0, 0, 1)));
Assertions.assertEquals(0.4 * Math.exp(6), penalty); //161.37
}

Expand All @@ -40,7 +40,7 @@ void weightedSum() {
double weight2 = 0.8;

BellochePenaltyFunction bellochePenaltyFunction = new BellochePenaltyFunction(0.4, -6);
double penalty = bellochePenaltyFunction.calculatePenalty(Map.of(getDummyLinkId(), new ParkingCount(occ1, cap1, weight1), Id.createLinkId("dummyLinkId2"), new ParkingCount(occ2, cap2, weight2)));
double penalty = bellochePenaltyFunction.calculateParkingSearchTime(Map.of(getDummyLinkId(), new ParkingCount(occ1, cap1, weight1), Id.createLinkId("dummyLinkId2"), new ParkingCount(occ2, cap2, weight2)));

double weightedOcc = occ1 * weight1 + occ2 * weight2;
double weightedK = cap1 * weight1 + cap2 * weight2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ private Network getNetwork() {
// Chessboard network has 10x10 nodes, starting from (0,0) to (9000,9000). The distance between two adjacent nodes is 1000 on the x- or y-axis.
Network network = NetworkUtils.readNetwork(testUtils.getPackageInputDirectory() + "./chessboard_network.xml");
network.getLinks().values().forEach(l -> {
l.getAttributes().putAttribute(ParkingObserver.LINK_ON_STREET_SPOTS, Integer.valueOf(l.getId().toString()));
l.getAttributes().putAttribute(ParkingObserver.LINK_OFF_STREET_SPOTS, 0);
l.getAttributes().putAttribute(ParkingTimeEstimator.LINK_ON_STREET_SPOTS, Integer.valueOf(l.getId().toString()));
l.getAttributes().putAttribute(ParkingTimeEstimator.LINK_OFF_STREET_SPOTS, 0);
});

return network;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import java.util.List;

class ParkingObserverTest {
class ParkingTimeEstimatorTest {
@RegisterExtension
MatsimTestUtils testUtils = new MatsimTestUtils();

Expand Down Expand Up @@ -87,7 +87,7 @@ void testScoreEvents_onlyOffStreetSpots() {
}

private void runAndCheckEvents() {
injector.getInstance(ParkingObserver.class).notifyAfterMobsim(new AfterMobsimEvent(null, 0, false));
injector.getInstance(ParkingTimeEstimator.class).notifyAfterMobsim(new AfterMobsimEvent(null, 0, false));
injector.getInstance(TestHandler.class).checkAllEventsProcessed();
}

Expand Down Expand Up @@ -117,9 +117,9 @@ public void install() {

private void prepareNetwork(Network network, boolean offStreet) {
network.getLinks().values().forEach(l -> {
l.getAttributes().putAttribute(ParkingObserver.LINK_ON_STREET_SPOTS, 1);
l.getAttributes().putAttribute(ParkingTimeEstimator.LINK_ON_STREET_SPOTS, 1);
if (offStreet) {
l.getAttributes().putAttribute(ParkingObserver.LINK_OFF_STREET_SPOTS, 1);
l.getAttributes().putAttribute(ParkingTimeEstimator.LINK_OFF_STREET_SPOTS, 1);
}
});
}
Expand Down

0 comments on commit ac123b9

Please sign in to comment.