-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericGA.java
More file actions
167 lines (137 loc) · 4.99 KB
/
GenericGA.java
File metadata and controls
167 lines (137 loc) · 4.99 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// ********************************************************
// Class: CS225
// Name: Lucien Hammond
// Date: 12/2/22
//
// Purpose: To have all generic methods of each genetic algorithm
// which are used to control the evolutionary process
//
//
// Attributes: #population: Chromosome[]
// #bufferPopulation: Chromosome[]
// #bufferPointer: int
// #cycles: int
// #polynomialSize: int
// #points: double[][]
//
// Methods: +setPoints(double[][]): void
// +setPolySize(int): void
// +initPopulation(int): void
// +calcPopulationFitness(Chromosome[]): Chromosome[]
// +orderPopulation(Chromosome[]): Chromosome[]
// +selectParents(): Chromosome[]
// +generateOffspring(Chromosome[]): Chromosome
// +insertOffspring(Chromosome): void
// +checkForSolution(): boolean
// +printSolution(): void
// +getPopulationSize(): int
// +resetPopulation(): void
//
// ********************************************************
public class GenericGA {
protected Chromosome[] population;
protected Chromosome[] bufferPopulation;
protected int bufferPointer = 0;
protected int cycles = 0;
protected int polynomialSize;
protected double points[][];
public void setPoints(double points[][]) {
this.points = points;
}
public void setPolySize(int polynomialSize) {
this.polynomialSize = polynomialSize + 1;
}
public void initPopulation(int size) {
population = new Chromosome[size];
bufferPopulation = new Chromosome[size];
for(int i = 0; i < population.length; i++) {
population[i] = new Chromosome(polynomialSize);
for(int j = 0; j < polynomialSize; j++) {
population[i].setRandomValue(j);
}
}
population = calcPopulationFitness(population);
population = orderPopulation(population);
}
public Chromosome[] calcPopulationFitness(Chromosome[] population) {
for(int i = 0; i < population.length; i++) {
population[i].calcFitness(points);
}
return population;
}
public Chromosome[] orderPopulation(Chromosome[] population) { // Uses bubble sorting
Chromosome bufferChromosome;
int numMoves = 1;
while(numMoves != 0) {
numMoves = 0;
for(int i = 0; i < population.length - 1; i++) {
if(population[i].getFitness() < population[i+1].getFitness()) {
bufferChromosome = population[i+1];
population[i+1] = population[i];
population[i] = bufferChromosome;
numMoves = numMoves + 1;
}
}
}
return population;
}
public Chromosome[] selectParents() {
Chromosome[] parents = new Chromosome[2];
parents[0] = population[0];
parents[1] = population[1];
return parents;
}
public Chromosome generateOffspring(Chromosome[] parents) {
Chromosome offspring = new Chromosome(polynomialSize);
for(int i = 0; i < polynomialSize; i++) {
if((Math.floor(Math.random() * 10) + 1) == 1) {
offspring.setRandomValue(i);
} else {
offspring.setValue(i, (parents[0].getValue(i) + parents[1].getValue(i))/2);
}
}
return offspring;
}
public void insertOffspring(Chromosome offspring) {
}
public boolean checkForSolution() {
boolean solutionMet = false;
if(population[0].getFitness() >= 1) {
solutionMet = true;
} else if(cycles > 500000) {
solutionMet = true;
}
return solutionMet;
}
public boolean checkForSolutionTEST(double r, int cycles) { // WILL BE REMOVED FOR FINAL PRODUCT
boolean solutionMet = false;
if(r >= 1) {
solutionMet = true;
} else if(cycles > 500000) {
solutionMet = true;
}
return solutionMet;
}
public void printSolution() {
System.out.println("");
System.out.print("The solution is: ");
System.out.print(Math.round(population[0].getValue(0) * 1000.0) / 1000.0);
for(int i = 1; i < polynomialSize; i++) {
System.out.print(" + " + Math.round(population[0].getValue(i) * 1000.0) / 1000.0 + "x^" + i);
}
System.out.println("");
System.out.println("R^2 Value: " + population[0].getFitness());
System.out.println("GA reached a solution in: " + cycles + " cycles.");
}
public int getPopulationSize() {
return population.length;
}
public void resetPopulation() {
for(int i = 0; i < population.length; i++) {
population[i] = bufferPopulation[i];
}
population = calcPopulationFitness(population);
population = orderPopulation(population);
cycles++;
}
}