-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathserver_selection_functions.py
151 lines (115 loc) · 4.14 KB
/
server_selection_functions.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
'''
Server selection - Learning system functions
'''
import numpy as np
def server_selection(probabilities, U, S, **params):
'''
Each user selects a server to whom it will offload the data.
Parameters
----------
probabilities: 1-D array
The probabilities that the user will select the specific server
Returns
-------
servers: 1-D array
list containing the server to which each user is associated
'''
# Each user selects the server to which he will offload his data based
# on the probabilities distribution he has
servers = np.array([np.random.choice(np.arange(S), replace=True, p=probabilities[user])
for user in np.arange(U)])
return servers
def all_users_sure(probabilities):
'''
Check if all users are certain of the selection they made on the server
Parameters
----------
probabilities: 2-D array
The probabilities that each user will select the specific server
Returns
-------
Boolean
Boolean on whether all users are sure of the selected server or not
'''
# check if all rows have at least one server with probability > 0.9
if np.all(np.max(probabilities, axis=1) > 0.9):
return True
return False
def calculate_competitiveness(all_bytes_to_server, all_fs, all_prices, U, S, b_max, **params):
'''
Calculate the competitiveness score Rs used on the update function
Parameters
----------
all_bytes_to_server: 2-D array
The number of bytes the users have offloaded to the specific server
up to now
all_fs: 2-D array
The discount the servers have offered up to now
all_prices: 2-D array
The prices the servers have set up to now
U: int
Number of users
S: int
Number of servers
b_max: int
Maximum number of bits that the user is willing to offload
Returns
-------
Rs: 1-D array
the competitiveness score of each server
relative_price: 1-D array
the relative pricing of each server
congestion: 1-D array
the congestion of each server
penetration: 1-D array
the penetration of each server on the offloading market
'''
# calculate relative pricing
denominator = all_prices[-1] - all_fs[-1]*all_prices[-1]
numerator = np.sum(denominator)/S
relative_price = numerator / denominator
# calculate congestion
# set B_max of each server to be able to handle all traffic
tmp1 = all_bytes_to_server[-1]
tmp2 = b_max * U
congestion = np.power((1 + (tmp1/tmp2)),3)
# calculate "penetration"
# use np.divide to handle cases where tmp2=0
tmp1 = np.sum(all_bytes_to_server, axis=0)
tmp2 = np.sum(all_bytes_to_server)
penetration = np.divide(tmp1, tmp2, out=np.zeros_like(tmp1), where=tmp2!=0)
w1 = w2 = w3 = 1/3
Rs = w1*relative_price + w2*1/congestion + w2*penetration
return Rs,relative_price,congestion,penetration
def update_probabilities(Rs, probabilities, server_selected, b, learning_rate, **params):
'''
Update action probabilities of users on choosing a server
Parameters
----------
Rs: 1-D array
the competitiveness score of each server
probabilities: 2-D array
The probabilities that the user will select the specific server
server_selected: 1-D array
list containing the server to which each user is associated
b: 1-D array
offloading data each user had decided to send
Returns
-------
probabilities: 2-D array
The new probabilities that the user will select the specific server
'''
# use np.divide to handle cases where sum(Rs)=0
tmp1 = Rs
tmp2 = np.sum(Rs)
reward = np.divide(tmp1, tmp2, out=np.zeros_like(tmp1), where=tmp2!=0)
# create second part of probabilities update
Pr = np.copy(probabilities)
Pr[np.arange(Pr.shape[0]), server_selected] = -(1-Pr[np.arange(Pr.shape[0]), server_selected])
Pr = -Pr*learning_rate
tmp = reward[tuple([server_selected])]
# change tmp row array to column array
tmp = tmp[:, np.newaxis]
Pr = Pr * tmp
probabilities = probabilities + Pr
return probabilities