private void calculateAbsoluteAverages(PSO pso) {

    int dimension = pso.getTopology().last().getDimension();
    absoluteAverageVelocityVector = Vector.of();
    averageSpeedVector = Vector.of();

    for (Entity e : pso.getTopology()) {
      Vector velocity = (Vector) e.getProperties().get(EntityType.Particle.VELOCITY);
      for (int i = 0; i < dimension; i++) {
        if (absoluteAverageVelocityVector.size() < dimension) {
          absoluteAverageVelocityVector.add(velocity.get(i));
          averageSpeedVector.add(Real.valueOf(Math.abs(velocity.doubleValueOf(i))));
        } else {
          absoluteAverageVelocityVector.setReal(
              i, absoluteAverageVelocityVector.doubleValueOf(i) + velocity.doubleValueOf(i));
          averageSpeedVector.setReal(
              i, averageSpeedVector.doubleValueOf(i) + Math.abs(velocity.doubleValueOf(i)));
        }
      }
    }

    for (int i = 0; i < dimension; i++) {
      absoluteAverageVelocityVector.setReal(
          i, Math.abs(absoluteAverageVelocityVector.doubleValueOf(i) / (double) dimension));
      averageSpeedVector.setReal(i, averageSpeedVector.doubleValueOf(i) / (double) dimension);
    }
  }
Exemplo n.º 2
0
  /** Test for Vincent Function */
  @Test
  public void testEvaluate() {
    Vector x = Vector.of(5, 5);

    assertEquals(-0.246258053, function.apply(x), 0.000000009);

    x.setReal(0, 10.0);
    x.setReal(1, 10.0);
    assertEquals(0.719420726, function.apply(x), 0.000000009);

    x.setReal(0, 8.0);
    x.setReal(1, 8.0);
    assertEquals(-2.861700698, function.apply(x), 0.000000009);
  }
  /**
   * Calculate the {@linkplain Vector} that is the resultant of several difference vectors.
   *
   * @param participants The {@linkplain Entity} list to create the difference vectors from. It is
   *     very important to note that the {@linkplain Entity} objects within this list should not
   *     contain duplicates. If duplicates are contained, this will severely reduce the diversity of
   *     the population as not all entities will be considered.
   * @return A {@linkplain Vector} representing the resultant of all calculated difference vectors.
   */
  protected Vector determineDistanceVector(List<Entity> participants) {
    RandomProvider random = new MersenneTwister();
    Vector distanceVector = Vector.fill(0.0, participants.get(0).getCandidateSolution().size());
    Iterator<Entity> iterator;
    int number = Double.valueOf(this.numberOfDifferenceVectors.getParameter()).intValue();
    List<Entity> currentParticipants;

    Vector first, second;
    double difference;

    for (int d = 0; d < distanceVector.size(); d++) {
      // get random participants for this dimension
      currentParticipants =
          (List<Entity>)
              Selection.copyOf(participants)
                  .orderBy(new RandomArrangement(random))
                  .select(Samples.first(number));
      iterator = currentParticipants.iterator();

      while (iterator.hasNext()) {
        first = (Vector) iterator.next().getCandidateSolution();
        second = (Vector) iterator.next().getCandidateSolution();

        difference = first.doubleValueOf(d) - second.doubleValueOf(d);

        distanceVector.setReal(d, distanceVector.get(d).doubleValue() + difference);
      }
    }

    return distanceVector;
  }
Exemplo n.º 4
0
  /** {@inheritDoc} */
  public Real getValue(Algorithm algorithm) {
    PSO pso = (PSO) algorithm;

    int numberParticles = pso.getTopology().size();

    Iterator<Particle> k = pso.getTopology().iterator();
    Particle particle = k.next();
    Vector averageParticlePosition = (Vector) particle.getPosition().getClone();
    while (k.hasNext()) {
      particle = k.next();
      Vector v = (Vector) particle.getPosition();
      for (int j = 0; j < averageParticlePosition.size(); ++j) {
        averageParticlePosition.setReal(
            j, averageParticlePosition.doubleValueOf(j) + v.doubleValueOf(j));
      }
    }
    for (int j = 0; j < averageParticlePosition.size(); ++j) {
      averageParticlePosition.setReal(
          j, averageParticlePosition.doubleValueOf(j) / numberParticles);
    }

    Iterator<Particle> i = pso.getTopology().iterator();
    double particleSum = 0.0;
    while (i.hasNext()) {
      particle = i.next();

      double dimensionSum = 0.0;
      Vector v = (Vector) particle.getPosition();
      for (int j = 0; j < particle.getDimension(); ++j) {
        dimensionSum +=
            (v.doubleValueOf(j) - averageParticlePosition.doubleValueOf(j))
                * (v.doubleValueOf(j) - averageParticlePosition.doubleValueOf(j));
      }
      particleSum += Math.sqrt(dimensionSum);
    }

    double diversity = particleSum / numberParticles;

    DiameterVisitor diameterVisitor = new DiameterVisitor();
    pso.accept(diameterVisitor);
    double diameter = diameterVisitor.getResult();

    return Real.valueOf(diversity / diameter);
  }
Exemplo n.º 5
0
  /** {@inheritDoc} */
  @Override
  public Double apply(Vector input) {
    Vector tmp = input.getClone();

    for (int i = 0; i < input.size(); i++) {
      tmp.setReal(i, (horizontalScale * input.doubleValueOf(i)));
    }

    return (verticalScale * function.apply(tmp));
  }
Exemplo n.º 6
0
  /** Test of evaluate method, of class {@link Neumaier3}. */
  @Test
  public void testApply() {
    Vector x = Vector.of(0.0, 2.0);

    assertEquals(2.0, function.f(x), Maths.EPSILON);

    x.setReal(0, 2.0);
    x.setReal(1, 2.0);
    assertEquals(-2.0, function.f(x), Maths.EPSILON);
  }
Exemplo n.º 7
0
Arquivo: HS.java Projeto: vervas/cilib
  /** {@inheritDoc} */
  @Override
  public void algorithmIteration() {
    // TO-DO: Make sure that all fitnesses are evaluated initially, and
    // that FE is incremented only once per iteration

    // calculate a new harmony
    Harmony newHarmony = new Harmony();
    newHarmony.initialise(getOptimisationProblem());
    Vector newHarmonyVector = (Vector) newHarmony.getCandidateSolution();

    OptimisationProblem problem = getOptimisationProblem();
    //        Real newHarmonyValue;
    for (int i = 0; i < problem.getDomain().getDimension(); ++i) {
      if (uniform1.getRandomNumber() < harmonyMemoryConsideringRate.getParameter()) {
        Harmony selectedHarmony =
            this.harmonyMemory.get((int) uniform2.getRandomNumber(0, harmonyMemory.size() - 1));
        Vector selectedHarmonyContents = (Vector) selectedHarmony.getCandidateSolution();
        double newHarmonyValue = selectedHarmonyContents.doubleValueOf(i);
        Bounds bounds = selectedHarmonyContents.boundsOf(i);
        if (uniform1.getRandomNumber() < pitchAdjustingRate.getParameter()) {
          double pitchedValue =
              newHarmonyValue + uniform3.getRandomNumber(-1, 1) * distanceBandwidth.getParameter();
          if ((pitchedValue > bounds.getLowerBound()) && (pitchedValue < bounds.getUpperBound())) {
            newHarmonyValue = pitchedValue;
          }
        }

        newHarmonyVector.setReal(i, newHarmonyValue);
      } else {
        double upper =
            ((Vector) problem.getDomain().getBuiltRepresenation()).boundsOf(i).getUpperBound();
        double lower =
            ((Vector) problem.getDomain().getBuiltRepresenation()).boundsOf(i).getLowerBound();
        newHarmonyVector.setReal(i, uniform3.getRandomNumber(lower, upper));
      }
    }

    newHarmony.calculateFitness();
    harmonyMemory.add(newHarmony);
    harmonyMemory.remove(
        harmonyMemory.get(0) /*getFirst()*/); // Remove the worst harmony in the memory
  }
  @Override
  public void initialize(Enum<?> key, E entity) {
    Type type = entity.getProperties().get(key);
    Vector velocity = (Vector) type;

    for (int i = 0; i < velocity.size(); i++) {
      changeBoundsForNextDimension(i);
      velocity.setReal(
          i, random.getRandomNumber(lowerBound.getParameter(), upperBound.getParameter()));
    }
  }
  /** {@inheritDoc} */
  @Override
  public Double apply(Vector input) {
    Vector tmp = input.getClone();

    if (horizontalReflection) {
      for (int i = 0; i < input.size(); i++) {
        tmp.setReal(i, -input.doubleValueOf(i));
      }
    }

    if (verticalReflection) {
      return -function.apply(tmp);
    }

    return function.apply(tmp);
  }
  /**
   * Reinitialises all the output weights of a cascade network within a PSO.
   *
   * <p>{@inheritDoc}
   */
  @Override
  public void performReaction(E algorithm) {
    NNDataTrainingProblem problem = (NNDataTrainingProblem) algorithm.getOptimisationProblem();
    NeuralNetwork network = problem.getNeuralNetwork();

    int precedingLayersSize =
        network.getArchitecture().getArchitectureBuilder().getLayerConfigurations().get(0).getSize()
            + network
                .getArchitecture()
                .getArchitectureBuilder()
                .getLayerConfigurations()
                .get(1)
                .getSize();
    if (network.getArchitecture().getArchitectureBuilder().getLayerConfigurations().get(0).isBias())
      precedingLayersSize++;

    int outputLayerSize =
        network
            .getArchitecture()
            .getArchitectureBuilder()
            .getLayerConfigurations()
            .get(2)
            .getSize();
    int nrOfweightsToDo = precedingLayersSize * outputLayerSize;

    Topology<? extends Entity> entities = algorithm.getTopology();

    for (Entity entity : entities) {
      DynamicParticle particle = (DynamicParticle) entity;

      Vector position = (Vector) particle.getPosition();
      Vector velocity = (Vector) particle.getVelocity();
      for (int curElement = position.size() - nrOfweightsToDo;
          curElement < position.size();
          ++curElement) {
        ((Real) position.get(curElement)).randomize(randomGenerator);
        velocity.setReal(curElement, 0.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));//;
      }
    }
  }