質問編集履歴

2

ソースコード追加

2017/01/05 06:23

投稿

Yoshi-5630
Yoshi-5630

スコア11

test CHANGED
File without changes
test CHANGED
@@ -32,74 +32,448 @@
32
32
 
33
33
  で作りたいと考えていますが、類似度を測った後、類似度が高い要素、又はクラスタを**線で結ぶ**という表現をどのようにすればよいか(コードの書き方)が**わからない**という感じです。
34
34
 
35
+
36
+
37
+
38
+
39
+ このように3種類をそれぞれメソッドで実装したまでは普通なのですが、そこから全く手がついていません。
40
+
41
+
42
+
43
+
44
+
45
+ ###試したこと
46
+
47
+ 本を引っ張り出して読んで、HashMapとか文字列結合などありましたが、どれも実装できませんでした。
48
+
49
+ 単に自分がプログラミングが嫌いなので、逃げてるだけかもしれません。(このような場で言うことではないですが)
50
+
51
+
52
+
53
+ ###補足情報(言語/FW/ツール等のバージョンなど)
54
+
55
+ 上記の説明ではわからないと思いますので、パワーポイントのスライドを添付させていただきます。
56
+
57
+
58
+
59
+
60
+
61
+ 大変わかりづらい説明で恐縮でございますが、どうぞ宜しくお願いします。
62
+
63
+
64
+
65
+
66
+
67
+ [![イメージ説明](36f0fbf6d0ed736e9c61f38bae3fdced.jpeg)]
68
+
69
+
70
+
71
+
72
+
35
73
  ```
36
74
 
75
+ public class Plan1 {
76
+
77
+
78
+
79
+ public static void main(String[] args) throws IOException {
80
+
81
+
82
+
83
+ final String MODE = "CSV";
84
+
85
+ final String ClusteringMode = "Avarage";
86
+
87
+ double hreshold = 0.5d;
88
+
89
+ int clusterSize = 32;
90
+
91
+ String ontFile = "IaaS.owl";
92
+
93
+
94
+
95
+ int elemSize = 0;
96
+
97
+ HashMap<String, Integer> elemList = new HashMap<String, Integer>();
98
+
99
+ double[][] similarity = null;
100
+
101
+
102
+
103
+ OntModel m = ModelFactory.createOntologyModel();
104
+
105
+ try {
106
+
107
+ m.read(new FileInputStream("IaaS.owl"), "RDF/XML-ABBREV"); // 指定したファイルをバイナリファイルとして読み込む
108
+
109
+ } catch (FileNotFoundException e) {
110
+
111
+ e.printStackTrace();
112
+
113
+
114
+
115
+ }
116
+
117
+ HashMap<String, String> query = new HashMap<String, String>();
118
+
119
+ OntModel ontModel = loadOnt("IaaS.owl");
120
+
121
+ String mode = "NORMAL";
122
+
123
+ String[] csv = loadCSV("cluster.csv");
124
+
125
+ query.put("CPU", "");
126
+
127
+ query.put("OS", "");
128
+
129
+ query.put("Memory", "");
130
+
131
+ query.put("GHz", "");
132
+
133
+ query.put("HDD", "");
134
+
135
+ query.put("SLA", "");
136
+
137
+ query.put("RAM", "");
138
+
139
+ query.put("Hourly_Usage_Rate", "");
140
+
141
+ query.put("Monthly_Usage_Rate", "");
142
+
143
+ }
144
+
145
+
146
+
147
+ static String setClusterName(String line, OntModel m) throws IOException {
148
+
149
+ String[] elems = toArray(line);
150
+
151
+ HashMap<String, Integer> names = new HashMap<String, Integer>();
152
+
153
+ for (int i = 0; i < elems.length; i++) {
154
+
155
+ if (!elems[i].isEmpty() && !elems[i].equals("")) {
156
+
157
+ OntClass targetClass = m
158
+
159
+ .getOntClass("http://www.owl-ontologies.com/Ontology1383285788.owl#" + elems[i]);
160
+
161
+
162
+
163
+ @SuppressWarnings("unchecked")
164
+
165
+ Iterator<OntClass> superClasses = targetClass.listSuperClasses(true);
166
+
167
+ while (superClasses.hasNext()) {
168
+
169
+ OntClass superClass = superClasses.next();
170
+
171
+ if ((getString(superClass).equals("CPU") || getString(superClass).equals("Memory")
172
+
173
+ || getString(superClass).equals("GHz") || getString(superClass).equals("HDD")
174
+
175
+ || getString(superClass).equals("SLA") || getString(superClass).equals("RAM")
176
+
177
+ || getString(superClass).equals("Hourly_Usage_Rate")
178
+
179
+ || getString(superClass).equals("Hourly_Usage_Rate"))
180
+
181
+ && !names.containsKey(getString(superClass))) {
182
+
183
+ names.put(getString(superClass), 1);
184
+
185
+ } else if ((getString(superClass).equals("OS") || getString(superClass.getSuperClass()).equals("OS")
186
+
187
+ || getString(superClass.getSuperClass().getSuperClass()).equals("OS"))
188
+
189
+ && !names.containsKey(getString(superClass))) {
190
+
191
+ names.put("OS", 1);
192
+
193
+ }
194
+
195
+ }
196
+
197
+ }
198
+
199
+ }
200
+
201
+ line += ":";
202
+
203
+ Iterator<String> name = names.keySet().iterator();
204
+
205
+ while (name.hasNext()) {
206
+
207
+ String key = name.next();
208
+
209
+ line += key + ",";
210
+
211
+ }
212
+
213
+ return line.substring(0, line.length() - 1);
214
+
215
+ }
216
+
217
+
218
+
219
+ static double[][] setSimilarity(double[][] similarity) {// similarityの計算方法
220
+
221
+
222
+
223
+ System.out.println("---- Set Similarity ----");
224
+
225
+ for (int i = 0; i < similarity.length; i++) {
226
+
227
+ for (int j = i + 1; j < similarity.length; j++) {
228
+
229
+ similarity[i][j] = Math.random() + 0.1 * Math.random();
230
+
231
+ if (similarity[i][j] == 0)
232
+
233
+ similarity[i][j] = 0.001;
234
+
235
+ similarity[j][i] = similarity[i][j];
236
+
237
+ System.out.println(i + "-" + j + ":" + similarity[i][j]);
238
+
239
+ }
240
+
241
+ }
242
+
243
+ return similarity; // 計算結果をsimilarityに返す
244
+
245
+ }
246
+
247
+
248
+
249
+ @SuppressWarnings("unused")
250
+
37
- static void Elem_And_Elem(ArrayList<Integer>[] cluster, double[][] similarity, int elemSize, int currentNum)
251
+ static void Elem_And_Elem(ArrayList<Integer>[] cluster, double[][] similarity, int elemSize, int currentNum)
38
252
 
39
253
  throws IOException {
40
254
 
41
255
 
42
256
 
257
+ int a_min = 0;
258
+
259
+ int b_min = 0;
260
+
261
+ double sim_min = 1.0d;
262
+
263
+ double sim_max = 0.0d;
264
+
265
+
266
+
267
+ for (int i = 0; i < cluster[currentNum].size(); i++) {
268
+
269
+ int num1 = cluster[currentNum].get(i);
270
+
271
+ for (int j = 0; j < cluster[currentNum].size(); j++) {
272
+
273
+ int num2 = cluster[currentNum].get(j);
274
+
275
+ if (similarity[num1][num2] < sim_min && num1 != num2) {
276
+
277
+ if (cluster[currentNum * 2].isEmpty() && cluster[currentNum * 2 + 1].isEmpty()) {
278
+
279
+ a_min = num1;
280
+
281
+ b_min = num2;
282
+
283
+ sim_min = similarity[num1][num2];
284
+
285
+ } else if (!cluster[currentNum * 2].contains(num1) && !cluster[currentNum * 2 + 1].contains(num2)) {
286
+
287
+ a_min = num1;
288
+
289
+ b_min = num2;
290
+
291
+ sim_min = similarity[num1][num2];
292
+
293
+ }
294
+
295
+ }
296
+
297
+ }
298
+
299
+ if (sim_min == 1.0d)
300
+
301
+ break;
302
+
303
+ }
304
+
305
+ }
306
+
307
+
308
+
309
+ static void Cluster_And_Elem(ArrayList<Integer>[] cluster, double[][] similarity, int elemSize, int currentNum) throws IOException {
310
+
311
+
312
+
313
+ @SuppressWarnings("unchecked") // ClusteringTest.javaより引用
314
+
315
+ static void Cluster_And_Cluster(double[][] similarity, int elemSize, int clusterSize,
316
+
317
+ HashMap<String, Integer> elemList, String ontFile, OntModel m) throws IOException {
318
+
319
+
320
+
321
+ ArrayList<Integer>[] cluster = new ArrayList[clusterSize * 2];
322
+
323
+ // ArrayList
324
+
325
+ for (int i = 0; i < clusterSize * 2; i++) { // 0からクラスタサイズの2倍作成を繰り返す
326
+
327
+ cluster[i] = new ArrayList<Integer>();
328
+
329
+ }
330
+
331
+
332
+
333
+
334
+
335
+ for (int i = 0; i < elemSize; i++) {
336
+
337
+ cluster[1].add(i);
338
+
339
+ }
340
+
341
+
342
+
343
+ cluster = ClusteringProccess(cluster, similarity, elemSize, 1); //
344
+
345
+
346
+
347
+ // クラスタリングの結果
348
+
349
+ System.out.println("---- Clustering Result ----");
350
+
351
+ for (int i = 0; i < cluster.length; i++) {
352
+
353
+ System.out.print("Cluster[" + i + "]:");
354
+
355
+ for (int j = 0; j < cluster[i].size(); j++) {
356
+
357
+ System.out.print(cluster[i].get(j) + " ");
358
+
359
+ }
360
+
361
+ System.out.println("");
362
+
363
+ return;
364
+
365
+ }
366
+
367
+ }
368
+
369
+
370
+
371
+ private static ArrayList<Integer>[] ClusteringProccess(ArrayList<Integer>[] cluster, double[][] similarity,
372
+
373
+ int elemSize, int i) throws IOException {
374
+
375
+ // TODO 自動生成されたメソッド・スタブ
376
+
377
+ return null;
378
+
379
+ }
380
+
381
+
382
+
383
+ @SuppressWarnings("unused")
384
+
385
+ private static String[] toArray(String line) throws IOException {
386
+
387
+ return line.split(":")[1].split(",");
388
+
389
+ }
390
+
391
+
392
+
393
+ /**
394
+
395
+ *
396
+
397
+ * @param rdfNode
398
+
399
+ * @return
400
+
401
+ */
402
+
403
+ @SuppressWarnings("unused")
404
+
405
+ private static String getString(RDFNode rdfNode) {
406
+
407
+ return rdfNode.toString().substring(rdfNode.toString().indexOf("#") + 1, rdfNode.toString().length());
408
+
409
+ }
410
+
411
+
412
+
413
+
414
+
415
+ private static String[] loadCSV(String file) throws IOException {
416
+
417
+ ArrayList<String> csv = new ArrayList<String>();
418
+
419
+ FileReader fr;
420
+
421
+ try {
422
+
423
+ fr = new FileReader(file);
424
+
425
+ BufferedReader br = new BufferedReader(fr);
426
+
427
+ String line;
428
+
429
+ while ((line = br.readLine()) != null) {
430
+
431
+ csv.add(line);
432
+
433
+ }
434
+
435
+ } catch (FileNotFoundException e) {
436
+
437
+ e.printStackTrace();
438
+
439
+ }
440
+
441
+ return (String[]) csv.toArray(new String[0]);
442
+
443
+ }
444
+
445
+
446
+
447
+
448
+
449
+ private static OntModel loadOnt(String file) {
450
+
451
+ OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
452
+
453
+
454
+
455
+ try {
456
+
457
+ ontModel.read(new FileInputStream(file), "RDF/XML-ABBREV");
458
+
459
+ } catch (FileNotFoundException ex) {
460
+
461
+ ex.printStackTrace();
462
+
463
+ }
464
+
465
+ return ontModel;
466
+
467
+ }
468
+
43
469
  }
44
470
 
45
471
 
46
472
 
473
+
474
+
47
475
  ```
48
476
 
49
477
 
50
478
 
51
- ```
52
-
53
- static void Cluster_And_Elem(ArrayList<Integer>[] cluster, double[][] similarity, int elemSize, int currentNum)
54
-
55
- throws IOException {
479
+ ※ソースコードの全体を載せます。
56
-
57
- }
58
-
59
- ```
60
-
61
-
62
-
63
- ```
64
-
65
- Static void Cluster_And_Cluster(ArrayList<Integer>[] cluster, double[][] similarity, int elemSize, int currentNum)
66
-
67
- throws IOException {
68
-
69
-
70
-
71
- ```
72
-
73
-
74
-
75
-
76
-
77
- このように3種類をそれぞれメソッドで実装したまでは普通なのですが、そこから全く手がついていません。
78
-
79
-
80
-
81
-
82
-
83
- ###試したこと
84
-
85
- 本を引っ張り出して読んで、HashMapとか文字列結合などありましたが、どれも実装できませんでした。
86
-
87
- 単に自分がプログラミングが嫌いなので、逃げてるだけかもしれません。(このような場で言うことではないですが)
88
-
89
-
90
-
91
- ###補足情報(言語/FW/ツール等のバージョンなど)
92
-
93
- 上記の説明ではわからないと思いますので、パワーポイントのスライドを添付させていただきます。
94
-
95
-
96
-
97
-
98
-
99
- 大変わかりづらい説明で恐縮でございますが、どうぞ宜しくお願いします。
100
-
101
-
102
-
103
-
104
-
105
- [![イメージ説明](36f0fbf6d0ed736e9c61f38bae3fdced.jpeg)]

1

加筆

2017/01/05 06:23

投稿

Yoshi-5630
Yoshi-5630

スコア11

test CHANGED
File without changes
test CHANGED
@@ -86,10 +86,20 @@
86
86
 
87
87
  単に自分がプログラミングが嫌いなので、逃げてるだけかもしれません。(このような場で言うことではないですが)
88
88
 
89
+
90
+
89
91
  ###補足情報(言語/FW/ツール等のバージョンなど)
90
92
 
91
93
  上記の説明ではわからないと思いますので、パワーポイントのスライドを添付させていただきます。
92
94
 
93
95
 
94
96
 
97
+
98
+
99
+ 大変わかりづらい説明で恐縮でございますが、どうぞ宜しくお願いします。
100
+
101
+
102
+
103
+
104
+
95
105
  [![イメージ説明](36f0fbf6d0ed736e9c61f38bae3fdced.jpeg)]