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

beam utilities #873

Merged
merged 5 commits into from
Nov 7, 2018
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
21 changes: 20 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ allprojects {

dependencies {


//beam-utilities
compile group: 'com.github.LBNL-UCB-STI', name: 'beam-utilities', version: '-SNAPSHOT'
////////////////////////////
// Java dependencies
////////////////////////////

compile group: 'com.google.inject', name: 'guice', version: '4.1.0'
compile group: 'com.google.inject.extensions', name: 'guice-assistedinject', version: '4.1.0'
compile group: 'com.google.inject.extensions', name: 'guice-multibindings', version: '4.1.0'
Expand Down Expand Up @@ -136,6 +138,7 @@ dependencies {
// GPLv3
compile group: 'org.matsim.contrib', name: 'multimodal', version: '0.10.0'
compile group: 'org.matsim.contrib', name: 'bicycle', version: '0.10.0'

compile (group: 'org.matsim.contrib', name: 'decongestion', version: '0.11.0-2018w44'){
exclude group: 'org.matsim', module: 'matsim'
}
Expand Down Expand Up @@ -244,6 +247,22 @@ scalafmt {
// configFilePath = ".scalafmt.conf" // .scalafmt.conf in the project root is default value, provide only if other location is needed
}



configurations.all {
resolutionStrategy {
eachDependency { DependencyResolveDetails dependencyResolveDetails ->
final requestedDependency = dependencyResolveDetails.requested
if (requestedDependency.name != 'beam-utilities') {
force 'javax.media:jai_core:1.1.3'
}
}
}
exclude group: 'javax.media', module: 'jai_codec'
exclude group: 'javax.media', module: 'jai_imageio'

}

//compileScala.dependsOn(scalafmtAll)

// Task to run scala tests, as Scala tests not picked up by Gradle by default.
Expand Down
127 changes: 13 additions & 114 deletions src/main/java/beam/analysis/plots/PersonVehicleTransitionAnalysis.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,28 @@
package beam.analysis.plots;

import beam.analysis.plot.PlotGraph;
import beam.sim.config.BeamConfig;
import beam.sim.metrics.MetricsSupport;
import com.google.common.base.CaseFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.matsim.api.core.v01.events.Event;
import org.matsim.api.core.v01.events.PersonEntersVehicleEvent;
import org.matsim.api.core.v01.events.PersonLeavesVehicleEvent;
import org.matsim.core.controler.events.IterationEndsEvent;
import org.matsim.core.utils.io.UncheckedIOException;
import org.matsim.core.utils.misc.Time;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
import java.util.List;

public class PersonVehicleTransitionAnalysis implements GraphAnalysis, MetricsSupport {

private static final List<String> vehicleType = new ArrayList<>(Arrays.asList("body", "rideHail" , "others"));
private static final List<String> vehicleType = new ArrayList<>(Arrays.asList("body", "rideHail", "others"));

private static Map<String, TreeMap<Integer, Integer>> personEnterCount = new HashMap<>();
private static Map<String, TreeMap<Integer, Integer>> personExitCount = new HashMap<>();
private static Map<String, TreeMap<Integer, Integer>> onRoutes = new HashMap<>();
private static Map<String, Integer> modePerson = new HashMap<>();
private static final String fileName = "tripHistogram";
private static final String xAxisLabel = "time (binSize=<?> sec)";
private PlotGraph plotGraph = new PlotGraph();
private int binSize;
private int numOfBins;

Expand Down Expand Up @@ -70,12 +57,13 @@ public void createGraph(IterationEndsEvent event) {
if (personEnterCount.size() == 0 && personExitCount.size() == 0) {
continue;
}
writeGraphic(event.getIteration(), mode);
plotGraph.writeGraphic(GraphsStatsAgentSimEventsListener.CONTROLLER_IO, event.getIteration(), mode, fileName, personEnterCount, personExitCount, onRoutes, xAxisLabel, binSize);

}
}

private void processPersonVehicleTransition(Event event) {
int index = getBinIndex(event.getTime());
int index = plotGraph.getBinIndex(event.getTime(), this.binSize, this.numOfBins);
if (PersonEntersVehicleEvent.EVENT_TYPE.equals(event.getEventType())) {

String personId = event.getAttributes().get(PersonEntersVehicleEvent.ATTRIBUTE_PERSON);
Expand All @@ -92,11 +80,10 @@ private void processPersonVehicleTransition(Event event) {
}

String unitVehicle;
boolean isDigit = vehicleId.replace("-","").chars().allMatch( Character::isDigit );
if(isDigit){
boolean isDigit = vehicleId.replace("-", "").chars().allMatch(Character::isDigit);
if (isDigit) {
unitVehicle = "car";
}
else {
} else {
unitVehicle = vehicleType.stream().filter(vehicleId::contains).findAny().orElse("others");
}

Expand Down Expand Up @@ -144,11 +131,10 @@ private void processPersonVehicleTransition(Event event) {
}

String unitVehicle;
boolean isDigit = vehicleId.replace("-","").chars().allMatch( Character::isDigit );
if(isDigit){
boolean isDigit = vehicleId.replace("-", "").chars().allMatch(Character::isDigit);
if (isDigit) {
unitVehicle = "car";
}
else {
} else {
unitVehicle = vehicleType.stream().filter(vehicleId::contains).findAny().orElse("others");
}

Expand Down Expand Up @@ -183,91 +169,4 @@ private void processPersonVehicleTransition(Event event) {
}

}

private JFreeChart getGraphic(String mode, int iteration) {

final XYSeriesCollection xyData = new XYSeriesCollection();
final XYSeries enterSeries = new XYSeries("Enter", false, true);
final XYSeries exitSeries = new XYSeries("Leave", false, true);
final XYSeries onRouteSeries = new XYSeries("en route", false, true);

Map<Integer, Integer> personEnter = personEnterCount.get(mode);
if (personEnter != null && personEnter.size() > 0) {
Set<Integer> enterKeys = personEnter.keySet();
for (Integer key : enterKeys) {
enterSeries.add(key, personEnter.get(key));
}
}


Map<Integer, Integer> personExit = personExitCount.get(mode);
if (personExit != null && personExit.size() > 0) {
Set<Integer> exitKeys = personExit.keySet();
for (Integer key : exitKeys) {
exitSeries.add(key, personExit.get(key));
}
}


Map<Integer, Integer> indexCount = onRoutes.get(mode);
if (indexCount != null && indexCount.size() > 0) {
Set<Integer> indexKeys = indexCount.keySet();
for (Integer key : indexKeys) {
onRouteSeries.add(key, indexCount.get(key));
}
}

xyData.addSeries(enterSeries);
xyData.addSeries(exitSeries);
xyData.addSeries(onRouteSeries);

final JFreeChart chart = ChartFactory.createXYLineChart(
"Trip Histogram, " + mode + ", it." + iteration,
xAxisLabel.replace("<?>", String.valueOf(binSize)), "# persons",
xyData,
PlotOrientation.VERTICAL,
true, // legend
false, // tooltips
false // urls
);


XYPlot plot = chart.getXYPlot();
plot.getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
final CategoryAxis axis1 = new CategoryAxis("sec");
axis1.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 7));
plot.setDomainAxis(new NumberAxis(xAxisLabel.replace("<?>",String.valueOf(binSize))));

plot.getRenderer().setSeriesStroke(0, new BasicStroke(2.0f));
plot.getRenderer().setSeriesStroke(1, new BasicStroke(2.0f));
plot.getRenderer().setSeriesStroke(2, new BasicStroke(2.0f));
plot.setBackgroundPaint(Color.white);
plot.setRangeGridlinePaint(Color.gray);
plot.setDomainGridlinePaint(Color.gray);

return chart;
}

private void writeGraphic(Integer iteration, String mode) {
try {

String filename = fileName + CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, mode) + ".png";
String path = GraphsStatsAgentSimEventsListener.CONTROLLER_IO.getIterationFilename(iteration, filename);
int index = path.lastIndexOf("/");
File outDir = new File(path.substring(0, index) + "/tripHistogram");
if (!outDir.isDirectory()) Files.createDirectories(outDir.toPath());
String newPath = outDir.getPath() + path.substring(index);
ChartUtilities.saveChartAsPNG(new File(newPath), getGraphic(mode, iteration), 1024, 768);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private int getBinIndex(final double time) {
int bin = (int) (time / this.binSize);
if (bin >= this.numOfBins) {
return this.numOfBins;
}
return bin;
}
}
}
Loading