Esempio n. 1
0
  public double[][] getBestWeights(int rounds, int popSize) {
    double[][][] currentWeights = new double[popSize][][];
    double[][][] localBestWeights = new double[popSize][][];
    double[][] globalBest = null;
    double[] localBestScores = new double[popSize];
    double globalBestScore;
    double[] currentFitness = new double[popSize];

    // initialization
    for (int i = 0; i < popSize; i++) {
      currentWeights[i] = new double[1][];
      localBestWeights[i] = new double[1][];

      net.generateRandomWeights();
      currentWeights[i][0] = net.getWeights();
      localBestWeights[i][0] = net.getWeights();
      localBestScores[i] = 0;
    }

    globalBestScore = 0;
    globalBest = currentWeights[0];

    for (int i = 0; i < rounds; i++) {
      // first update all of the particles
      for (int j = 0; j < popSize; j++) {
        currentWeights[j] = updateParticle(currentWeights[j], localBestWeights[j], globalBest);
      }

      // update all of the scores for global and local best
      for (int j = 0; j < popSize; j++) {
        for (int k = 0; k < popSize; k++) {
          // run match and record scores
          if (j != k) {
            double[][] results =
                Main.runFightSimulation(currentWeights[j], currentWeights[k], 0, 0, false, i);
            currentFitness[j] += results[0][0];
            currentFitness[k] += results[1][0];
          }
        }
      }

      // loop over scores and update local and global best as necessary
      for (int j = 0; j < popSize; j++) {
        if (currentFitness[j] > localBestScores[j]) {
          localBestScores[j] = currentFitness[j];
          localBestWeights[j] = currentWeights[j];
        }
        if (currentFitness[j] > globalBestScore) {
          globalBestScore = currentFitness[j];
          globalBest = currentWeights[j];
        }
      }
      System.out.println("Finished round " + i + " of PSO");
    }

    return globalBest;
  }
  /**
   * Test a Feedforward neural net using 5x2-fold cross validation.
   *
   * @param size
   * @param dimensions
   * @param repeats
   * @param rbfBasisFunction
   * @param activationFunction
   * @param learningRate
   * @param clusters
   * @param momentums
   * @param hidden
   * @param dataset
   * @return 2D array of results
   */
  public static double[][] trainFF(
      int size,
      int dimensions,
      int[] repeats,
      int rbfBasisFunction,
      ActivationFunction activationFunction,
      double learningRate,
      int[] clusters,
      double momentums,
      int hidden,
      double[][] dataset) {
    // Create the sizes array used by the feed forward neural net
    int[] sizesArray = new int[hidden + 2];
    sizesArray[0] = dimensions + 1;
    for (int i = 1; i < sizesArray.length - 1; i++) {
      sizesArray[i] = clusters[0];
    }
    sizesArray[sizesArray.length - 1] = 2;

    // Initialize the storage for the output of this test.
    double[][] output = new double[7][10]; // Bundle of statistics
    double[] confidences = new double[size * 10]; // Store all confidence values
    double confidenceAR = 0.0; // Sum of confidences above when guessed right
    double confidenceAW = 0.0; // Sum of confidences above when guessed wrong
    double confidenceBR = 0.0; // Sum of confidences below when guessed wrong
    double confidenceBW = 0.0; // Sum of confidences below when guessed right
    System.out.println("Feed Forward Neural Network:");
    // For 5 repetitions of 2-fold cross validation
    for (int i = 0; i < 5; i++) {
      int count = i + 1;
      System.out.println("Two-fold cross validation repetition " + count);
      double[][][] datasets = partitionData(dataset);

      // First: train on datasets[0], test on datasets[1]
      FeedForwardNeuralNetwork ff =
          RunFeedForward.run(
              datasets[0],
              hidden,
              sizesArray,
              learningRate,
              momentums,
              activationFunction,
              repeats[1]);

      double confidenceSum = 0.0;
      double aboveRight = 0.0;
      double aboveWrong = 0.0;
      double belowRight = 0.0;
      double belowWrong = 0.0;

      for (int j = 0; j < datasets[1].length; j++) {
        // Index of the output of the Rosenbrock function
        int index = datasets[1][j].length - 1;
        // Actual output of the Rosenbrock function
        double actualValue = datasets[1][j][index];
        // Value plus or minus a constant percent of the actual value
        double offsetValue = plusOrMinus10(actualValue);

        // Compute the confidences that the offset value is above or below the function
        datasets[1][j][index] = offsetValue;
        double[] prediction = ff.compute(datasets[1][j]);
        datasets[1][j][index] = actualValue;

        boolean abovePredicted = prediction[1] < prediction[0];
        boolean aboveActual = actualValue < offsetValue;
        double confidence;
        if (abovePredicted && aboveActual) {
          confidence = prediction[0];
          confidenceAR += confidence;
          aboveRight += 1.0;
        } else if (abovePredicted) {
          confidence = prediction[0];
          confidenceAW += confidence;
          aboveWrong += 1.0;
        } else if (aboveActual) {
          confidence = prediction[1];
          confidenceBW += confidence;
          belowWrong += 1.0;
        } else {
          confidence = prediction[1];
          confidenceBR += confidence;
          belowRight += 1.0;
        }
        confidences[size * i + j * 2] = confidence;
        confidenceSum += confidence;
      }

      confidenceSum /= datasets[1].length;

      output[i][0] = aboveRight;
      output[i][1] = aboveWrong;
      output[i][2] = belowRight;
      output[i][3] = belowWrong;
      output[i][4] = confidenceSum;

      // Second: train on datasets[1], test on datasets[0]
      ff =
          RunFeedForward.run(
              datasets[1],
              hidden,
              sizesArray,
              learningRate,
              momentums,
              activationFunction,
              repeats[1]);

      confidenceSum = 0.0;
      aboveRight = 0.0;
      aboveWrong = 0.0;
      belowRight = 0.0;
      belowWrong = 0.0;

      for (int j = 0; j < datasets[0].length; j++) {
        // Index of the output of the Rosenbrock function
        int index = datasets[0][j].length - 1;
        // Actual output of the Rosenbrock function
        double actualValue = datasets[0][j][index];
        // Value plus or minus a constant percent of the actual value
        double offsetValue = plusOrMinus10(actualValue);

        // Compute the confidences that the offset value is above or below the function
        datasets[0][j][index] = offsetValue;
        double[] prediction = ff.compute(datasets[0][j]);
        datasets[0][j][index] = actualValue;

        // True iff the prediction is above the function
        boolean abovePredicted = prediction[1] < prediction[0];

        // True iff the offset value is above the function
        boolean aboveActual = actualValue < offsetValue;

        double confidence; // This will be the selected confidence
        if (abovePredicted && aboveActual) {
          confidence = prediction[0];
          confidenceAR += confidence;
          aboveRight += 1.0;
        } else if (abovePredicted) {
          confidence = prediction[0];
          confidenceAW += confidence;
          aboveWrong += 1.0;
        } else if (aboveActual) {
          confidence = prediction[1];
          confidenceBW += confidence;
          belowWrong += 1.0;
        } else {
          confidence = prediction[1];
          confidenceBR += confidence;
          belowRight += 1.0;
        }

        // Store confidence data
        confidences[size * i + j * 2 + 1] = confidence;
        confidenceSum += confidence;
      }

      confidenceSum /= datasets[0].length;

      // Store data from every train/test separately
      output[i][5] = aboveRight;
      output[i][6] = aboveWrong;
      output[i][7] = belowRight;
      output[i][8] = belowWrong;
      output[i][9] = confidenceSum;
    }

    // Store all confidence values
    output[5] = confidences;
    output[6] = new double[] {confidenceAR, confidenceAW, confidenceBW, confidenceBR};
    return output;
  }