PageRenderTime 34ms CodeModel.GetById 2ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 1ms

/sigmah/src/test/java/org/sigmah/server/report/generator/map/ComplexClusterTest.java

http://sigma-h.googlecode.com/
Java | 134 lines | 79 code | 37 blank | 18 comment | 9 complexity | e703204cf2850e9ecfb03e1eab90cdde MD5 | raw file
  1/*
  2 * All Sigmah code is released under the GNU General Public License v3
  3 * See COPYRIGHT.txt and LICENSE.txt.
  4 */
  5
  6package org.sigmah.server.report.generator.map;
  7
  8import org.junit.Assert;
  9import org.junit.Ignore;
 10import org.junit.Test;
 11
 12import java.io.BufferedWriter;
 13import java.io.FileWriter;
 14import java.util.ArrayList;
 15import java.util.List;
 16
 17/**
 18 * @author Alex Bertram
 19 */
 20public class ComplexClusterTest extends GraphTest {
 21    private static final double DELTA = 0.0001;
 22
 23    @Test
 24    @Ignore("quite slow")
 25    public void dump2dBruteForce() throws Exception {
 26
 27        ComplexPoints data = new ComplexPoints();
 28
 29
 30        FitnessFunctor fitFtor = new CircleFitnessFunctor();
 31        GsLogCalculator rCtor = new GsLogCalculator(5, 30);
 32
 33        BufferedWriter csv = new BufferedWriter(new FileWriter("target/report-tests/brute.csv"));
 34
 35        List<List<MarkerGraph.Node>> subgraphs = data.getLargestN(2);
 36
 37        // write column headers
 38        for (int j = 0; j != subgraphs.get(0).size(); ++j) {
 39            csv.write("," + (j + 1));
 40        }
 41        csv.newLine();
 42
 43        // write rows
 44        for (int i = 1; i <= subgraphs.get(0).size(); ++i) {
 45
 46            // write row header
 47            csv.write(Integer.toString(i));
 48
 49            // write cell values
 50            for (int j = 1; j <= subgraphs.get(1).size(); ++j) {
 51                List<Cluster> clusters = new ArrayList<Cluster>();
 52                clusters.addAll(KMeans.cluster(subgraphs.get(0), i));
 53                clusters.addAll(KMeans.cluster(subgraphs.get(1), j));
 54                rCtor.calculate(clusters);
 55
 56                csv.write(Double.toString(fitFtor.score(clusters)));
 57            }
 58
 59            csv.newLine();
 60        }
 61        csv.close();
 62    }
 63
 64    @Test
 65    public void testUpperBounds() throws Exception {
 66
 67        ComplexPoints data = new ComplexPoints();
 68        List<Integer> ub = UpperBoundsCalculator.calculate(data.graph, new FixedRadiiCalculator(5),
 69                new UpperBoundsCalculator.Tracer() {
 70                    public void onSubgraph(int nodeCount) {
 71                        System.out.println("Calculating upperbounds for subgraph with " + nodeCount + " node(s)");
 72                    }
 73
 74                    public void incremented(int count, List<Cluster> clusters, double fitness) {
 75                        System.out.println(count + " cluster(s) = " + fitness);
 76                    }
 77                });
 78//        Assert.assertEquals(23, ub.get(0));
 79//        Assert.assertEquals(9, ub.get(1));
 80
 81    }
 82
 83    @Test
 84    public void testComplexCluster() throws Exception {
 85
 86        ComplexPoints data = new ComplexPoints();
 87
 88        saveGraphImage("ComplexClusterTest-graph", data.graph, 15);
 89
 90        GsLogCalculator radiiCalc = new GsLogCalculator(5, 10);
 91
 92        double sumAfterMerging = 0;
 93        for (MarkerGraph.Node node : data.graph.getNodes()) {
 94            sumAfterMerging += node.getPointValue().value;
 95        }
 96        Assert.assertEquals("originalSum==sumAfterMerging", data.originalSum, sumAfterMerging, DELTA);
 97
 98
 99        // becuase the algorithm is stochiastic, we want need to make sure
100        // it works consistently
101
102        int count = 10;
103        while (count-- > 0) {
104
105
106            GeneticSolver solver = new GeneticSolver();
107            solver.setTracer(new GeneticTracer());
108            List<Cluster> clusters = solver.solve(data.graph, radiiCalc, new CircleFitnessFunctor(),
109                    UpperBoundsCalculator.calculate(data.graph, new FixedRadiiCalculator(5)));
110
111
112            double sumAfterClustering = 0;
113            for (Cluster cluster : clusters) {
114                sumAfterClustering += cluster.sumValues();
115            }
116            Assert.assertEquals("originalSum==sumAfterClustering", data.originalSum, sumAfterClustering, DELTA);
117
118            if (count == 0) {
119                saveClusters(data.graph, "ComplexClusterTest-solution", clusters);
120            }
121
122            System.out.println(String.format("pop size = %d", solver.getPopulation().size()));
123            System.out.println(String.format("subgraph count = %d", data.graph.getSubgraphs().size()));
124            System.out.println(String.format("cluster count = %d", clusters.size()));
125
126            System.out.println(String.format("fitness = %f", solver.getSolutionFitness()));
127
128            // TODO: this seems to be successful about 75% of the time. We need to move that to 100%
129            // and reduce variation
130            //Assert.assertTrue("Did not meet success criteria at run "+ count, solver.getSolutionFitness() > 5000);
131        }
132    }
133
134}