-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteadyGA.java
More file actions
55 lines (46 loc) · 1.79 KB
/
SteadyGA.java
File metadata and controls
55 lines (46 loc) · 1.79 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
// ********************************************************
// Class: CS225
// Name: Lucien Hammond
// Date: 12/2/22
//
// Purpose: To have all overloaded methods of the steady state
// genetic algorithm which are used to control the evolutionary
// process of this genetic algorithm type
//
//
// Attributes: none
//
// Methods: +selectParents(): Chromosome[]
// +insertOffspring(Chromosome): void
// +resetPopulation(): void
// +printSolution(): void
//
// ********************************************************
public class SteadyGA extends GenericGA {
public Chromosome[] selectParents() {
Chromosome[] parents = new Chromosome[2];
parents[0] = population[(int) Math.floor(Math.random() * (population.length))];
parents[1] = population[(int) Math.floor(Math.random() * (population.length))];
return parents;
}
public void insertOffspring(Chromosome offspring) {
population[population.length - 1] = offspring;
}
public void resetPopulation() {
population = calcPopulationFitness(population);
population = orderPopulation(population);
cycles++;
}
public void printSolution() {
System.out.println("");
System.out.println("Steady-State GA:");
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("Steady-State GA reached a solution in: " + cycles + " cycles.");
}
}