From 9aa60eaf2f55213901e8f5834a0fead061d7241c Mon Sep 17 00:00:00 2001 From: "a.croix" <croix.alexandre@gmail.com> Date: Tue, 19 Feb 2019 17:30:15 +0100 Subject: [PATCH] Fix style issues --- .../java/wowa/training/SolutionDistance.java | 24 ++++- .../be/cylab/java/wowa/training/Trainer.java | 96 +++++++++---------- 2 files changed, 66 insertions(+), 54 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 b1041f4..35e4741 100644 --- a/src/main/java/be/cylab/java/wowa/training/SolutionDistance.java +++ b/src/main/java/be/cylab/java/wowa/training/SolutionDistance.java @@ -1,19 +1,26 @@ package be.cylab.java.wowa.training; import info.debatty.java.aggregation.WOWA; -import sun.nio.cs.ext.MacHebrew; import java.util.Arrays; import java.util.List; +/** + * Class of Solution. + * Solution with distance performance evaluation criterion + */ public class SolutionDistance implements Comparable<SolutionDistance> { - public double[] weights_w; - public double[] weights_p; + private double[] weights_w; + private double[] weights_p; private double distance = Double.POSITIVE_INFINITY; - - public SolutionDistance(int weights_number) { + /** + * SolutionDistance constructor. + * Needs weight number as parameter + * @param weights_number + */ + public SolutionDistance(final int weights_number) { this.weights_w = new double[weights_number]; this.weights_p = new double[weights_number]; } @@ -97,4 +104,11 @@ public class SolutionDistance implements Comparable<SolutionDistance> { return distance; } + public double[] getWeightsW() { + return this.weights_w; + } + + public double[] getWeightsP() { + return this.weights_p; + } } 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 f862739..da7ebfd 100644 --- a/src/main/java/be/cylab/java/wowa/training/Trainer.java +++ b/src/main/java/be/cylab/java/wowa/training/Trainer.java @@ -72,7 +72,7 @@ public class Trainer { final List<SolutionDistance> solutions ) { SolutionDistance best_solution = - new SolutionDistance(solutions.get(0).weights_p.length); + new SolutionDistance(solutions.get(0).getWeightsP().length); for (SolutionDistance solution : solutions) { if (solution.getDistance() < best_solution.getDistance()) { @@ -95,8 +95,8 @@ public class Trainer { for (int i = 0; i < population_size; i++) { SolutionDistance solution = new SolutionDistance(number_of_weights); for (int j = 0; j < number_of_weights; j++) { - solution.weights_p[j] = Math.random(); - solution.weights_w[j] = Math.random(); + solution.getWeightsP()[j] = Math.random(); + solution.getWeightsW()[j] = Math.random(); } solution.normalize(); population.add(solution); @@ -241,9 +241,9 @@ public class Trainer { * @return */ private List<SolutionDistance> doReproduction( - List<SolutionDistance> solutions + final List<SolutionDistance> solutions ) { - int nbr_weights = solutions.get(0).weights_p.length; + int nbr_weights = solutions.get(0).getWeightsP().length; //we add children to the current list of solutions while (solutions.size() < this.parameters.getPopulationSize()) { @@ -274,49 +274,49 @@ public class Trainer { * @param dad * @param mom * @param solutions - * @param cutPosition + * @param cut_position * @param beta * @return */ private void reproduce(final SolutionDistance dad, final SolutionDistance mom, - List<SolutionDistance> solutions, - int cutPosition, - double beta + final List<SolutionDistance> solutions, + final int cut_position, + final double beta ) { - double p_new1W = dad.weights_w[cutPosition] - beta - * (dad.weights_w[cutPosition] - mom.weights_w[cutPosition]); - double p_new2W = mom.weights_w[cutPosition] + beta - * (dad.weights_w[cutPosition] - mom.weights_w[cutPosition]); + double p_new1_W = dad.getWeightsW()[cut_position] - beta + * (dad.getWeightsW()[cut_position] - mom.getWeightsW()[cut_position]); + double p_new2_W = mom.getWeightsW()[cut_position] + beta + * (dad.getWeightsW()[cut_position] - mom.getWeightsW()[cut_position]); - double p_new1P = dad.weights_p[cutPosition] - beta - * (dad.weights_p[cutPosition] - mom.weights_p[cutPosition]); - double p_new2P = mom.weights_p[cutPosition] + beta - * (dad.weights_p[cutPosition] - mom.weights_p[cutPosition]); + double p_new1_P = dad.getWeightsP()[cut_position] - beta + * (dad.getWeightsP()[cut_position] - mom.getWeightsP()[cut_position]); + double p_new2_P = mom.getWeightsP()[cut_position] + beta + * (dad.getWeightsP()[cut_position] - mom.getWeightsP()[cut_position]); - SolutionDistance child1 = new SolutionDistance(dad.weights_p.length); - SolutionDistance child2 = new SolutionDistance(dad.weights_p.length); + SolutionDistance child1 = new SolutionDistance(dad.getWeightsP().length); + SolutionDistance child2 = new SolutionDistance(dad.getWeightsP().length); - for (int i = 0; i < cutPosition; i++) { - child1.weights_w[i] = dad.weights_w[i]; - child1.weights_p[i] = dad.weights_p[i]; + for (int i = 0; i < cut_position; i++) { + child1.getWeightsW()[i] = dad.getWeightsW()[i]; + child1.getWeightsP()[i] = dad.getWeightsP()[i]; - child2.weights_w[i] = mom.weights_w[i]; - child2.weights_p[i] = mom.weights_p[i]; + child2.getWeightsW()[i] = mom.getWeightsW()[i]; + child2.getWeightsP()[i] = mom.getWeightsP()[i]; } - child1.weights_w[cutPosition] = p_new1W; - child2.weights_w[cutPosition] = p_new2W; - child1.weights_p[cutPosition] = p_new1P; - child2.weights_p[cutPosition] = p_new2P; + child1.getWeightsW()[cut_position] = p_new1_W; + child2.getWeightsW()[cut_position] = p_new2_W; + child1.getWeightsP()[cut_position] = p_new1_P; + child2.getWeightsP()[cut_position] = p_new2_P; - int nbre_weights = dad.weights_w.length; - for (int i = cutPosition + 1; i < nbre_weights; i++) { - child1.weights_w[i] = mom.weights_w[i]; - child1.weights_p[i] = mom.weights_p[i]; + int nbre_weights = dad.getWeightsW().length; + for (int i = cut_position + 1; i < nbre_weights; i++) { + child1.getWeightsW()[i] = mom.getWeightsW()[i]; + child1.getWeightsP()[i] = mom.getWeightsP()[i]; - child2.weights_w[i] = dad.weights_w[i]; - child2.weights_p[i] = dad.weights_p[i]; + child2.getWeightsW()[i] = dad.getWeightsW()[i]; + child2.getWeightsP()[i] = dad.getWeightsP()[i]; } //Check and correct only if we used a QuasiRandom generation child1.normalize(); @@ -331,7 +331,7 @@ public class Trainer { * @return */ private List<SolutionDistance> randomlyMutateGenes( - List<SolutionDistance> solutions + final List<SolutionDistance> solutions ) { double probability = this.getParameters().getMutationRate() / 100; @@ -357,15 +357,15 @@ public class Trainer { * @param expected * @return */ - public List<SolutionDistance> generateInitialPopulationAndComputeDistances( - int population_size, - List<double[]> data, - double[] expected + private List<SolutionDistance> generateInitialPopulationAndComputeDistances( + final int population_size, + final List<double[]> data, + final double[] expected ) { - int numberOfWeights = data.get(0).length; + int number_of_weights = data.get(0).length; List<SolutionDistance> initial_population = this.generateInitialPopulation( - numberOfWeights, + number_of_weights, population_size ); return this.computeDistances(initial_population, data, expected); @@ -378,9 +378,9 @@ public class Trainer { * @return */ private List<SolutionDistance> performReproduction( - List<SolutionDistance> population, - List<double[]> data, - double[] expected + final List<SolutionDistance> population, + final List<double[]> data, + final double[] expected ) { List<SolutionDistance> parents = this.selectParents(population, this.getParameters().getNumberOfParents(), @@ -392,12 +392,10 @@ public class Trainer { return this.computeDistances(mutated, data, expected); } - /** - * @param child - */ - private void checkAndCorrectNullWeightVector(SolutionDistance child) { + /* + private void checkAndCorrectNullWeightVector(final SolutionDistance child) { } - +*/ } -- GitLab