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

Implementation of quasi-random initialization. Not Wworking

parent 0b7a61e2
No related branches found
No related tags found
No related merge requests found
Pipeline #1121 failed
......@@ -26,16 +26,28 @@ public final class Example {
int population_size = Integer.parseInt(args[0]);
int crossover_rate = Integer.parseInt(args[1]);
int mutation_rate = Integer.parseInt(args[2]);
int max_generation_number = Integer.parseInt(args[3]);
int selection_method;
if (args[3].matches("RWS")) {
if (args[4].matches("RWS")) {
selection_method = TrainerParameters.SELECTION_METHOD_RWS;
} else if (args[3].matches("TOS")) {
} else if (args[4].matches("TOS")) {
selection_method = TrainerParameters.SELECTION_METHOD_TOS;
} else {
throw new IllegalArgumentException(
"Selection method must be RWS ar TOS");
}
int max_generation_number = Integer.parseInt(args[4]);
int generation_population_method;
if (args[5].matches("RANDOM")) {
generation_population_method
= TrainerParameters.POPULATION_INITIALIZATION_RANDOM;
} else if (args[5].matches("QUASI_RANDOM")) {
generation_population_method
= TrainerParameters.POPULATION_INITIALIZATION_QUASI_RANDOM;
} else {
throw new IllegalArgumentException(
"Initialization must be RANDOM or QUASI_RANDOM");
}
Logger logger = Logger.getLogger(Trainer.class.getName());
logger.setLevel(Level.INFO);
......@@ -43,8 +55,12 @@ public final class Example {
new SimpleFormatter());
logger.addHandler(handler);
TrainerParameters parameters = new TrainerParameters(
logger, population_size, crossover_rate, mutation_rate,
selection_method, max_generation_number);
logger, population_size,
crossover_rate,
mutation_rate,
max_generation_number,
selection_method,
generation_population_method);
Trainer trainer = new Trainer(parameters);
List<double[]> data = Utils.convertJsonToDataForTrainer(
"./ressources/webshell_data.json");
......
......@@ -152,4 +152,18 @@ public class SolutionDistance
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;
}
}
......@@ -98,6 +98,44 @@ public class Trainer {
return population;
}
final List<SolutionDistance> generateQuasiRandomInitialPopulation(
final int number_of_weight,
final int population_size) {
List<SolutionDistance> population = new ArrayList<>();
int number_of_random_element = population_size / 2;
if (Math.pow(number_of_weight, 2) < population_size / 2) {
number_of_random_element =
(int) (population_size - Math.pow(number_of_weight, 2));
}
for (int i = 0; i < number_of_random_element; i++) {
SolutionDistance solution = new SolutionDistance(number_of_weight);
population.add(solution);
}
int[][] random_memory = new int[5][5];
for (int[] memory_line : random_memory) {
for (int memory_element : memory_line) {
memory_element = 0;
}
}
while (population.size() < population_size) {
int position_w = Utils.randomInteger(0, number_of_weight - 1);
int position_p = Utils.randomInteger(0, number_of_weight - 1);
if (random_memory[position_w][position_p] == 0) {
SolutionDistance solution = new SolutionDistance(5);
double[] weights_w = new double[] {0, 0, 0, 0, 0};
weights_w[position_w] = 1;
double[] weights_p = new double[] {0, 0, 0, 0, 0};
weights_p[position_p] = 1;
solution.setWeightsW(weights_w);
solution.setWeightsP(weights_p);
population.add(solution);
random_memory[position_w][position_p] = 1;
}
}
return population;
}
/**
* @param solutions
* @param data
......@@ -363,11 +401,19 @@ public class Trainer {
final double[] expected) {
int number_of_weights = data.get(0).length;
List<SolutionDistance> initial_population
= this.generateInitialPopulation(
number_of_weights,
population_size
);
List<SolutionDistance> initial_population = new ArrayList<>();
if (this.parameters.getPopulationInitializationMethod()
== TrainerParameters.POPULATION_INITIALIZATION_RANDOM) {
initial_population = this.generateInitialPopulation(
number_of_weights,
population_size
);
} else if (this.parameters.getPopulationInitializationMethod()
== TrainerParameters.POPULATION_INITIALIZATION_QUASI_RANDOM) {
initial_population = this.generateQuasiRandomInitialPopulation(
number_of_weights,
population_size);
}
return this.computeDistances(initial_population, data, expected);
}
......
......@@ -8,7 +8,7 @@ import java.util.logging.Logger;
public class TrainerParameters {
/**
* Constant for choosing the selection method. SELECTION_METHODRWS for the
* Constant for choosing the selection method. SELECTION_METHOD_RWS for the
* roulette wheel selection method
*/
public static final int SELECTION_METHOD_RWS = 374;
......@@ -17,6 +17,17 @@ public class TrainerParameters {
* tournament selection
*/
public static final int SELECTION_METHOD_TOS = 942;
/**
* Constant for choosing generation method.
* POPULATION_INITIALIZATION_RANDOM for random initialization
*/
public static final int POPULATION_INITIALIZATION_RANDOM = 758;
/**
* Constant for choosing generation method.
* POPULATION_INITIALIZATION_QUASI_RANDOM
* for quasi_random initialization
*/
public static final int POPULATION_INITIALIZATION_QUASI_RANDOM = 329;
private Logger logger;
private int population_size;
......@@ -24,6 +35,7 @@ public class TrainerParameters {
private int mutation_rate;
private int selection_method;
private int number_of_parents;
private int population_initialization_method;
private int max_generation_number;
//private float trigger_distance;
......@@ -48,8 +60,10 @@ public class TrainerParameters {
final int population_size,
final int crossover_rate,
final int mutation_rate,
final int max_generation_number,
final int selection_method,
final int max_generation_number) {
final int population_initialization_method
) {
this.setLogger(logger);
this.setPopulationSize(population_size);
......@@ -57,6 +71,8 @@ public class TrainerParameters {
this.mutation_rate = mutation_rate;
this.setSelectionMethod(selection_method);
this.setMaxGenerationNumber(max_generation_number);
this.setPopulationInitializationMethod(
population_initialization_method);
}
......@@ -170,4 +186,26 @@ public class TrainerParameters {
this.max_generation_number = max_generation_number;
}
/**
* Setter for initialization method.
* @param population_initialization_method
*/
final void setPopulationInitializationMethod(
final int population_initialization_method) {
if (population_initialization_method != POPULATION_INITIALIZATION_RANDOM
&& population_initialization_method
!= POPULATION_INITIALIZATION_QUASI_RANDOM) {
throw new IllegalArgumentException(
"Incorrect initialization method");
}
this.population_initialization_method =
population_initialization_method;
}
/**
* @return
*/
final int getPopulationInitializationMethod() {
return this.population_initialization_method;
}
}
......@@ -17,7 +17,7 @@ class TrainerTest {
@BeforeEach
void setUp() {
TrainerParameters parameters = new TrainerParameters(null, 100, 30, 10, TrainerParameters.SELECTION_METHOD_RWS, 100);
TrainerParameters parameters = new TrainerParameters(null, 100, 30, 10, 100, TrainerParameters.SELECTION_METHOD_RWS, TrainerParameters.POPULATION_INITIALIZATION_RANDOM);
this.trainer = new Trainer(parameters);
}
......
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