From 115a79fb178a77a1bf495bdb33e34ecb2b4e19dc Mon Sep 17 00:00:00 2001 From: "a.croix" <croix.alexandre@gmail.com> Date: Thu, 21 Mar 2019 15:54:09 +0100 Subject: [PATCH] Revert last commits --- checkstyle.xml | 4 +- .../java/wowa/training/AbstractSolution.java | 170 ------------------ .../cylab/java/wowa/training/SolutionAUC.java | 28 --- .../java/wowa/training/SolutionDistance.java | 141 ++++++++++++++- .../be/cylab/java/wowa/training/Utils.java | 6 +- 5 files changed, 138 insertions(+), 211 deletions(-) delete mode 100644 src/main/java/be/cylab/java/wowa/training/AbstractSolution.java delete mode 100644 src/main/java/be/cylab/java/wowa/training/SolutionAUC.java diff --git a/checkstyle.xml b/checkstyle.xml index 74a0e0f..77b8d98 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -205,9 +205,7 @@ <module name="FinalClass"/> <module name="HideUtilityClassConstructor"/> <module name="InterfaceIsType"/> - <module name="VisibilityModifier"> - <property name="protectedAllowed" value="true"/> - </module> + <module name="VisibilityModifier"/> <!-- Miscellaneous other checks. --> diff --git a/src/main/java/be/cylab/java/wowa/training/AbstractSolution.java b/src/main/java/be/cylab/java/wowa/training/AbstractSolution.java deleted file mode 100644 index 1e67a94..0000000 --- a/src/main/java/be/cylab/java/wowa/training/AbstractSolution.java +++ /dev/null @@ -1,170 +0,0 @@ -package be.cylab.java.wowa.training; - -import java.util.Arrays; -import java.util.List; -import java.util.Random; - -/** - * Abstract class Solution. - */ -public abstract class AbstractSolution - implements Comparable<AbstractSolution>, Cloneable { - - protected double[] weights_w; - protected double[] weights_p; - protected double distance = Double.POSITIVE_INFINITY; - - /** - * SolutionDistance constructor. Needs weight number as parameter - * - * @param weights_number - */ - public AbstractSolution(final int weights_number) { - this.weights_w = new double[weights_number]; - this.weights_p = new double[weights_number]; - for (int i = 0; i < weights_number; i++) { - this.weights_w[i] = Math.random(); - this.weights_p[i] = Math.random(); - } - this.normalize(); - } - - /** - * Constructor solution distance. Made for test to initialize the Random - * Generator - * - * @param weights_number int - * @param seed int - */ - - public AbstractSolution(final int weights_number, final int seed) { - Random rnd = new Random(seed); - this.weights_w = new double[weights_number]; - this.weights_p = new double[weights_number]; - for (int i = 0; i < weights_number; i++) { - this.weights_w[i] = rnd.nextDouble(); - this.weights_p[i] = rnd.nextDouble(); - } - this.normalize(); - } - - abstract void computeScoreTo(List<double[]> data, double[] expected); - - /** - * @return - */ - @Override - public final String toString() { - return "SolutionDistance{" - + "weights_w=" + Arrays.toString(weights_w) - + ", weights_p=" + Arrays.toString(weights_p) - + ", distance=" + Math.abs(distance) - + '}'; - } - - /** - * Change a random gene in a weight vector. - * The gene is changed is the probability is higher than a random double - * - * @param probability - */ - final void randomlyMutateWithProbability(final double probability) { - double tos = Math.random(); - if (tos > probability) { - // do nothing - return; - } - - //new weight value and position - double new_weight_value = Math.random(); - int random_index = 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[random_index] = new_weight_value; - } else { - this.weights_p[random_index] = new_weight_value; - } - this.normalize(); - - } - - /** - * @param solution - * @return int - */ - @Override - public final int compareTo(final AbstractSolution solution) { - if (this.getDistance() > solution.getDistance()) { - return 1; - } else if (this.getDistance() < solution.getDistance()) { - return -1; - } else { - return 0; - } - } - - /** - * Function to normalize SolutionDistance weights. - * Weights must be between 0 and 1 and the sum of the weight in a vector - * must be equal to 1 - */ - final void normalize() { - this.weights_w = Utils.normalizeWeights(this.weights_w); - this.weights_p = Utils.normalizeWeights(this.weights_p); - } - - /** - * @return - */ - public final double getDistance() { - return distance; - } - - /** - * @param distance - */ - public final void setDistance(final double distance) { - return; - } - - /** - * @return - */ - public final double[] getWeightsW() { - return this.weights_w; - } - - /** - * @return - */ - public final double[] getWeightsP() { - return this.weights_p; - } - - /** - * @param weights_w - */ - final void setWeightsW(final double[] weights_w) { - this.weights_w = weights_w; - } - - /** - * @param weights_p - */ - final void setWeightsP(final double[] weights_p) { - this.weights_p = weights_p; - } - - - /** - * Copy. - * - * @return - * @throws java.lang.CloneNotSupportedException if clone is not supported - */ - public final SolutionDistance clone() throws CloneNotSupportedException { - return (SolutionDistance) super.clone(); - } - -} diff --git a/src/main/java/be/cylab/java/wowa/training/SolutionAUC.java b/src/main/java/be/cylab/java/wowa/training/SolutionAUC.java deleted file mode 100644 index f2dfe41..0000000 --- a/src/main/java/be/cylab/java/wowa/training/SolutionAUC.java +++ /dev/null @@ -1,28 +0,0 @@ -package be.cylab.java.wowa.training; - -import info.debatty.java.aggregation.WOWA; - -import java.util.List; - -/** - * Child class Solution. - * Fitness criterion is AUC - */ -public class SolutionAUC extends AbstractSolution { - /** - * Default constructor. - * @param weight_number - */ - public SolutionAUC(final int weight_number) { - super(weight_number); - } - - final void computeScoreTo( - final List<double[]> data, - final double[] expected) { - this.distance = 0; - double[] aggregated_values = new double[data.size()]; - WOWA wowa = new WOWA(this.weights_w, this.weights_p); - this.distance = Utils.computeAUC(this, data, expected); - } -} 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 9f8a2a9..e829bb1 100644 --- a/src/main/java/be/cylab/java/wowa/training/SolutionDistance.java +++ b/src/main/java/be/cylab/java/wowa/training/SolutionDistance.java @@ -2,13 +2,19 @@ package be.cylab.java.wowa.training; import info.debatty.java.aggregation.WOWA; +import java.util.Arrays; import java.util.List; +import java.util.Random; /** * Class of Solution. Solution with distance performance evaluation criterion */ -public class SolutionDistance extends AbstractSolution { +public class SolutionDistance + implements Comparable<SolutionDistance>, Cloneable { + private double[] weights_w; + private double[] weights_p; + private double distance = Double.POSITIVE_INFINITY; /** * SolutionDistance constructor. Needs weight number as parameter @@ -16,19 +22,45 @@ public class SolutionDistance extends AbstractSolution { * @param weights_number */ public SolutionDistance(final int weights_number) { - super(weights_number); + this.weights_w = new double[weights_number]; + this.weights_p = new double[weights_number]; + for (int i = 0; i < weights_number; i++) { + this.weights_w[i] = Math.random(); + this.weights_p[i] = Math.random(); + } + this.normalize(); } /** - * Constructor used for tests. - * @param weights_number - * @param seed + * Constructor solution distance. Made for test to initialize the Random + * Generator + * + * @param weights_number int + * @param seed int */ + public SolutionDistance(final int weights_number, final int seed) { - super(weights_number, seed); + Random rnd = new Random(seed); + this.weights_w = new double[weights_number]; + this.weights_p = new double[weights_number]; + for (int i = 0; i < weights_number; i++) { + this.weights_w[i] = rnd.nextDouble(); + this.weights_p[i] = rnd.nextDouble(); + } + this.normalize(); } - + /** + * @return + */ + @Override + public final String toString() { + return "SolutionDistance{" + + "weights_w=" + Arrays.toString(weights_w) + + ", weights_p=" + Arrays.toString(weights_p) + + ", distance=" + distance + + '}'; + } /** * Compute the solution fitness score. @@ -55,4 +87,99 @@ public class SolutionDistance extends AbstractSolution { } + /** + * Change a random gene in a weight vector. + * The gene is changed is the probability is higher than a random double + * @param probability + */ + final void randomlyMutateWithProbability(final double probability) { + double tos = Math.random(); + if (tos > probability) { + // do nothing + return; + } + + //new weight value and position + double new_weight_value = Math.random(); + int random_index = 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[random_index] = new_weight_value; + } else { + this.weights_p[random_index] = new_weight_value; + } + this.normalize(); + + } + + /** + * @param solution + * @return int + */ + @Override + public final int compareTo(final SolutionDistance solution) { + if (this.getDistance() > solution.getDistance()) { + return 1; + } else if (this.getDistance() < solution.getDistance()) { + return -1; + } else { + return 0; + } + } + + /** + *Function to normalize SolutionDistance weights. + * Weights must be between 0 and 1 and the sum of the weight in a vector + * must be equal to 1 + */ + final void normalize() { + this.weights_w = Utils.normalizeWeights(this.weights_w); + this.weights_p = Utils.normalizeWeights(this.weights_p); + } + + /** + * Copy. + * + * @return + * @throws java.lang.CloneNotSupportedException if clone is not supported + */ + public final SolutionDistance clone() throws CloneNotSupportedException { + return (SolutionDistance) super.clone(); + } + + /** + * @return + */ + public final double getDistance() { + return distance; + } + + /** + * @return + */ + public final double[] getWeightsW() { + return this.weights_w; + } + + /** + * @return + */ + public final double[] getWeightsP() { + return this.weights_p; + } + + /** + * @param weights_w + */ + final void setWeightsW(final double[] weights_w) { + this.weights_w = weights_w; + } + + /** + * @param weights_p + */ + final void setWeightsP(final double[] weights_p) { + this.weights_p = weights_p; + } } 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 824d335..250718c 100644 --- a/src/main/java/be/cylab/java/wowa/training/Utils.java +++ b/src/main/java/be/cylab/java/wowa/training/Utils.java @@ -152,7 +152,7 @@ public final class Utils { * @return */ public static double computeAUC( - final AbstractSolution solution, + final SolutionDistance solution, final List<double[]> data, final double[] expected) { if (data.size() != expected.length) { @@ -172,7 +172,7 @@ public final class Utils { * @return */ public static List<RocCoordinates> computeRocPoints( - final AbstractSolution solution, + final SolutionDistance solution, final List<double[]> data, final double[] expected, final boolean save_on_csv) { @@ -199,7 +199,7 @@ public final class Utils { * @return */ private static double[] computeWOWAScoreWithData( - final AbstractSolution solution, + final SolutionDistance solution, final List<double[]> data) { double[] score = new double[data.size()]; WOWA wowa = new WOWA(solution.getWeightsW(), solution.getWeightsP()); -- GitLab