-
Notifications
You must be signed in to change notification settings - Fork 14
/
helper_functions.py
224 lines (161 loc) · 6.56 KB
/
helper_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
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
'''
Helper functions that are used in the simulation
'''
import numpy as np
from game_functions import *
def initialize(N, bpar, bn, **params):
'''
Initialize the probabilities for the simulation
Parameters
----------
N: int
Number of users
Returns
-------
b: 1-D array
Each column represents the amount of data a user wants to offload to the MEC server.
b_old: 1-D array
Each column represents the amount of data a user wanted to offload on the previous round
to the MEC server.
'''
b = bpar * bn
# set b_old different to b so that we don't falsely converge
b_old = 1 * np.ones(N)
return b, b_old
def calculate_costs(b, bn, dn, c, **params):
'''
Calcuate costs imposed by the MEC server to each user
Parameters
----------
b: 1-D array
Each column represents the amount of data a user wants to offload to the MEC server.
bn: 1-D array
Each column represents the amount of data a user has.
dn: 1-D array
Each column represents the cycles each user's job needs.
c: 1-D array
Each column repesents the pricing factor for each user
Returns
-------
costs: 1-D array
Each column contains the cost imposed by the MEC server to each user
'''
costs = c * dn * b/bn
return costs
def calculate_PoF(b, bn, dn, **params):
'''
Calculate probability of failure of the MEC server
Parameters
----------
b: 1-D array
Each column represents the amount of data a user wants to offload to the MEC server.
bn: 1-D array
Each column represents the amount of data a user has.
dn: 1-D array
Each column represents the cycles each user's job needs.
Returns
-------
Pr: float
The probability of failure of the MEC server
'''
# dt = 1/(1+np.exp(-1.2e-11*np.sum(dn*b/bn)))
dt = -1 + 2/(1+np.exp(-2.0e-11*np.sum(dn*b/bn)))
PoF = dt**2
return PoF
def calculate_user_energy(b, PoF, bn, dn, en, fixed_transm_rate, fixed_transm_power, **params):
'''
Calcuate energy that each user expenses based on the offloading chosen
Parameters
----------
b: 1-D array
Each column represents the amount of data a user wants to offload to the MEC server.
bn: 1-D array
Each column represents the amount of data a user has.
dn: 1-D array
Each column represents the cycles each user's job needs.
en: 1-D array
Energy to process the data locally by each user
fixed_transm_rate : float
The transmission rate for offloading 1 bit per second [fixed]
fixed_transm_power : float
The transmission power used for the transmission [fixed]
Returns
-------
user_energy: 1-D array
Each column contains the energy of each user
'''
transmission_power = fixed_transm_power * (b/fixed_transm_rate)
blocal = bn - b
energy = en*PoF + (en*(blocal/bn) + transmission_power)*(1-PoF)
return energy
def check_all_parameters(bn, dn, an, kn, c, tn, en, **params):
working = 0
eps = 10
for an1 in np.linspace(0.3, 1, 8):
an = an1
for kn1 in np.linspace(0.1, 2, 10):
kn = kn1
working += 1
print(working)
for dn1 in np.linspace(1, 10, 10):
dn = dn1 * 1e9 * np.ones(params["N"])
for fn1 in np.linspace(1, 10, 10):
fn = fn1 * 1e9 * np.ones(params["N"])
tn = dn/fn
for gn1 in np.linspace(1, 10, 10):
gn = gn1 * 1e-9 * np.ones(params["N"])
en = gn * dn
for bn1 in np.linspace(1, 10, 10):
bn = bn1 * 1e6 * np.ones(params["N"])
for c1 in np.linspace(2e-1, 9e-1, 8):
c = c1 * bn/dn * (1 - (1/(tn*en)))
if c.all() > 0:
counter = 0
for i, value in enumerate(np.linspace(0,bn[0],11)):
b_others = value * np.ones(5)
res = fminbound(utility_function, 0, bn[0], args=(0, b_others, dn, bn, an, kn, c, tn, en), disp=False)
result_utility = -utility_function(res, 0, b_others, dn, bn, an, kn, c, tn, en)
if (res > eps) and (res < bn[0] - eps) and (np.isfinite(result_utility)):
counter += 1
if counter > 8:
my_string = ("Counter: " + str(counter) + "\n" +
"an: " + str(an) +
" kn: " + str(kn) +
" bn: " + str(bn1) +
" dn: " + str(dn1) +
" fn: " + str(fn1) +
" gn: " + str(gn1) +
" c: " + str(c1) + "\n")
with open('result.txt' , 'a') as writer:
writer.write(my_string)
return "Done"
def check_best_parameters(bn, dn, an, kn, c, tn, en, **params):
N = 10000
fig, ax = plt.subplots(11,1)
plt.suptitle("an: " + str(an) +
" bn: " + str(bn[0]) +
" dn: " + str(dn[0]) +
" kn: " + str(kn) +
" tn: " + str(tn[0]) +
" en: " + str(en[0]) +
" c: " + str(c[0]), fontsize=6)
for i, value in enumerate(np.linspace(0,bn[0],11)):
b_others = value * np.ones(params["N"])
eps = 0.2
res = fminbound(utility_function, 0, bn[0], args=(0, b_others, dn, bn, an, kn, c, tn, en), disp=False)
result_utility = -utility_function(res, 0, b_others, dn, bn, an, kn, c, tn, en)
if (res > eps) and (res < bn[0] - eps) and (np.isfinite(result_utility)):
print()
print(res)
print(result_utility)
x = np.linspace(0, bn[0], N)
res = np.empty_like(x)
for j in range(len(x)):
res[j] = -utility_function(x[j], 0, b_others, dn, bn, an, kn, c, tn, en)
plt.subplot(11,1,i+1)
print(x[-1])
plt.text(x[-1], res[-1], "others = " + str(value), fontsize=8)
plt.plot(x, res)
plt.xlabel('bnMEC', fontweight='normal')
plt.show()
return "Done"