-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneticAlgorithm.java
90 lines (79 loc) · 2.66 KB
/
GeneticAlgorithm.java
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.*;
public class GeneticAlgorithm {
static double mutationRate = 0.001;
static int breedingGroup = 5;
static Random ran = new Random();
public static Population crossOverPopulation(Population pop) {
Population newPopulation = new Population(pop.size(), false);
// elistist approach, keep the best individual and dont evolve
newPopulation.keepIndividual(0, pop.getbest());
// cross over the whole population, by choosing the best indivuals from breeding pools, it is
// stochastic, only biased in selection
for (int i = 1; i < pop.size(); i++) {
// pick 2 random group and get 2 fit parents from them
Individual parent1 = BreedingSelection(pop);
Individual parent2 = BreedingSelection(pop);
// cross over the parents
Individual child = crossover(parent1, parent2);
// save the result
newPopulation.keepIndividual(i, child);
}
return newPopulation;
}
// Crossover method
public static Individual crossover(Individual p1, Individual p2) {
// create a random breakpoint in the genome
int breakPoint = ran.nextInt(10000);
for (int i = 0; i < breakPoint; i++) {
// swap fragments
byte temp = p1.getGene(i);
p1.setGene(i, p2.getGene(i));
;
p2.setGene(i, temp);
}
// evaluate fitness and get the best one of the resultant
if (p1.getFitness() > p2.getFitness())
return p1;
else
return p2;
}
// Mutate an individual
static void mutate(Individual individual) {
// Loop through genes
for (int i = 0; i < individual.size(); i++) {
float r = (float) Math.random();
if (individual.genes[i] == 1) // assume that its harder to mutate a good gene so lower the rate a bit
if (r <= 0.0009) { // mutate
byte gene = 0;
individual.setGene(i, gene);
}
if (individual.genes[i] == 0)
if (r < mutationRate) {
byte gene = 1;
individual.setGene(i, gene);
}
}
}
// method that measures the fitness based on the number of 1 in the genome
static int getFitness(Individual individual) {
int fitness = 0;
for (int i = 0; i < individual.size(); i++) {
if (individual.getGene(i) == 1)
fitness++;
}
return fitness;
}
// Select individuals for crossover
private static Individual BreedingSelection(Population pop) {
// Create a breeding population
Population breedingPopulation = new Population(breedingGroup, false);
// For each place in the group get a random individual
for (int i = 0; i < breedingGroup; i++) {
int randomId = (int) (Math.random() * pop.size());
breedingPopulation.keepIndividual(i, pop.getIndividual(randomId));
}
// Get the fittest from the breeding group
Individual fittest = breedingPopulation.getbest();
return fittest;
}
}