From 67599b1cbc5f50ddeb5553c0f8c5ab60ae5698bf Mon Sep 17 00:00:00 2001 From: Donato Rimenti Date: Sat, 24 Feb 2018 14:05:44 +0100 Subject: [PATCH] - Extracted fitness function to separate class instead of Lambda - Cleaned up javadoc - Refactored fitness function code to add more significative variables names --- .../baeldung/algorithms/multiswarm/Swarm.java | 1 - .../multiswarm/LolFitnessFunction.java | 52 +++++++++++++++++++ .../multiswarm/MultiswarmUnitTest.java | 44 ++++------------ 3 files changed, 62 insertions(+), 35 deletions(-) create mode 100644 algorithms/src/test/java/com/baeldung/algorithms/multiswarm/LolFitnessFunction.java diff --git a/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Swarm.java b/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Swarm.java index 56ab712a1d..e6d37bb7e6 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Swarm.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/multiswarm/Swarm.java @@ -3,7 +3,6 @@ package com.baeldung.algorithms.multiswarm; import java.util.Arrays; import java.util.Random; -// TODO: Auto-generated Javadoc /** * Represents a collection of {@link Particle}. * diff --git a/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/LolFitnessFunction.java b/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/LolFitnessFunction.java new file mode 100644 index 0000000000..726d4c135d --- /dev/null +++ b/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/LolFitnessFunction.java @@ -0,0 +1,52 @@ +package com.baeldung.algorithms.multiswarm; + +/** + * Specific fitness function implementation to solve the League of Legends + * problem. This is the problem statement:
+ *
+ * In League of Legends, a player's Effective Health when defending against + * physical damage is given by E=H(100+A)/100, where H is health and A is armor. + * Health costs 2.5 gold per unit, and Armor costs 18 gold per unit. You have + * 3600 gold, and you need to optimize the effectiveness E of your health and + * armor to survive as long as possible against the enemy team's attacks. How + * much of each should you buy?
+ *
+ * + * @author Donato Rimenti + * + */ +public class LolFitnessFunction implements FitnessFunction { + + /* + * (non-Javadoc) + * + * @see + * com.baeldung.algorithms.multiswarm.FitnessFunction#getFitness(long[]) + */ + @Override + public double getFitness(long[] particlePosition) { + + long health = particlePosition[0]; + long armor = particlePosition[1]; + + // No negatives values accepted. + if (health < 0 && armor < 0) { + return -(health * armor); + } else if (health < 0) { + return health; + } else if (armor < 0) { + return armor; + } + + // Checks if the solution is actually feasible provided our gold. + double cost = (health * 2.5) + (armor * 18); + if (cost > 3600) { + return 3600 - cost; + } else { + // Check how good is the solution. + long fitness = (health * (100 + armor)) / 100; + return fitness; + } + } + +} diff --git a/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/MultiswarmUnitTest.java b/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/MultiswarmUnitTest.java index f1c1609a9e..3455cd3932 100644 --- a/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/MultiswarmUnitTest.java +++ b/algorithms/src/test/java/com/baeldung/algorithms/multiswarm/MultiswarmUnitTest.java @@ -22,46 +22,22 @@ public class MultiswarmUnitTest { public MayFailRule mayFailRule = new MayFailRule(); /** - * Tests the multiswarm algorithm with a generic problem. - * - * The problem is the following: - * + * Tests the multiswarm algorithm with a generic problem. The problem is the + * following:
+ *
* In League of Legends, a player's Effective Health when defending against * physical damage is given by E=H(100+A)/100, where H is health and A is - * armor. - * - * Health costs 2.5 gold per unit, and Armor costs 18 gold per unit. You - * have 3600 gold, and you need to optimize the effectiveness E of your + * armor. Health costs 2.5 gold per unit, and Armor costs 18 gold per unit. + * You have 3600 gold, and you need to optimize the effectiveness E of your * health and armor to survive as long as possible against the enemy team's - * attacks. How much of each should you buy? - * - * The solution is H = 1080, A = 50 for a total fitness of 1620. - * - * Tested with 50 swarms each with 1000 particles. + * attacks. How much of each should you buy?
+ *
+ * The solution is H = 1080, A = 50 for a total fitness of 1620. Tested with + * 50 swarms each with 1000 particles. */ @Test public void givenMultiswarm_whenThousandIteration_thenSolutionFound() { - Multiswarm multiswarm = new Multiswarm(50, 1000, values -> { - - // No negatives values accepted. - if (values[0] < 0 && values[1] < 0) { - return -(values[0] * values[1]); - } else if (values[0] < 0) { - return values[0]; - } else if (values[1] < 0) { - return values[1]; - } - - // Checks if the solution is actually feasible provided our gold. - double cost = (values[0] * 2.5) + (values[1] * 18); - if (cost > 3600) { - return 3600 - cost; - } else { - // Check how good is the solution. - long fitness = (values[0] * (100 + values[1])) / 100; - return fitness; - } - }); + Multiswarm multiswarm = new Multiswarm(50, 1000, new LolFitnessFunction()); // Iterates 1000 times through the main loop and prints the result. for (int i = 0; i < 1000; i++) {