From 8affc6af9dc10f4466e6d3318df593fa01319c61 Mon Sep 17 00:00:00 2001 From: "a.croix" <croix.alexandre@gmail.com> Date: Mon, 18 Feb 2019 17:25:24 +0100 Subject: [PATCH] Fix style --- .../java/wowa/training/SolutionDistance.java | 36 +++++++------------ .../be/cylab/java/wowa/training/Trainer.java | 17 ++++----- .../be/cylab/java/wowa/training/Utils.java | 12 +++---- 3 files changed, 26 insertions(+), 39 deletions(-) diff --git a/src/main/java/be/cylab/java/wowa/training/SolutionDistance.java b/src/main/java/be/cylab/java/wowa/training/SolutionDistance.java index 3b375c3..dd10c6d 100644 --- a/src/main/java/be/cylab/java/wowa/training/SolutionDistance.java +++ b/src/main/java/be/cylab/java/wowa/training/SolutionDistance.java @@ -17,13 +17,14 @@ public class SolutionDistance implements Comparable<SolutionDistance> { this.weights_w = new double[weights_number]; this.weights_p = new double[weights_number]; } + @Override public String toString() { return "SolutionDistance{" + - "weights_w=" + Arrays.toString(weights_w) + - ", weights_p=" + Arrays.toString(weights_p) + - ", distance=" + distance + - '}'; + "weights_w=" + Arrays.toString(weights_w) + + ", weights_p=" + Arrays.toString(weights_p) + + ", distance=" + distance + + '}'; } public void computeScoreTo(List<double[]> data, double[] expected) { @@ -33,7 +34,7 @@ public class SolutionDistance implements Comparable<SolutionDistance> { } - public void randomlyMutateWithProbability(double probability) { + public void randomlyMutateWithProbability(final double probability) { double tos = Math.random(); if (tos > probability) { // do nothing @@ -41,21 +42,21 @@ public class SolutionDistance implements Comparable<SolutionDistance> { } //new weight value and position - double new_weight_value = Math.random(); - int randomIndex = Utils.randomInteger(0, this.weights_p.length -1); + double newWeightValue = Math.random(); + int randomIndex = Utils.randomInteger(0, this.weights_p.length - 1); //Select w or p weights - int weight_selection = Utils.randomInteger(0, 1); - if (weight_selection == 0) { - this.weights_w[randomIndex] = new_weight_value; + int weightSelection = Utils.randomInteger(0, 1); + if (weightSelection == 0) { + this.weights_w[randomIndex] = newWeightValue; } else { - this.weights_p[randomIndex] = new_weight_value; + this.weights_p[randomIndex] = newWeightValue; } this.normalize(); } @Override - public int compareTo(SolutionDistance solution) { + public int compareTo(final SolutionDistance solution) { return this.getDistance() > solution.getDistance() ? -1 : this.getDistance() < solution.getDistance() ? 1 : 0; } @@ -66,19 +67,8 @@ public class SolutionDistance implements Comparable<SolutionDistance> { } - public double[] getWeights_w() { - return weights_w; - } - - public double[] getWeights_p() { - return weights_p; - } - public double getDistance() { return distance; } - public void setDistance(double distance) { - this.distance = distance; - } } diff --git a/src/main/java/be/cylab/java/wowa/training/Trainer.java b/src/main/java/be/cylab/java/wowa/training/Trainer.java index 2e7e2dd..bbcd0b5 100644 --- a/src/main/java/be/cylab/java/wowa/training/Trainer.java +++ b/src/main/java/be/cylab/java/wowa/training/Trainer.java @@ -12,7 +12,7 @@ public class Trainer { this.parameters = parameters; } - public SolutionDistance run(List<double[]> data, double[] expected) { + public SolutionDistance run(final List<double[]> data, final double[] expected) { List<SolutionDistance> current_population = this.generateInitialPopulationAndComputeDistances(this.getParameters().getPopulationSize(), data, expected); Collections.sort(current_population); @@ -37,7 +37,7 @@ public class Trainer { return best_solution; } - public SolutionDistance findBestSolution(List<SolutionDistance> solutions) { + public SolutionDistance findBestSolution(final List<SolutionDistance> solutions) { SolutionDistance best_solution = new SolutionDistance(solutions.get(0).weights_p.length); for (SolutionDistance solution : solutions) { @@ -78,7 +78,7 @@ public class Trainer { if (solutions.size() < count) { - throw new IllegalArgumentException("Not enough elements in population to select "+ count + "parents"); + throw new IllegalArgumentException("Not enough elements in population to select " + count + "parents"); } while (selected_elements.size() < count) { double tos = Math.random(); @@ -95,7 +95,7 @@ public class Trainer { return selected_elements; } - private List<SolutionDistance> tournamentSelection(List<SolutionDistance> solutions, List<SolutionDistance> selected_elements, int count){ + private List<SolutionDistance> tournamentSelection(List<SolutionDistance> solutions, List<SolutionDistance> selected_elements, int count) { while (selected_elements.size() < count) { Collections.sort(solutions); @@ -134,11 +134,10 @@ public class Trainer { solutions.remove(0); solutions.remove(1); - if(selectionMethod == TrainerParameters.SELECTION_METHOD_RWS) { + if (selectionMethod == TrainerParameters.SELECTION_METHOD_RWS) { return this.rouletteWheelSelection(solutions, selected_parents, count - 2); - } - else if (selectionMethod == TrainerParameters.SELECTION_METHOD_TOS) { + } else if (selectionMethod == TrainerParameters.SELECTION_METHOD_TOS) { return this.tournamentSelection(solutions, selected_parents, count - 2); } throw new IllegalArgumentException("Invalid selection method"); @@ -162,7 +161,7 @@ public class Trainer { double beta = Math.random(); solutions = this.reproduce(solutions.get(dad), solutions.get(mom), solutions, cut_position, beta); } - while(solutions.size() > this.getParameters().getPopulationSize()) { + while (solutions.size() > this.getParameters().getPopulationSize()) { solutions.remove(solutions.get(solutions.size() - 1)); } return solutions; @@ -243,6 +242,4 @@ public class Trainer { } - - } diff --git a/src/main/java/be/cylab/java/wowa/training/Utils.java b/src/main/java/be/cylab/java/wowa/training/Utils.java index 680f633..27a5b87 100644 --- a/src/main/java/be/cylab/java/wowa/training/Utils.java +++ b/src/main/java/be/cylab/java/wowa/training/Utils.java @@ -7,7 +7,7 @@ import java.util.Random; public class Utils { - public static double[] normalizeWeights(double[] weights) { + public static double[] normalizeWeights(final double[] weights) { double sum_weight = Utils.sumArrayElements(weights); double[] weightsNormalized = new double[weights.length]; for (int i = 0; i < weights.length; i++) { @@ -16,7 +16,7 @@ public class Utils { return weightsNormalized; } - public static double findMaxDistance(List<SolutionDistance> solutions) { + public static double findMaxDistance(final List<SolutionDistance> solutions) { double max = Double.NEGATIVE_INFINITY; for (SolutionDistance solution : solutions) { if(solution.getDistance() > max) { @@ -26,7 +26,7 @@ public class Utils { return max; } - public static double findMinDistance(List<SolutionDistance> solutions) { + public static double findMinDistance(final List<SolutionDistance> solutions) { double min = Double.POSITIVE_INFINITY; for (SolutionDistance solution : solutions) { if (solution.getDistance() < min){ @@ -36,7 +36,7 @@ public class Utils { return min; } - public static double sumTotalDistance(List<SolutionDistance> solutions) { + public static double sumTotalDistance(final List<SolutionDistance> solutions) { double sum = 0; for (SolutionDistance solution : solutions) { sum += solution.getDistance(); @@ -44,7 +44,7 @@ public class Utils { return sum; } - public static double sumArrayElements(double[] array) { + public static double sumArrayElements(final double[] array) { float sum = 0; for (double weight : array) { sum += weight; @@ -52,7 +52,7 @@ public class Utils { return sum; } - public static int randomInteger(int min, int max) { + public static int randomInteger(final int min, final int max) { if (min >= max) { throw new IllegalArgumentException("Max must be greater then min"); } -- GitLab