-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSettings.java
More file actions
172 lines (132 loc) · 4.13 KB
/
Settings.java
File metadata and controls
172 lines (132 loc) · 4.13 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
168
169
170
171
172
package gp_project;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class Settings {
private static String crossoverString = "CROSS_PERCENT";
private static String eliminateString = "ELIMINATE_PERCENT";
private static String maxDepthString = "MAX_DEPTH";
private static String mutateString = "MUTATE_PERCENT";
private static String operandString = "OPERANDS";
private static String operatorString = "OPERATORS";
private static String populationString = "POPULATION_SIZE";
private double crossoverPercent = 0.3;
private double eliminatePercent = 0.2;
private int maxDepth = 3;
private double mutatePercent = 0.2;
private List<String> operands = new LinkedList<String>(Arrays.asList("x",
"1", "2"));
private List<String> operators = new LinkedList<String>(Arrays.asList("-",
"/", "*"));
private int populationSize = 100;
public Settings() {
parseSettings();
}
public double getCrossoverPercent() {
return crossoverPercent;
}
public double getEliminatePercent() {
return eliminatePercent;
}
public int getMaxDepth() {
return maxDepth;
}
public double getMutatePercent() {
return mutatePercent;
}
public List<String> getOperands() {
return operands;
}
public List<String> getOperators() {
return operators;
}
public int getPopulationSize() {
return populationSize;
}
private void parseOperands(String[] parts) {
int numEntries = parts.length;
if (parts[0].equals(operandString)) {
operands.clear(); // clear out the default values, since there is an
// entry to read from the config file
} else {
throw new RuntimeException("Error while parsing settings file");
}
for (int i = 1; i < numEntries; i++) {
operands.add(parts[i]);
}
}
private void parseOperators(String[] parts) {
int numEntries = parts.length;
if (parts[0].equals(operatorString)) {
operators.clear(); // clear out the default values, since there is
// an entry to read from the config file
} else {
throw new RuntimeException("Error while parsing settings file");
}
for (int i = 1; i < numEntries; i++) {
operators.add(parts[i]);
}
}
public void parseSettings() {
String line = null;
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("settings.txt"));
while ((line = reader.readLine()) != null) {
String[] parts = line.split("\\s");
if (parts.length < 2) {
reader.close();
throw new RuntimeException(
"Unexpected characters found in training data file");
}
if (parts[0].equals(mutateString)) {
mutatePercent = Double.parseDouble(parts[1]);
} else if (parts[0].equals(crossoverString)) {
crossoverPercent = Double.parseDouble(parts[1]);
} else if (parts[0].equals(eliminateString)) {
eliminatePercent = Double.parseDouble(parts[1]);
} else if (parts[0].equals(populationString)) {
populationSize = Integer.parseInt(parts[1]);
} else if (parts[0].equals(maxDepthString)) {
maxDepth = Integer.parseInt(parts[1]);
} else if (parts[0].equals(operatorString)) {
parseOperators(parts);
} else if (parts[0].equals(operandString)) {
parseOperands(parts);
} else {
reader.close();
throw new RuntimeException(
"Unknown keyword found in settings file");
}
}
reader.close();
} catch (IOException ex) {
System.out.println(ex);
System.out.println("Exception during settings file read");
}
}
public void setCrossoverPercent(double crossoverPercent) {
this.crossoverPercent = crossoverPercent;
}
public void setEliminatePercent(double eliminatePercent) {
this.eliminatePercent = eliminatePercent;
}
public void setMaxDepth(int maxDepth) {
this.maxDepth = maxDepth;
}
public void setMutatePercent(double mutatePercent) {
this.mutatePercent = mutatePercent;
}
public void setOperands(List<String> operands) {
this.operands = operands;
}
public void setOperators(List<String> operators) {
this.operators = operators;
}
public void setPopulationSize(int populationSize) {
this.populationSize = populationSize;
}
}