|
| 1 | +package org.matsim.contrib.drt.sharingmetrics; |
| 2 | + |
| 3 | +import com.google.inject.Inject; |
| 4 | +import org.jfree.chart.ChartFactory; |
| 5 | +import org.jfree.chart.ChartUtils; |
| 6 | +import org.jfree.chart.JFreeChart; |
| 7 | +import org.jfree.chart.renderer.category.BoxAndWhiskerRenderer; |
| 8 | +import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset; |
| 9 | +import org.matsim.api.core.v01.Id; |
| 10 | +import org.matsim.contrib.drt.run.DrtConfigGroup; |
| 11 | +import org.matsim.contrib.dvrp.optimizer.Request; |
| 12 | +import org.matsim.core.config.Config; |
| 13 | +import org.matsim.core.controler.MatsimServices; |
| 14 | +import org.matsim.core.controler.events.IterationEndsEvent; |
| 15 | +import org.matsim.core.controler.listener.IterationEndsListener; |
| 16 | +import org.matsim.core.utils.io.IOUtils; |
| 17 | + |
| 18 | +import java.io.BufferedWriter; |
| 19 | +import java.io.FileOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author nkuehnel / MOIA |
| 28 | + */ |
| 29 | +public class SharingMetricsControlerListener implements IterationEndsListener { |
| 30 | + private final MatsimServices matsimServices; |
| 31 | + |
| 32 | + private final DrtConfigGroup drtConfigGroup; |
| 33 | + private final SharingMetricsTracker sharingFactorTracker; |
| 34 | + |
| 35 | + private boolean headerWritten = false; |
| 36 | + |
| 37 | + private final String runId; |
| 38 | + |
| 39 | + private final String delimiter; |
| 40 | + |
| 41 | + private static final String notAvailableString = "NA"; |
| 42 | + |
| 43 | + |
| 44 | + @Inject |
| 45 | + public SharingMetricsControlerListener(Config config, |
| 46 | + DrtConfigGroup drtConfigGroup, |
| 47 | + SharingMetricsTracker sharingFactorTracker, |
| 48 | + MatsimServices matsimServices) { |
| 49 | + this.drtConfigGroup = drtConfigGroup; |
| 50 | + this.sharingFactorTracker = sharingFactorTracker; |
| 51 | + this.matsimServices = matsimServices; |
| 52 | + runId = Optional.ofNullable(config.controller().getRunId()).orElse(notAvailableString); |
| 53 | + this.delimiter = config.global().getDefaultDelimiter(); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void notifyIterationEnds(IterationEndsEvent event) { |
| 59 | + int createGraphsInterval = event.getServices().getConfig().controller().getCreateGraphsInterval(); |
| 60 | + boolean createGraphs = createGraphsInterval >0 && event.getIteration() % createGraphsInterval == 0; |
| 61 | + |
| 62 | + Map<Id<Request>, Double> sharingFactors = sharingFactorTracker.getSharingFactors(); |
| 63 | + Map<Id<Request>, Boolean> poolingRates = sharingFactorTracker.getPoolingRates(); |
| 64 | + |
| 65 | + writeAndPlotSharingMetrics( |
| 66 | + sharingFactors, |
| 67 | + poolingRates, |
| 68 | + filename(event, "sharingFactors", ".png"), |
| 69 | + filename(event, "poolingRates", ".png"), |
| 70 | + filename(event, "sharingMetrics", ".csv"), |
| 71 | + createGraphs); |
| 72 | + |
| 73 | + double nPooled = poolingRates.values().stream().filter(b -> b).count(); |
| 74 | + double nTotal = poolingRates.values().size(); |
| 75 | + double meanPoolingRate = nPooled / nTotal; |
| 76 | + double meanSharingFactor = sharingFactors.values().stream().mapToDouble(d -> d).average().orElse(Double.NaN); |
| 77 | + |
| 78 | + writeIterationPoolingStats(meanPoolingRate + delimiter + meanSharingFactor + delimiter + nPooled +delimiter + nTotal, event.getIteration()); |
| 79 | + } |
| 80 | + |
| 81 | + private void writeAndPlotSharingMetrics(Map<Id<Request>, Double> sharingFactorByRequest, |
| 82 | + Map<Id<Request>, Boolean> rates, |
| 83 | + String sharingFactors, |
| 84 | + String poolingRates, |
| 85 | + String csvFile, |
| 86 | + boolean createGraphs) { |
| 87 | + try (var bw = IOUtils.getBufferedWriter(csvFile)) { |
| 88 | + bw.append(line("Request", "SharingFactor", "Pooled")); |
| 89 | + |
| 90 | + for (Map.Entry<Id<Request>, Double> sharingFactorEntry : sharingFactorByRequest.entrySet()) { |
| 91 | + bw.append(line(sharingFactorEntry.getKey(), sharingFactorEntry.getValue(),rates.get(sharingFactorEntry.getKey()))); |
| 92 | + } |
| 93 | + bw.flush(); |
| 94 | + |
| 95 | + if (createGraphs) { |
| 96 | + final DefaultBoxAndWhiskerCategoryDataset sharingFactorDataset |
| 97 | + = new DefaultBoxAndWhiskerCategoryDataset(); |
| 98 | + |
| 99 | + final DefaultBoxAndWhiskerCategoryDataset poolingRateDataset |
| 100 | + = new DefaultBoxAndWhiskerCategoryDataset(); |
| 101 | + |
| 102 | + sharingFactorDataset.add(sharingFactorByRequest.values().stream().toList(), "", ""); |
| 103 | + poolingRateDataset.add(rates.values().stream().toList(), "", ""); |
| 104 | + |
| 105 | + JFreeChart chartRides = ChartFactory.createBoxAndWhiskerChart("Sharing factor", "", "Factor", sharingFactorDataset, false); |
| 106 | + JFreeChart chartPooling = ChartFactory.createBoxAndWhiskerChart("Pooling rate", "", "Rate", poolingRateDataset, false); |
| 107 | + |
| 108 | + ((BoxAndWhiskerRenderer) chartRides.getCategoryPlot().getRenderer()).setMeanVisible(true); |
| 109 | + ((BoxAndWhiskerRenderer) chartPooling.getCategoryPlot().getRenderer()).setMeanVisible(true); |
| 110 | + ChartUtils.writeChartAsPNG(new FileOutputStream(sharingFactors), chartRides, 1500, 1500); |
| 111 | + ChartUtils.writeChartAsPNG(new FileOutputStream(poolingRates), chartPooling, 1500, 1500); |
| 112 | + } |
| 113 | + } catch ( |
| 114 | + IOException e) { |
| 115 | + throw new RuntimeException(e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private String filename(IterationEndsEvent event, String prefix, String extension) { |
| 120 | + return matsimServices.getControlerIO() |
| 121 | + .getIterationFilename(event.getIteration(), prefix + "_" + drtConfigGroup.getMode() + extension); |
| 122 | + } |
| 123 | + |
| 124 | + private String line(Object... cells) { |
| 125 | + return Arrays.stream(cells).map(Object::toString).collect(Collectors.joining(delimiter, "", "\n")); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + private void writeIterationPoolingStats(String summarizePooling, int it) { |
| 130 | + try (var bw = getAppendingBufferedWriter("drt_sharing_metrics", ".csv")) { |
| 131 | + if (!headerWritten) { |
| 132 | + headerWritten = true; |
| 133 | + bw.write(line("runId", "iteration", "poolingRate", "sharingFactor", "nPooled", "nTotal")); |
| 134 | + } |
| 135 | + bw.write(runId + delimiter + it + delimiter + summarizePooling); |
| 136 | + bw.newLine(); |
| 137 | + } catch (IOException e) { |
| 138 | + throw new RuntimeException(e); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private BufferedWriter getAppendingBufferedWriter(String prefix, String extension) { |
| 143 | + return IOUtils.getAppendingBufferedWriter(matsimServices.getControlerIO().getOutputFilename(prefix + "_" + drtConfigGroup.getMode() + extension)); |
| 144 | + } |
| 145 | +} |
0 commit comments