Skip to content

Commit

Permalink
update analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoerl committed Feb 25, 2024
1 parent bb27887 commit f2d7711
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ public void reset(int iteration) {

@Override
public void handleEvent(VehicleAddedEvent event) {
var state = vehicleStates.get(Id.createVehicleId(event.getVehicleId()));
var state = vehicleStates.get(Id.createVehicleId(event.getDvrpVehicleId()));

if (state == null) {
vehicleStates.put(Id.createVehicleId(event.getVehicleId()), new VehicleState(event.getCapacity(), Double.NaN));
vehicleStates.put(Id.createVehicleId(event.getDvrpVehicleId()), new VehicleState(event.getCapacity(), Double.NaN));
} else {
state.active = true;
Verify.verify(state.maxCapacity == event.getCapacity());
Expand All @@ -118,7 +118,7 @@ public void handleEvent(VehicleAddedEvent event) {

@Override
public void handleEvent(VehicleRemovedEvent event) {
Objects.requireNonNull(vehicleStates.get(Id.createVehicleId(event.getVehicleId()))).active = false;
Objects.requireNonNull(vehicleStates.get(Id.createVehicleId(event.getDvrpVehicleId()))).active = false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public FleetSizeProfileCalculator(String dvrpMode, FleetSpecification fleet, int

@Override
public void handleEvent(VehicleAddedEvent event) {
if (event.getMode().equals(dvrpMode)) {
if (event.getDvrpMode().equals(dvrpMode)) {
increment(event.getTime());
}
}

@Override
public void handleEvent(VehicleRemovedEvent event) {
if (event.getMode().equals(dvrpMode)) {
if (event.getDvrpMode().equals(dvrpMode)) {
decrement(event.getTime());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
import org.matsim.contrib.dvrp.fleet.DvrpVehicle;
import org.matsim.contrib.dvrp.fleet.DvrpVehicleSpecification;
import org.matsim.contrib.dvrp.fleet.FleetSpecification;
import org.matsim.contrib.dvrp.fleet.VehicleAddedEvent;
import org.matsim.contrib.dvrp.fleet.VehicleAddedEventHandler;
import org.matsim.contrib.dvrp.fleet.VehicleRemovedEvent;
import org.matsim.contrib.dvrp.fleet.VehicleRemovedEventHandler;
import org.matsim.contrib.dvrp.schedule.Task;
import org.matsim.contrib.dvrp.vrpagent.TaskEndedEvent;
import org.matsim.contrib.dvrp.vrpagent.TaskEndedEventHandler;
Expand All @@ -53,10 +57,11 @@

/**
* @author michalm (Michal Maciejewski)
* @author Sebastian Hörl (sebhoerl), IRT SystemX
*/
public class VehicleOccupancyProfileCalculator
implements PersonEntersVehicleEventHandler, PersonLeavesVehicleEventHandler, TaskStartedEventHandler,
TaskEndedEventHandler {
TaskEndedEventHandler, VehicleAddedEventHandler, VehicleRemovedEventHandler {

private static class VehicleState {
private Task.TaskType taskType;
Expand Down Expand Up @@ -161,7 +166,12 @@ public TimeDiscretizer getTimeDiscretizer() {
}

private void increment(VehicleState state, double endTime) {
Verify.verify(state.taskType != null);
if (state.taskType == null) {
// will be null if a vehicle has been added to the fleet, but the task that has
// ended came from a different dvrp mode
return;
}

Verify.verify(state.occupancy >= 0);

boolean servingPassengers = passengerServingTaskTypes.contains(state.taskType) || state.occupancy > 0;
Expand Down Expand Up @@ -197,18 +207,30 @@ private void increment(double[] values, double beginTime, double endTime) {
/* Event handling starts here */

@Override
public void handleEvent(TaskStartedEvent event) {
public void handleEvent(VehicleAddedEvent event) {
if (!event.getDvrpMode().equals(dvrpMode)) {
return;
}

vehicleStates.put(event.getDvrpVehicleId(), new VehicleState());
}

final VehicleState state;
if (event.getTaskIndex() == 0) {
state = new VehicleState();
vehicleStates.put(event.getDvrpVehicleId(), state);
} else {
state = vehicleStates.get(event.getDvrpVehicleId());
@Override
public void handleEvent(VehicleRemovedEvent event) {
if (!event.getDvrpMode().equals(dvrpMode)) {
return;
}

increment(vehicleStates.remove(event.getDvrpVehicleId()), event.getTime());
}

@Override
public void handleEvent(TaskStartedEvent event) {
if (!event.getDvrpMode().equals(dvrpMode)) {
return;
}

VehicleState state = vehicleStates.get(event.getDvrpVehicleId());
state.taskType = event.getTaskType();
state.beginTime = event.getTime();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import org.matsim.contrib.dvrp.fleet.DvrpVehicle;
import org.matsim.contrib.dvrp.fleet.DvrpVehicleSpecification;
import org.matsim.contrib.dvrp.fleet.FleetSpecification;
import org.matsim.contrib.dvrp.fleet.VehicleAddedEvent;
import org.matsim.contrib.dvrp.fleet.VehicleAddedEventHandler;
import org.matsim.contrib.dvrp.fleet.VehicleRemovedEvent;
import org.matsim.contrib.dvrp.fleet.VehicleRemovedEventHandler;
import org.matsim.contrib.dvrp.schedule.Task;
import org.matsim.contrib.common.timeprofile.TimeDiscretizer;
import org.matsim.contrib.dvrp.vrpagent.TaskEndedEvent;
Expand All @@ -36,15 +40,15 @@
import org.matsim.core.config.groups.QSimConfigGroup;

import com.google.common.base.Preconditions;
import com.google.common.base.Verify;

/**
* Collects task profiles of DVRP vehicles. Based on {@link VehicleOccupancyProfileCalculator} but only collects tasks.
*
* @author nkuehnel / MOIA
* @author Sebastian Hörl (sebhoerl), IRT SystemX
*/
public class VehicleTaskProfileCalculator implements TaskStartedEventHandler,
TaskEndedEventHandler {
TaskEndedEventHandler, VehicleAddedEventHandler, VehicleRemovedEventHandler {

private static class VehicleState {
private Task.TaskType taskType;
Expand Down Expand Up @@ -114,7 +118,11 @@ public TimeDiscretizer getTimeDiscretizer() {
}

private void increment(VehicleState state, double endTime) {
Verify.verify(state.taskType != null);
if (state.taskType == null) {
// will be null if a vehicle has been added to the fleet, but the task that has
// ended came from a different dvrp mode
return;
}

double[] profile = taskProfiles.computeIfAbsent(state.taskType,
v -> new double[timeDiscretizer.getIntervalCount()]);
Expand Down Expand Up @@ -145,18 +153,30 @@ private void increment(double[] values, double beginTime, double endTime) {
/* Event handling starts here */

@Override
public void handleEvent(TaskStartedEvent event) {
public void handleEvent(VehicleAddedEvent event) {
if (!event.getDvrpMode().equals(dvrpMode)) {
return;
}

vehicleStates.put(event.getDvrpVehicleId(), new VehicleState());
}

final VehicleState state;
if (event.getTaskIndex() == 0) {
state = new VehicleState();
vehicleStates.put(event.getDvrpVehicleId(), state);
} else {
state = vehicleStates.get(event.getDvrpVehicleId());
@Override
public void handleEvent(VehicleRemovedEvent event) {
if (!event.getDvrpMode().equals(dvrpMode)) {
return;
}

increment(vehicleStates.remove(event.getDvrpVehicleId()), event.getTime());
}

@Override
public void handleEvent(TaskStartedEvent event) {
if (!event.getDvrpMode().equals(dvrpMode)) {
return;
}

VehicleState state = vehicleStates.get(event.getDvrpVehicleId());
state.taskType = event.getTaskType();
state.beginTime = event.getTime();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ protected AbstractFleetEvent(double time, String mode, Id<DvrpVehicle> vehicleId
this.vehicleId = vehicleId;
}

public String getMode() {
public String getDvrpMode() {
return mode;
}

public Id<DvrpVehicle> getVehicleId() {
public Id<DvrpVehicle> getDvrpVehicleId() {
return vehicleId;
}

Expand Down

0 comments on commit f2d7711

Please sign in to comment.