aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Gabor Szarnyas <szarnyasg@gmail.com>2019-08-16 19:52:20 +0200
committerLibravatar Gabor Szarnyas <szarnyasg@gmail.com>2019-08-16 19:52:20 +0200
commitf34c02f5f99583f96a5289b7f7c46c55b11a97f4 (patch)
tree6511745b5515b547770766e204768b266038ba33
parentmeasurement1 format (diff)
downloadVIATRA-Generator-f34c02f5f99583f96a5289b7f7c46c55b11a97f4.tar.gz
VIATRA-Generator-f34c02f5f99583f96a5289b7f7c46c55b11a97f4.tar.zst
VIATRA-Generator-f34c02f5f99583f96a5289b7f7c46c55b11a97f4.zip
Initial implementation of TCC1 metric
-rw-r--r--Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/metrics/TypedClusteringCoefficientMetric.xtend90
1 files changed, 90 insertions, 0 deletions
diff --git a/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/metrics/TypedClusteringCoefficientMetric.xtend b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/metrics/TypedClusteringCoefficientMetric.xtend
new file mode 100644
index 00000000..93f5ccd4
--- /dev/null
+++ b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/metrics/TypedClusteringCoefficientMetric.xtend
@@ -0,0 +1,90 @@
1package ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics
2
3import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.graph.GraphStatistic
4import java.text.DecimalFormat
5import java.util.ArrayList
6import java.util.HashMap
7import org.eclipse.emf.ecore.EObject
8
9class TypedClusteringCoefficientMetric extends Metric {
10 public static val countName = "TCCCount";
11 public static val valueName = "TCCValue";
12 val formatter = new DecimalFormat("#0.00000");
13
14 override evaluate(GraphStatistic g) {
15 //because the precision issue of double, we translate double values into String to be the key
16
17 val map = new HashMap<String, Integer>();
18 //calculate the metric distribution
19 g.allNodes.forEach[n|
20 var coef = calculateTCC1(n, g);
21
22 //format number to String
23 val value = formatter.format(coef);
24 if(!map.containsKey(value)){
25 map.put(value, 1);
26 }else{
27 map.put(value, map.get(value) + 1);
28 }
29
30 ]
31
32 //convert it into a 2 dimentional array
33 val String[][] datas = newArrayOfSize(2, map.size+1);
34 datas.get(0).set(0, valueName);
35 datas.get(1).set(0, countName)
36 var count = 1;
37 for(entry : map.entrySet.sortBy[it.key]){
38 datas.get(0).set(count, entry.key+"");
39 datas.get(1).set(count, entry.value+"");
40 count++;
41 }
42
43 return datas;
44 }
45
46 override evaluateSamples(GraphStatistic g){
47 val samples = new ArrayList<Double>();
48 //calculate the metric distribution
49 g.allNodes.forEach[
50 samples.add(calculateTCC1(it, g));
51 ]
52
53 return samples;
54 }
55
56 /**
57 * Compute TCC1 metric for node n
58 */
59 def double calculateTCC1(EObject n, GraphStatistic g){
60 var wedges = 0;
61 var triangles = 0;
62
63 for(type1 : g.allTypes){
64 val type1EdgeTargetNodes = g.outgoingEdges.get(type1).values;
65
66 // number of wedges
67 val d = type1EdgeTargetNodes.size
68 wedges += d * (d-1)
69
70 // pairs of neighbors
71 for (n1: type1EdgeTargetNodes) {
72 for (n2: type1EdgeTargetNodes) {
73 for(type2 : g.allTypes){
74 if ((type1 != type2) &&
75 (g.outgoingEdges.get(type2).containsEntry(n1, n2) ||
76 g.outgoingEdges.get(type2).containsEntry(n2, n1)
77 )) {
78 triangles++
79 }
80 }
81 }
82 }
83 }
84 if (wedges == 0.0) {
85 return 0.0
86 } else {
87 return triangles/wedges
88 }
89 }
90} \ No newline at end of file