-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resources.py
137 lines (101 loc) · 4.5 KB
/
Resources.py
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
'''
Created on Jan 11, 2015
@author: zhaojie
'''
from Resource import Resource
import math
class Resources(object):
'''
classdocs
'''
NONE = Resource(0, 0, 0, 0)
NOTFIT = Resource(-1, -1, -1 ,-1)
@staticmethod
def createResource(memory, cpu, disk, network):
return Resource(memory, cpu, disk, network)
@staticmethod
def clone(resource):
return Resource(resource.getMemory(), resource.getCPU(), resource.getDisk(), resource.getNetwork())
@staticmethod
def greaterAtLeastOne(r1, r2):
return r1.getMemory() > r2.getMemory() or r1.getCPU() > r2.getCPU() or r1.getDisk() > r2.getDisk() or r1.getNetwork() > r2.getNetwork()
@staticmethod
def allAvailable(r):
return r.getMemory() > 0 and r.getCPU() > 0 and r.getDisk() > 0 and r.getNetwork() > 0
@staticmethod
def addTo(lhs, rhs):
lhs.setMemory(lhs.getMemory() + rhs.getMemory())
lhs.setCPU(lhs.getCPU() + rhs.getCPU())
lhs.setDisk(lhs.getDisk() + rhs.getDisk())
lhs.setNetwork(lhs.getNetwork() + rhs.getNetwork())
return lhs
@staticmethod
def subtractFrom(lhs, rhs):
lhs.setMemory(lhs.getMemory() - rhs.getMemory())
lhs.setCPU(lhs.getCPU() - rhs.getCPU())
lhs.setDisk(lhs.getDisk() - rhs.getDisk())
lhs.setNetwork(lhs.getNetwork() - rhs.getNetwork())
return lhs
@staticmethod
def fitsIn(smaller, bigger):
return smaller.getMemory() <= bigger.getMemory() and smaller.getCPU() <= bigger.getCPU()
@staticmethod
def allFitIn(smaller, bigger):
return smaller.getMemory() <= bigger.getMemory() and \
smaller.getCPU() <= bigger.getCPU() and \
smaller.getDisk() <= bigger.getDisk() and \
smaller.getNetwork() <= bigger.getNetwork()
@staticmethod
def localFitIn(request, local):
return request.getMemory() <= local.getMemory() and \
request.getCPU() <= local.getCPU() and \
request.getDisk() <= local.getDisk()
@staticmethod
def remoteFitIn(request, local, remote):
return request.getMemory() <= local.getMemory() and \
request.getCPU() <= local.getCPU() and \
request.getDisk() <= local.getDisk() and \
request.getNetwork() <= local.getNetwork() and \
request.getDisk() <= remote.getDisk() and \
request.getNetwork() <= remote.getNetwork()
@classmethod
def none(cls):
return cls.NONE
@classmethod
def notFit(cls):
return cls.NOTFIT
@staticmethod
def equals(lhs, rhs):
return lhs == rhs
@staticmethod
def normalizedDotProduct(r1, r2, capacity, considerIO = False):
m1 = float(r1.getMemory()) / capacity.getMemory()
c1 = float(r1.getCPU()) / capacity.getCPU()
d1 = float(r1.getDisk()) / capacity.getDisk()
n1 = float(r1.getNetwork()) / capacity.getNetwork()
m2 = float(r2.getMemory()) / capacity.getMemory()
c2 = float(r2.getCPU()) / capacity.getCPU()
d2 = float(r2.getDisk()) / capacity.getDisk()
n2 = float(r2.getNetwork()) / capacity.getNetwork()
if not considerIO:
return m1 * m2 + c1 * c2
else:
return m1 * m2 + c1 * c2 + d1 * d2 + n1 * n2
@staticmethod
def cosineSimilarity(r1, r2, considerIO = False):
if not considerIO:
dotProduct = r1.getMemory() * r2.getMemory() + r1.getCPU() * r2.getCPU()
len1 = math.sqrt(math.pow(r1.getMemory(), 2) + math.pow(r1.getCPU(), 2))
len2 = math.sqrt(math.pow(r2.getMemory(), 2) + math.pow(r2.getCPU(), 2))
if len1 == 0 or len2 == 0:
return 0.0
else:
return float(dotProduct) / (len1 * len2)
else:
dotProduct = r1.getMemory() * r2.getMemory() + r1.getCPU() * r2.getCPU() + r1.getDisk() * r2.getDisk() + r1.getNetwork() * r2.getNetwork()
len1 = math.sqrt(math.pow(r1.getMemory(), 2) + math.pow(r1.getCPU(), 2) + math.pow(r1.getDisk(), 2) + math.pow(r1.getNetwork(), 2))
len2 = math.sqrt(math.pow(r2.getMemory(), 2) + math.pow(r2.getCPU(), 2) + math.pow(r2.getDisk(), 2) + math.pow(r2.getNetwork(), 2))
if len1 == 0 or len2 == 0:
return 0.0
else:
return float(dotProduct) / (len1 * len2)