/**
   * Process the file and cluster.
   *
   * @param outputFile The output file.
   * @param clusters The number of clusters.
   * @param theAnalyst The analyst to use.
   * @param iterations The number of iterations to use.
   */
  public void process(
      final File outputFile,
      final int clusters,
      final EncogAnalyst theAnalyst,
      final int iterations) {

    final PrintWriter tw =
        this.prepareOutputFile(
            outputFile, analyst.getScript().getNormalize().countActiveFields() - 1, 1);

    resetStatus();

    final KMeansClustering cluster = new KMeansClustering(clusters, this.data);
    cluster.iteration(iterations);

    int clusterNum = 0;
    for (final MLCluster cl : cluster.getClusters()) {
      for (final MLData item : cl.getData()) {
        final int clsIndex = item.size();
        final LoadedRow lr = new LoadedRow(this.getFormat(), item.getData(), 1);
        lr.getData()[clsIndex] = "" + clusterNum;
        writeRow(tw, lr);
      }
      clusterNum++;
    }

    reportDone(false);
    tw.close();
  }
Exemplo n.º 2
0
 /** Utility method to switch from MLData to List<Double> */
 public static List<Double> doubleListFromNeuralOutput(MLData neuralOutput) {
   List<Double> result = new ArrayList<Double>(neuralOutput.size());
   for (double value : neuralOutput.getData()) {
     result.add(value);
   }
   return result;
 }
Exemplo n.º 3
0
  @Override
  protected void putPrediction(MLData output, Map<StateParameter, Double> nextState) {
    int i = 0;

    for (StateParameter stateParameter : StateParameter.values())
      if (stateParameter != width && stateParameter != height)
        nextState.put(stateParameter, output.getData(i++));
  }
Exemplo n.º 4
0
 /**
  * Format neural data as a list of numbers.
  *
  * @param data The neural data to format.
  * @return The formatted neural data.
  */
 public static String formatNeuralData(final MLData data) {
   final StringBuilder result = new StringBuilder();
   for (int i = 0; i < data.size(); i++) {
     if (i != 0) {
       result.append(',');
     }
     result.append(Format.formatDouble(data.getData(i), 4));
   }
   return result.toString();
 }
  /**
   * Calculate the Euclidean distance for the specified output neuron and the input vector. This is
   * the square root of the squares of the differences between the weight and input vectors.
   *
   * @param matrix The matrix to get the weights from.
   * @param input The input vector.
   * @param outputNeuron The neuron we are calculating the distance for.
   * @return The Euclidean distance.
   */
  public double calculateEuclideanDistance(
      final Matrix matrix, final MLData input, final int outputNeuron) {
    double result = 0;

    // Loop over all input data.
    for (int i = 0; i < input.size(); i++) {
      final double diff = input.getData(i) - matrix.get(outputNeuron, i);
      result += diff * diff;
    }
    return BoundMath.sqrt(result);
  }
Exemplo n.º 6
0
  /**
   * Determine the classes for the specified input.
   *
   * @param input The input.
   * @return An array of class indexes.
   */
  public int[] determineClasses(MLData input) {
    int[] result = new int[input.size()];

    for (int i = 0; i < input.size(); i++) {
      BayesianEvent event = this.events.get(i);
      int classIndex = event.matchChoiceToRange(input.getData(i));
      result[i] = classIndex;
    }

    return result;
  }
Exemplo n.º 7
0
  public static double calculateRegressionError(MLRegression method, MLDataSet data) {

    final ErrorCalculation errorCalculation = new ErrorCalculation();
    if (method instanceof MLContext) ((MLContext) method).clearContext();

    for (final MLDataPair pair : data) {
      final MLData actual = method.compute(pair.getInput());
      errorCalculation.updateError(
          actual.getData(), pair.getIdeal().getData(), pair.getSignificance());
    }
    return errorCalculation.calculate();
  }
  public double[] converteEntradaEmTabuleiro(MLData entrada, boolean mantemNormalizacao) {
    double[] entradas = entrada.getData();
    double[] tabuleiro = new double[9];

    for (int i = 0; i < 9; i++) {
      if (entradas[i] > 0) tabuleiro[i] = mantemNormalizacao ? entradas[i] : Caractere.X.getValor();
      else if (entradas[i + 9] < 0)
        tabuleiro[i] = mantemNormalizacao ? entradas[i + 9] : Caractere.O.getValor();
    }

    return tabuleiro;
  }
Exemplo n.º 9
0
  @Override
  public MLData compute(MLData input) {
    if (input.size() != this.inputCount) {
      throw new EncogError("Invalid input size, must be " + inputCount);
    }
    double[] sum = new double[1];

    sum[0] += this.weights[0];

    for (int i = 0; i < input.size(); i++) {
      sum[0] += this.weights[i + 1] * Math.pow(input.getData(i), i + 1);
    }

    return new BasicMLData(sum);
  }
Exemplo n.º 10
0
  public double computeProbability(MLData input) {

    // copy the input to evidence
    int inputIndex = 0;
    for (int i = 0; i < this.events.size(); i++) {
      BayesianEvent event = this.events.get(i);
      EventState state = this.query.getEventState(event);
      if (state.getEventType() == EventType.Evidence) {
        state.setValue((int) input.getData(inputIndex++));
      }
    }

    // execute the query
    this.query.execute();

    return this.query.getProbability();
  }
Exemplo n.º 11
0
  /** {@inheritDoc} */
  @Override
  public String denormalizeColumn(ColumnDefinition colDef, MLData data, int dataColumn) {

    double value = data.getData(dataColumn);
    final double result =
        ((colDef.getLow() - colDef.getHigh()) * value
                - this.normalizedHigh * colDef.getLow()
                + colDef.getHigh() * this.normalizedLow)
            / (this.normalizedLow - this.normalizedHigh);

    // typically caused by a number that should not have been normalized
    // (i.e. normalization or actual range is infinitely small.
    if (Double.isNaN(result)) {
      return "" + (((this.normalizedHigh - this.normalizedLow) / 2) + this.normalizedLow);
    }
    return "" + result;
  }
Exemplo n.º 12
0
  /**
   * The main method.
   *
   * @param args No arguments are used.
   */
  public static void main(final String args[]) {

    // create a neural network, without using a factory
    BasicNetwork network = new BasicNetwork();
    network.addLayer(new BasicLayer(null, true, 2));
    network.addLayer(new BasicLayer(new ActivationSigmoid(), true, 3));
    network.addLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
    network.getStructure().finalizeStructure();
    network.reset();

    // create training data
    MLDataSet trainingSet = new BasicMLDataSet(XOR_INPUT, XOR_IDEAL);

    // train the neural network
    final StochasticGradientDescent train = new StochasticGradientDescent(network, trainingSet);
    train.setUpdateRule(new RMSPropUpdate());

    int epoch = 1;

    do {
      train.iteration();
      System.out.println("Epoch #" + epoch + " Error:" + train.getError());
      epoch++;
    } while (train.getError() > 0.01);
    train.finishTraining();

    // test the neural network
    System.out.println("Neural Network Results:");
    for (MLDataPair pair : trainingSet) {
      final MLData output = network.compute(pair.getInput());
      System.out.println(
          pair.getInput().getData(0)
              + ","
              + pair.getInput().getData(1)
              + ", actual="
              + output.getData(0)
              + ",ideal="
              + pair.getIdeal().getData(0));
    }

    PerturbationFeatureImportanceCalc d;

    Encog.getInstance().shutdown();
  }
Exemplo n.º 13
0
  /** {@inheritDoc} */
  @Override
  public final void iteration() {

    if (this.mustInit) {
      initWeights();
    }

    double worstDistance = Double.NEGATIVE_INFINITY;

    for (final MLDataPair pair : this.training) {
      final MLData out = this.network.computeInstar(pair.getInput());

      // determine winner
      final int winner = EngineArray.indexOfLargest(out.getData());

      // calculate the distance
      double distance = 0;
      for (int i = 0; i < pair.getInput().size(); i++) {
        final double diff =
            pair.getInput().getData(i) - this.network.getWeightsInputToInstar().get(i, winner);
        distance += diff * diff;
      }
      distance = BoundMath.sqrt(distance);

      if (distance > worstDistance) {
        worstDistance = distance;
      }

      // train
      for (int j = 0; j < this.network.getInputCount(); j++) {
        final double delta =
            this.learningRate
                * (pair.getInput().getData(j)
                    - this.network.getWeightsInputToInstar().get(j, winner));

        this.network.getWeightsInputToInstar().add(j, winner, delta);
      }
    }

    setError(worstDistance);
  }
Exemplo n.º 14
0
  public MLMethod decode(final NEATPopulation pop, final Substrate substrate, final Genome genome) {
    // obtain the CPPN
    final NEATCODEC neatCodec = new NEATCODEC();
    final NEATNetwork cppn = (NEATNetwork) neatCodec.decode(genome);

    final List<NEATLink> linkList = new ArrayList<NEATLink>();

    final ActivationFunction[] afs = new ActivationFunction[substrate.getNodeCount()];

    final ActivationFunction af = new ActivationSteepenedSigmoid();
    // all activation functions are the same
    for (int i = 0; i < afs.length; i++) {
      afs[i] = af;
    }

    final double c = this.maxWeight / (1.0 - this.minWeight);
    final MLData input = new BasicMLData(cppn.getInputCount());

    // First create all of the non-bias links.
    for (final SubstrateLink link : substrate.getLinks()) {
      final SubstrateNode source = link.getSource();
      final SubstrateNode target = link.getTarget();

      int index = 0;
      for (final double d : source.getLocation()) {
        input.setData(index++, d);
      }
      for (final double d : target.getLocation()) {
        input.setData(index++, d);
      }
      final MLData output = cppn.compute(input);

      double weight = output.getData(0);
      if (Math.abs(weight) > this.minWeight) {
        weight = (Math.abs(weight) - this.minWeight) * c * Math.signum(weight);
        linkList.add(new NEATLink(source.getId(), target.getId(), weight));
      }
    }

    // now create biased links
    input.clear();
    final int d = substrate.getDimensions();
    final List<SubstrateNode> biasedNodes = substrate.getBiasedNodes();
    for (final SubstrateNode target : biasedNodes) {
      for (int i = 0; i < d; i++) {
        input.setData(d + i, target.getLocation()[i]);
      }

      final MLData output = cppn.compute(input);

      double biasWeight = output.getData(1);
      if (Math.abs(biasWeight) > this.minWeight) {
        biasWeight = (Math.abs(biasWeight) - this.minWeight) * c * Math.signum(biasWeight);
        linkList.add(new NEATLink(0, target.getId(), biasWeight));
      }
    }

    // check for invalid neural network
    if (linkList.size() == 0) {
      return null;
    }

    Collections.sort(linkList);

    final NEATNetwork network =
        new NEATNetwork(substrate.getInputCount(), substrate.getOutputCount(), linkList, afs);

    network.setActivationCycles(substrate.getActivationCycles());
    return network;
  }
 public int winner(MLData output) {
   return EngineArray.maxIndex(output.getData());
 }
Exemplo n.º 16
0
 /** {@inheritDoc} */
 @Override
 public MLData compute(final MLData input) {
   final MLData output = new BasicMLData(getOutputCount());
   this.flat.compute(input.getData(), output.getData());
   return output;
 }