use of org.activityinfo.server.report.generator.map.cluster.Cluster in project activityinfo by bedatadriven.
the class PiechartLayerGenerator method generate.
@Override
public void generate(TiledMap map, MapContent content) {
// create the list of input point values
List<PointValue> points = new ArrayList<PointValue>();
List<PointValue> unmapped = new ArrayList<PointValue>();
// define our symbol scaling
RadiiCalculator radiiCalculator;
if (layer.getScaling() == ScalingType.None || layer.getMinRadius() == layer.getMaxRadius()) {
radiiCalculator = new FixedRadiiCalculator(layer.getMinRadius());
} else if (layer.getScaling() == ScalingType.Graduated) {
radiiCalculator = new GsLogCalculator(layer.getMinRadius(), layer.getMaxRadius());
} else {
radiiCalculator = new FixedRadiiCalculator(layer.getMinRadius());
}
Clusterer clusterer = ClustererFactory.fromClustering(layer.getClustering(), radiiCalculator, new BubbleIntersectionCalculator(layer.getMaxRadius()));
generatePoints(map, layer, clusterer, points, unmapped);
// add unmapped sites
for (PointValue pv : unmapped) {
content.getUnmappedSites().add(pv.getSite().getId());
}
List<Cluster> clusters = clusterer.cluster(map, points);
// create the markers
List<BubbleMapMarker> markers = new ArrayList<BubbleMapMarker>();
for (Cluster cluster : clusters) {
Point px = cluster.getPoint();
AiLatLng latlng = map.fromPixelToLatLng(px);
BubbleMapMarker marker = new PieMapMarker();
sumSlices((PieMapMarker) marker, cluster.getPointValues());
for (PointValue pv : cluster.getPointValues()) {
marker.getSiteIds().add(pv.getSite().getId());
}
marker.setX(px.getX());
marker.setY(px.getY());
marker.setValue(cluster.sumValues());
marker.setRadius((int) cluster.getRadius());
marker.setLat(latlng.getLat());
marker.setLng(latlng.getLng());
marker.setAlpha(layer.getAlpha());
marker.setIndicatorIds(new HashSet<Integer>(layer.getIndicatorIds()));
marker.setClusterAmount(cluster.getPointValues().size());
marker.setClustering(layer.getClustering());
markers.add(marker);
}
// number markers if applicable
if (layer.getLabelSequence() != null) {
numberMarkers(markers);
}
PieChartLegend legend = new PieChartLegend();
legend.setDefinition(layer);
content.getMarkers().addAll(markers);
content.addLegend(legend);
}
use of org.activityinfo.server.report.generator.map.cluster.Cluster in project activityinfo by bedatadriven.
the class GeneticSolver method solve.
public List<Cluster> solve(MarkerGraph graph, RadiiCalculator radiiCalculator, FitnessFunctor fitnessFunctor, List<Integer> allUpperBounds) {
this.radiiCalculator = radiiCalculator;
this.fitnessFunctor = fitnessFunctor;
this.random = new Random();
// for subgraphs that contain only one node, create a cluster
// for them right away and keep them out of the problem.
// this will make crossover / mutations more effective
List<List<MarkerGraph.Node>> allSubGraphs = graph.getSubgraphs();
int popSize = 1;
subgraphs = Lists.newArrayList();
simpleClusters = Lists.newArrayList();
upperBounds = Lists.newArrayList();
for (int i = 0; i != allSubGraphs.size(); i++) {
List<MarkerGraph.Node> subGraph = allSubGraphs.get(i);
if (subGraph.size() == 1) {
simpleClusters.add(new Cluster(subGraph.get(0).getPointValue()));
} else if (allUpperBounds.get(i) == 1) {
simpleClusters.addAll(KMeans.cluster(subGraph, 1));
} else {
upperBounds.add(allUpperBounds.get(i));
subgraphs.add(subGraph);
popSize += 1 + (int) Math.log(allUpperBounds.get(i) * 32);
}
}
if (popSize > 50) {
popSize = 50;
}
this.population = new ArrayList<Phenotype>(popSize);
addRandomPhenotypes(this.population, popSize);
orderPopulation();
double lastFitnessScore = 0;
int generationsStagnated = 0;
if (subgraphs.size() > 0) {
for (int generation = 0; generation < 100; ++generation) {
double fitness = evolve(generationsStagnated);
if (tracer != null) {
tracer.evolved(this, generation, generationsStagnated);
}
if (fitness != lastFitnessScore) {
lastFitnessScore = fitness;
generationsStagnated = 0;
} else {
generationsStagnated++;
}
if (generationsStagnated == 8) {
break;
}
}
}
solutionFitness = getFittest().getFitness();
return getFittest().getClusters();
}
use of org.activityinfo.server.report.generator.map.cluster.Cluster 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.Cluster in project activityinfo by bedatadriven.
the class ComplexClusterTest method testComplexCluster.
@Test
public void testComplexCluster() throws Exception {
ComplexPoints data = new ComplexPoints();
saveGraphImage("ComplexClusterTest-graph", data.graph, 15);
GsLogCalculator radiiCalc = new GsLogCalculator(5, 10);
double sumAfterMerging = 0;
for (MarkerGraph.Node node : data.graph.getNodes()) {
sumAfterMerging += node.getPointValue().getValue();
}
Assert.assertEquals("originalSum==sumAfterMerging", data.originalSum, sumAfterMerging, DELTA);
// becuase the algorithm is stochiastic, we want need to make sure
// it works consistently
int count = 10;
while (count-- > 0) {
GeneticSolver solver = new GeneticSolver();
solver.setTracer(new GeneticTracer());
List<Cluster> clusters = solver.solve(data.graph, radiiCalc, new BubbleFitnessFunctor(), UpperBoundsCalculator.calculate(data.graph, new FixedRadiiCalculator(5)));
double sumAfterClustering = 0;
for (Cluster cluster : clusters) {
sumAfterClustering += cluster.sumValues();
}
Assert.assertEquals("originalSum==sumAfterClustering", data.originalSum, sumAfterClustering, DELTA);
if (count == 0) {
saveClusters(data.graph, "ComplexClusterTest-solution", clusters);
}
System.out.println(String.format("pop size = %d", solver.getPopulation().size()));
System.out.println(String.format("subgraph count = %d", data.graph.getSubgraphs().size()));
System.out.println(String.format("cluster count = %d", clusters.size()));
System.out.println(String.format("fitness = %f", solver.getSolutionFitness()));
// TODO: this seems to be successful about 75% of the time. We need
// to move that to 100%
// and reduce variation
// Assert.assertTrue("Did not meet success criteria at run "+ count,
// solver.getSolutionFitness() > 5000);
}
}
use of org.activityinfo.server.report.generator.map.cluster.Cluster in project activityinfo by bedatadriven.
the class ComplexClusterTest method dump2dBruteForce.
@Test
@Ignore("quite slow")
public void dump2dBruteForce() throws Exception {
ComplexPoints data = new ComplexPoints();
FitnessFunctor fitFtor = new BubbleFitnessFunctor();
GsLogCalculator rCtor = new GsLogCalculator(5, 30);
BufferedWriter csv = new BufferedWriter(new FileWriter("build/report-tests/brute.csv"));
List<List<MarkerGraph.Node>> subgraphs = data.getLargestN(2);
// write column headers
for (int j = 0; j != subgraphs.get(0).size(); ++j) {
csv.write("," + (j + 1));
}
csv.newLine();
// write rows
for (int i = 1; i <= subgraphs.get(0).size(); ++i) {
// write row header
csv.write(Integer.toString(i));
// write cell values
for (int j = 1; j <= subgraphs.get(1).size(); ++j) {
List<Cluster> clusters = new ArrayList<Cluster>();
clusters.addAll(KMeans.cluster(subgraphs.get(0), i));
clusters.addAll(KMeans.cluster(subgraphs.get(1), j));
rCtor.calculate(clusters);
csv.write(Double.toString(fitFtor.score(clusters)));
}
csv.newLine();
}
csv.close();
}
Aggregations