Skip to content

Commit

Permalink
improve fleet analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoerl committed Feb 21, 2024
1 parent bb27887 commit 6845243
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
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.schedule.Task.TaskType;
import org.matsim.contrib.dvrp.vrpagent.TaskEndedEvent;
import org.matsim.contrib.dvrp.vrpagent.TaskEndedEventHandler;
import org.matsim.contrib.dvrp.vrpagent.TaskStartedEvent;
Expand All @@ -56,7 +61,7 @@
*/
public class VehicleOccupancyProfileCalculator
implements PersonEntersVehicleEventHandler, PersonLeavesVehicleEventHandler, TaskStartedEventHandler,
TaskEndedEventHandler {
TaskEndedEventHandler, VehicleAddedEventHandler, VehicleRemovedEventHandler {

private static class VehicleState {
private Task.TaskType taskType;
Expand Down Expand Up @@ -196,19 +201,38 @@ private void increment(double[] values, double beginTime, double endTime) {

/* Event handling starts here */

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

VehicleState state = new VehicleState();
state.beginTime = event.getTime();
state.taskType = VEHICLE_ADDED;
vehicleStates.put(event.getVehicleId(), state);
}

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

VehicleState state = vehicleStates.remove(event.getVehicleId());

if (state.taskType != null) {
increment(state, event.getTime());
}
}

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

final VehicleState state;
if (event.getTaskIndex() == 0) {
state = new VehicleState();
vehicleStates.put(event.getDvrpVehicleId(), state);
} else {
state = vehicleStates.get(event.getDvrpVehicleId());
}
VehicleState state = vehicleStates.get(event.getDvrpVehicleId());
state.taskType = event.getTaskType();
state.beginTime = event.getTime();
}
Expand All @@ -220,6 +244,8 @@ public void handleEvent(TaskEndedEvent event) {
}

VehicleState state = vehicleStates.get(event.getDvrpVehicleId());
state.taskType = state.taskType == VEHICLE_ADDED ? event.getTaskType() : state.taskType;

increment(state, event.getTime());
state.taskType = null;
}
Expand Down Expand Up @@ -266,4 +292,6 @@ public void reset(int iteration) {
nonPassengerServingTaskProfiles = new HashMap<>();
wasConsolidatedInThisIteration = false;
}

static private final TaskType VEHICLE_ADDED = () -> "VEHICLE_ADDED";
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
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.schedule.Task.TaskType;
import org.matsim.contrib.common.timeprofile.TimeDiscretizer;
import org.matsim.contrib.dvrp.vrpagent.TaskEndedEvent;
import org.matsim.contrib.dvrp.vrpagent.TaskEndedEventHandler;
Expand All @@ -44,7 +49,7 @@
* @author nkuehnel / MOIA
*/
public class VehicleTaskProfileCalculator implements TaskStartedEventHandler,
TaskEndedEventHandler {
TaskEndedEventHandler, VehicleAddedEventHandler, VehicleRemovedEventHandler {

private static class VehicleState {
private Task.TaskType taskType;
Expand Down Expand Up @@ -144,19 +149,38 @@ private void increment(double[] values, double beginTime, double endTime) {

/* Event handling starts here */

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

VehicleState state = new VehicleState();
state.beginTime = event.getTime();
state.taskType = VEHICLE_ADDED;
vehicleStates.put(event.getVehicleId(), state);
}

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

VehicleState state = vehicleStates.remove(event.getVehicleId());

if (state.taskType != null) {
increment(state, event.getTime());
}
}

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

final VehicleState state;
if (event.getTaskIndex() == 0) {
state = new VehicleState();
vehicleStates.put(event.getDvrpVehicleId(), state);
} else {
state = vehicleStates.get(event.getDvrpVehicleId());
}
final VehicleState state = vehicleStates.get(event.getDvrpVehicleId());
state.taskType = event.getTaskType();
state.beginTime = event.getTime();
}
Expand All @@ -168,6 +192,8 @@ public void handleEvent(TaskEndedEvent event) {
}

VehicleState state = vehicleStates.get(event.getDvrpVehicleId());
state.taskType = state.taskType == VEHICLE_ADDED ? event.getTaskType() : state.taskType;

increment(state, event.getTime());
state.taskType = null;
}
Expand All @@ -179,4 +205,6 @@ public void reset(int iteration) {
taskProfiles = new HashMap<>();
wasConsolidatedInThisIteration = false;
}

static private final TaskType VEHICLE_ADDED = () -> "VEHICLE_ADDED";
}

0 comments on commit 6845243

Please sign in to comment.