From 645044be72ebc28e851527dcb220ec82736b182f Mon Sep 17 00:00:00 2001 From: rajnikantsh Date: Wed, 31 Jul 2019 00:01:25 +0530 Subject: [PATCH] Add charging Graph --- src/main/java/beam/analysis/StatsFactory.java | 5 +- .../analysis/VehicleChargingAnalysis.scala | 84 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/main/scala/beam/analysis/VehicleChargingAnalysis.scala diff --git a/src/main/java/beam/analysis/StatsFactory.java b/src/main/java/beam/analysis/StatsFactory.java index 0b74aa91969..2db41f3465a 100644 --- a/src/main/java/beam/analysis/StatsFactory.java +++ b/src/main/java/beam/analysis/StatsFactory.java @@ -30,7 +30,8 @@ public enum StatsType { TollRevenue, AgencyRevenue, ParkingDelay, - RideHailUtilization + RideHailUtilization, + VehicleChargingAnalysis } private final BeamConfig beamConfig; @@ -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; } diff --git a/src/main/scala/beam/analysis/VehicleChargingAnalysis.scala b/src/main/scala/beam/analysis/VehicleChargingAnalysis.scala new file mode 100644 index 00000000000..c16309caf50 --- /dev/null +++ b/src/main/scala/beam/analysis/VehicleChargingAnalysis.scala @@ -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 + ) + + } +}