Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add charging Graph #2018

Merged
merged 1 commit into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/beam/analysis/StatsFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public enum StatsType {
TollRevenue,
AgencyRevenue,
ParkingDelay,
RideHailUtilization
RideHailUtilization,
VehicleChargingAnalysis
}

private final BeamConfig beamConfig;
Expand Down Expand Up @@ -106,6 +107,8 @@ private BeamAnalysis createStats(StatsType statsType) {
return new ParkingStatsCollector(beamServices);
case RideHailUtilization:
return new SimpleRideHailUtilization();
case VehicleChargingAnalysis:
return new VehicleChargingAnalysis();
default:
return null;
}
Expand Down
84 changes: 84 additions & 0 deletions src/main/scala/beam/analysis/VehicleChargingAnalysis.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package beam.analysis

//import beam.agentsim.events.{ChargingPlugInEvent, ChargingPlugOutEvent}
import beam.agentsim.events.{ChargingPlugInEvent, ChargingPlugOutEvent}
import beam.analysis.plots.{GraphAnalysis, GraphUtils, GraphsStatsAgentSimEventsListener}
import beam.utils.logging.ExponentialLazyLogging
import org.jfree.chart.ChartFactory
import org.jfree.chart.plot.PlotOrientation
import org.jfree.data.category.{CategoryDataset, DefaultCategoryDataset}
import org.matsim.api.core.v01.events.Event
import org.matsim.core.controler.events.IterationEndsEvent

import scala.collection.mutable

class VehicleChargingAnalysis extends GraphAnalysis with ExponentialLazyLogging {

private val vehicleChargingFileBaseName = "vehicleCharging"

private val vehicleChargingTime = mutable.Map[String, Int]()
private val hourlyChargingCount = mutable.TreeMap[Int, Int]().withDefaultValue(0)

override def processStats(event: Event): Unit = {
val hourOfEvent = (event.getTime / 3600).toInt
event match {
case pluginEvent: ChargingPlugInEvent =>
val vehicle = pluginEvent.getAttributes().get(ChargingPlugInEvent.ATTRIBUTE_VEHICLE_ID)
vehicleChargingTime.update(vehicle, hourOfEvent)

case plugoutEvent: ChargingPlugOutEvent =>
val vehicle = plugoutEvent.getAttributes().get(ChargingPlugOutEvent.ATTRIBUTE_VEHICLE_ID)
val pluginTime = vehicleChargingTime.remove(vehicle)
pluginTime match {
case Some(time) =>
(time until hourOfEvent) foreach (hour => {
hourlyChargingCount.update(hour, hourlyChargingCount(hour) + 1)
})
case None =>
logger.warn("Found ChargingPlugOutEvent without ChargingPlugInEvent")

}

case _ =>
}
}

override def resetStats(): Unit = {
vehicleChargingTime.clear()
hourlyChargingCount.clear()
}

override def createGraph(event: IterationEndsEvent): Unit = {
val outputDirectoryHiearchy = event.getServices.getControlerIO

val chargingDataset = createChargingDataset()
val chargingGraphImageFile =
outputDirectoryHiearchy.getIterationFilename(event.getIteration, s"$vehicleChargingFileBaseName.png")
createGraph(chargingDataset, chargingGraphImageFile, "Vehicle Charging")

}

private def createChargingDataset(): CategoryDataset = {
val dataset = new DefaultCategoryDataset

hourlyChargingCount.foreach({
case (hour, count) => dataset.addValue(count, "charging-vehicle", hour)
})

dataset
}

private def createGraph(dataSet: CategoryDataset, graphImageFile: String, title: String): Unit = {

val chart =
ChartFactory.createLineChart(title, "Hour", "#Count", dataSet, PlotOrientation.VERTICAL, true, true, false)

GraphUtils.saveJFreeChartAsPNG(
chart,
graphImageFile,
GraphsStatsAgentSimEventsListener.GRAPH_WIDTH,
GraphsStatsAgentSimEventsListener.GRAPH_HEIGHT
)

}
}