-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multi_Dimension.py
392 lines (319 loc) · 16.8 KB
/
Multi_Dimension.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import math
import numpy as np
from DataStructure import *
from config import *
def getMeanHyperplane(inputStoryVectorList):
"""Input story vector list, return unbiased mean line.
This function is dimension free.
"""
if not inputStoryVectorList:
raise ValueError("The consumerPointList is empty. Please check the input point list.")
meanPoint = []
for i in range(len(inputStoryVectorList[0])):
n = 0
for line in inputStoryVectorList:
n += line[i]
n /= len(inputStoryVectorList)
meanPoint.append(n)
meanHyperplane = Hyperplane([*meanPoint, 0])
return meanHyperplane
#TODO: HANDLE PARALLEL VECTORS IN HIGHER DIMENSIONS
def getHyperplaneEquation(pointList):
"""Input the point that require to form ONE hyperplane. Return the hyperplane equation.
Dimension Free.
"""
for i in range(len(pointList)):
for j in range(i+1, len(pointList)):
if pointList[i] == pointList[j]:
raise ValueError("There are two same points in the point list. Failed to form a hyperplane.\n" +
"PointA is " + str(pointList[i]) + " and pointB is " + str(pointList[j])+".")
dimension = len(pointList[0])
b = np.ones(dimension)
pointMatrix = np.array(pointList)
hyperplaneMatrix = np.linalg.solve(pointMatrix,b)
hyperplaneEauation = hyperplaneMatrix.tolist()
hyperplaneEauation.append(-1)
if hyperplaneEauation[1] < 0:
hyperplaneEauation = [-x for x in hyperplaneEauation]
if hyperplaneEauation[1] == 0:
raise ValueError("Hyperplane parallel to the y axis. Error!")
else:
devider = hyperplaneEauation[1]
hyperplaneEauation = [x/devider for x in hyperplaneEauation]
outputHyperplaneEquation = Hyperplane(hyperPlaneEquation = hyperplaneEauation)
return outputHyperplaneEquation
def getOrthogonalUnitVector(inputHyperplane:Hyperplane):
orthogonalVector = inputHyperplane.hyperPlaneEquation[:-1]
meg = math.sqrt(sum([x**2 for x in orthogonalVector]))
orthogonalUnitVector = [ x/meg for x in orthogonalVector]
if orthogonalUnitVector[1] < 0: #Just in case. Not gonna happen.
orthogonalUnitVector = [-x for x in orthogonalUnitVector]
return orthogonalUnitVector
def getOriginalHyperplaneListWithUtilities2(inputHyperPlaneList: [Hyperplane], consumerPointList, unbiasedVector):
"""Return hyperPlane list with utilities.
Dimension Free.
InputHyperPlaneList is a Hyperplane instance. But UnbiasedVector is just a vector list.
"""
for i in range(len(inputHyperPlaneList)):
inputHyperPlaneList[i].pointSubscription, inputHyperPlaneList[i].adversaryUtility = \
countSubscribersOfHyperplane2(
inputHyperPlaneList[i], consumerPointList, ci)
# L2 Norm:
norm = 0
#get unit vector for unbiased vector and hyperplaneEquation
# Change the normal vector to unit vector.
unbiasedVector2 = unbiasedVector
unbiasedVecMagnitude = (sum([x ** 2 for x in unbiasedVector[:-1]])) ** 0.5
if unbiasedVecMagnitude != 0:
unbiasedVector2 = [x/unbiasedVecMagnitude for x in unbiasedVector]
else:
raise ValueError("Getting a all zeros hyperplane.")
# Change the hyperplane vector to unit vector.
currHyperplane = inputHyperPlaneList[i].hyperPlaneEquation
hyperplaneMagnitude = (sum([x ** 2 for x in inputHyperPlaneList[i].hyperPlaneEquation[:-1]])) ** 0.5
if hyperplaneMagnitude != 0:
currHyperplane = [x / hyperplaneMagnitude for x in inputHyperPlaneList[i].hyperPlaneEquation]
else:
raise ValueError("Getting a all zeros hyperplane.")
#for k in range(len(inputHyperPlaneList[i].hyperPlaneEquation)-1): # Don't count the constant variable??? Not
# counting now.
for k in range(len(currHyperplane)-1):
norm += (currHyperplane[k] - unbiasedVector2[k]) ** 2
#norm += (inputHyperPlaneList[i].hyperPlaneEquation[k]/inputHyperPlaneList[i].hyperPlaneEquation[1] -
#unbiasedVector[k]/unbiasedVector[1]) ** 2 # When calculating the norm, I keep the y parameter to be 1.
norm = math.sqrt(norm)
inputHyperPlaneList[i].defenderUtility = norm
return inputHyperPlaneList
def getOriginalHyperplaneListWithUtilities(inputHyperPlaneList: [Hyperplane], consumerPointList, unbiasedVector):
"""Return hyperPlane list with utilities.
Dimension Free.
InputHyperPlaneList is a Hyperplane instance. But UnbiasedVector is just a vector list.
"""
for i in range(len(inputHyperPlaneList)):
inputHyperPlaneList[i].pointSubscription, inputHyperPlaneList[i].adversaryUtility = \
countSubscribersOfHyperplane(
inputHyperPlaneList[i], consumerPointList, ci)
# L2 Norm:
norm = 0
# get unit vector for unbiased vector and hyperplaneEquation
# Change the normal vector to unit vector.
unbiasedVector2 = unbiasedVector
unbiasedVecMagnitude = (sum([x ** 2 for x in unbiasedVector[:-1]])) ** 0.5
if unbiasedVecMagnitude != 0:
unbiasedVector2 = [x / unbiasedVecMagnitude for x in unbiasedVector]
else:
raise ValueError("Getting a all zeros hyperplane.")
# Change the hyperplane vector to unit vector.
currHyperplane = inputHyperPlaneList[i].hyperPlaneEquation
hyperplaneMagnitude = (sum([x ** 2 for x in inputHyperPlaneList[i].hyperPlaneEquation[:-1]])) ** 0.5
if hyperplaneMagnitude != 0:
currHyperplane = [x / hyperplaneMagnitude for x in inputHyperPlaneList[i].hyperPlaneEquation]
else:
raise ValueError("Getting a all zeros hyperplane.")
for k in range(len(currHyperplane)-1): # Don't count the constant variable??? Not
# counting now.
norm += (currHyperplane[k] - unbiasedVector2[k]) ** 2
#norm += (inputHyperPlaneList[i].hyperPlaneEquation[k]/inputHyperPlaneList[i].hyperPlaneEquation[1] -
#unbiasedVector[k]/unbiasedVector[1]) ** 2 # When calculating the norm, I keep the y parameter to be 1.
norm = math.sqrt(norm)
inputHyperPlaneList[i].defenderUtility = norm
return inputHyperPlaneList
def getConvertedHyperplaneListWithUtilities(originalConvertedHyperplaneMatchList: [[Hyperplane, Hyperplane]],
consumerPointList, unbiasedVector, ci):
"""Dimension Free."""
originalHyperplaneList = [originalConvertedHyperplaneMatchList[i][0] for i in range(len(originalConvertedHyperplaneMatchList))]
convertedHyperplaneList = [originalConvertedHyperplaneMatchList[i][1] for i in range(len(
originalConvertedHyperplaneMatchList))]
for i in range(len(convertedHyperplaneList)):
convertedHyperplaneList[i].pointSubscription, convertedHyperplaneList[i].adversaryUtility = countSubscribersOfHyperplane(
convertedHyperplaneList[i], consumerPointList, ci)
if convertedHyperplaneList[i].pointSubscription != originalHyperplaneList[i].pointSubscription:
raise ValueError("The converted hyperplane has different point subscription compare to the original.")
# L2 Norm:
norm = 0
# get unit vector for unbiased vector and hyperplaneEquation
# Change the normal vector to unit vector.
unbiasedVector2 = unbiasedVector
unbiasedVecMagnitude = (sum([x ** 2 for x in unbiasedVector[:-1]])) ** 0.5
if unbiasedVecMagnitude != 0:
unbiasedVector2 = [x / unbiasedVecMagnitude for x in unbiasedVector]
else:
raise ValueError("Getting a all zeros hyperplane.")
# Change the hyperplane vector to unit vector.
currHyperplane = convertedHyperplaneList[i].hyperPlaneEquation
hyperplaneMagnitude = (sum([x ** 2 for x in convertedHyperplaneList[i].hyperPlaneEquation[:-1]])) ** 0.5
if hyperplaneMagnitude != 0:
currHyperplane = [x / hyperplaneMagnitude for x in convertedHyperplaneList[i].hyperPlaneEquation]
else:
raise ValueError("Getting a all zeros hyperplane.")
for k in range(len(currHyperplane) - 1): # Don't count the constant variable??? Not
# counting now.
norm += (currHyperplane[k] - unbiasedVector2[k]) ** 2
#norm += (convertedHyperplaneList[i].hyperPlaneEquation[k] / convertedHyperplaneList[i].hyperPlaneEquation[1] -
# unbiasedVector[k] / unbiasedVector[1]) ** 2 # When calculating the norm, I keep the y parameter to be 1.
norm = math.sqrt(norm)
convertedHyperplaneList[i].defenderUtility = norm
return convertedHyperplaneList
def twoPointsDistance (pointA, pointB):
dimension = len(pointA)
distance = 0
for i in range(dimension):
distance += ( pointB[i] - pointA[i]) * ( pointB[i] - pointA[i])
distance = math.sqrt(distance)
return distance
def movePoints(defenderHyperplane: Hyperplane, adversaryHyperplane:Hyperplane, inputPointList, oringinalPointList, ci):
"""Try to move points so that the defender hyperplane can has more point counts than adversary hyperplane.
If succeed, return True, finalMovedPointList, defenderMaximumPointNumber, adveraryMaximumPointNumber
If failed, return False, [empty list], defenderMaximumPointNumber, adveraryMaximumPointNumber
"""
movedDefenderPointsList = []
finalMovedPointList = []
# Move points to benefits defender.
for i in range(len(inputPointList)):
if defenderHyperplane.pointSubscription[i] == 0:
beta = []
gamma = []
normalUnitVector = getOrthogonalUnitVector(defenderHyperplane)
for j in range(len(inputPointList[i])):
beta.append(inputPointList[i][j] * defenderHyperplane.hyperPlaneEquation[j])
gamma.append(normalUnitVector[j] * defenderHyperplane.hyperPlaneEquation[j])
beta = sum(beta)
gamma = sum(gamma)
lowerBoundonDistance = (ci - beta)/ gamma
movedPoint = [x + y for x, y in
zip(inputPointList[i], [lowerBoundonDistance * z for z in getOrthogonalUnitVector(
defenderHyperplane)])]
distanceToOriginalPoints = twoPointsDistance(oringinalPointList[i], movedPoint)
if distanceToOriginalPoints <= longestMovingDistance and singlePointSubscribeOfHyperplane(
defenderHyperplane, movedPoint, ci) == 1:
movedDefenderPointsList.append(movedPoint)
else:
movedDefenderPointsList.append(inputPointList[i]) # Not moving this point because the total moving
# distance is too large or cannot change the subscription status.
else:
movedDefenderPointsList.append(inputPointList[i]) # This point is already subscribed.
# # ##########################Haven't implemented!!!!!!!!!!!!!!!!!!
finalMovedPointList = movedDefenderPointsList # TODO: Delete this line when moving points to hurt adversary
# hyperplane is enable.
_, defenderTotalSubscriptionNumber = countSubscribersOfHyperplane(defenderHyperplane,
finalMovedPointList,
ci= ci)
_, adversaryTotalSubscriptionNumber = countSubscribersOfHyperplane(adversaryHyperplane,
finalMovedPointList, ci = ci)
# if finalMovedPointList == inputPointList or finalMovedPointList == oringinalPointList:
# return False, [], defenderTotalSubscriptionNumber
if defenderTotalSubscriptionNumber >= adversaryTotalSubscriptionNumber and defenderTotalSubscriptionNumber > 0:
return True, finalMovedPointList, defenderTotalSubscriptionNumber
elif defenderTotalSubscriptionNumber == 0:
raise Exception("The defender total subscription number equals to 0. Bug!")
else:
return False, [], defenderTotalSubscriptionNumber
def isTwoPointsOnTheSameSideOfHyperplane(pointA, pointB, hyperplane:Hyperplane):
m = 0
n = 0
dimension = len(pointA)
for i in range(dimension):
m += pointA[i] * hyperplane.hyperPlaneEquation[i]
n += pointB[i] * hyperplane.hyperPlaneEquation[i]
m += hyperplane.hyperPlaneEquation[dimension]
n += hyperplane.hyperPlaneEquation[dimension]
if m * n < 0:
return False
else:
# print("Point Moved.")
return True
def countSubscribersOfHyperplane(inputHyperplane:Hyperplane, inputPointList, ci): #Attension, when we don't need ci,
# set ci to be 0.
pointSubscribedList = []
totalSubscribeNumber = 0
for inputPoint in inputPointList:
pointSubscribed = singlePointSubscribeOfHyperplane(inputHyperplane=inputHyperplane, inputPoint=inputPoint,
ci = ci)
if pointSubscribed == 1:
totalSubscribeNumber += 1
pointSubscribedList.append(pointSubscribed)
return pointSubscribedList, totalSubscribeNumber
def countSubscribersOfHyperplane2(inputHyperplane:Hyperplane, inputPointList, ci): #Attension, when we don't need ci,
# set ci to be 0.
pointSubscribedList = []
totalSubscribeNumber = 0
for inputPoint in inputPointList:
pointSubscribed = singlePointSubscribeOfHyperplane2(inputHyperplane=inputHyperplane, inputPoint=inputPoint,
ci = ci)
if pointSubscribed == 1:
totalSubscribeNumber += 1
pointSubscribedList.append(pointSubscribed)
return pointSubscribedList, totalSubscribeNumber
#to be used by original generated hyperplanes, which have a constant term and do not need ci
def singlePointSubscribeOfHyperplane2(inputHyperplane:Hyperplane, inputPoint, ci):
n = []
for j in range(len(inputPoint)):
n.append(inputHyperplane.hyperPlaneEquation[j] * inputPoint[j])
n.append(inputHyperplane.hyperPlaneEquation[-1]) # Re-enable the constant variable.
n = sum(n)
if n>=0:
return 1
else:
return 0
def debugsinglePointSubscribeOfHyperplane2(inputHyperplane:Hyperplane, inputPoint, ci):
n = []
for j in range(len(inputPoint)):
n.append(inputHyperplane.hyperPlaneEquation[j] * inputPoint[j])
n.append(inputHyperplane.hyperPlaneEquation[-1]) # Re-enable the constant variable.
n = sum(n)
# print(n)
#if n > 0 - precisionError:
if n>=0:
return 1
else:
return 0
#to be used by converted hyperplane directions, which have no constant term other than ci
def singlePointSubscribeOfHyperplane(inputHyperplane:Hyperplane, inputPoint, ci):
n = []
for j in range(len(inputPoint)):
n.append(inputHyperplane.hyperPlaneEquation[j] * inputPoint[j])
#n.append(inputHyperplane.hyperPlaneEquation[-1]) # Re-enable the constant variable.
n = sum(n)
n=n-ci
if n >=-1*precisionError and n<=0:
n=0
if n>=0:
return 1
else:
return 0
def debugsinglePointSubscribeOfHyperplane(inputHyperplane:Hyperplane, inputPoint, ci):
n = []
for j in range(len(inputPoint)):
n.append(inputHyperplane.hyperPlaneEquation[j] * inputPoint[j])
#n.append(inputHyperplane.hyperPlaneEquation[-1]) # Re-enable the constant variable.
n = sum(n)
n=n-ci
# print(n)
if n >=-1*precisionError and n<=0:
n=0
#if n > 0 - precisionError:
if n>=0:
return 1
else:
return 0
### Test
def testGetHyperplaneEquation():
inputPoints = [[4, 0, -1, 0], [1, 2, 3, -1], [0, -1, 2, 0], [-1, 1, -1, 1]]
outputHyperPlane = getHyperplaneEquation(inputPoints)
if outputHyperPlane.hyperPlaneEquation != [0.40625, 0.25, 0.625, 1.7812500000000002, -1]:
raise ValueError("hyperPlane Calculation ERROR!\n" +
"Calculated hyperPlane is " + str(outputHyperPlane.hyperPlaneEquation) + ". Correct hyperplane should be: [[["
"0.40625, 0.25, 0.625, 1.7812500000000002, -1]],[[4, 0, -1, 0], [1, 2, 3, -1], [0, -1, 2, 0], [-1, 1, -1, 1]]]:")
inputPoints = [[3,2],[2,3]]
outputHyperPlane = getHyperplaneEquation(inputPoints)
print(outputHyperPlane.hyperPlaneEquation)
outputUnitNormalVector = getOrthogonalUnitVector(outputHyperPlane)
print(outputUnitNormalVector)
def testGetHyperplaneListWithUtilities():
hyperPlaneA = Hyperplane([1, 1, -5], [[0, 5], [5, 0]])
hyperPlaneB = Hyperplane([1, 2, -5], [[0, 2.5], [5, 0]])
hyperPlaneList = [hyperPlaneA,hyperPlaneB]
unbiasedPlane = [1,1,-5]
pointList = [[1,1],[2,2],[3,3],[4,4],[5,5]]
getOriginalHyperplaneListWithUtilities(hyperPlaneList, pointList, unbiasedPlane)
print(hyperPlaneList[1].maximumPointNumber)