Skip to content
Snippets Groups Projects
Commit 115a79fb authored by a.croix's avatar a.croix
Browse files

Revert last commits

parent 6559a015
No related branches found
No related tags found
No related merge requests found
Pipeline #1297 passed
......@@ -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. -->
......
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();
}
}
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);
}
}
......@@ -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;
}
}
......@@ -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());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment