Skip to content

Commit

Permalink
plot fleet size
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoerl committed Feb 20, 2024
1 parent db3fbca commit bb27887
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,18 @@ public interface ProfileView {
private final String mode;
private final ProfileView view;
private final String outputFile;
private final boolean createdStacked;

public ProfileWriter(MatsimServices matsimServices, String mode, ProfileView profileView, String fileName) {
this(matsimServices, mode, profileView, fileName, true);
}

public ProfileWriter(MatsimServices matsimServices, String mode, ProfileView profileView, String fileName, boolean createdStacked) {
this.matsimServices = matsimServices;
this.mode = mode;
this.view = profileView;
this.outputFile = fileName;
this.createdStacked = createdStacked;
}

@Override
Expand All @@ -91,7 +97,10 @@ public void notifyIterationEnds(IterationEndsEvent event) {
if (createGraphsInterval > 0 && event.getIteration() % createGraphsInterval == 0) {
DefaultTableXYDataset xyDataset = createXYDataset(times, profiles);
generateImage(xyDataset, TimeProfileCharts.ChartType.Line);
generateImage(xyDataset, TimeProfileCharts.ChartType.StackedArea);

if (createdStacked) {
generateImage(xyDataset, TimeProfileCharts.ChartType.StackedArea);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.matsim.contrib.drt.scheduler.EmptyVehicleRelocator;
import org.matsim.contrib.drt.sharingmetrics.SharingMetricsModule;
import org.matsim.contrib.dvrp.analysis.ExecutedScheduleCollector;
import org.matsim.contrib.dvrp.analysis.FleetSizeProfileCalculator;
import org.matsim.contrib.dvrp.analysis.FleetSizeProfileView;
import org.matsim.contrib.dvrp.fleet.FleetSpecification;
import org.matsim.contrib.dvrp.run.AbstractDvrpModeModule;
import org.matsim.contrib.dvrp.schedule.Task;
Expand Down Expand Up @@ -124,6 +126,18 @@ public void install() {
getter -> new VehicleTaskProfileCalculator(getMode(), getter.getModal(FleetSpecification.class), 300,
getter.get(QSimConfigGroup.class)))).asEagerSingleton();
addEventHandlerBinding().to(modalKey(VehicleTaskProfileCalculator.class));

addControlerListenerBinding().toProvider(modalProvider(getter -> {
MatsimServices matsimServices = getter.get(MatsimServices.class);
String mode = drtCfg.getMode();
var profileView = new FleetSizeProfileView(getter.getModal(FleetSizeProfileCalculator.class));
return new ProfileWriter(matsimServices, mode, profileView, "fleet_size_profile", false);
}));

bindModal(FleetSizeProfileCalculator.class).toProvider(modalProvider(
getter -> new FleetSizeProfileCalculator(getMode(), getter.getModal(FleetSpecification.class), 300,
getter.get(QSimConfigGroup.class)))).asEagerSingleton();
addEventHandlerBinding().to(modalKey(FleetSizeProfileCalculator.class));

addControlerListenerBinding().toProvider(modalProvider(getter -> {
MatsimServices matsimServices = getter.get(MatsimServices.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* *********************************************************************** *
* project: org.matsim.*
* *********************************************************************** *
* *
* copyright : (C) 2023 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* *********************************************************************** *
*/
package org.matsim.contrib.dvrp.analysis;

import org.matsim.contrib.common.timeprofile.TimeDiscretizer;
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.core.config.groups.QSimConfigGroup;

/**
* @author Sebastian Hörl (sebhoerl), IRT SystemX
*/
public class FleetSizeProfileCalculator implements VehicleAddedEventHandler, VehicleRemovedEventHandler {

private final String dvrpMode;

private final double analysisEndTime;
private final TimeDiscretizer timeDiscretizer;

private double[] activeCount;

public FleetSizeProfileCalculator(String dvrpMode, FleetSpecification fleet, int timeInterval,
QSimConfigGroup qsimConfig) {
this.dvrpMode = dvrpMode;

double qsimEndTime = qsimConfig.getEndTime().orElse(0);
double maxServiceEndTime = fleet.getVehicleSpecifications().values().stream()
.mapToDouble(DvrpVehicleSpecification::getServiceEndTime).max().orElse(0);
analysisEndTime = Math.max(qsimEndTime, maxServiceEndTime);
timeDiscretizer = new TimeDiscretizer((int) Math.ceil(analysisEndTime), timeInterval);

activeCount = new double[timeDiscretizer.getIntervalCount()];
}

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

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

private void increment(double startTime) {
double timeInterval = timeDiscretizer.getTimeInterval();
int fromIdx = timeDiscretizer.getIdx(startTime);

for (int i = fromIdx; i < timeDiscretizer.getIntervalCount(); i++) {
activeCount[i] += 1.0;
}

activeCount[fromIdx] -= (startTime % timeInterval) / timeInterval;
}

private void decrement(double startTime) {
double timeInterval = timeDiscretizer.getTimeInterval();
int fromIdx = timeDiscretizer.getIdx(startTime);

for (int i = fromIdx; i < timeDiscretizer.getIntervalCount(); i++) {
activeCount[i] -= 1.0;
}

activeCount[fromIdx] += (startTime % timeInterval) / timeInterval;
}

public double[] getActiveVehiclesProfile() {
return activeCount;
}

public TimeDiscretizer getTimeDiscretizer() {
return timeDiscretizer;
}

@Override
public void reset(int iteration) {
activeCount = new double[timeDiscretizer.getIntervalCount()];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* *********************************************************************** *
* project: org.matsim.*
* *********************************************************************** *
* *
* copyright : (C) 2023 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* *********************************************************************** *
*/

package org.matsim.contrib.dvrp.analysis;

import java.awt.Color;
import java.awt.Paint;
import java.util.Map;

import org.matsim.contrib.common.timeprofile.ProfileWriter;

import com.google.common.collect.ImmutableMap;

public class FleetSizeProfileView implements ProfileWriter.ProfileView {
private final FleetSizeProfileCalculator calculator;

public FleetSizeProfileView(FleetSizeProfileCalculator calculator) {
this.calculator = calculator;
}

@Override
public ImmutableMap<String, double[]> profiles() {
return ImmutableMap.of("Fleet size", calculator.getActiveVehiclesProfile());
}

@Override
public Map<String, Paint> seriesPaints() {
return ImmutableMap.of("Fleet size", Color.RED);
}

@Override
public double[] times() {
return calculator.getTimeDiscretizer().getTimes();
}
}

0 comments on commit bb27887

Please sign in to comment.