Search in sources :

Example 6 with Cluster

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);
}
Also used : PointValue(org.activityinfo.legacy.shared.reports.model.PointValue) Cluster(org.activityinfo.server.report.generator.map.cluster.Cluster) Clusterer(org.activityinfo.server.report.generator.map.cluster.Clusterer) AiLatLng(org.activityinfo.model.type.geo.AiLatLng)

Example 7 with Cluster

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();
}
Also used : Cluster(org.activityinfo.server.report.generator.map.cluster.Cluster)

Example 8 with Cluster

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);
}
Also used : MarkerGraph(org.activityinfo.server.report.generator.map.cluster.genetic.MarkerGraph) PointValue(org.activityinfo.legacy.shared.reports.model.PointValue) ArrayList(java.util.ArrayList) Cluster(org.activityinfo.server.report.generator.map.cluster.Cluster) MapSymbol(org.activityinfo.legacy.shared.reports.model.MapSymbol) Point(org.activityinfo.legacy.shared.reports.content.Point) GeneticSolver(org.activityinfo.server.report.generator.map.cluster.genetic.GeneticSolver) BubbleFitnessFunctor(org.activityinfo.server.report.generator.map.cluster.genetic.BubbleFitnessFunctor) SiteDTO(org.activityinfo.legacy.shared.model.SiteDTO) Test(org.junit.Test)

Example 9 with Cluster

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);
    }
}
Also used : Cluster(org.activityinfo.server.report.generator.map.cluster.Cluster) Test(org.junit.Test)

Example 10 with Cluster

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();
}
Also used : FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) Cluster(org.activityinfo.server.report.generator.map.cluster.Cluster) BufferedWriter(java.io.BufferedWriter) List(java.util.List) ArrayList(java.util.ArrayList) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Cluster (org.activityinfo.server.report.generator.map.cluster.Cluster)11 PointValue (org.activityinfo.legacy.shared.reports.model.PointValue)7 ArrayList (java.util.ArrayList)6 AiLatLng (org.activityinfo.model.type.geo.AiLatLng)5 Point (org.activityinfo.legacy.shared.reports.content.Point)4 Test (org.junit.Test)4 SiteDTO (org.activityinfo.legacy.shared.model.SiteDTO)3 Clusterer (org.activityinfo.server.report.generator.map.cluster.Clusterer)3 MarkerGraph (org.activityinfo.server.report.generator.map.cluster.genetic.MarkerGraph)3 FileWriter (java.io.FileWriter)2 List (java.util.List)2 MapSymbol (org.activityinfo.legacy.shared.reports.model.MapSymbol)2 BubbleFitnessFunctor (org.activityinfo.server.report.generator.map.cluster.genetic.BubbleFitnessFunctor)2 GeneticSolver (org.activityinfo.server.report.generator.map.cluster.genetic.GeneticSolver)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1 IconLayerLegend (org.activityinfo.legacy.shared.reports.content.IconLayerLegend)1 IconMapMarker (org.activityinfo.legacy.shared.reports.content.IconMapMarker)1