-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/FleetSizeProfileCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/analysis/FleetSizeProfileView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |