-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGPConfig.java
More file actions
143 lines (111 loc) · 3.16 KB
/
GPConfig.java
File metadata and controls
143 lines (111 loc) · 3.16 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
package gp_project;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class GPConfig {
private static GPConfig instance = null;
private static List<TrainingDataPair> m_trainingData;
public static GPConfig getInstance() {
if (instance == null) {
instance = new GPConfig();
}
return instance;
}
private Random m_rnd;
private Settings m_settings;
protected GPConfig() {
m_rnd = new Random(System.currentTimeMillis());
m_trainingData = parseTrainingData();
m_settings = new Settings();
}
public double getCrossoverPercent() {
return m_settings.getCrossoverPercent();
}
public double getEliminatePercent() {
return m_settings.getEliminatePercent();
}
public int getMaxDepth() {
return m_settings.getMaxDepth();
}
public double getMutatePercent() {
return m_settings.getMutatePercent();
}
public Random getRand() {
return m_rnd;
}
public String getRandOperand() {
if (m_rnd != null) {
List<String> operands = m_settings.getOperands();
int numoperands = operands.size();
int index = m_rnd.nextInt(numoperands);
if (index < 0 || index >= numoperands) {
throw new IllegalArgumentException("INVALID");
}
return operands.get(index);
} else {
throw new RuntimeException(
"Internal error: random number seed not initialized");
}
}
public String getRandOperator() {
if (m_rnd != null) {
List<String> operators = m_settings.getOperators();
int numOperators = operators.size();
int index = m_rnd.nextInt(numOperators);
if (index < 0 || index >= numOperators) {
throw new IllegalArgumentException("INVALID");
}
String oper = operators.get(index);
return oper;
} else {
throw new RuntimeException(
"Internal error: random number seed not initialized");
}
}
public String getRandOperatorOrOperand() {
int flipCoin = m_rnd.nextInt(2);
if (flipCoin == 1) {
return getRandOperand();
} else {
return getRandOperator();
}
}
public int getSizePopulation() {
return m_settings.getPopulationSize();
}
public List<TrainingDataPair> getTrainingData() {
return m_trainingData;
}
private List<TrainingDataPair> parseTrainingData() {
List<TrainingDataPair> list = new LinkedList<TrainingDataPair>();
String line = null;
BufferedReader reader;
try {
// Print out for debugging in case we can't find the training data
// file
// System.out.println("Working Directory = " +
// System.getProperty("user.dir"));
reader = new BufferedReader(new FileReader("data.txt"));
while ((line = reader.readLine()) != null) {
TrainingDataPair pair = new TrainingDataPair();
String[] parts = line.split("\\s");
if (parts.length != 2) {
reader.close();
throw new RuntimeException(
"Unexpected characters found in training data file");
}
pair.setxValue(Double.parseDouble(parts[0]));
pair.setyValue(Double.parseDouble(parts[1]));
list.add(pair);
}
reader.close();
} catch (IOException ex) {
System.out.println(ex);
System.out.println("Exception during training data file read");
}
return list;
}
}