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

Refactoring. double[] change to List<Double>

parent 7e25850f
No related branches found
No related tags found
No related merge requests found
Source diff could not be displayed: it is too large. Options to address this: view the blob.
This diff is collapsed.
package be.cylab.java.wowa.training;
import com.owlike.genson.GenericType;
import com.owlike.genson.Genson;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Convert {
public static void main(final String[] args) {
String data_json = null;
String expected_json = null;
try {
data_json = new String(Files.readAllBytes(Paths.get("./ressources/webshell_data.json")),
StandardCharsets.UTF_8);
expected_json = new String(Files.readAllBytes(Paths.get("./ressources/webshell_expected.json")),
StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
Genson genson = new Genson();
List<double[]> data = genson.deserialize(data_json,
new GenericType<List<double[]>>() {
});
List<List<Double>> data_list = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
List<Double> vector_list = new ArrayList<>();
double[] vector = new double[data.get(i).length];
for (double el : vector) {
vector_list.add(el);
}
data_list.add(vector_list);
}
double[] expected = genson.deserialize(expected_json, double[].class);
List<Double> expected_list = new ArrayList<>();
for (double el : expected) {
expected_list.add(el);
}
String data_final = genson.serialize(data_list);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("./ressources/webshell_data_list.json"));
writer.write(data_final);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
String expected_final = genson.serialize(expected_list);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("./ressources/webshell_expected_list.json"));
writer.write(expected_final);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
...@@ -16,9 +16,9 @@ public class SolutionAUC extends AbstractSolution { ...@@ -16,9 +16,9 @@ public class SolutionAUC extends AbstractSolution {
} }
final void computeScoreTo( final void computeScoreTo(
final List<double[]> data, final List<List<Double>> data,
final double[] expected) { final List<Double> expected) {
this.fitness_score = 0; this.fitness_score = 0.0;
this.fitness_score = -(Utils.computeAUC(this, data, expected)); this.fitness_score = -(Utils.computeAUC(this, data, expected));
} }
} }
...@@ -45,7 +45,7 @@ public class SolutionDistance extends AbstractSolution { ...@@ -45,7 +45,7 @@ public class SolutionDistance extends AbstractSolution {
final List<Double> expected final List<Double> expected
) { ) {
this.fitness_score = 0; this.fitness_score = 0.0;
for (int i = 0; i < data.size(); i++) { for (int i = 0; i < data.size(); i++) {
List<Double> vector = data.get(i); List<Double> vector = data.get(i);
Double target_value = expected.get(i); Double target_value = expected.get(i);
......
...@@ -178,9 +178,9 @@ final class Utils { ...@@ -178,9 +178,9 @@ final class Utils {
*/ */
public static double computeAUC( public static double computeAUC(
final AbstractSolution solution, final AbstractSolution solution,
final List<double[]> data, final List<List<Double>> data,
final double[] expected) { final List<Double> expected) {
if (data.size() != expected.length) { if (data.size() != expected.size()) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Data and expected have different size"); "Data and expected have different size");
} }
...@@ -201,10 +201,10 @@ final class Utils { ...@@ -201,10 +201,10 @@ final class Utils {
*/ */
public static List<RocCoordinates> computeRocPoints( public static List<RocCoordinates> computeRocPoints(
final AbstractSolution solution, final AbstractSolution solution,
final List<double[]> data, final List<List<Double>> data,
final double[] expected, final List<Double> expected,
final boolean save_on_csv) { final boolean save_on_csv) {
if (data.size() != expected.length) { if (data.size() != expected.size()) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Data and expected have different size"); "Data and expected have different size");
} }
...@@ -228,7 +228,7 @@ final class Utils { ...@@ -228,7 +228,7 @@ final class Utils {
*/ */
private static double[] computeWOWAScoreWithData( private static double[] computeWOWAScoreWithData(
final AbstractSolution solution, final AbstractSolution solution,
final List<double[]> data) { final List<List<Double>> data) {
double[] score = new double[data.size()]; double[] score = new double[data.size()];
double[] weights_w double[] weights_w
= convertListDoubleToArrayDouble(solution.getWeightsW()); = convertListDoubleToArrayDouble(solution.getWeightsW());
...@@ -236,7 +236,8 @@ final class Utils { ...@@ -236,7 +236,8 @@ final class Utils {
= convertListDoubleToArrayDouble(solution.getWeightsP()); = convertListDoubleToArrayDouble(solution.getWeightsP());
WOWA wowa = new WOWA(weights_w, weights_p); WOWA wowa = new WOWA(weights_w, weights_p);
for (int i = 0; i < data.size(); i++) { for (int i = 0; i < data.size(); i++) {
score[i] = wowa.aggregate(data.get(i)); score[i] =
wowa.aggregate(convertListDoubleToArrayDouble(data.get(i)));
} }
return score; return score;
} }
...@@ -246,12 +247,12 @@ final class Utils { ...@@ -246,12 +247,12 @@ final class Utils {
* @return * @return
*/ */
private static boolean[] convertExpectedToBooleanArrayTrueAlert( private static boolean[] convertExpectedToBooleanArrayTrueAlert(
final double[] expected) { final List<Double> expected) {
boolean[] true_alert = new boolean[expected.length]; boolean[] true_alert = new boolean[expected.size()];
for (int i = 0; i < expected.length; i++) { for (int i = 0; i < expected.size(); i++) {
if (expected[i] == 1) { if (expected.get(i) == 1) {
true_alert[i] = true; true_alert[i] = true;
} else if (expected[i] == 0) { } else if (expected.get(i) == 0) {
true_alert[i] = false; true_alert[i] = false;
} else { } else {
throw new IllegalStateException( throw new IllegalStateException(
......
...@@ -24,8 +24,8 @@ class SolutionDistanceTest { ...@@ -24,8 +24,8 @@ class SolutionDistanceTest {
@org.junit.jupiter.api.Test @org.junit.jupiter.api.Test
public void computeScoreTo() { public void computeScoreTo() {
SolutionDistance solution = new SolutionDistance(5,1234); SolutionDistance solution = new SolutionDistance(5,1234);
List<double[]> data = generateData(20, 5); List<List<Double>> data = generateData(20, 5);
double[] expected = generateExpected(20); List<Double> expected = generateExpected(20);
solution.computeScoreTo(data, expected); solution.computeScoreTo(data, expected);
assertEquals(1.5925246410672433, solution.getFitnessScore()); assertEquals(1.5925246410672433, solution.getFitnessScore());
} }
...@@ -44,24 +44,24 @@ class SolutionDistanceTest { ...@@ -44,24 +44,24 @@ class SolutionDistanceTest {
} }
static List<double[]> generateData(final int size, final int weight_number) { static List<List<Double>> generateData(final int size, final int weight_number) {
Random rnd = new Random(5489); Random rnd = new Random(5489);
List<double[]> data = new ArrayList<>(); List<List<Double>> data = new ArrayList<>();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
double[] vector = new double[weight_number]; List<Double> vector = new ArrayList<>();
for (int j = 0; j < weight_number; j++) { for (int j = 0; j < weight_number; j++) {
vector[j] = rnd.nextDouble(); vector.add(rnd.nextDouble());
} }
data.add(vector); data.add(vector);
} }
return data; return data;
} }
static double[] generateExpected(final int size) { static List<Double> generateExpected(final int size) {
Random rnd = new Random(5768); Random rnd = new Random(5768);
double[] expected = new double[size]; List<Double> expected = new ArrayList<>();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
expected[i] = rnd.nextDouble(); expected.add(rnd.nextDouble());
} }
return expected; return expected;
} }
......
...@@ -29,8 +29,8 @@ class TrainerTest { ...@@ -29,8 +29,8 @@ class TrainerTest {
AbstractSolution solution = new SolutionDistance(5, i); AbstractSolution solution = new SolutionDistance(5, i);
population.add(solution); population.add(solution);
} }
List<double[]> data = generateData(100, 5); List<List<Double>> data = generateData(100, 5);
double[] expected = generateExpected(100); List<Double> expected = generateExpected(100);
List<AbstractSolution> computed_population = this.trainer.computeDistances(population, data, expected); List<AbstractSolution> computed_population = this.trainer.computeDistances(population, data, expected);
AbstractSolution bestSolution = this.trainer.findBestSolution(computed_population); AbstractSolution bestSolution = this.trainer.findBestSolution(computed_population);
assertEquals(3.0482902643223135, bestSolution.getFitnessScore()); assertEquals(3.0482902643223135, bestSolution.getFitnessScore());
...@@ -55,8 +55,8 @@ class TrainerTest { ...@@ -55,8 +55,8 @@ class TrainerTest {
@Test @Test
void testSelectParents() { void testSelectParents() {
List<AbstractSolution> population = this.trainer.generateInitialPopulation(5, 100); List<AbstractSolution> population = this.trainer.generateInitialPopulation(5, 100);
List<double[]> data = generateData(100, 5); List<List<Double>> data = generateData(100, 5);
double[] expected = generateExpected(100); List<Double> expected = generateExpected(100);
List<AbstractSolution> computed_population = this.trainer.computeDistances(population, data, expected); List<AbstractSolution> computed_population = this.trainer.computeDistances(population, data, expected);
AbstractSolution best_solution = this.trainer.findBestSolution(computed_population); AbstractSolution best_solution = this.trainer.findBestSolution(computed_population);
List<AbstractSolution> parents = this.trainer.selectParents(computed_population, 30, TrainerParameters.SELECTION_METHOD_TOS); List<AbstractSolution> parents = this.trainer.selectParents(computed_population, 30, TrainerParameters.SELECTION_METHOD_TOS);
...@@ -82,8 +82,8 @@ class TrainerTest { ...@@ -82,8 +82,8 @@ class TrainerTest {
AbstractSolution solution = new SolutionDistance(5, 2*i); AbstractSolution solution = new SolutionDistance(5, 2*i);
population.add(solution); population.add(solution);
} }
List<double[]> data = generateData(100, 5); List<List<Double>> data = generateData(100, 5);
double[] expected = generateExpected(100); List<Double> expected = generateExpected(100);
population = this.trainer.computeDistances(population, data, expected); population = this.trainer.computeDistances(population, data, expected);
List<AbstractSolution> parents = this.trainer.selectParents(population, 10, TrainerParameters.SELECTION_METHOD_RWS); List<AbstractSolution> parents = this.trainer.selectParents(population, 10, TrainerParameters.SELECTION_METHOD_RWS);
this.trainer.reproduce(parents.get(0), parents.get(1), parents, 1, 0.25875); this.trainer.reproduce(parents.get(0), parents.get(1), parents, 1, 0.25875);
...@@ -116,24 +116,24 @@ class TrainerTest { ...@@ -116,24 +116,24 @@ class TrainerTest {
} }
static List<double[]> generateData(final int size, final int weight_number) { static List<List<Double>> generateData(final int size, final int weight_number) {
Random rnd = new Random(5489); Random rnd = new Random(5489);
List<double[]> data = new ArrayList<>(); List<List<Double>> data = new ArrayList<>();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
double[] vector = new double[weight_number]; List<Double> vector = new ArrayList<>();
for (int j = 0; j < weight_number; j++) { for (int j = 0; j < weight_number; j++) {
vector[j] = rnd.nextDouble(); vector.add(rnd.nextDouble());
} }
data.add(vector); data.add(vector);
} }
return data; return data;
} }
static double[] generateExpected(final int size) { static List<Double> generateExpected(final int size) {
Random rnd = new Random(5768); Random rnd = new Random(5768);
double[] expected = new double[size]; List<Double> expected = new ArrayList<>();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
expected[i] = rnd.nextDouble(); expected.add(rnd.nextDouble());
} }
return expected; return expected;
} }
......
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