gym-examples 2.0.81__py3-none-any.whl → 2.0.83__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gym_examples/__init__.py +1 -1
- gym_examples/envs/wsn_env.py +47 -49
- {gym_examples-2.0.81.dist-info → gym_examples-2.0.83.dist-info}/METADATA +1 -1
- gym_examples-2.0.83.dist-info/RECORD +7 -0
- gym_examples-2.0.81.dist-info/RECORD +0 -7
- {gym_examples-2.0.81.dist-info → gym_examples-2.0.83.dist-info}/WHEEL +0 -0
- {gym_examples-2.0.81.dist-info → gym_examples-2.0.83.dist-info}/top_level.txt +0 -0
gym_examples/__init__.py
CHANGED
gym_examples/envs/wsn_env.py
CHANGED
@@ -10,11 +10,9 @@ import torch.nn as nn
|
|
10
10
|
import torch.nn.functional as F
|
11
11
|
|
12
12
|
# Define the network parameters for the final reward function
|
13
|
-
input_dim =
|
13
|
+
input_dim = 4 # number of individual rewards
|
14
14
|
output_dim = 1 # final reward
|
15
15
|
|
16
|
-
|
17
|
-
stats_file_path_base = 'C:\\Users\\djime\\Documents\\PHD\\THESIS\\CODES\\RL_Routing\\Results_EPyMARL\\stats_over_time'
|
18
16
|
Eelec = 50e-9 # energy consumption per bit in joules
|
19
17
|
Eamp = 100e-12 # energy consumption per bit per square meter in joules
|
20
18
|
info_amount = 3072 # data size in bits
|
@@ -47,7 +45,7 @@ net = net.double() # Convert the weights to Double
|
|
47
45
|
|
48
46
|
|
49
47
|
class WSNRoutingEnv(gym.Env):
|
50
|
-
def __init__(self, n_sensors = 20, coverage_radius=
|
48
|
+
def __init__(self, n_sensors = 20, coverage_radius=(upper_bound - lower_bound)/2):
|
51
49
|
|
52
50
|
super(WSNRoutingEnv, self).__init__()
|
53
51
|
|
@@ -82,7 +80,6 @@ class WSNRoutingEnv(gym.Env):
|
|
82
80
|
# print("=================================================\n")
|
83
81
|
# Initialize remaining energy of each sensor to initial_energy joule
|
84
82
|
self.remaining_energy = np.ones(self.n_sensors) * initial_energy
|
85
|
-
self.consumption_energy = np.zeros(self.n_sensors)
|
86
83
|
self.number_of_packets = np.ones(self.n_sensors, dtype=int) * initial_number_of_packets # number of packets to transmit
|
87
84
|
self.episode_count += 1
|
88
85
|
return self._get_obs()
|
@@ -109,8 +106,10 @@ class WSNRoutingEnv(gym.Env):
|
|
109
106
|
if self.distance_to_base[i] > self.coverage_radius:
|
110
107
|
continue # Skip if the distance to the base station is greater than the coverage radius
|
111
108
|
|
112
|
-
# Calculate the energy consumption
|
109
|
+
# Calculate the energy consumption for transmitting data to the base station
|
113
110
|
transmission_energy = self.transmission_energy(self.number_of_packets[i], self.distance_to_base[i])
|
111
|
+
if self.remaining_energy[i] < transmission_energy:
|
112
|
+
continue # Skip if the sensor does not have enough energy to transmit data to the base station
|
114
113
|
self.update_sensor_energies(i, transmission_energy)
|
115
114
|
rewards[i] = self.compute_individual_rewards(i, action)
|
116
115
|
dones[i] = True
|
@@ -121,6 +120,8 @@ class WSNRoutingEnv(gym.Env):
|
|
121
120
|
|
122
121
|
transmission_energy = self.transmission_energy(self.number_of_packets[i], distance)
|
123
122
|
reception_energy = self.reception_energy(self.number_of_packets[i])
|
123
|
+
if self.remaining_energy[i] < transmission_energy or self.remaining_energy[action] < reception_energy:
|
124
|
+
continue
|
124
125
|
self.update_sensor_energies(i, transmission_energy)
|
125
126
|
self.update_sensor_energies(action, reception_energy)
|
126
127
|
# Compute individual rewards
|
@@ -155,15 +156,15 @@ class WSNRoutingEnv(gym.Env):
|
|
155
156
|
|
156
157
|
def _get_obs(self):
|
157
158
|
return [{'remaining_energy': np.array([e]),
|
159
|
+
'consumption_energy': np.array([initial_energy - e]),
|
158
160
|
'sensor_positions': p,
|
159
|
-
'
|
160
|
-
'number_of_packets': np.array([d])} for e, p, c, d in zip(self.remaining_energy, self.sensor_positions, self.consumption_energy, self.number_of_packets)]
|
161
|
+
'number_of_packets': np.array([d])} for e, p, d in zip(self.remaining_energy, self.sensor_positions, self.number_of_packets)]
|
161
162
|
|
162
163
|
def _get_observation_space(self):
|
163
164
|
return Dict({
|
164
165
|
'remaining_energy': Box(low=0, high=initial_energy, shape=(1,), dtype=np.float64),
|
165
|
-
'sensor_positions': Box(low=lower_bound, high=upper_bound, shape=(2,), dtype=np.float64),
|
166
166
|
'consumption_energy': Box(low=0, high=initial_energy, shape=(1,), dtype=np.float64),
|
167
|
+
'sensor_positions': Box(low=lower_bound, high=upper_bound, shape=(2,), dtype=np.float64),
|
167
168
|
'number_of_packets': Box(low=0, high=self.n_sensors * initial_number_of_packets + 1, shape=(1,), dtype=int)
|
168
169
|
})
|
169
170
|
|
@@ -174,7 +175,6 @@ class WSNRoutingEnv(gym.Env):
|
|
174
175
|
return [list(range(self.n_sensors + 1)) for _ in range(self.n_sensors)]
|
175
176
|
|
176
177
|
def update_sensor_energies(self, i, delta_energy):
|
177
|
-
self.consumption_energy[i] += delta_energy
|
178
178
|
self.remaining_energy[i] -= delta_energy
|
179
179
|
|
180
180
|
def transmission_energy(self, number_of_packets, distance):
|
@@ -214,7 +214,7 @@ class WSNRoutingEnv(gym.Env):
|
|
214
214
|
Compute the reward based on the distance to the next hop
|
215
215
|
'''
|
216
216
|
if action == self.n_sensors:
|
217
|
-
distance = np.linalg.norm(self.sensor_positions[i] -
|
217
|
+
distance = np.linalg.norm(self.sensor_positions[i] - self.distance_to_base[i])
|
218
218
|
else:
|
219
219
|
distance = np.linalg.norm(self.sensor_positions[i] - self.sensor_positions[action])
|
220
220
|
# Normalize the distance to the next hop
|
@@ -234,37 +234,35 @@ class WSNRoutingEnv(gym.Env):
|
|
234
234
|
transmission_energy = self.transmission_energy(self.number_of_packets[i], distance)
|
235
235
|
reception_energy = self.reception_energy(self.number_of_packets[i])
|
236
236
|
total_energy = transmission_energy + reception_energy
|
237
|
-
|
238
|
-
|
237
|
+
|
239
238
|
# Normalize the total energy consumption
|
240
239
|
max_transmission_energy = self.transmission_energy(self.n_sensors * initial_number_of_packets, self.coverage_radius)
|
241
240
|
max_reception_energy = self.reception_energy(self.n_sensors * initial_number_of_packets)
|
242
241
|
max_total_energy = max_transmission_energy + max_reception_energy
|
243
|
-
# max_total_energy = max_transmission_energy
|
244
242
|
normalized_total_energy = total_energy / max_total_energy
|
245
243
|
|
246
244
|
return np.clip(1 - normalized_total_energy, 0, 1)
|
247
245
|
|
248
|
-
def compute_reward_dispersion_remaining_energy(self,i):
|
249
|
-
'''
|
250
|
-
Compute the reward based on the difference between the remaining energy of the sensor i and the mean remaining energy of all sensors
|
251
|
-
'''
|
252
|
-
difference = np.abs(self.remaining_energy[i] - np.mean(self.remaining_energy))
|
253
|
-
# Normalize the difference
|
254
|
-
normalized_difference = difference / initial_energy
|
255
|
-
|
256
|
-
return np.clip(1 - normalized_difference, 0, 1)
|
257
|
-
|
258
|
-
# def compute_reward_dispersion_remaining_energy(self):
|
246
|
+
# def compute_reward_dispersion_remaining_energy(self,i):
|
259
247
|
# '''
|
260
|
-
# Compute the reward based on the
|
248
|
+
# Compute the reward based on the difference between the remaining energy of the sensor i and the mean remaining energy of all sensors
|
261
249
|
# '''
|
262
|
-
#
|
263
|
-
# # Normalize the
|
264
|
-
#
|
265
|
-
# normalized_dispersion_remaining_energy = dispersion_remaining_energy / max_dispersion_remaining_energy
|
250
|
+
# difference = np.abs(self.remaining_energy[i] - np.mean(self.remaining_energy))
|
251
|
+
# # Normalize the difference
|
252
|
+
# normalized_difference = difference / initial_energy
|
266
253
|
|
267
|
-
# return np.clip(1 -
|
254
|
+
# return np.clip(1 - normalized_difference, 0, 1)
|
255
|
+
|
256
|
+
def compute_reward_dispersion_remaining_energy(self):
|
257
|
+
'''
|
258
|
+
Compute the reward based on the standard deviation of the remaining energy
|
259
|
+
'''
|
260
|
+
dispersion_remaining_energy = np.std(self.remaining_energy)
|
261
|
+
# Normalize the standard deviation of the remaining energy
|
262
|
+
max_dispersion_remaining_energy = initial_energy / 2 # maximum standard deviation of the remaining energy if n_sensors is even
|
263
|
+
normalized_dispersion_remaining_energy = dispersion_remaining_energy / max_dispersion_remaining_energy
|
264
|
+
|
265
|
+
return np.clip(1 - normalized_dispersion_remaining_energy, 0, 1)
|
268
266
|
|
269
267
|
def compute_reward_number_of_packets(self, action):
|
270
268
|
'''
|
@@ -292,27 +290,27 @@ class WSNRoutingEnv(gym.Env):
|
|
292
290
|
# return [reward_angle, reward_distance, reward_consumption_energy, reward_number_of_packets]
|
293
291
|
return [reward_angle, reward_distance, reward_dispersion_remaining_energy, reward_number_of_packets]
|
294
292
|
|
295
|
-
def network_reward_dispersion_remaining_energy(self):
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
293
|
+
# def network_reward_dispersion_remaining_energy(self):
|
294
|
+
# '''
|
295
|
+
# Compute the reward based on the standard deviation of the remaining energy at the network level
|
296
|
+
# '''
|
297
|
+
# dispersion_remaining_energy = np.std(self.remaining_energy)
|
298
|
+
# # Normalize the standard deviation of the remaining energy
|
299
|
+
# max_dispersion_remaining_energy = initial_energy / 2 # maximum standard deviation of the remaining energy if n_sensors is even
|
300
|
+
# normalized_dispersion_remaining_energy = dispersion_remaining_energy / max_dispersion_remaining_energy
|
303
301
|
|
304
|
-
|
302
|
+
# return np.clip(1 - normalized_dispersion_remaining_energy, 0, 1)
|
305
303
|
|
306
|
-
def network_reward_consumption_energy(self):
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
304
|
+
# def network_reward_consumption_energy(self):
|
305
|
+
# '''
|
306
|
+
# Compute the reward based on the total energy consumption (transmission, reception) at the network level
|
307
|
+
# '''
|
308
|
+
# total_energy = self.n_sensors * initial_energy - np.sum(self.remaining_energy)
|
309
|
+
# # Normalize the total energy consumption
|
310
|
+
# max_total_energy = self.n_sensors * initial_energy
|
311
|
+
# normalized_total_energy = total_energy / max_total_energy
|
314
312
|
|
315
|
-
|
313
|
+
# return np.clip(1 - normalized_total_energy, 0, 1)
|
316
314
|
|
317
315
|
def integrate_mobility(self):
|
318
316
|
'''
|
@@ -0,0 +1,7 @@
|
|
1
|
+
gym_examples/__init__.py,sha256=YEKG8R03ekJWLJpmVPZiwKo5zvrqXgZbsoY-7HGY7sM,193
|
2
|
+
gym_examples/envs/__init__.py,sha256=lgMe4pyOuUTgTBUddM0iwMlETsYTwFShny6ifm8PGM8,53
|
3
|
+
gym_examples/envs/wsn_env.py,sha256=smTNOcnzz5rgGkvpdgZycyVbeAJA3DpXVA1BJoRsQ4s,17142
|
4
|
+
gym_examples-2.0.83.dist-info/METADATA,sha256=-81toWYa0rYvAKiPCHLGlSi1otu9Pt7w8jjfOJZ4VDE,411
|
5
|
+
gym_examples-2.0.83.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
6
|
+
gym_examples-2.0.83.dist-info/top_level.txt,sha256=rJRksoAF32M6lTLBEwYzRdo4PgtejceaNnnZ3HeY_Rk,13
|
7
|
+
gym_examples-2.0.83.dist-info/RECORD,,
|
@@ -1,7 +0,0 @@
|
|
1
|
-
gym_examples/__init__.py,sha256=bowSmkeNAOZSe_xKiNCfFdMmeF7fyV4gyu5a13H9RZE,193
|
2
|
-
gym_examples/envs/__init__.py,sha256=lgMe4pyOuUTgTBUddM0iwMlETsYTwFShny6ifm8PGM8,53
|
3
|
-
gym_examples/envs/wsn_env.py,sha256=ePw-tBdSKYUR2G1l5VD0yTMqFSIRerTxBf49kLw5DGg,17066
|
4
|
-
gym_examples-2.0.81.dist-info/METADATA,sha256=mp1kGR4uMvgCxdrjfBjt0QPTyJqIKd8T-Fwi7YvgoZ0,411
|
5
|
-
gym_examples-2.0.81.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
6
|
-
gym_examples-2.0.81.dist-info/top_level.txt,sha256=rJRksoAF32M6lTLBEwYzRdo4PgtejceaNnnZ3HeY_Rk,13
|
7
|
-
gym_examples-2.0.81.dist-info/RECORD,,
|
File without changes
|
File without changes
|