Skip to content

Commit

Permalink
Merge branch 'master' into addROuterToPrebooking
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuehnel authored Jun 28, 2024
2 parents b49541f + bd9e865 commit b4b9429
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private void runCommand(Class<? extends MATSimAppCommand> clazz, Path input) thr
MATSimAppCommand command = clazz.getDeclaredConstructor().newInstance();
String[] args = this.args.get(clazz);
args = ArrayUtils.addAll(args, createArgs(clazz, args, input));
log.info("Running {} with arguments: {}", clazz, Arrays.toString(args));
log.info("Running {} with arguments: {}", clazz, String.join(" ", args));

command.execute(args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ final class TripByGroupAnalysis {
}
}

// Norm shares per instance of each group to sum of 1
for (Group group : this.groups) {

String norm = group.columns.get(0);
if (group.columns.size() > 1)
throw new UnsupportedOperationException("Multiple columns not supported yet");

Table df = group.data;
for (String label : df.stringColumn(norm).asSet()) {
DoubleColumn dist_group = df.doubleColumn("share");
Selection sel = df.stringColumn(norm).isEqualTo(label);
double total = dist_group.where(sel).sum();
if (total > 0)
dist_group.set(sel, dist_group.divide(total));
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,31 @@ public class GeneralParkingModule implements StartupListener, BeforeMobsimListen

private final Controler controler;
private ParkingScore parkingScoreManager;
// public final ParkingScore getParkingScoreManager() {
// return parkingScoreManager;
// }

public final void setParkingScoreManager(ParkingScore parkingScoreManager) {
this.parkingScoreManager = parkingScoreManager;
}

private ParkingInfrastructure parkingInfrastructureManager;
private ParkingChoiceSimulation parkingChoiceSimulation;

public GeneralParkingModule(Controler controler){
this.controler = controler ;

controler.addControlerListener(this);
}

@Override
public void notifyStartup(StartupEvent event) {

@Override public void notifyStartup(StartupEvent event) {
parkingChoiceSimulation = new ParkingChoiceSimulation(controler.getScenario(), parkingInfrastructureManager);
controler.getEvents().addHandler( parkingChoiceSimulation );
// controler.addControlerListener(parkingSimulation);
// was not doing anything there. kai, jul'15
}

// public final ParkingInfrastructure getParkingInfrastructure() {
// return parkingInfrastructureManager;
// }

public final void setParkingInfrastructurManager(ParkingInfrastructure parkingInfrastructureManager) {
this.parkingInfrastructureManager = parkingInfrastructureManager;
@Override public void notifyBeforeMobsim(BeforeMobsimEvent event) {
parkingScoreManager.notifyBeforeMobsim();
parkingInfrastructureManager.notifyBeforeMobsim();
parkingChoiceSimulation.notifyBeforeMobsim();
}

// @Deprecated
// // lower level objects may keep back pointers to higher level objects if they have to, but we prefer that they do not provide them
// // as a service. kai, apr'15
// public final Controler getControler() {
// return controler;
// }

@Override
public void notifyBeforeMobsim(BeforeMobsimEvent event) {
parkingScoreManager.prepareForNewIteration();
parkingInfrastructureManager.reset();
parkingChoiceSimulation.prepareForNewIteration();
public final void setParkingScoreManager(ParkingScore parkingScoreManager) {
this.parkingScoreManager = parkingScoreManager;
}

// protected final ParkingInfrastructure getParkingInfrastructureManager() {
// return parkingInfrastructureManager;
// }
//
// protected final ParkingChoiceSimulation getParkingSimulation() {
// return parkingSimulation;
// }
public final void setParkingInfrastructurManager(ParkingInfrastructure parkingInfrastructureManager) {
this.parkingInfrastructureManager = parkingInfrastructureManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface PC2Parking {

public Id<PC2Parking> getId();

public int getMaximumParkingCapacity();
// public int getMaximumParkingCapacity();

public int getAvailableParkingCapacity();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@

public class PublicParking implements PC2Parking {

private Id<PC2Parking> id = null;
private int capacity =0;
private final Id<PC2Parking> id;
private final int capacity;
private int availableParking=0;
private Coord coord=null;
private ParkingCostModel parkingCostModel=null;
private String groupName;
private final String groupName;

public PublicParking(Id<PC2Parking> id, int capacity, Coord coord, ParkingCostModel parkingCostModel, String groupName){
this.id=id;
Expand All @@ -56,14 +56,14 @@ public Coord getCoordinate(){
return coord;
}

public boolean isParkingAvailable(){
return availableParking>0;
}

@Override
public int getMaximumParkingCapacity(){
return capacity;
}
// public boolean isParkingAvailable(){
// return availableParking>0;
// }
//
// @Override
// public int getMaximumParkingCapacity(){
// return capacity;
// }

@Override
public int getAvailableParkingCapacity(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface ParkingScore {

void addScore(Id<Person> id, double incValue);

void prepareForNewIteration();
void notifyBeforeMobsim();

double getParkingScoreScalingFactor();

Expand All @@ -34,4 +34,4 @@ public interface ParkingScore {

void setRandomErrorTermManger(RandomErrorTermManager randomErrorTermManager);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public synchronized void addScore(Id<Person> id, double incValue) {
}

@Override
public synchronized void prepareForNewIteration() {
public synchronized void notifyBeforeMobsim() {
scores = new DoubleValueHashMap<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@
public final class ParkingChoiceSimulation
implements PersonDepartureEventHandler, PersonArrivalEventHandler, ActivityEndEventHandler {



private final ParkingInfrastructure parkingInfrastructureManager;
private final Scenario scenario;
private IntegerValueHashMap<Id<Person>> currentPlanElementIndex;
private IntegerValueHashMap<Id<Person>> currentPlanElementIndices;
private HashMap<Id<Person>, ParkingOperationRequestAttributes> parkingOperationRequestAttributes;
private DoubleValueHashMap<Id<Person>> firstDepartureTimeOfDay;

Expand All @@ -61,7 +59,7 @@ public ParkingChoiceSimulation(Scenario scenario, ParkingInfrastructure parkingI

@Override
public void handleEvent(ActivityEndEvent event) {
currentPlanElementIndex.increment(event.getPersonId());
currentPlanElementIndices.increment(event.getPersonId() );
}

@Override
Expand All @@ -76,25 +74,37 @@ public void handleEvent(PersonDepartureEvent event) {
}

if (isFirstCarDepartureOfDay(event.getPersonId())) {
// (special case, think about it later)
// for the first departure of the day, we do not know when the parking actually started. The scoring is, in
// consequence, not done here, but done when _starting_ the _last_ parking of the day (see there).
// (yy what happens if the agent arrives by car but departs by some other mode, or the other way round? Can happen in particular if there is an additional home stop during the day.)

ParkingOperationRequestAttributes parkingAttributes = new ParkingOperationRequestAttributes();
parkingAttributes.personId = event.getPersonId();
// this is a trick to get the correct departure time

// for the time being, we memorize a parking record with arrival time zero. This is corrected later, at the last arrival of the day.
parkingAttributes.arrivalTime = 0;
parkingAttributes.parkingDurationInSeconds = GeneralLib.getIntervalDuration(0, event.getTime());
parkingInfrastructureManager.personCarDepartureEvent(parkingAttributes);
} else {
// parking has just ended:

// finalize the corresponding record:
ParkingOperationRequestAttributes parkingAttributes = parkingOperationRequestAttributes.get(event.getPersonId());
parkingAttributes.parkingDurationInSeconds = GeneralLib.getIntervalDuration(parkingAttributes.arrivalTime, event.getTime());

// hedge against a special case:
if (parkingAttributes.parkingDurationInSeconds == 24 * 3600) {
// (yyyy no idea what this is and why. kai, jul'15)
// (Presumably, the code is such that it cannot handle a parking duration of zero. Presumably, a parking
// duration of zero cannot happen, and therefore this is ok. However, if someone parks for exactly 24 hours,
// then this is at some point mapped back to zero, and then it may happen. kai, feb'24)

parkingAttributes.parkingDurationInSeconds = 1; // not zero, because this might lead to NaN
}

PC2Parking parking = parkingInfrastructureManager.personCarDepartureEvent(parkingAttributes);
parkingInfrastructureManager.scoreParkingOperation(parkingAttributes, parking);
// score the parking:
final PC2Parking parking = parkingInfrastructureManager.personCarDepartureEvent( parkingAttributes );
parkingInfrastructureManager.scoreParkingOperation(parkingAttributes, parking );
}

}
Expand All @@ -106,7 +116,7 @@ public void handleEvent(PersonArrivalEvent event) {
if (event.getLegMode().equalsIgnoreCase(TransportMode.car) && !event.getPersonId().toString().contains("pt") && isNotTransitAgent(event.getPersonId())) {
// (exclude some cases (in a brittle way, i.e. based on IDs))

// I think that this just packages some information together into the parkingAttributes:
// Generate most of the parking record (departure time will be added later):
ParkingOperationRequestAttributes parkingAttributes = new ParkingOperationRequestAttributes();
{
Link link = scenario.getNetwork().getLinks().get( event.getLinkId() );
Expand All @@ -120,45 +130,50 @@ public void handleEvent(PersonArrivalEvent event) {
}

if (isLastCarLegOfDay(personId)) {
// (special case, think about it later)

// if this is the last arrival of the day, the parking record is already there, since it was generated at the first
// departure. However, the duration is not correct since at that time the arrival time was not known.
// (yy It looks to me that the arrivalTime remains at 0. why?)
parkingAttributes.parkingDurationInSeconds = GeneralLib.getIntervalDuration(event.getTime(), firstDepartureTimeOfDay.get(personId));

// scoring of this special case is done further down, see there

} else {
Activity activityBeforeNextCarLeg = getActivityBeforeNextCarLeg(personId);

double endTime= activityBeforeNextCarLeg.getEndTime().seconds();
double parkingDuration=0;

if (endTime==Double.NEGATIVE_INFINITY || endTime==Double.POSITIVE_INFINITY){
// (I think that this _can_ happen, especially in context of within-day replanning, if departure time is unknown. (*))
// (in general, we take the (parking) end time from above. Sometimes, this end time does not have a useful value, in which case the current

// try to estimate parking duration
// try to estimate parking duration:

Person person = scenario.getPopulation().getPersons().get(personId);
List<PlanElement> planElements = person.getSelectedPlan().getPlanElements();

for (int i = currentPlanElementIndex.get(personId); i < planElements.size(); i++) {
if (planElements.get(i) instanceof Activity) {
parkingDuration+= ((Activity)planElements.get(i)).getMaximumDuration().seconds();
for ( int ii = currentPlanElementIndices.get(personId ) ; ii < planElements.size(); ii++) {
if (planElements.get(ii) instanceof Activity) {
parkingDuration+= ((Activity)planElements.get(ii)).getMaximumDuration().seconds();
}

if (planElements.get(i) == activityBeforeNextCarLeg) {
if (planElements.get(ii) == activityBeforeNextCarLeg) {
endTime=event.getTime()+parkingDuration;
break;
}
}
}

parkingAttributes.parkingDurationInSeconds = GeneralLib.getIntervalDuration(event.getTime(), endTime);
// (This is the _estimated_ parking duration, since we are at arrival. This is needed to define the "best" parking
// (This is the _estimated_ parking duration, since we are at arrival, and in this special case we did not have the
// corresponding activity end time. This is needed to define the "best" parking
// location ... cf. short-term/long-term parking at airports. Could rename the attributed into "expected...", but we
// have seen at other places in the code that such attributes may change their interpretation based on context so will
// not do this here.)
}

parkingAttributes.legIndex = currentPlanElementIndex.get(personId);
parkingAttributes.legIndex = currentPlanElementIndices.get(personId );

PC2Parking parking = parkingInfrastructureManager.parkVehicle(parkingAttributes);
final PC2Parking parking = parkingInfrastructureManager.parkVehicle(parkingAttributes);
// to me this looks like first the agent arrives at his/her activity. And then the negative parking score is added after the
// fact, however without consuming time. I.e. there is no physics. kai, jul'15

Expand All @@ -171,15 +186,11 @@ public void handleEvent(PersonArrivalEvent event) {

}

currentPlanElementIndex.increment(personId);
currentPlanElementIndices.increment(personId );
}

private boolean isNotTransitAgent(Id<Person> persondId) {
return (Integer.parseInt(persondId.toString())< 1000000000);
}

public void prepareForNewIteration() {
currentPlanElementIndex = new IntegerValueHashMap<>();
public void notifyBeforeMobsim() {
currentPlanElementIndices = new IntegerValueHashMap<>();
parkingOperationRequestAttributes = new HashMap<>();
firstDepartureTimeOfDay = new DoubleValueHashMap<>();

Expand Down Expand Up @@ -215,6 +226,10 @@ public void prepareForNewIteration() {

// === only private helper functions below this line ===

private boolean isNotTransitAgent(Id<Person> persondId) {
return (Integer.parseInt(persondId.toString())< 1000000000);
}

private boolean isFirstCarDepartureOfDay(Id<Person> personId) {
Person person = scenario.getPopulation().getPersons().get(personId);

Expand All @@ -223,7 +238,7 @@ private boolean isFirstCarDepartureOfDay(Id<Person> personId) {
}

List<PlanElement> planElements = person.getSelectedPlan().getPlanElements();
for (int i = currentPlanElementIndex.get(personId) - 1; i >= 0; i--) {
for ( int i = currentPlanElementIndices.get(personId ) - 1 ; i >= 0; i--) {
if (planElements.get(i) instanceof Leg) {
Leg leg = (Leg) planElements.get(i);

Expand All @@ -240,7 +255,7 @@ private boolean isFirstCarDepartureOfDay(Id<Person> personId) {
private boolean isLastCarLegOfDay(Id<Person> personId) {
Person person = scenario.getPopulation().getPersons().get(personId);
List<PlanElement> planElements = person.getSelectedPlan().getPlanElements();
for (int i = currentPlanElementIndex.get(personId) + 1; i < planElements.size(); i++) {
for ( int i = currentPlanElementIndices.get(personId ) + 1 ; i < planElements.size(); i++) {
if (planElements.get(i) instanceof Leg) {
Leg Leg = (Leg) planElements.get(i);

Expand All @@ -257,7 +272,7 @@ private Activity getActivityBeforeNextCarLeg(Id<Person> personId) {
Person person = scenario.getPopulation().getPersons().get(personId);
List<PlanElement> planElements = person.getSelectedPlan().getPlanElements();
int indexOfNextCarLeg = -1;
for (int i = currentPlanElementIndex.get(personId) + 1; i < planElements.size(); i++) {
for ( int i = currentPlanElementIndices.get(personId ) + 1 ; i < planElements.size(); i++) {
if (planElements.get(i) instanceof Leg) {
Leg Leg = (Leg) planElements.get(i);

Expand All @@ -281,7 +296,7 @@ private Activity getActivityBeforeNextCarLeg(Id<Person> personId) {
private Activity getNextActivity(Id<Person> personId) {
Person person = scenario.getPopulation().getPersons().get(personId);
List<PlanElement> planElements = person.getSelectedPlan().getPlanElements();
for (int i = currentPlanElementIndex.get(personId); i < planElements.size(); i++) {
for ( int i = currentPlanElementIndices.get(personId ) ; i < planElements.size(); i++) {
if (planElements.get(i) instanceof Activity) {
return (Activity) planElements.get(i);
}
Expand Down
Loading

0 comments on commit b4b9429

Please sign in to comment.