-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.py
More file actions
192 lines (148 loc) · 5.24 KB
/
Copy pathUtility.py
File metadata and controls
192 lines (148 loc) · 5.24 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
'''
Created on Jan 16, 2015
@author: zhaojie
'''
#import numpy
#from numpy import array
#from scipy.cluster.vq import vq, kmeans, whiten, kmeans2
from Resources import Resources
import math
class Utility(object):
'''
classdocs
'''
@staticmethod
def sign(num):
if num == 0:
return 0
elif num > 0:
return 1
else:
return -1
@staticmethod
def compareTo(str1, str2):
if str1 == str2:
return 0
elif len(str1) < len(str2):
return -1
elif len(str1) > len(str2):
return 1
else:
if str1 < str2:
return -1
else:
return 1
@classmethod
def calEntropyOfWorkload(cls, apps, vectorQuantinationNum):
resVectorList = []
for app in apps:
resourceRequest = app.getCurrentResourceDemand()
if not Resources.equals(resourceRequest, Resources.none()):
resVector = resourceRequest.getResourceVector()
resVectorList.append(resVector)
if (len(resVectorList) == 0):
return 0
resVectorArray = numpy.asarray(resVectorList)
r, d = kmeans(resVectorArray, vectorQuantinationNum, 1000)
(code, distor) = vq(resVectorArray, r)
#print(code)
distributions = {}
for n in code:
if n in distributions.keys():
distributions[n] = distributions[n] + 1
else:
distributions[n] = 1
totalCount = len(code)
probDis = {}
for k, v in distributions.items():
probDis[k] = float(v) / totalCount
return cls.calEntropy(probDis)
@classmethod
def calEntropyOfVectorList(cls, codes, vectorQuantinationNum = 4):
#print(vectorList)
#dict4 = {(6, 1, 1, 1): 0, (2, 1, 1, 1): 0, (1, 6, 1, 1): 1, (1, 2, 1, 1): 1, (1, 1, 6, 1): 2, (1, 1, 2, 1): 2, (1, 1, 1, 6): 3, (1, 1, 1, 2): 3}
#dict8 = {(6, 1, 1, 1): 0, (3, 1, 1, 1): 1, (1, 6, 1, 1): 2, (1, 3, 1, 1): 3, (1, 1, 6, 1): 4, (1, 1, 3, 1): 5, (1, 1, 1, 6): 6, (1, 1, 1, 3): 7}
distributions = {}
for n in codes:
if n in distributions.keys():
distributions[n] = distributions[n] + 1
else:
distributions[n] = 1
totalCount = len(codes)
probDis = {}
for k, v in distributions.items():
probDis[k] = float(v) / totalCount
return cls.calEntropy(probDis)
@classmethod
def calEntropyOfApps(cls, apps):
resVectorList = []
for app in apps:
demand = app.getCurrentResourceDemand()
if not Resources.equals(demand, Resources.none()):
resVectorList.append(demand.getResourceVector())
return cls.calEntropyOfResourceVectorList(resVectorList)
@classmethod
def calEntropyOfResourceVectorList(cls, vectorList):
code = []
#mapping normalized resource vector list to category
for resVector in vectorList:
code.append(cls.getIndexOfDominantResource(resVector))
distributions = {}
for n in code:
if n in distributions.keys():
distributions[n] = distributions[n] + 1
else:
distributions[n] = 1
totalCount = len(code)
probDis = {}
for k, v in distributions.items():
probDis[k] = float(v) / totalCount
return cls.calEntropy(probDis)
@staticmethod
def calEntropy(probDict):
s = 0
for p in probDict.values():
if p > 0:
s += p * math.log(float(1) / p) / math.log(2)
return s
@staticmethod
def getIndexOfDominantResource(normalizedResVector):
return normalizedResVector.index(max(normalizedResVector))
@staticmethod
def isVectorComplementary(vec1, vec2):
if vec1.index(max(vec1)) == vec2.index(max(vec2)):
return False
else:
return True
@staticmethod
def vectorLen(v):
s = 0
for i in v:
s += i * i
return math.sqrt(s)
@staticmethod
def dotProject(v1, v2):
s = 0
for i in range(len(v1)):
s += v1[i] * v2[i]
return s
@classmethod
def calConsin(cls, v1, v2):
return float(cls.dotProject(v1, v2)) / (cls.vectorLen(v1) * cls.vectorLen(v2))
#the larger, the more complementary(better)
@classmethod
def calComplementarityOfTwoDVectors(cls, v1, v2):
if not cls.isVectorComplementary(v1, v2):
return 0
else:
diffV1 = cls.calConsin(v1, [1, 1])
diffV2 = cls.calConsin(v2, [1, 1])
return 1 - max(diffV1, diffV2)
# the larger, the more symmetry
@classmethod
def calSymmetryOfTwoDVectors(cls, vectorList):
nums = [0, 0]
for vec in vectorList:
index = vec.index(max(vec))
nums[index] += 1
return float(min(nums)) / max(nums)