@Test
  public void testSimpleData() throws Exception {
    List<PointValue> points = new ArrayList<PointValue>();
    points.add(new PointValue(null, new MapSymbol(), 7.0, new Point(0, 0)));
    points.add(new PointValue(null, new MapSymbol(), 2.0, new Point(0, 0)));
    points.add(new PointValue(null, new MapSymbol(), 41.0, new Point(100, 100)));
    points.add(new PointValue(null, new MapSymbol(), 9.0, new Point(0, 0)));
    points.add(new PointValue(null, 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 BubbleLayerGenerator.IntersectionCalculator(15));

    GeneticSolver solver = new GeneticSolver();

    List<Cluster> clusters =
        solver.solve(
            graph,
            new GsLogCalculator(5, 15),
            new CircleFitnessFunctor(),
            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);
  }
Пример #2
0
  @Test
  public void GIVEN_individual_WHEN_score_THEN_returnScore() {
    // GIVEN
    int expectedScore = 10;
    int[] individual = new int[10];
    for (int i = 0; i < individual.length; i++) {
      individual[i] = i;
    }

    // WHEN
    int actualScore = geneticSolver.score(individual);

    // THEN
    assertEquals(expectedScore, actualScore);
  }
Пример #3
0
  @Test
  public void GIVEN_mutationRateIsZero_WHEN_procreate_THEN_returnProperChild() {
    // GIVEN
    int[] parentOne = new int[10];
    int[] parentTwo = new int[10];
    for (int i = 0; i < parentOne.length; i++) {
      parentOne[i] = i;
      parentTwo[i] = 10 - i;
    }

    // WHEN
    geneticSolver = new GeneticSolver(0, defaultGenerationLimit);
    int[] child = geneticSolver.procreate(parentOne, parentTwo, -5, 10);

    // THEN
    assertEquals(parentOne.length, child.length);
    for (int i = 0; i < child.length; i++) {
      // This isn't a very good way to do this test, but I'm not sure of a better one atm
      if (child[i] != parentOne[i] || child[i] != parentTwo[i]) {
        fail("The child should not have mutated.");
      }
    }
  }
  @Test
  public void testRealData() throws Exception {

    // Define projection for the test case
    TiledMap map = new TiledMap(492, 690, new LatLng(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.px = map.fromLatLngToPixel(new LatLng(lat, lng));
      pv.value = Double.parseDouble(columns[2]);
      pv.symbol = new MapSymbol();

      originalSum += pv.value;

      points.add(pv);
    }

    // Now build the graph

    MarkerGraph graph =
        new MarkerGraph(points, new BubbleLayerGenerator.IntersectionCalculator(15));

    // make sure nothing was lost in the merging of coincident points
    double nodeSum = 0;
    for (MarkerGraph.Node node : graph.getNodes()) {
      nodeSum += node.getPointValue().value;
    }
    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 CircleFitnessFunctor(),
            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(16, clusters.size());

    saveClusters(graph, "clusterTest-solution", clusters);
  }