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

Refactoring: change double[] to List<Double>

parent 897afc26
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,7 @@ public abstract class AbstractSolution
protected List<Double> weights_w;
protected List<Double> weights_p;
protected double fitness_score = Double.POSITIVE_INFINITY;
protected Double fitness_score = Double.POSITIVE_INFINITY;
/**
* SolutionDistance constructor. Needs weight number as parameter
......@@ -49,7 +49,8 @@ public abstract class AbstractSolution
this.normalize();
}
abstract void computeScoreTo(List<double[]> data, double[] expected);
abstract void computeScoreTo(List<List<Double>> data,
List<Double> expected);
/**
* @return
......
......@@ -41,18 +41,19 @@ public class SolutionDistance extends AbstractSolution {
* @param data
* @param expected
*/
final void computeScoreTo(final List<double[]> data,
final double[] expected
final void computeScoreTo(final List<List<Double>> data,
final List<Double> expected
) {
this.fitness_score = 0;
for (int i = 0; i < data.size(); i++) {
double[] vector = data.get(i);
double target_value = expected[i];
List<Double> vector = data.get(i);
Double target_value = expected.get(i);
WOWA wowa = new WOWA(
Utils.convertListDoubleToArrayDouble(this.weights_w),
Utils.convertListDoubleToArrayDouble(this.weights_p));
double aggregated_value = wowa.aggregate(vector);
Double aggregated_value = wowa.aggregate(
Utils.convertListDoubleToArrayDouble(vector));
this.fitness_score += Math.pow(target_value - aggregated_value, 2);
}
this.fitness_score = Math.sqrt(this.fitness_score);
......
......@@ -37,8 +37,8 @@ public class Trainer {
public final AbstractSolution run(
final String data_file_name,
final String expected_file_name) {
List<double[]> data = Utils.convertJsonToDataForTrainer(data_file_name);
double[] expected
List<List<Double>> data = Utils.convertJsonToDataForTrainer(data_file_name);
List<Double> expected
= Utils.convertJsonToExpectedForTrainer(expected_file_name);
return this.run(data, expected);
}
......@@ -49,8 +49,8 @@ public class Trainer {
* @return
*/
public final AbstractSolution run(
final List<double[]> data,
final double[] expected) {
final List<List<Double>> data,
final List<Double> expected) {
List<AbstractSolution> current_population
= this.generateInitialPopulationAndComputeDistances(
......@@ -184,8 +184,8 @@ public class Trainer {
*/
final List<AbstractSolution> computeDistances(
final List<AbstractSolution> solutions,
final List<double[]> data,
final double[] expected) {
final List<List<Double>> data,
final List<Double> expected) {
for (AbstractSolution solution : solutions) {
solution.computeScoreTo(data, expected);
......@@ -477,10 +477,10 @@ public class Trainer {
*/
private List<AbstractSolution> generateInitialPopulationAndComputeDistances(
final int population_size,
final List<double[]> data,
final double[] expected) {
final List<List<Double>> data,
final List<Double> expected) {
int number_of_weights = data.get(0).length;
int number_of_weights = data.get(0).size();
List<AbstractSolution> initial_population = new ArrayList<>();
if (this.parameters.getPopulationInitializationMethod()
== TrainerParameters.POPULATION_INITIALIZATION_RANDOM) {
......@@ -505,8 +505,8 @@ public class Trainer {
*/
final List<AbstractSolution> performReproduction(
final List<AbstractSolution> population,
final List<double[]> data,
final double[] expected) {
final List<List<Double>> data,
final List<Double> expected) {
List<AbstractSolution> parents = this.selectParents(population,
this.getParameters().getNumberOfParents(),
......
......@@ -126,13 +126,13 @@ final class Utils {
* @param filename
* @return
*/
public static List<double[]> convertJsonToDataForTrainer(
public static List<List<Double>> convertJsonToDataForTrainer(
final String filename) {
Genson genson = new Genson();
String data_json = Utils.readFileToString(filename);
List<double[]> data = genson.deserialize(
List<List<Double>> data = genson.deserialize(
data_json,
new GenericType<List<double[]>>() {
new GenericType<List<List<Double>>>() {
});
return data;
}
......@@ -143,13 +143,14 @@ final class Utils {
* @param filename
* @return
*/
public static double[] convertJsonToExpectedForTrainer(
public static List<Double> convertJsonToExpectedForTrainer(
final String filename) {
Genson genson = new Genson();
String expected_json = Utils.readFileToString(filename);
double[] expected = genson.deserialize(expected_json, double[].class);
List<Double> expected = genson.deserialize(expected_json,
new GenericType<List<Double>>() {
});
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