@Test
  public void testBiDistribution() {
    Algorithm algorithm = mock(Algorithm.class);

    MOFitness fitness01 =
        Fitnesses.create(new MinimisationFitness(3.0), new MinimisationFitness(0.0));
    final Type type01 = mock(Type.class, "type2_01");

    MOFitness fitness02 =
        Fitnesses.create(new MinimisationFitness(2.0), new MinimisationFitness(1.0));
    final Type type02 = mock(Type.class, "type2_02");

    // Cloning returns the mocked objects themselves.
    when(type01.getClone()).thenReturn(type01);
    when(type02.getClone()).thenReturn(type02);

    OptimisationSolution solution01 = new OptimisationSolution(type01, fitness01);
    OptimisationSolution solution02 = new OptimisationSolution(type02, fitness02);

    Archive archive = Archive.Provider.get();
    archive.add(solution01);
    archive.add(solution02);

    Real distribution = new SolutionDistribution().getValue(algorithm);
    Assert.assertEquals(0.0, distribution.doubleValue(), 0.0000000000001);
  }
  @Test
  public void results() {
    Algorithm algorithm = this.mockery.mock(Algorithm.class);

    MOFitness fitness01 =
        Fitnesses.create(new MinimisationFitness(0.0), new MinimisationFitness(1.0));
    final Type type01 = this.mockery.mock(Type.class, "type_01");

    MOFitness fitness02 =
        Fitnesses.create(new MinimisationFitness(0.5), new MinimisationFitness(0.5));
    final Type type02 = this.mockery.mock(Type.class, "type_02");

    MOFitness fitness03 =
        Fitnesses.create(new MinimisationFitness(1.0), new MinimisationFitness(0.0));
    final Type type03 = this.mockery.mock(Type.class, "type_03");

    // Cloning returns the mocked objects themselves.
    this.mockery.checking(
        new Expectations() {

          {
            oneOf(type01).getClone();
            will(returnValue(type01));
            oneOf(type02).getClone();
            will(returnValue(type02));
            oneOf(type03).getClone();
            will(returnValue(type03));
          }
        });

    OptimisationSolution solution01 = new OptimisationSolution(type01, fitness01);
    OptimisationSolution solution02 = new OptimisationSolution(type02, fitness02);
    OptimisationSolution solution03 = new OptimisationSolution(type03, fitness03);

    Archive archive = Archive.Provider.get();
    archive.add(solution01);
    archive.add(solution02);
    archive.add(solution03);

    Real extent = new ParetoFrontExtent().getValue(algorithm);
    Assert.assertEquals(Math.sqrt(2.0), extent.doubleValue(), 0.00001);
  }
Beispiel #3
0
  /**
   * Calculates the AUC of one output.
   *
   * @param targets The expected outputs. Each value is discetised to the closest bound.
   * @param outputs The outputs of the NN.
   * @return The AUC in the range [0,1].
   */
  public double areaUnderCurve(ArrayList<Real> targets, ArrayList<Real> outputs) {

    double negatives = 0.0;
    double positives = 0.0;

    double centerBound = (upperBound + lowerBound) / 2.0;

    // determine total positives and negatives
    for (Real curTarget : targets) {
      if (curTarget.doubleValue() > centerBound) positives += 1;
      else negatives += 1;
    }

    // plot ROC curve coordinates
    double threshold = lowerBound;
    ArrayList<Real> tpCoords = new ArrayList<Real>();
    ArrayList<Real> fpCoords = new ArrayList<Real>();

    // when all outputs are seen as true
    tpCoords.add(Real.valueOf(1.0));
    fpCoords.add(Real.valueOf(1.0));

    double newThresholdL;
    double newThresholdU;
    do {

      // calculate next threshold
      newThresholdL = upperBound;
      newThresholdU = upperBound;
      for (Real curOutput : outputs) {
        if (curOutput.doubleValue() >= threshold && curOutput.doubleValue() < newThresholdL) {
          newThresholdL = curOutput.doubleValue();
        }
      }
      for (Real curOutput : outputs) {
        if (curOutput.doubleValue() > newThresholdL && curOutput.doubleValue() < newThresholdU) {
          newThresholdU = curOutput.doubleValue();
        }
      }
      threshold = (newThresholdL + newThresholdU) / 2.0;

      // calculate tp and fp for this threshold
      double tPositive = 0;
      double fPositive = 0;
      for (int curOutput = 0; curOutput < outputs.size(); ++curOutput) {
        if (targets.get(curOutput).doubleValue() > centerBound
            && outputs.get(curOutput).doubleValue() > threshold) {
          tPositive += 1;
        } else if (targets.get(curOutput).doubleValue() <= centerBound
            && outputs.get(curOutput).doubleValue() > threshold) {
          fPositive += 1;
        }
      }
      tpCoords.add(Real.valueOf(tPositive / positives));
      fpCoords.add(Real.valueOf(fPositive / negatives));

    } while (newThresholdU < upperBound);

    // when all outputs are seen as false
    tpCoords.add(Real.valueOf(0.0));
    fpCoords.add(Real.valueOf(0.0));

    // calculate area
    double area = 0.0;
    for (int curCoord = 1; curCoord < fpCoords.size(); ++curCoord) {
      area +=
          ((tpCoords.get(curCoord - 1).doubleValue() + tpCoords.get(curCoord).doubleValue()) / 2)
              * (fpCoords.get(curCoord - 1).doubleValue()
                  - fpCoords.get(curCoord).doubleValue()); // fpCoords sorted in decending order
    }

    return area;
  }