This repository was archived by the owner on Feb 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemeticAlg.java
More file actions
72 lines (64 loc) · 2.41 KB
/
MemeticAlg.java
File metadata and controls
72 lines (64 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Memetic;
import Principal.Main;
import Principal.Population;
import Principal.Solution;
import java.util.Random;
import SimulatedAnnealing.*;
/**
*
* @author kzr
*/
public class MemeticAlg {
//Numero de iteraciones en las que se mejora
public static int GENERATIONLOCALSEARCH = 10;
//Porcentaje de individuos a los que se aplica la mejora local
public static double RATELOCALSEARCH = 0.1;
//saber si se aplica al top o a unos individuos aleatorios
public static boolean BEST = true;
public static void run() {
Population pop = new Population();
pop.initializePopulationRandomly(Main.POPULATION_SIZE);
for (int i = 0; i < Main.NUM_EVOLUTION_ITERATIONS; i++) {
pop = pop.evolve();
if (i % 3 == 0&& Main.PRINT_ITERATION) {
System.out.println("Finished Iteration: " + i + ". Best Solution: " + pop.getBestIndividualInPop().getCost());
}
LocalSearch(pop,i);
}
Solution best = pop.getBestIndividualInPop();
System.out.println("\nMemetic Algorithm:\nFinal solution: " + best.getCost()
+ "\nCoste medicos: " + best.getDinero() + " €\tDistancia "
+ "total: " + best.getDistancia() + " km");
if(Main.PRINT_BEST)
System.out.println("Mejor solucion: "+ best.toString());
}
public static void LocalSearch(Population pop, int gen) {
if (gen % GENERATIONLOCALSEARCH == 0) {
if (BEST) {
int i;
for (i = 0; i < RATELOCALSEARCH * Main.POPULATION_SIZE; i++) {
Solution ind = pop.solutions.get(i);
SimulatedAnnealing(ind);
}
} else {
int i;
Random rnd = new Random();
for (i = 0; i < Main.POPULATION_SIZE; i++) {
if (rnd.nextDouble() < RATELOCALSEARCH) {
Solution ind = pop.solutions.get(i);
SimulatedAnnealing(ind);
}
}
}
}
}
public static void SimulatedAnnealing(Solution s) {
SimulatedAnnealing sa = new SimulatedAnnealing();
sa.run(s);
}
}