Skip to content

Commit 67599b1

Browse files
committed
- Extracted fitness function to separate class instead of Lambda
- Cleaned up javadoc - Refactored fitness function code to add more significative variables names
1 parent b0d83a8 commit 67599b1

3 files changed

Lines changed: 62 additions & 35 deletions

File tree

algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Swarm.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.Arrays;
44
import java.util.Random;
55

6-
// TODO: Auto-generated Javadoc
76
/**
87
* Represents a collection of {@link Particle}.
98
*
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.algorithms.multiswarm;
2+
3+
/**
4+
* Specific fitness function implementation to solve the League of Legends
5+
* problem. This is the problem statement: <br>
6+
* <br>
7+
* In League of Legends, a player's Effective Health when defending against
8+
* physical damage is given by E=H(100+A)/100, where H is health and A is armor.
9+
* Health costs 2.5 gold per unit, and Armor costs 18 gold per unit. You have
10+
* 3600 gold, and you need to optimize the effectiveness E of your health and
11+
* armor to survive as long as possible against the enemy team's attacks. How
12+
* much of each should you buy? <br>
13+
* <br>
14+
*
15+
* @author Donato Rimenti
16+
*
17+
*/
18+
public class LolFitnessFunction implements FitnessFunction {
19+
20+
/*
21+
* (non-Javadoc)
22+
*
23+
* @see
24+
* com.baeldung.algorithms.multiswarm.FitnessFunction#getFitness(long[])
25+
*/
26+
@Override
27+
public double getFitness(long[] particlePosition) {
28+
29+
long health = particlePosition[0];
30+
long armor = particlePosition[1];
31+
32+
// No negatives values accepted.
33+
if (health < 0 && armor < 0) {
34+
return -(health * armor);
35+
} else if (health < 0) {
36+
return health;
37+
} else if (armor < 0) {
38+
return armor;
39+
}
40+
41+
// Checks if the solution is actually feasible provided our gold.
42+
double cost = (health * 2.5) + (armor * 18);
43+
if (cost > 3600) {
44+
return 3600 - cost;
45+
} else {
46+
// Check how good is the solution.
47+
long fitness = (health * (100 + armor)) / 100;
48+
return fitness;
49+
}
50+
}
51+
52+
}

algorithms/src/test/java/com/baeldung/algorithms/multiswarm/MultiswarmUnitTest.java

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,22 @@ public class MultiswarmUnitTest {
2222
public MayFailRule mayFailRule = new MayFailRule();
2323

2424
/**
25-
* Tests the multiswarm algorithm with a generic problem.
26-
*
27-
* The problem is the following:
28-
*
25+
* Tests the multiswarm algorithm with a generic problem. The problem is the
26+
* following: <br>
27+
* <br>
2928
* In League of Legends, a player's Effective Health when defending against
3029
* physical damage is given by E=H(100+A)/100, where H is health and A is
31-
* armor.
32-
*
33-
* Health costs 2.5 gold per unit, and Armor costs 18 gold per unit. You
34-
* have 3600 gold, and you need to optimize the effectiveness E of your
30+
* armor. Health costs 2.5 gold per unit, and Armor costs 18 gold per unit.
31+
* You have 3600 gold, and you need to optimize the effectiveness E of your
3532
* health and armor to survive as long as possible against the enemy team's
36-
* attacks. How much of each should you buy?
37-
*
38-
* The solution is H = 1080, A = 50 for a total fitness of 1620.
39-
*
40-
* Tested with 50 swarms each with 1000 particles.
33+
* attacks. How much of each should you buy? <br>
34+
* <br>
35+
* The solution is H = 1080, A = 50 for a total fitness of 1620. Tested with
36+
* 50 swarms each with 1000 particles.
4137
*/
4238
@Test
4339
public void givenMultiswarm_whenThousandIteration_thenSolutionFound() {
44-
Multiswarm multiswarm = new Multiswarm(50, 1000, values -> {
45-
46-
// No negatives values accepted.
47-
if (values[0] < 0 && values[1] < 0) {
48-
return -(values[0] * values[1]);
49-
} else if (values[0] < 0) {
50-
return values[0];
51-
} else if (values[1] < 0) {
52-
return values[1];
53-
}
54-
55-
// Checks if the solution is actually feasible provided our gold.
56-
double cost = (values[0] * 2.5) + (values[1] * 18);
57-
if (cost > 3600) {
58-
return 3600 - cost;
59-
} else {
60-
// Check how good is the solution.
61-
long fitness = (values[0] * (100 + values[1])) / 100;
62-
return fitness;
63-
}
64-
});
40+
Multiswarm multiswarm = new Multiswarm(50, 1000, new LolFitnessFunction());
6541

6642
// Iterates 1000 times through the main loop and prints the result.
6743
for (int i = 0; i < 1000; i++) {

0 commit comments

Comments
 (0)