|
| 1 | +package com.baeldung.algorithms.multiswarm; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Random; |
| 5 | + |
| 6 | +/** |
| 7 | + * Represents a collection of {@link Swarm}. |
| 8 | + * |
| 9 | + * @author Donato Rimenti |
| 10 | + * |
| 11 | + */ |
| 12 | +public class Multiswarm { |
| 13 | + |
| 14 | + /** |
| 15 | + * The swarms managed by this multiswarm. |
| 16 | + */ |
| 17 | + private Swarm[] swarms; |
| 18 | + |
| 19 | + /** |
| 20 | + * The best position found within all the {@link #swarms}. |
| 21 | + */ |
| 22 | + private long[] bestPosition; |
| 23 | + |
| 24 | + /** |
| 25 | + * The best fitness score found within all the {@link #swarms}. |
| 26 | + */ |
| 27 | + private double bestFitness = Double.NEGATIVE_INFINITY; |
| 28 | + |
| 29 | + /** |
| 30 | + * A random generator. |
| 31 | + */ |
| 32 | + private Random random = new Random(); |
| 33 | + |
| 34 | + /** |
| 35 | + * The fitness function used to determine how good is a particle. |
| 36 | + */ |
| 37 | + private FitnessFunction fitnessFunction; |
| 38 | + |
| 39 | + /** |
| 40 | + * Instantiates a new Multiswarm. |
| 41 | + * |
| 42 | + * @param numSwarms |
| 43 | + * the number of {@link #swarms} |
| 44 | + * @param particlesPerSwarm |
| 45 | + * the number of particle for each {@link #swarms} |
| 46 | + * @param fitnessFunction |
| 47 | + * the {@link #fitnessFunction} |
| 48 | + */ |
| 49 | + public Multiswarm(int numSwarms, int particlesPerSwarm, FitnessFunction fitnessFunction) { |
| 50 | + this.fitnessFunction = fitnessFunction; |
| 51 | + this.swarms = new Swarm[numSwarms]; |
| 52 | + for (int i = 0; i < numSwarms; i++) { |
| 53 | + swarms[i] = new Swarm(particlesPerSwarm); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Main loop of the algorithm. Iterates all particles of all |
| 59 | + * {@link #swarms}. For each particle, computes the new fitness and checks |
| 60 | + * if a new best position has been found among itself, the swarm and all the |
| 61 | + * swarms and finally updates the particle position and speed. |
| 62 | + */ |
| 63 | + public void mainLoop() { |
| 64 | + for (Swarm swarm : swarms) { |
| 65 | + for (Particle particle : swarm.getParticles()) { |
| 66 | + |
| 67 | + long[] particleOldPosition = particle.getPosition().clone(); |
| 68 | + |
| 69 | + // Calculate the particle fitness. |
| 70 | + particle.setFitness(fitnessFunction.getFitness(particleOldPosition)); |
| 71 | + |
| 72 | + // Check if a new best position has been found for the particle |
| 73 | + // itself, within the swarm and the multiswarm. |
| 74 | + if (particle.getFitness() > particle.getBestFitness()) { |
| 75 | + particle.setBestFitness(particle.getFitness()); |
| 76 | + particle.setBestPosition(particleOldPosition); |
| 77 | + |
| 78 | + if (particle.getFitness() > swarm.getBestFitness()) { |
| 79 | + swarm.setBestFitness(particle.getFitness()); |
| 80 | + swarm.setBestPosition(particleOldPosition); |
| 81 | + |
| 82 | + if (swarm.getBestFitness() > bestFitness) { |
| 83 | + bestFitness = swarm.getBestFitness(); |
| 84 | + bestPosition = swarm.getBestPosition().clone(); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Updates the particle position by adding the speed to the |
| 91 | + // actual position. |
| 92 | + long[] position = particle.getPosition(); |
| 93 | + long[] speed = particle.getSpeed(); |
| 94 | + |
| 95 | + position[0] += speed[0]; |
| 96 | + position[1] += speed[1]; |
| 97 | + |
| 98 | + // Updates the particle speed. |
| 99 | + speed[0] = getNewParticleSpeedForIndex(particle, swarm, 0); |
| 100 | + speed[1] = getNewParticleSpeedForIndex(particle, swarm, 1); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Computes a new speed for a given particle of a given swarm on a given |
| 107 | + * axis. The new speed is computed using the formula: |
| 108 | + * |
| 109 | + * <pre> |
| 110 | + * ({@link Constants#INERTIA_FACTOR} * {@link Particle#getSpeed()}) + |
| 111 | + * (({@link Constants#COGNITIVE_WEIGHT} * random(0,1)) * ({@link Particle#getBestPosition()} - {@link Particle#getPosition()})) + |
| 112 | + * (({@link Constants#SOCIAL_WEIGHT} * random(0,1)) * ({@link Swarm#getBestPosition()} - {@link Particle#getPosition()})) + |
| 113 | + * (({@link Constants#GLOBAL_WEIGHT} * random(0,1)) * ({@link #bestPosition} - {@link Particle#getPosition()})) |
| 114 | + * </pre> |
| 115 | + * |
| 116 | + * @param particle |
| 117 | + * the particle whose new speed needs to be computed |
| 118 | + * @param swarm |
| 119 | + * the swarm which contains the particle |
| 120 | + * @param index |
| 121 | + * the index of the particle axis whose speeds needs to be |
| 122 | + * computed |
| 123 | + * @return the new speed of the particle passed on the given axis |
| 124 | + */ |
| 125 | + private int getNewParticleSpeedForIndex(Particle particle, Swarm swarm, int index) { |
| 126 | + return (int) ((Constants.INERTIA_FACTOR * particle.getSpeed()[index]) |
| 127 | + + (randomizePercentage(Constants.COGNITIVE_WEIGHT) |
| 128 | + * (particle.getBestPosition()[index] - particle.getPosition()[index])) |
| 129 | + + (randomizePercentage(Constants.SOCIAL_WEIGHT) |
| 130 | + * (swarm.getBestPosition()[index] - particle.getPosition()[index])) |
| 131 | + + (randomizePercentage(Constants.GLOBAL_WEIGHT) |
| 132 | + * (bestPosition[index] - particle.getPosition()[index]))); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Returns a random number between 0 and the value passed as argument. |
| 137 | + * |
| 138 | + * @param value |
| 139 | + * the value to randomize |
| 140 | + * @return a random value between 0 and the one passed as argument |
| 141 | + */ |
| 142 | + private double randomizePercentage(double value) { |
| 143 | + return random.nextDouble() * value; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Gets the {@link #bestPosition}. |
| 148 | + * |
| 149 | + * @return the {@link #bestPosition} |
| 150 | + */ |
| 151 | + public long[] getBestPosition() { |
| 152 | + return bestPosition; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Gets the {@link #bestFitness}. |
| 157 | + * |
| 158 | + * @return the {@link #bestFitness} |
| 159 | + */ |
| 160 | + public double getBestFitness() { |
| 161 | + return bestFitness; |
| 162 | + } |
| 163 | + |
| 164 | + /* |
| 165 | + * (non-Javadoc) |
| 166 | + * |
| 167 | + * @see java.lang.Object#hashCode() |
| 168 | + */ |
| 169 | + @Override |
| 170 | + public int hashCode() { |
| 171 | + final int prime = 31; |
| 172 | + int result = 1; |
| 173 | + long temp; |
| 174 | + temp = Double.doubleToLongBits(bestFitness); |
| 175 | + result = prime * result + (int) (temp ^ (temp >>> 32)); |
| 176 | + result = prime * result + Arrays.hashCode(bestPosition); |
| 177 | + result = prime * result + ((fitnessFunction == null) ? 0 : fitnessFunction.hashCode()); |
| 178 | + result = prime * result + ((random == null) ? 0 : random.hashCode()); |
| 179 | + result = prime * result + Arrays.hashCode(swarms); |
| 180 | + return result; |
| 181 | + } |
| 182 | + |
| 183 | + /* |
| 184 | + * (non-Javadoc) |
| 185 | + * |
| 186 | + * @see java.lang.Object#equals(java.lang.Object) |
| 187 | + */ |
| 188 | + @Override |
| 189 | + public boolean equals(Object obj) { |
| 190 | + if (this == obj) |
| 191 | + return true; |
| 192 | + if (obj == null) |
| 193 | + return false; |
| 194 | + if (getClass() != obj.getClass()) |
| 195 | + return false; |
| 196 | + Multiswarm other = (Multiswarm) obj; |
| 197 | + if (Double.doubleToLongBits(bestFitness) != Double.doubleToLongBits(other.bestFitness)) |
| 198 | + return false; |
| 199 | + if (!Arrays.equals(bestPosition, other.bestPosition)) |
| 200 | + return false; |
| 201 | + if (fitnessFunction == null) { |
| 202 | + if (other.fitnessFunction != null) |
| 203 | + return false; |
| 204 | + } else if (!fitnessFunction.equals(other.fitnessFunction)) |
| 205 | + return false; |
| 206 | + if (random == null) { |
| 207 | + if (other.random != null) |
| 208 | + return false; |
| 209 | + } else if (!random.equals(other.random)) |
| 210 | + return false; |
| 211 | + if (!Arrays.equals(swarms, other.swarms)) |
| 212 | + return false; |
| 213 | + return true; |
| 214 | + } |
| 215 | + |
| 216 | + /* |
| 217 | + * (non-Javadoc) |
| 218 | + * |
| 219 | + * @see java.lang.Object#toString() |
| 220 | + */ |
| 221 | + @Override |
| 222 | + public String toString() { |
| 223 | + return "Multiswarm [swarms=" + Arrays.toString(swarms) + ", bestPosition=" + Arrays.toString(bestPosition) |
| 224 | + + ", bestFitness=" + bestFitness + ", random=" + random + ", fitnessFunction=" + fitnessFunction + "]"; |
| 225 | + } |
| 226 | + |
| 227 | +} |
0 commit comments