-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
133 lines (96 loc) · 3.34 KB
/
Controller.java
File metadata and controls
133 lines (96 loc) · 3.34 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
// ********************************************************
// Class: CS225
// Name: Lucien Hammond
// Date: 12/2/22
//
// Purpose: Is in charge of loading solution points from a
// file, user Input, and containing the main method
//
//
// Attributes: -points: double[][]
// -GAs: GenericGA[]
// -polynomialSize: int
//
// Methods: +loadGAs(): void
// +loadPoints(): void
// +userPolySize(): void
// +run(int, double[][]): void
// +main(): void
//
// ********************************************************
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
public class Controller {
private double[][] points;
private GenericGA[] GAs = new GenericGA[4];
int polynomialSize;
public void loadGAs() {
GAs[0] = new GenerationalGA();
GAs[1] = new SteadyGA();
GAs[2] = new SteadyGenGA();
GAs[3] = new MuPlusMuGA();
}
public void loadPoints() {
File file = new File("points.csv");
try {
FileReader fr = new FileReader(file);
BufferedReader counter = new BufferedReader(fr);
String line;
int lineNumber = 0;
int count = 0;
while ((line = counter.readLine()) != null) {
count++;
}
points = new double[count][2];
counter.close();
FileReader fileRead = new FileReader(file);
BufferedReader br = new BufferedReader(fileRead);
while ((line = br.readLine()) != null) {
String[] row;
row = line.split(",");
points[lineNumber][0] = Double.parseDouble(row[0]);
points[lineNumber][1] = Double.parseDouble(row[1]);
lineNumber = lineNumber + 1;
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void userPolySize() { //
Scanner input = new Scanner(System.in);
System.out.print("Enter Highest Polynomial Power: ");
try {
polynomialSize = Integer.parseInt(input.nextLine());
} catch(Exception e) {
System.out.println("INVALID INPUT");
polynomialSize = 2;
}
System.out.println("");
input.close();
}
public void run(int GA_ID, double points[][]) {
GAs[GA_ID].setPolySize(polynomialSize);
GAs[GA_ID].setPoints(points);
GAs[GA_ID].initPopulation(10);
while(GAs[GA_ID].checkForSolution() == false) {
for(int i = 0; i < GAs[GA_ID].getPopulationSize(); i++) {
GAs[GA_ID].insertOffspring(GAs[GA_ID].generateOffspring(GAs[GA_ID].selectParents()));
}
GAs[GA_ID].resetPopulation();
}
GAs[GA_ID].printSolution();
}
public static void main(String[] args) {
Controller controller = new Controller();
controller.userPolySize();
controller.loadGAs();
controller.loadPoints();
controller.run(0,controller.points);
controller.run(1,controller.points);
controller.run(2, controller.points);
controller.run(3, controller.points);
}
}