Skip to content

Commit d338d06

Browse files
author
alv21
committed
Merge branch 'master' of https://github.com/eugenp/tutorials
2 parents ae47827 + 00f5648 commit d338d06

394 files changed

Lines changed: 12520 additions & 645 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties
4343
spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
4444
*.springBeans
4545

46+
20171220-JMeter.csv
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.algorithms.multiswarm;
2+
3+
/**
4+
* Constants used by the Multi-swarm optimization algorithms.
5+
*
6+
* @author Donato Rimenti
7+
*
8+
*/
9+
public class Constants {
10+
11+
/**
12+
* The inertia factor encourages a particle to continue moving in its
13+
* current direction.
14+
*/
15+
public static final double INERTIA_FACTOR = 0.729;
16+
17+
/**
18+
* The cognitive weight encourages a particle to move toward its historical
19+
* best-known position.
20+
*/
21+
public static final double COGNITIVE_WEIGHT = 1.49445;
22+
23+
/**
24+
* The social weight encourages a particle to move toward the best-known
25+
* position found by any of the particle’s swarm-mates.
26+
*/
27+
public static final double SOCIAL_WEIGHT = 1.49445;
28+
29+
/**
30+
* The global weight encourages a particle to move toward the best-known
31+
* position found by any particle in any swarm.
32+
*/
33+
public static final double GLOBAL_WEIGHT = 0.3645;
34+
35+
/**
36+
* Upper bound for the random generation. We use it to reduce the
37+
* computation time since we can rawly estimate it.
38+
*/
39+
public static final int PARTICLE_UPPER_BOUND = 10000000;
40+
41+
/**
42+
* Private constructor for utility class.
43+
*/
44+
private Constants() {
45+
}
46+
47+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.algorithms.multiswarm;
2+
3+
/**
4+
* Interface for a fitness function, used to decouple the main algorithm logic
5+
* from the specific problem solution.
6+
*
7+
* @author Donato Rimenti
8+
*
9+
*/
10+
public interface FitnessFunction {
11+
12+
/**
13+
* Returns the fitness of a particle given its position.
14+
*
15+
* @param particlePosition
16+
* the position of the particle
17+
* @return the fitness of the particle
18+
*/
19+
public double getFitness(long[] particlePosition);
20+
21+
}
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
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

Comments
 (0)