-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEvaluationMetric.java
90 lines (76 loc) · 4.38 KB
/
EvaluationMetric.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
package org.aksw.simba.quetsal.core.error;
import java.util.*;
/**
* This class provides methods pertaining to the accuracy of the cardinality estimators of the cost/cardinality-based engines. In particular, it contains methods to calculate
* the relative error, q-error, and cosine similarity error of the individual triple patterns, joins, and the complete query execution plan generated by the uderlying query processing engines.
*
*/
public class EvaluationMetric {
/**
* This function returns the q-error corresponding to two vectors in arguments: 1. vector of actual cardinalities, 2. vector of estimated cardinalities. Note the ordering in both list is important, i.e., the ith element in both vectors corresponds to each other: real cardinality vs. corresponding estimated cardinaltiy.
* @param vecActualcard This vector contains values of actual cardinalities in a query plan (Joins or TPs or Both)
* @param vecEstimatedCard This vector contains values of estimated cardinalities in a query plan (Joins or TPs or Both)
* @return q-error value
* @throws IllegalArgumentException
*/
public double getQerror (Vector vecActualcard, Vector vecEstimatedCard) throws IllegalArgumentException {
//check if both vectors are not equal
if(vecActualcard.size()!=vecEstimatedCard.size()){
throw new IllegalArgumentException ("Invalid Parameters: Please provide both vectors " +
"estimated and real cardinality (should be equal in size)");
}
//maximum value
double max = 0;
//calculate q-error
// Max(est/real) if est> real
// Max (real/est) if real >est
for (int i=0; i < vecEstimatedCard.size(); i++){
double err = 0;
if((Integer) vecEstimatedCard.get(i) < (Integer) vecActualcard.get(i)){
err = (Integer) vecActualcard.get(i) / (Integer) vecEstimatedCard.get(i);
}else {
err = (Integer) vecEstimatedCard.get(i) / (Integer) vecActualcard.get(i);
}
if (err > max){
max = err;
}
}
return max;
}
/**
* This method is for calculating the cosine similarity of query plan. It also expect two vectors 1. vector of actual cardinalities of all joins and triple patterns in the query plan, 2. vector of estimated cardinalities of all joins and triple patterns in the query plan. Note the ordering in both list is important, i.e., the ith element in both vectors corresponds to each other: real cardinality vs. corresponding estimated cardinaltiy.
* @param vecActualcard This vector contains values of actual cardinalities in a query plan (Joins or TPs or Both)
* @param vecEstimatedCard This vector contains values of estimated cardinalities in a query plan (Joins or TPs or Both)
* @return cosine similarity error value
* @throws IllegalArgumentException
*/
public double getCosSimError(Vector vecActualcard, Vector vecEstimatedCard) throws IllegalArgumentException {
//check if both vectors are not equal
if(vecActualcard.size()!=vecEstimatedCard.size()){
throw new IllegalArgumentException ("Invalid Parameters: Please provide both vectors " +
"estimated and real cardinality (should be equal in size)");
}
double result = 0;
long ab=0;
double a_norm =0;
double b_norm =0;
for(int i=0;i < vecEstimatedCard.size(); i++){
ab += ((Integer)vecActualcard.get(i))* ((Integer)vecEstimatedCard.get(i));
a_norm += Math.pow((Integer)vecActualcard.get(i),2);
b_norm += Math.pow((Integer)vecEstimatedCard.get(i),2);
}
a_norm = Math.sqrt(a_norm);
b_norm = Math.sqrt(b_norm);
result = ab / (a_norm*b_norm);
return result;
}
/**
* This method is for calculating relative error between estimated and real cardinality
* @param actualCard: Actual number of triples as a result of JOINs or triple patern query to endpoint.
* @param estCard: Estimated number of triples as a result
* @return relative error between actual and estimated results
*/
protected double getRelativeError(int actualCard, int estCard){
return (actualCard-estCard)/estCard;
}
}