/** Evaluates the function for a specific iteration. */ public double apply(int iteration, Vector x) { Vector y = x; if (x.size() > 1) { y = x.copyOfRange(this.M - 1, this.n); } double g = ((HEF6_g) this.hef6_g).apply(iteration, y); double value = 1.0 + g; double val1 = Math.cos(0.5 * Math.abs(x.doubleValueOf(0)) * Math.PI) * Math.sin(0.5 * Math.abs(x.doubleValueOf(1)) * Math.PI); double sum = 0.0; int index = 0; for (int i = 2; i < x.size(); i++) { if ((i - 1) % 3 == 0) { sum += Math.pow( Math.abs(x.doubleValueOf(i)) - 2.0 * Math.abs(x.doubleValueOf(1)) * Math.sin( 2.0 * Math.PI * Math.abs(x.doubleValueOf(0)) + (i * Math.PI / x.size())), 2); index++; } } sum *= 2.0 / index; sum += val1; sum *= value; return sum; }
/** {@inheritDoc} */ @Override public Double apply(Vector input) { double tmp = 0; for (int i = 0; i < input.size(); i++) { for (int k = 0; k <= kMax; k++) { tmp += Math.pow(a, k) * Math.cos(2 * Math.PI * Math.pow(b, k) * (input.doubleValueOf(i) + 0.5)); } } return tmp - input.size() * constant; }
@Override public Double apply(Vector input) { int nDims = input.size(); // Get the raw weights double wMax = Double.NEGATIVE_INFINITY; double wSum = 0.0; for (SingleFunction f : functions) { f.shift(input); double sumSqr = Math.pow(f.getShifted().norm(), 2); f.setWeight(Math.exp(-1.0 * sumSqr / (2.0 * nDims * f.getSigma() * f.getSigma()))); if (wMax < f.getWeight()) wMax = f.getWeight(); wSum += f.getWeight(); } // Modify the weights double w1mMaxPow = 1.0 - Math.pow(wMax, 10.0); for (SingleFunction f : functions) { if (f.getWeight() != wMax) { f.setWeight(f.getWeight() * w1mMaxPow); } f.setWeight(f.getWeight() / wSum); } double sumF = 0.0; for (SingleFunction f : functions) { sumF += f.getWeight() * (scaleConstant * f.apply(input) + f.getBias()); } return sumF; }
/** * 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; }
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); } }
/** * 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); }
public static Vector calculate_x(Vector t_p, Vector A) { Preconditions.checkArgument(Misc.vector_in_01(t_p)); Preconditions.checkArgument(!t_p.isEmpty()); Preconditions.checkArgument(A.size() == t_p.size() - 1); Vector.Builder result = Vector.newBuilder(); for (int i = 0; i < t_p.size() - 1; i++) { Preconditions.checkArgument(A.doubleValueOf(i) == 0 || A.doubleValueOf(i) == 1); double tmp1 = Math.max(t_p.doubleValueOf(t_p.size() - 1), A.doubleValueOf(i)); result.add(tmp1 * (t_p.doubleValueOf(i) - 0.5) + 0.5); } result.add(t_p.doubleValueOf(t_p.size() - 1)); return result.build(); }
public VectorReader(Vector vector, int columnCount) { Preconditions.checkArgument(vector != null); Preconditions.checkArgument(columnCount > 0); Preconditions.checkArgument( vector.size() % columnCount == 0, "Vector cannot be split up into rows."); this.columnCount = columnCount; this.vector = vector; this.index = 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); }
public static Vector normalise_z(Vector z, Vector z_max) { Vector.Builder result = Vector.newBuilder(); for (int i = 0; i < z.size(); i++) { Preconditions.checkArgument(z.doubleValueOf(i) >= 0.0); Preconditions.checkArgument(z.doubleValueOf(i) <= z_max.doubleValueOf(i)); Preconditions.checkArgument(z_max.doubleValueOf(i) > 0.0); result.add(z.doubleValueOf(i) / z_max.doubleValueOf(i)); } return result.build(); }
/** {@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)); }
@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) { double sum = 0; for (int i = 0; i < input.size(); i++) { double x = input.doubleValueOf(i); if (x > 500) { sum += (500 - (x % 500)) * Math.sin(Math.sqrt(Math.abs(500 - (x % 500)))) - (Math.pow(x - 500, 2) / 10000 * input.size()); } else if (x < -500) { sum += ((Math.abs(x) % 500) - 500) * Math.sin(Math.sqrt(Math.abs((Math.abs(x) % 500) - 500))) - (Math.pow(x + 500, 2) / 10000 * input.size()); } else { sum += x * Math.sin(Math.pow(Math.abs(x), 0.5)); } } return 418.9829 * input.size() - sum; }
/* (non-Javadoc) * @see net.sourceforge.cilib.functions.redux.ContinuousFunction#evaluate(net.sourceforge.cilib.type.types.container.Vector) */ @Override public Double apply(Vector input) { final int size = input.size(); double sumsq = 0.0; double sumcos = 0.0; for (int i = 0; i < size; ++i) { sumsq += input.doubleValueOf(i) * input.doubleValueOf(i); sumcos += Math.cos(2 * Math.PI * input.doubleValueOf(i)); } return -20.0 * Math.exp(-0.2 * Math.sqrt(sumsq / size)) - Math.exp(sumcos / size) + 20 + Math.E; }
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(); }
/** {@inheritDoc} */ @Override public Double apply(Vector input) { double dResult = 0.0; for (int i = 0; i < input.size(); i++) { double x = Math.pow(Math.sin(5.0 * Math.PI * input.doubleValueOf(i)), 6.0); double exp1 = -2.0 * Math.log(2); double exp2 = Math.pow((input.doubleValueOf(i) - 0.1) / 0.8, 2.0); double y = Math.exp(exp1 * exp2); dResult += x * y; } return dResult; }
/** Evaluates the function for a specific iteration. */ public Double apply(int iteration, Vector x) { double t = (1.0 / (double) n_t) * Math.floor((double) iteration / (double) this.tau_t); double G = Math.abs(Math.sin(0.5 * Math.PI * t)); double value = 1.0; value += G; for (int k = 0; k < x.size(); k++) { value += Math.pow((x.doubleValueOf(k) - G), 2); } return value; }
/** * 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); } } }
/** {@inheritDoc} */ @Override public Double f(Vector input) { double sum = 0.0; for (int i = 0; i < input.size(); i++) { double x = Math.pow(Math.sin(5.0 * Math.PI * (Math.pow(input.doubleValueOf(i), 0.75) - 0.05)), 6.0); double exp1 = -2.0 * Math.log(2); double exp2 = Math.pow((input.doubleValueOf(i) - 0.08) / 0.854, 2.0); double y = Math.exp(exp1 * exp2); sum += x * y; } return sum; }
/** {@inheritDoc} */ @Override public Double apply(Vector input) { double result = 0.0; for (int i = 0; i < input.size(); i++) { double square = input.doubleValueOf(i) * input.doubleValueOf(i); double square2 = square * square; result += (i + 1) * square2; } return result; }
/** * Calculates the AUC measurment. * * @param algorithm The optimisation algorithm with a NNTrainingProblem. * @return A Vector with the AUC for each NN output. */ @Override public Vector getValue(Algorithm algorithm) { Vector solution = (Vector) algorithm.getBestSolution().getPosition(); NNTrainingProblem problem = (NNTrainingProblem) algorithm.getOptimisationProblem(); StandardPatternDataTable generalisationSet = problem.getGeneralisationSet(); NeuralNetwork neuralNetwork = problem.getNeuralNetwork(); neuralNetwork.setWeights(solution); // Arrange outputs and target values into ArrayLists. ArrayList<ArrayList<Real>> targets = new ArrayList<ArrayList<Real>>(); ArrayList<ArrayList<Real>> outputs = new ArrayList<ArrayList<Real>>(); // case of multiple outputs if (generalisationSet.getRow(0).getTarget() instanceof Vector) { int size = ((Vector) generalisationSet.getRow(0).getTarget()).size(); for (int i = 0; i < size; ++i) { targets.add(new ArrayList<Real>()); outputs.add(new ArrayList<Real>()); } for (StandardPattern pattern : generalisationSet) { Vector target = (Vector) pattern.getTarget(); Vector output = neuralNetwork.evaluatePattern(pattern); for (int curOutput = 0; curOutput < target.size(); ++curOutput) { targets.get(curOutput).add((Real) target.get(curOutput)); outputs.get(curOutput).add((Real) output.get(curOutput)); } } } // case of single output else { targets.add(new ArrayList<Real>()); outputs.add(new ArrayList<Real>()); for (StandardPattern pattern : generalisationSet) { Real target = (Real) pattern.getTarget(); Vector output = neuralNetwork.evaluatePattern(pattern); targets.get(0).add(target); outputs.get(0).add((Real) output.get(0)); } } // Calculate the Vector of AUC values Vector results = Vector.of(); for (int curOutput = 0; curOutput < outputs.size(); ++curOutput) { results.add(Real.valueOf(areaUnderCurve(targets.get(curOutput), outputs.get(curOutput)))); } return results; }
@Override public <E extends Entity> List<E> crossover(List<E> parentCollection) { Preconditions.checkArgument( parentCollection.size() == 2, "BlendCrossoverStrategy requires 2 parents."); // How do we handle variable sizes? Resizing the entities? E offspring1 = (E) parentCollection.get(0).getClone(); E offspring2 = (E) parentCollection.get(1).getClone(); Vector parentChromosome1 = (Vector) parentCollection.get(0).getCandidateSolution(); Vector parentChromosome2 = (Vector) parentCollection.get(1).getCandidateSolution(); Vector.Builder offspringChromosome1 = Vector.newBuilder(); Vector.Builder offspringChromosome2 = Vector.newBuilder(); int sizeParent1 = parentChromosome1.size(); int sizeParent2 = parentChromosome2.size(); int minDimension = Math.min(sizeParent1, sizeParent2); for (int i = 0; i < minDimension; i++) { double gamma = (1 + 2 * alpha.getParameter()) * random.getRandomNumber() - alpha.getParameter(); double value1 = (1 - gamma) * parentChromosome1.doubleValueOf(i) + gamma * parentChromosome2.doubleValueOf(i); double value2 = (1 - gamma) * parentChromosome2.doubleValueOf(i) + gamma * parentChromosome1.doubleValueOf(i); offspringChromosome1.add(Real.valueOf(value1, parentChromosome1.boundsOf(i))); offspringChromosome2.add(Real.valueOf(value2, parentChromosome1.boundsOf(i))); } offspring1.setCandidateSolution(offspringChromosome1.build()); offspring2.setCandidateSolution(offspringChromosome2.build()); return Arrays.asList(offspring1, offspring2); }
/** {@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); }
@Override public Vector get(Particle particle) { Vector newPos = (Vector) delegate.get(particle); Particle tmp = particle.getClone(); tmp.setPosition(newPos); Fitness newFitness = particle.getBehaviour().getFitnessCalculator().getFitness(tmp); final UniformDistribution uniform = new UniformDistribution(); Vector newPBest = newPos.plus( Vector.newBuilder() .repeat(newPos.size(), Real.valueOf(1.0)) .build() .multiply( new P1<Number>() { @Override public Number _1() { return uniform.getRandomNumber( -granularity.getParameter(), granularity.getParameter()); } })); tmp.setPosition(newPos); Fitness newPBestFitness = particle.getBehaviour().getFitnessCalculator().getFitness(tmp); if (newPBestFitness.compareTo(newFitness) < 0) { Vector tmpVector = Vector.copyOf(newPos); newPos = newPBest; newPBest = tmpVector; newPBestFitness = newFitness; } double dot = ((Vector) particle.getNeighbourhoodBest().getBestPosition()) .subtract(newPos) .dot(newPBest.subtract(newPos)); if (dot < 0) { return (Vector) particle.getPosition(); } particle.put(Property.BEST_POSITION, newPBest); particle.put(Property.BEST_FITNESS, newPBestFitness); return newPos; }
public static Vector calculate_f(double D, Vector x, Vector h, Vector S) { Preconditions.checkArgument(D > 0.0); Preconditions.checkArgument(Misc.vector_in_01(x)); Preconditions.checkArgument(Misc.vector_in_01(h)); Preconditions.checkArgument(x.size() == h.size()); Preconditions.checkArgument(h.size() == S.size()); Vector.Builder result = Vector.newBuilder(); for (int i = 0; i < h.size(); i++) { Preconditions.checkArgument(S.doubleValueOf(i) > 0.0); result.add(D * x.doubleValueOf(x.size() - 1) + S.doubleValueOf(i) * h.doubleValueOf(i)); } return result.build(); }
@Test public void initialize() { final Bounds bounds = new Bounds(-8.0, 8.0); Vector vector = Vector.newBuilder() .addWithin(0.0, bounds) .addWithin(0.0, bounds) .addWithin(0.0, bounds) .build(); Particle particle = new StandardParticle(); particle.getProperties().put(EntityType.Particle.VELOCITY, vector); DomainPercentageInitializationStrategy<Particle> strategy = new DomainPercentageInitializationStrategy<Particle>(); strategy.setVelocityInitialisationStrategy(new ConstantInitializationStrategy(1.0)); strategy.initialize(EntityType.Particle.VELOCITY, particle); Vector velocity = (Vector) particle.getVelocity(); for (int i = 0; i < velocity.size(); i++) { Assert.assertThat(velocity.doubleValueOf(i), is(lessThanOrEqualTo(0.1))); } }
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));//; } } }