-
Notifications
You must be signed in to change notification settings - Fork 16
/
energy.py
65 lines (56 loc) · 2.4 KB
/
energy.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
""""
#################################
# Energy function
#################################
"""
#########################################################
# import libraries
import numpy as np
import scipy.io as sio
#########################################################
# Function definition
def init_energy(num_UAV, min_energy, max_energy, savefile, pthenergy):
"""
This function initializes the energy values for all drones and UAV randomly based on the energy config file
:param num_UAV: Number of UAVs
:param min_energy: Minimum possible value for the initialization
:param max_energy: Maximum possible value for the initialization
:param savefile: A FLAG to Save or Not Save the energy values on disk
:param pthenergy: The path to save the energy values
:return: This function returns the initial energy values
"""
if savefile:
energy = np.random.uniform(min_energy, max_energy, num_UAV)
energy_dict = dict([('energy', energy)])
sio.savemat(pthenergy, energy_dict)
else:
energy_dict = sio.loadmat(pthenergy)
energy = energy_dict.get('energy')
energy = np.squeeze(energy)
return energy
def update_energy_movement(energy, actions, mobility_rate):
"""
This function updates the UAVs' energy after choosing an action for the mobility.
:param energy: The energy matrix(vector) for all UAVs
:param actions: Chosen action from Q learning
:param mobility_rate: The energy consumption rate for mobility
:return: This function returns the updated energy values after the chosen action
"""
num_uav = actions.size
no_movement = 4
returned_energy = np. zeros([num_uav], dtype=float)
for uav in np.arange(num_uav):
returned_energy[uav] = energy[uav] - mobility_rate if actions[uav] < no_movement else energy[uav]
return returned_energy
def update_energy_transmission(energy, tran_rate):
"""
This function updates the UAVs' energy after data transmission.
:param energy: The energy matrix(vector) for all UAVs
:param tran_rate: The energy consumption rate for transmission
:return: This function returns the updated energy values after each transmission
"""
num_uav = energy.size
returned_energy = np.zeros([num_uav], dtype=float)
for uav in np.arange(num_uav):
returned_energy[uav] = energy[uav] - tran_rate if energy[uav] > 0 else 0
return returned_energy