-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKohonen.java
185 lines (166 loc) · 5.47 KB
/
Kohonen.java
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
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.util.ArrayList;
/**
* This class is the driver class of our project.
*
* @author Michail Panagiotis Bofos
*
*/
public class Kohonen {
private static boolean PYPLOT = true, LVQ = false;
private static int DIMENSION;
private static int ITERATIONS;
private static int INPUTS;
private static int TEST_LINES, TRAIN_LINES;
private static double RATE;
private static String dst_file;
private static String src_file;
private static String train_file;
private static String test_file;
private static ArrayList<double[]> train_inputs = new ArrayList<>();
private static ArrayList<Character> train_outputs = new ArrayList<>();
private static ArrayList<double[]> test_inputs = new ArrayList<>();
private static ArrayList<Character> test_outputs = new ArrayList<>();
/**
* This function labels each node based on the minimum distance of the testing
* set.
*
* @param m The SOM we use
*/
private static void labelData(Map m) {
System.out.println("Labeling . . .");
for (Node[] ar : m.matrix)
for (Node n : ar) {
double min = Double.MAX_VALUE;
int place = -1;
for (int i = 0; i < TEST_LINES; i++) {
double dist = 0;
for (int a = 0; a < test_inputs.get(i).length; a++)
dist += Math.pow(test_inputs.get(i)[a] - n.weights[a], 2);
if (dist <= min) {
min = dist;
place = i;
}
}
n.setLabel(test_outputs.get(place));
}
}
/**
* This function selects the parameters given by the input file.
*
* @param list ArrayList(String) all the parameters
*/
public static void handleParameters(ArrayList<String> list) {
DIMENSION = Integer.parseInt(list.get(0));
ITERATIONS = Integer.parseInt(list.get(1));
RATE = Double.parseDouble(list.get(2));
INPUTS = Integer.parseInt(list.get(3));
src_file = new String(list.get(4));
dst_file = new String(list.get(5));
train_file = new String(list.get(6));
test_file = new String(list.get(7));
PYPLOT = Boolean.parseBoolean(list.get(8));
LVQ = Boolean.parseBoolean(list.get(9));
Tools.createTrainAndTestSets(src_file);
printArguments();
TRAIN_LINES = Tools.findLines(train_file);
for (int i = 0; i < TRAIN_LINES; i++)
train_inputs.add(new double[INPUTS]);
Tools.fillData(train_file, train_inputs, train_outputs, INPUTS);
/*******************************************************/
TEST_LINES = Tools.findLines(test_file);
for (int i = 0; i < TEST_LINES; i++)
test_inputs.add(new double[INPUTS]);
Tools.fillData(test_file, test_inputs, test_outputs, INPUTS);
/*******************************************************/
Tools.deleteFile("results.txt");
Tools.deleteFile("cluster.txt");
}
/**
* This function prints some of the arguments given.
*/
public static void printArguments() {
System.out.println("Kohonen Map: " + DIMENSION + "x" + DIMENSION);
System.out.println("Iterations: " + ITERATIONS);
}
/**
* This function creates the cluster visualization using pyplot if selected and
* a txt file with the clusters by default.
*
* @param m The SOM we use
* @param b True if we create the LVQ cluster too
*/
public static void createClusterFile(Map m, boolean b) {
ArrayList<String> cluster = new ArrayList<>();
for (Node[] ar : m.matrix)
for (Node n : ar)
cluster.add(new String(n.getLabel() + " " + n.getX() + " " + n.getY()));
String dump = new String();
if (b) {
Tools.writeFile(dst_file, cluster);
if (PYPLOT)
Tools.runPython("CreateCluster.py", dst_file, true);
dump = "data_cluster.txt";
} else {
String lvq = "LVQ_";
String newS = lvq.concat(dst_file);
Tools.writeFile(newS, cluster);
if (PYPLOT)
Tools.runPython("CreateCluster.py", newS, false);
dump = "LVQ_data_cluster.txt";
}
Tools.deleteFile(dump);
cluster = new ArrayList<>();
for (int j = m.matrix[0].length - 1; j >= 0; j--) {
for (int i = 0; i < m.matrix.length; i++)
Tools.appendToFile(dump, new String(m.matrix[i][j].getLabel() + ""), false);
Tools.appendToFile(dump, new String(""), true);
}
}
/**
* Main function of your project.
*
* @param args The parameters file we use
*/
public static void main(String[] args) {
String filename = "parameters.txt";
if (args.length >= 1)
filename = args[0];
ArrayList<String> list = Tools.getParameters(filename);
handleParameters(list);
Map m = new Map(DIMENSION, INPUTS, ITERATIONS, RATE);
m.printDetails();
ArrayList<String> results = new ArrayList<>();
for (int epochs = 0; epochs < ITERATIONS; epochs++) {
System.out.println("Epoch: " + (epochs + 1));
/*******************************************************/
for (int inLine = 0; inLine < TRAIN_LINES; inLine++) {
m.enterNewInput(train_inputs.get(inLine));
Node winner = m.findWinner();
m.addTrainError(winner.getDistance());
m.updateWeights(winner);
}
m.updateSigma(epochs);
m.updateRate(epochs);
for (int inLine = 0; inLine < TEST_LINES; inLine++) {
m.enterNewInput(test_inputs.get(inLine));
Node winner = m.findWinner();
m.addTestError(winner.getDistance());
}
results.add(
new String(epochs + " " + m.getTrainningError(TRAIN_LINES) + " " + m.getTestingError(TEST_LINES)));
m.resetErrors();
}
Tools.writeFile("results.txt", results);
labelData(m);
createClusterFile(m, true);
if (LVQ) {
System.out.println("LVQ enabled:");
m.LVQ(train_inputs, train_outputs);
labelData(m);
createClusterFile(m, false);
}
if (PYPLOT)
Tools.runPython("CreateErrorPlot.py", "results.txt", false);
System.out.println("Process Ended");
}
}