use of org.activityinfo.server.report.generator.map.cluster.genetic.MarkerGraph in project activityinfo by bedatadriven.
the class CoincidentPointsClusterTest method testRealData.
@Test
public void testRealData() throws Exception {
// Define projection for the test case
TiledMap map = new TiledMap(492, 690, new AiLatLng(2.293492496, 30.538372993), 9);
// Read data
BufferedReader in = new BufferedReader(new InputStreamReader(GraphTest.class.getResourceAsStream("/distribscolaire-points.csv")));
double originalSum = 0;
List<PointValue> points = new ArrayList<PointValue>();
while (in.ready()) {
String line = in.readLine();
String[] columns = line.split(",");
double lat = Double.parseDouble(columns[0]);
double lng = Double.parseDouble(columns[1]);
PointValue pv = new PointValue();
pv.setPx(map.fromLatLngToPixel(new AiLatLng(lat, lng)));
pv.setValue(Double.parseDouble(columns[2]));
pv.setSymbol(new MapSymbol());
pv.setSite(new SiteDTO());
originalSum += pv.getValue();
points.add(pv);
}
// Now build the graph
MarkerGraph graph = new MarkerGraph(points, new BubbleIntersectionCalculator(15));
// make sure nothing was lost in the merging of coincident points
double nodeSum = 0;
for (MarkerGraph.Node node : graph.getNodes()) {
nodeSum += node.getPointValue().getValue();
}
Assert.assertEquals("values after construction of graph", originalSum, nodeSum, 0.001);
saveGraphImage("clusterTest2", graph, 15);
GeneticSolver solver = new GeneticSolver();
List<Cluster> clusters = solver.solve(graph, new GsLogCalculator(5, 15), new BubbleFitnessFunctor(), UpperBoundsCalculator.calculate(graph, new FixedRadiiCalculator(5)));
// check to make sure all values were included
double sumAfterClustering = 0;
for (Cluster cluster : clusters) {
sumAfterClustering += cluster.sumValues();
}
Assert.assertEquals(originalSum, sumAfterClustering, 0.001);
Assert.assertEquals(15, clusters.size());
saveClusters(graph, "clusterTest-solution", clusters);
}
use of org.activityinfo.server.report.generator.map.cluster.genetic.MarkerGraph in project activityinfo by bedatadriven.
the class GeneticClusterer method cluster.
/*
* Clusters points using a genetic algorithm to determine what nearby points
* are logical candidates to cluster
*
* @see
* org.activityinfo.legacy.model.reports.model.clustering.Clustering#cluster(java
* .util.List, org.activityinfo.server.report.generator.map.RadiiCalculator)
*/
@Override
public List<Cluster> cluster(TiledMap map, List<PointValue> points) {
List<Cluster> clusters;
MarkerGraph graph = new MarkerGraph(points, intersectionCalculator);
GeneticSolver solver = new GeneticSolver();
clusters = solver.solve(graph, radiiCalculator, new BubbleFitnessFunctor(), UpperBoundsCalculator.calculate(graph, radiiCalculator));
return clusters;
}
use of org.activityinfo.server.report.generator.map.cluster.genetic.MarkerGraph in project activityinfo by bedatadriven.
the class CoincidentPointsClusterTest method testSimpleData.
@Test
public void testSimpleData() throws Exception {
List<PointValue> points = new ArrayList<PointValue>();
points.add(new PointValue(new SiteDTO(), new MapSymbol(), 7.0, new Point(0, 0)));
points.add(new PointValue(new SiteDTO(), new MapSymbol(), 2.0, new Point(0, 0)));
points.add(new PointValue(new SiteDTO(), new MapSymbol(), 41.0, new Point(100, 100)));
points.add(new PointValue(new SiteDTO(), new MapSymbol(), 9.0, new Point(0, 0)));
points.add(new PointValue(new SiteDTO(), new MapSymbol(), 39.0, new Point(100, 100)));
double originalSum = 7 + 2 + 9 + 41 + 39;
// Now build the graph
MarkerGraph graph = new MarkerGraph(points, new BubbleIntersectionCalculator(15));
GeneticSolver solver = new GeneticSolver();
List<Cluster> clusters = solver.solve(graph, new GsLogCalculator(5, 15), new BubbleFitnessFunctor(), UpperBoundsCalculator.calculate(graph, new FixedRadiiCalculator(5)));
// check to make sure all values were included
double sumAfterClustering = 0;
for (Cluster cluster : clusters) {
sumAfterClustering += cluster.sumValues();
}
Assert.assertEquals(originalSum, sumAfterClustering, 0.0001);
Assert.assertEquals(2, clusters.size());
saveClusters(graph, "clusterTest-solution", clusters);
}
use of org.activityinfo.server.report.generator.map.cluster.genetic.MarkerGraph in project activityinfo by bedatadriven.
the class GraphTest method saveGraphImage.
protected void saveGraphImage(String testName, MarkerGraph graph, int maxRadius) throws Exception {
File outputDir = new File("build/report-tests");
outputDir.mkdirs();
File outputFile = new File("build/report-tests/" + testName + ".svg");
FileWriter svg = new FileWriter(outputFile);
svg.write("<svg width='100%' height='100%' transform='scale(50)' version='1.1' xmlns='http://www.w3.org/2000/svg'>");
for (MarkerGraph.Edge edge : graph.getEdges()) {
svg.write(String.format("<line x1='%d' y1='%d' x2='%d' y2='%d' " + "style='stroke:rgb(99,99,99);stroke-width:1'/>", edge.getA().getPoint().getX(), edge.getA().getPoint().getY(), edge.getB().getPoint().getX(), edge.getB().getPoint().getY()));
}
for (MarkerGraph.Node node : graph.getNodes()) {
svg.write(String.format("<circle cx='%d' cy='%d' r='1.5' " + "style='stroke:none; fill: blue'/>", node.getPoint().getX(), node.getPoint().getY()));
// svg.write(String.format("<circle cx='%d' cy='%d' r='%d' " +
// "style='stroke:blue; stroke-width:1; fill: none' title='node%d'/>",
// node.getPoint().getX(),
// node.getPoint().getY(),
// maxRadius,
// node.index));
}
svg.write("</svg>");
svg.close();
}
use of org.activityinfo.server.report.generator.map.cluster.genetic.MarkerGraph in project activityinfo by bedatadriven.
the class GraphTest method saveClusters.
protected void saveClusters(MarkerGraph graph, String fileName, List<Cluster> clusters) throws IOException {
File outputDir = new File("build/report-tests");
outputDir.mkdirs();
File outputFile = new File("build/report-tests/" + fileName + ".svg");
FileWriter svg = new FileWriter(outputFile);
svg.write("<svg width='100%' height='100%' transform='scale(500)' version='1.1' " + "xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' >\n");
for (MarkerGraph.Edge edge : graph.getEdges()) {
svg.write(String.format("<path d='M%d %d L%d %d' " + "style='stroke:rgb(92,92,92);stroke-width:0.25'/>\n", edge.getA().getPoint().getX(), edge.getA().getPoint().getY(), edge.getB().getPoint().getX(), edge.getB().getPoint().getY()));
}
String[] colors = new String[] { "antiquewhite", "blue", "brown", "chartreuse", "cornflowerblue", "crimson", "darkkhaki", "darkorange", "darkorchid", "lightpink", "lightseagreen", "lightskyblue", "lime", "limegreen", "magenta", "mediumaqua" };
int colorIndex = 0;
for (int i = 0; i != clusters.size(); ++i) {
Cluster cluster = clusters.get(i);
svg.write(String.format("<circle cx='%d' cy='%d' r='%f' " + "style='fill: %s; fill-opacity: 0.5; stroke:none'/>\n", cluster.getPoint().getX(), cluster.getPoint().getY(), cluster.getRadius(), colors[colorIndex]));
for (PointValue pointValue : cluster.getPointValues()) {
svg.write(String.format("<circle cx='%d' cy='%d' r='1.5' " + "style='stroke:rgb(92,92,92); stroke-width:0.1; fill: %s' title='%f'/>\n", pointValue.getPx().getX(), pointValue.getPx().getY(), colors[colorIndex], pointValue.getValue()));
}
colorIndex++;
if (colorIndex == colors.length) {
colorIndex = 0;
}
}
// label subgraphs
List<List<MarkerGraph.Node>> subgraphs = graph.getSubgraphs();
for (int i = 0; i != subgraphs.size(); ++i) {
Point p = findLR(subgraphs.get(i));
svg.write(String.format("<text x='%d' y='%d'>%d</text>\n", p.getX() + 5, p.getY(), i));
}
svg.write("</svg>\n");
svg.close();
}
Aggregations