Пример #1
0
  /**
   * Calculates the fitness of the given solution by setting the neural network weights to the
   * solution and evaluating the training set in order to calculate the MSE (which is minimized).
   *
   * @param solution the weights representing a solution.
   * @return a new MinimisationFitness wrapping the MSE training error.
   */
  @Override
  protected Fitness calculateFitness(Type solution) {
    if (trainingSet == null) {
      this.initialise();
    }

    int currentIteration = AbstractAlgorithm.get().getIterations();
    if (currentIteration != previousShuffleIteration) {
      try {
        shuffler.operate(trainingSet);
      } catch (CIlibIOException exception) {
        exception.printStackTrace();
      }
    }

    neuralNetwork.getArchitecture().accept(solutionConversionStrategy.interpretSolution(solution));

    double errorTraining = 0.0;
    OutputErrorVisitor visitor = new OutputErrorVisitor();
    Vector error = null;
    for (StandardPattern pattern : trainingSet) {
      Vector output = neuralNetwork.evaluatePattern(pattern);
      visitor.setInput(pattern);
      neuralNetwork.getArchitecture().accept(visitor);
      error = visitor.getOutput();
      for (Numeric real : error) {
        errorTraining += real.doubleValue() * real.doubleValue();
      }
    }
    errorTraining /= trainingSet.getNumRows() * error.size();

    return objective.evaluate(errorTraining);
  }
  /**
   * @param x
   * @return
   */
  private int length(Vector x) {
    int count = 0;

    for (Numeric n : x) {
      if (n.booleanValue()) {
        count++;
      }
    }

    return count;
  }
  @Override
  public void performIteration(PSO algorithm) {
    delegate.performIteration(algorithm);
    Topology<Particle> topology = algorithm.getTopology();

    // calculate vAvg
    Vector avgV =
        Vectors.mean(
            Lists.transform(
                topology,
                new Function<Particle, Vector>() {
                  @Override
                  public Vector apply(Particle f) {
                    return (Vector) f.getVelocity();
                  }
                }));

    Vector.Builder builder = Vector.newBuilder();
    for (Numeric n : avgV) {
      if (Math.abs(n.doubleValue()) > vMax.getParameter()) {
        builder.add(vMax.getParameter());
      } else {
        builder.add(n);
      }
    }

    avgV = builder.build();

    // mutation
    Particle gBest = Topologies.getBestEntity(topology, new SocialBestFitnessComparator());
    Particle mutated = gBest.getClone();
    Vector pos = (Vector) gBest.getBestPosition();
    final Bounds bounds = pos.boundsOf(0);

    pos =
        pos.plus(
            avgV.multiply(
                new Supplier<Number>() {
                  @Override
                  public Number get() {
                    return distribution.getRandomNumber() * bounds.getRange()
                        + bounds.getLowerBound();
                  }
                }));

    mutated.setCandidateSolution(pos);
    mutated.calculateFitness();

    if (gBest.getBestFitness().compareTo(mutated.getFitness()) < 0) {
      gBest.getProperties().put(EntityType.Particle.BEST_FITNESS, mutated.getBestFitness());
      gBest.getProperties().put(EntityType.Particle.BEST_POSITION, mutated.getBestPosition());
    }
  }
  private String getSubSequence(Vector x, String target) {
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < x.size(); i++) {
      Numeric n = x.get(i);
      if (n.booleanValue()) {
        builder.append(target.charAt(i));
      }
    }

    return builder.toString();
  }
  /** Test velocity clamping. */
  @Test
  public void testClamping() {
    Particle particle = createParticle(Vector.of(0.0));
    Particle nBest = createParticle(Vector.of(1.0));
    particle.setNeighbourhoodBest(nBest);
    nBest.setNeighbourhoodBest(nBest);

    ClampingVelocityProvider velocityProvider = new ClampingVelocityProvider();
    velocityProvider.setVMax(new ConstantControlParameter(0.5));
    Vector velocity = velocityProvider.get(particle);

    for (Numeric number : velocity) {
      Assert.assertTrue(Double.compare(number.doubleValue(), 0.5) <= 0);
      Assert.assertTrue(Double.compare(number.doubleValue(), -0.5) >= 0);
    }
  }
  private void updateInertia(PSO pso) {

    int dimension = pso.getTopology().last().getDimension();

    if (inertiaWeight.size() < dimension) {
      Vector.Builder builder = Vector.newBuilder();
      builder.repeat(dimension, Real.valueOf(initialInertiaWeight.getParameter()));
      inertiaWeight = builder.build();
    }
    Vector.Builder builder = Vector.newBuilder();
    for (int i = 0; i < dimension; i++) {
      builder.add(
          Math.sqrt(
              Math.pow(absoluteAverageVelocityVector.doubleValueOf(i), 2)
                  + Math.pow(averageSpeedVector.doubleValueOf(i), 2)));
    }
    Vector d = builder.build(); // get the degree of convergence vector
    double max_d = 0;
    for (Numeric component : d) {
      if (component.doubleValue() > max_d) {
        max_d = component.doubleValue();
      }
    }
    if (max_d != 0) {
      Vector.Builder builder2 = Vector.newBuilder();
      for (Numeric component : d) {
        builder2.add(max_d / (max_d + component.doubleValue()));
      }
      Vector w = builder2.build();

      /*double sum_w = 0;
      for(Numeric component : w) {
          sum_w += component.doubleValue();
      }

      /*
      Vector.Builder builder3 = Vector.newBuilder();
      for(Numeric component : w) {
          builder3.add(Math.pow(dimension * component.doubleValue() / sum_w, pwr.getParameter()));
      } */

      /*
      for(Numeric component : w) {
          //builder3.add(component.doubleValue() - w_mean / w_stdDiv);
          builder3.add(component.doubleValue() * initialInertiaWeight.getParameter());
      }
      for(int i = 0; i < inertiaWeight.size(); i++) {
          builder3.add(w.doubleValueOf(i) * inertiaWeight.doubleValueOf(i));
      }
      */
      /*
      Vector m = builder3.build();
      double sum_m = 0;
      for (Numeric num : m) {
          sum_m += num.doubleValue();
      }

      double m_mean = sum_m / (double) dimension;
      double sum_diff_squared = 0;
      for(Numeric component : m) {
          sum_diff_squared += Math.pow(component.doubleValue() - m_mean, 2);
      }
      double m_stdDiv = Math.sqrt(sum_diff_squared / (double) dimension);
      */
      // System.out.println("VEL: StdDiv of M: " + m_stdDiv + ", mean of M: " + m_mean);

      for (int i = 0; i < inertiaWeight.size(); i++) {
        inertiaWeight.setReal(
            i,
            (1 - filter.getParameter()) * w.doubleValueOf(i)
                + filter.getParameter()
                    * inertiaWeight.doubleValueOf(i)); // w.doubleValueOf(i));//;
      }
    }
  }