-
Notifications
You must be signed in to change notification settings - Fork 1
/
GeneticAlgorithm.java
108 lines (93 loc) · 3.34 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import java.util.Iterator;
public class GeneticAlgorithm {
//// mo ta:
// Khoi tao quan the (population) ban dau
// Danh gia quan the
// Fitness: do phu hop
// Chon loc
// Tao ra the he tiep thep ( next generation): lai ghep, dot bien
// Lap lai qua trinh
// -------------------------------
// initPopulation();
// evalPopulation();
// while(generation<maxGeneration) {
// crossover();
// mutate()
// evalPopulation()
// generation++;
private int populationSize;
private double mutationRate;
private int elitismCount;
private double crossoverRate;
protected int tournamentSize;
public GeneticAlgorithm(int populationSize, double mutationRate, double crossoveRate, int elitismCount,
int tournamentSize) {
this.populationSize = populationSize;
this.mutationRate = mutationRate;
this.crossoverRate = crossoveRate;
this.tournamentSize = tournamentSize;
}
// tinh fitness
public double calFitness(Individual individual, City cities[]) {
Route route = new Route(individual, cities);
double fitness = 1 / route.getDistance();
individual.setFitness(fitness);
return fitness;
}
// Khoi tao quan the
public Population initPopulation(int chromosomeLength) {
Population population = new Population(this.populationSize, chromosomeLength);
return population;
}
// Danh gia quan the: tinh theo fitness
public void evalPopulation(Population population, City cities[]) {
double populationFitness = 0;// tong fitness cua moi individual
for (Individual individual : population.getIndividual()) {
populationFitness += this.calFitness(individual, cities);
}
double avgFitness = populationFitness / populationSize;
population.setPopulationFitness(avgFitness);
}
// lựa chọn cha cho phép lai
// Selects parent for crossover using tournament selection
// * Tournament selection works by choosing N random individuals, and then
// * choosing the best of those.
public Individual selectParent(Population population) {
}
// Lai ghep
public Population crossoverPopulation(Population population) {
return newPopulation;
}
// Mutation
public Population mutatePopulation(Population population) {
// initialize new population
Population newPopulation = new Population(this.populationSize);
// Loop over current population by fitness
for (int populationIndex = 0; populationIndex < population.size(); populationIndex++) {
Individual individual = population.getFittest(populationIndex);
// skip mutation if this an elite individual
if (populationIndex >= this.elitismCount) {
// system.out.println("mutating population member"+populationIndex);
// loop over individual's genes
for (int geneIndex = 0; geneIndex < individual.getChromosomeLength(); geneIndex++) {
// system.out.println("/tGene index "+geneIndex);
// does this gene need mutation
if (this.mutationRate > Math.random()) {
// get new gene position
int newGenePos = (int) (Math.random() * individual.getChromosomeLength());
// get genes to swap
int gene1 = individual.getGene(newGenePos);
int gene2 = individual.getGene(geneIndex);
// swap genes
individual.setGene(geneIndex, gene1);
individual.setGene(newGenePos, gene2);
}
}
}
// add individual to population
newPopulation.setIndividual(populationIndex, individual);
}
// return mutated population
return newPopulation;
}
}