Skip to content

Commit

Permalink
Merge pull request #2891 from matsim-org/developParkingSearch
Browse files Browse the repository at this point in the history
Develop parking search
  • Loading branch information
rewertvsp authored Oct 20, 2023
2 parents 8e64186 + cffffc9 commit e3075f8
Show file tree
Hide file tree
Showing 13 changed files with 879 additions and 456 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package org.matsim.contrib.parking.parkingsearch.DynAgent;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.Plan;
import org.matsim.contrib.parking.parkingsearch.events.RemoveParkingActivityEvent;
import org.matsim.contrib.parking.parkingsearch.events.ReserveParkingLocationEvent;
import org.matsim.contrib.parking.parkingsearch.events.SelectNewParkingLocationEvent;
import org.matsim.contrib.parking.parkingsearch.events.StartParkingSearchEvent;
import org.matsim.contrib.parking.parkingsearch.ParkingUtils;
import org.matsim.contrib.parking.parkingsearch.events.*;
import org.matsim.contrib.parking.parkingsearch.manager.FacilityBasedParkingManager;
import org.matsim.contrib.parking.parkingsearch.manager.ParkingSearchManager;
import org.matsim.contrib.parking.parkingsearch.search.NearestParkingSpotSearchLogic;
import org.matsim.contrib.parking.parkingsearch.search.ParkingSearchLogic;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.mobsim.framework.MobsimTimer;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.routes.NetworkRoute;
import org.matsim.core.utils.collections.Tuple;
import org.matsim.vehicles.Vehicle;
Expand All @@ -26,6 +27,7 @@
*/
public class NearestParkingDynLeg extends ParkingDynLeg {
private boolean parkingAtEndOfLeg = true;
private boolean passangerInteractionAtParkingFacilityAtEndOfLeg = false;
private boolean reachedDestinationWithoutParking = false;
private boolean alreadyReservedParking = false;
private boolean driveToBaseWithoutParking = false;
Expand All @@ -34,18 +36,19 @@ public class NearestParkingDynLeg extends ParkingDynLeg {
private final int planIndexNextActivity;
private Plan plan;
private Id<Link> nextSelectedParkingLink = null;
protected static final Logger log = LogManager.getLogger(NearestParkingDynLeg.class);

public NearestParkingDynLeg(Leg currentPlannedLeg, NetworkRoute route, Plan plan, int planIndexNextActivity, ParkingSearchLogic logic,
ParkingSearchManager parkingManager, Id<Vehicle> vehicleId, MobsimTimer timer, EventsManager events) {
super(currentPlannedLeg.getMode(), route, logic, parkingManager, vehicleId, timer, events);
this.followingActivity = (Activity) plan.getPlanElements().get(planIndexNextActivity);
followingActivity.setStartTime(timer.getTimeOfDay());
this.currentPlannedLeg = currentPlannedLeg;
this.plan = plan;
this.planIndexNextActivity = planIndexNextActivity;
if (followingActivity.getAttributes().getAsMap().containsKey("parking") && followingActivity.getAttributes().getAttribute("parking").equals(
"noParking"))
if (ParkingUtils.checkIfActivityHasNoParking(followingActivity))
parkingAtEndOfLeg = false;
if (ParkingUtils.checkIfActivityHasPassengerInteraction(followingActivity))
passangerInteractionAtParkingFacilityAtEndOfLeg = true;
}

@Override
Expand Down Expand Up @@ -85,7 +88,7 @@ public void movedOverNode(Id<Link> newLinkId) {
@Override
public Id<Link> getNextLinkId() {

if (!parkingMode && parkingAtEndOfLeg) {
if (!passangerInteractionAtParkingFacilityAtEndOfLeg && (!parkingMode && parkingAtEndOfLeg)) {
parkingMode = true;
this.events.processEvent(new StartParkingSearchEvent(timer.getTimeOfDay(), vehicleId, currentLinkId));
}
Expand All @@ -98,12 +101,18 @@ public Id<Link> getNextLinkId() {
return linkIds.get(currentLinkIdx + 1);

} else {
if (passangerInteractionAtParkingFacilityAtEndOfLeg && !hasFoundParking && followingActivity.getLinkId().equals(currentLinkId)) {
createWaitingActivityUntilPassengerInteractionIsPossible(currentLinkId, vehicleId, timer.getTimeOfDay());
this.events
.processEvent(new StartWaitingForParkingEvent(timer.getTimeOfDay(), vehicleId, currentLinkId));
}
if (hasFoundParking || reachedDestinationWithoutParking) {
// easy, we can just park where at our destination link
if (hasFoundParking) {
if (hasFoundParking && !passangerInteractionAtParkingFacilityAtEndOfLeg) {
//calculate parkingTime for parking_activity
double parkingDuration;
double expectedDrivingDurationToPickup;
double drivingDurationFromDropOff = timer.getTimeOfDay() - currentPlannedLeg.getDepartureTime().seconds();
double drivingDurationFromGetOff = timer.getTimeOfDay() - currentPlannedLeg.getDepartureTime().seconds();

if (nextSelectedParkingLink.equals(currentLinkId)) {
expectedDrivingDurationToPickup = ((NearestParkingSpotSearchLogic) this.logic).getExpectedTravelTime(
Expand All @@ -112,7 +121,9 @@ public Id<Link> getNextLinkId() {
expectedDrivingDurationToPickup = ((NearestParkingSpotSearchLogic) this.logic).getExpectedTravelTime(
currentPlannedLeg.getRoute().getStartLinkId(), timer.getTimeOfDay(), currentLinkId);
}
parkingDuration = followingActivity.getMaximumDuration().seconds() - drivingDurationFromDropOff - expectedDrivingDurationToPickup;
parkingDuration = followingActivity.getMaximumDuration().seconds()
- drivingDurationFromGetOff - expectedDrivingDurationToPickup
- ((FacilityBasedParkingManager) parkingManager).getParkStageActivityDuration();
followingActivity.setMaximumDuration(parkingDuration);
}
this.logic.reset();
Expand All @@ -125,10 +136,19 @@ public Id<Link> getNextLinkId() {
}
}
// need to find the next link
double nextPickupTime = followingActivity.getStartTime().seconds() + followingActivity.getMaximumDuration().seconds();
double maxParkingDuration = followingActivity.getMaximumDuration().seconds() - (followingActivity.getStartTime().seconds() - timer.getTimeOfDay());
double nextPickupTime;
double maxParkingDuration;
if (passangerInteractionAtParkingFacilityAtEndOfLeg){
nextPickupTime = 0.;
maxParkingDuration = followingActivity.getMaximumDuration().seconds();
}
else {
nextPickupTime = currentPlannedLeg.getDepartureTime().seconds() + followingActivity.getMaximumDuration().seconds();
maxParkingDuration = nextPickupTime - timer.getTimeOfDay();
}
Id<Link> nextLinkId = ((NearestParkingSpotSearchLogic) this.logic).getNextLink(currentLinkId, route.getEndLinkId(), vehicleId, mode,
timer.getTimeOfDay(), maxParkingDuration, nextPickupTime);
timer.getTimeOfDay(), maxParkingDuration, nextPickupTime, passangerInteractionAtParkingFacilityAtEndOfLeg,
followingActivity.getCoord());
if (((NearestParkingSpotSearchLogic) this.logic).isNextParkingActivitySkipped() && parkingAtEndOfLeg) {
removeNextActivityAndFollowingLeg();
parkingAtEndOfLeg = false;
Expand Down Expand Up @@ -163,9 +183,19 @@ public Id<Link> getNextLinkId() {
}
}

private void createWaitingActivityUntilPassengerInteractionIsPossible(Id<Link> newLinkId, Id<Vehicle> vehicleId, double now) {
Activity waitingActivity = PopulationUtils.createActivityFromLinkId(ParkingUtils.WaitingForParkingActivityType, newLinkId);
ParkingUtils.setNoParkingForActivity(waitingActivity);
plan.getPlanElements().add(planIndexNextActivity, waitingActivity);
hasFoundParking = true;
((FacilityBasedParkingManager) parkingManager).addVehicleForWaitingForParking(newLinkId, vehicleId, now);
}

private void removeNextActivityAndFollowingLeg() {
plan.getPlanElements().remove(planIndexNextActivity);
plan.getPlanElements().remove(planIndexNextActivity);
// log.info(
// plan.getPerson().getId().toString() + ": Parking activity after getOff point '" + ((Activity)plan.getPlanElements().get(planIndexNextActivity - 2)).getType() + "' is removed, because no parking facility was found.");
}

public boolean driveToBaseWithoutParking() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
*
*
*/
package org.matsim.contrib.parking.parkingsearch.DynAgent.agentLogic;

Expand All @@ -9,7 +9,6 @@
import org.matsim.api.core.v01.population.Route;
import org.matsim.contrib.dynagent.DynAction;
import org.matsim.contrib.parking.parkingsearch.DynAgent.BenensonDynLeg;
import org.matsim.contrib.parking.parkingsearch.DynAgent.agentLogic.ParkingAgentLogic;
import org.matsim.contrib.parking.parkingsearch.manager.ParkingSearchManager;
import org.matsim.contrib.parking.parkingsearch.manager.vehicleteleportationlogic.VehicleTeleportationLogic;
import org.matsim.contrib.parking.parkingsearch.routing.ParkingRouter;
Expand All @@ -29,37 +28,37 @@ public class BenensonParkingAgentLogic extends ParkingAgentLogic {
/**
* @param plan
* @param parkingManager
* @param walkLegFactory
* @param walkRouter
* @param parkingRouter
* @param events
* @param parkingLogic
* @param timer
* @param teleportationLogic
*/


public BenensonParkingAgentLogic(Plan plan, ParkingSearchManager parkingManager, RoutingModule walkRouter, Network network,
ParkingRouter parkingRouter, EventsManager events, ParkingSearchLogic parkingLogic, MobsimTimer timer,
VehicleTeleportationLogic teleportationLogic, ParkingSearchConfigGroup configGroup) {
super(plan, parkingManager, walkRouter, network, parkingRouter, events, parkingLogic, timer, teleportationLogic, configGroup);
}


@Override
protected DynAction nextStateAfterUnParkActivity(DynAction oldAction, double now) {
// we have unparked, now we need to get going by car again.

Leg currentPlannedLeg = (Leg) currentPlanElement;
Route plannedRoute = currentPlannedLeg.getRoute();
NetworkRoute actualRoute = this.parkingRouter.getRouteFromParkingToDestination(plannedRoute.getEndLinkId(), now, agent.getCurrentLinkId());
if ((this.parkingManager.unParkVehicleHere(currentlyAssignedVehicleId, agent.getCurrentLinkId(), now))||(isinitialLocation)){
if ((this.parkingManager.unParkVehicleHere(currentlyAssignedVehicleId, agent.getCurrentLinkId(), now))||(isInitialLocation)){
this.lastParkActionState = LastParkActionState.CARTRIP;
isinitialLocation = false;
isInitialLocation = false;
Leg currentLeg = (Leg) this.currentPlanElement;
//this could be Car, Carsharing, Motorcylce, or whatever else mode we have, so we want our leg to reflect this.
return new BenensonDynLeg(currentLeg.getMode(), actualRoute, parkingLogic, parkingManager, currentlyAssignedVehicleId, timer, events);
}
else throw new RuntimeException("parking location mismatch");

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
*
*
*/
package org.matsim.contrib.parking.parkingsearch.DynAgent.agentLogic;

Expand Down Expand Up @@ -31,22 +31,22 @@ public MemoryBasedParkingAgentLogic(Plan plan, ParkingSearchManager parkingManag
super(plan, parkingManager, walkRouter, network, parkingRouter, events, parkingLogic, timer, teleportationLogic, configGroup);
}


@Override
protected DynAction nextStateAfterUnParkActivity(DynAction oldAction, double now) {
// we have unparked, now we need to get going by car again.

Leg currentPlannedLeg = (Leg) currentPlanElement;
Route plannedRoute = currentPlannedLeg.getRoute();
NetworkRoute actualRoute = this.parkingRouter.getRouteFromParkingToDestination(plannedRoute.getEndLinkId(), now, agent.getCurrentLinkId());
if ((this.parkingManager.unParkVehicleHere(currentlyAssignedVehicleId, agent.getCurrentLinkId(), now))||(isinitialLocation)){
if ((this.parkingManager.unParkVehicleHere(currentlyAssignedVehicleId, agent.getCurrentLinkId(), now))||(isInitialLocation)){
this.lastParkActionState = LastParkActionState.CARTRIP;
isinitialLocation = false;
isInitialLocation = false;
Leg currentLeg = (Leg) this.currentPlanElement;
//this could be Car, Carsharing, Motorcylce, or whatever else mode we have, so we want our leg to reflect this.
return new DistanceMemoryDynLeg(currentLeg.getMode(), actualRoute, parkingLogic, parkingManager, currentlyAssignedVehicleId, timer, events);
}
else throw new RuntimeException("parking location mismatch");

}
}
Loading

0 comments on commit e3075f8

Please sign in to comment.