-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunFSIPSO.java
More file actions
228 lines (188 loc) · 6.34 KB
/
Copy pathRunFSIPSO.java
File metadata and controls
228 lines (188 loc) · 6.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Copyright (c) 2023, An-Da Li. All rights reserved.
* Please read LICENCE for license terms.
* Coded by An-Da Li
* Email: andali1989@163.com
*
* Li, A.-D., Xue, B., & Zhang, M. (2021). A Forward Search Inspired Particle Swarm Optimization Algorithm
* for Feature Selection in Classification. IEEE Congress on Evolutionary Computation, CEC 2021, Kraków,
* Poland, June 28 - July 1, 2021, 786–793. https://doi.org/10.1109/CEC45853.2021.9504949
*
*/
package fs;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import fs.pso.PSO;
import fs.pso.SubCPSOMU2;
import fs.eval.ModiWrapperSubsetEval;
import fs.utils.Basicfunc;
import fs.utils.Matcd;
import fs.utils.Wkc;
import weka.attributeSelection.ASEvaluation;
import weka.core.Instances;
public class RunFSIPSO {
static int popNum = 30;
static int iterTime = 100;
static String classifierName = "KNN";
static int knnNum = 5;
static int objtype = 0; // 0 denotes the acc and number
static int seed = 1;
static int innerfold = 5;
static boolean ifnorm = true; // using standardization
static int step = 20;
static int retime = 30; //repetition time
public static void main(String[] args) throws Exception {
// run FSIPSO
weka.core.Instances data = null;
int foldop = -1; // when reading the data do not stratify
String filpath = "./data/wbcd/wbcd.arff";
data = Basicfunc.readData(filpath, seed, ifnorm, foldop);
// divide the data into trainset and testset where 30% percent is the testset
Instances[] data2 = Basicfunc.divData(data, 3, 10, innerfold);
/**
* initialize the PSOs
*/
Wkc wkc = new Wkc(classifierName);
if (classifierName.equals("KNN")) {
wkc.classifier.setOptions(new String[] { "-K", String.valueOf(knnNum) });
}
// define and set evaluator
ASEvaluation subsetEval;
subsetEval = new ModiWrapperSubsetEval();
((ModiWrapperSubsetEval) subsetEval).setClassifier(wkc.classifier);
PSO pso = null;
pso = new SubCPSOMU2(data2[0], wkc, popNum, iterTime, step);
((SubCPSOMU2) pso).iniMethod = "acc";
((SubCPSOMU2) pso).vThred = 0.6;
subsetEval.buildEvaluator(data2[0]);
pso.setEvaluator(subsetEval);
long rnd = 1;
double[][] re = new double[retime][7];
double[][] info = new double[retime][iterTime + 1];
for (int i = 0; i < retime; i++) {
pso.setSeed(rnd);
pso.run(); // get the solutions
getTestResult(i, re, info, pso, data2[0], data2[1], rnd);
}
// Output info
try {
String outPath = "re.csv"; // output name;
// File file = new File(outPath);
OutputStream fop = new FileOutputStream(outPath);
OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");
writeInfo("data", writer, re, info);
writer.flush();
writer.close();
fop.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
}
public static void getTestResult(int i, double[][] re, double[][] iterInfo, PSO pso, Instances trainset,
Instances testset, long rnd) throws Exception {
Wkc classifier = new Wkc(new weka.classifiers.lazy.IBk(knnNum));
classifier.setTtdata(trainset, testset, pso.gbest());
double[] tempacc = classifier.run();
re[i][0] = (int) i + 1.0;
re[i][1] = tempacc[tempacc.length - 1];
// train accuracy on training data
tempacc = classifier.getTrainacc();
re[i][2] = tempacc[tempacc.length - 1];
re[i][3] = pso.getNum(pso.gbest());
re[i][4] = pso.gbestFit();
re[i][5] = pso.runtime();
re[i][6] = (double) rnd;
iterInfo[i] = Matcd.mergeVector(new double[] { i + 1 }, pso.iterInfo());
System.out.printf("Testing Accuray=%f, Training Accuracy=%f, numberSelected=%f\r\n", re[i][1], re[i][2],
re[i][3]);
}
public static void writeInfo(String methodname, OutputStreamWriter writer, double[][] result, double[][] iterInfo)
throws IOException, Exception {
/**
* write the results of PSO
*/
writer.append(methodname + "\r\n");
writer.append("repeat, testacc, trainacc, featurenum, fitness,runtime, rndseed\r\n");
writefile(result, writer);
writer.append("Avg.,");
writefile(Matcd.meanMatrix(result, 1), writer, true);
writer.append("Std.,");
writefile(Matcd.stdMatrix(result, 0, 1), writer, true);
writer.append("\r\n\r\n");
/**
* iteration info
*/
// PSO
writer.append("iteration info of " + methodname + "\r\n");
writefile(iterInfo, writer);
writer.append("iterAvg.,");
writefile(Matcd.meanMatrix(iterInfo, 1), writer, true);
writer.append("\r\n\r\n");
writer.flush();
}
@SuppressWarnings("unused")
private static void writefile(double[] data, OutputStreamWriter writer) throws IOException {
// write a row to the file
int J = data.length;
DecimalFormat df = new DecimalFormat("#.############");
String value;
for (int j = 0; j < J - 1; j++) {
value = df.format(data[j]);
writer.append(value + ",");
}
value = df.format(data[J - 1]);
writer.append(value + "\r\n");
writer.flush();
}
@SuppressWarnings("unused")
private static void writefile(int[] data, OutputStreamWriter writer) throws IOException {
// write a row to the file
int J = data.length;
DecimalFormat df = new DecimalFormat("#.############");
String value;
for (int j = 0; j < J - 1; j++) {
value = df.format(data[j]);
writer.append(value + ",");
}
value = df.format(data[J - 1]);
writer.append(value + "\r\n");
writer.flush();
}
private static void writefile(double[] data, OutputStreamWriter writer, boolean first) throws IOException {
// write a row to the file
// if first == true then do not write the first one;
int J = data.length;
DecimalFormat df = new DecimalFormat("#.############");
String value;
int start = 0;
if (first = true) {
start = 1;
}
for (int j = start; j < J - 1; j++) {
value = df.format(data[j]);
writer.append(value + ",");
}
value = df.format(data[J - 1]);
writer.append(value + "\r\n");
writer.flush();
}
private static void writefile(double[][] data, OutputStreamWriter writer) throws IOException {
int I = data.length;
int J = data[0].length;
DecimalFormat df = new DecimalFormat("#.############");
String value;
for (int i = 0; i < I; i++) {
for (int j = 0; j < J - 1; j++) {
value = df.format(data[i][j]);
writer.append(value + ",");
}
value = df.format(data[i][J - 1]);
writer.append(value + "\r\n");
writer.flush();
}
}
}