gym-examples 3.0.31__py3-none-any.whl → 3.0.33__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 +42 -18
- {gym_examples-3.0.31.dist-info → gym_examples-3.0.33.dist-info}/METADATA +1 -1
- gym_examples-3.0.33.dist-info/RECORD +7 -0
- gym_examples-3.0.31.dist-info/RECORD +0 -7
- {gym_examples-3.0.31.dist-info → gym_examples-3.0.33.dist-info}/WHEEL +0 -0
- {gym_examples-3.0.31.dist-info → gym_examples-3.0.33.dist-info}/top_level.txt +0 -0
gym_examples/__init__.py
CHANGED
gym_examples/envs/wsn_env.py
CHANGED
@@ -10,7 +10,7 @@ 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 = 4 #
|
13
|
+
input_dim = 4 # lenght of the individual rewards vector
|
14
14
|
output_dim = 1 # final reward
|
15
15
|
|
16
16
|
Eelec = 50e-9 # energy consumption per bit in joules
|
@@ -40,11 +40,6 @@ class Attention(nn.Module):
|
|
40
40
|
x = self.linear2(x) # Pass the result through another linear layer
|
41
41
|
return x
|
42
42
|
|
43
|
-
# Calculate the reward
|
44
|
-
net = Attention(input_dim, output_dim)
|
45
|
-
net = net.double() # Convert the weights to Double
|
46
|
-
|
47
|
-
|
48
43
|
class WSNRoutingEnv(gym.Env):
|
49
44
|
def __init__(self, n_sensors = 20, coverage_radius=(upper_bound - lower_bound)/4):
|
50
45
|
|
@@ -56,7 +51,6 @@ class WSNRoutingEnv(gym.Env):
|
|
56
51
|
self.episode_count = 0
|
57
52
|
self.scale_displacement = 0.01 * (upper_bound - lower_bound) # scale of the random displacement of the sensors
|
58
53
|
self.epsilon = 1e-10 # small value to avoid division by zero
|
59
|
-
# self.rewards_individual = [0] * self.n_sensors
|
60
54
|
# Initialize the position of the sensors randomly
|
61
55
|
|
62
56
|
# Define observation space
|
@@ -129,7 +123,6 @@ class WSNRoutingEnv(gym.Env):
|
|
129
123
|
self.total_latency += self.packet_latency[i] + latency_per_hop
|
130
124
|
self.packet_latency[i] = 0
|
131
125
|
|
132
|
-
# rewards[i] = self.compute_individual_rewards(i, action)
|
133
126
|
rewards[i] = np.ones(input_dim) # input_dim should be equal to the number of individual rewards
|
134
127
|
dones[i] = True
|
135
128
|
else:
|
@@ -157,13 +150,7 @@ class WSNRoutingEnv(gym.Env):
|
|
157
150
|
self.number_of_packets[action] += self.number_of_packets[i]
|
158
151
|
self.number_of_packets[i] = 0 # Reset the number of packets of the sensor i
|
159
152
|
# Calculate final reward
|
160
|
-
|
161
|
-
final_reward = net(rewards_individual)
|
162
|
-
# final_reward = np.sum(rewards[i])
|
163
|
-
# weights = np.ones(self.n_sensors, dtype=int)
|
164
|
-
# final_reward = np.sum(reward * weight for reward, weight in zip(rewards[i], weights))
|
165
|
-
rewards[i] = final_reward
|
166
|
-
# rewards = np.mean(rewards)
|
153
|
+
rewards[i] = self.compute_attention_rewards(rewards[i])
|
167
154
|
for i in range(self.n_sensors):
|
168
155
|
if (self.remaining_energy[i] <= 0) or (self.number_of_packets[i] <= 0):
|
169
156
|
dones[i] = True
|
@@ -178,12 +165,16 @@ class WSNRoutingEnv(gym.Env):
|
|
178
165
|
|
179
166
|
self.get_metrics()
|
180
167
|
|
168
|
+
reward_packet_delivery_ratio = self.compute_reward_packet_delivery_ratio()
|
169
|
+
reward_latency = self.compute_reward_latency()
|
170
|
+
rewards_metrics = [reward_packet_delivery_ratio, reward_latency]
|
171
|
+
rewards_metrics = self.compute_attention_rewards(rewards_metrics)
|
172
|
+
rewards = np.array([r + rewards_metrics.detach().numpy() for r in rewards])
|
173
|
+
|
181
174
|
return self._get_obs(), rewards, dones, {}
|
182
175
|
|
183
176
|
def _get_obs(self):
|
184
177
|
|
185
|
-
performance = self.get_metrics()
|
186
|
-
|
187
178
|
return [{'remaining_energy': np.array([e]),
|
188
179
|
'consumption_energy': np.array([initial_energy - e]),
|
189
180
|
'sensor_positions': p,
|
@@ -301,7 +292,7 @@ class WSNRoutingEnv(gym.Env):
|
|
301
292
|
Compute the individual rewards
|
302
293
|
'''
|
303
294
|
reward_angle = self.compute_reward_angle(i, action)
|
304
|
-
reward_distance = self.compute_reward_distance(i, action)
|
295
|
+
# reward_distance = self.compute_reward_distance(i, action)
|
305
296
|
reward_consumption_energy = self.compute_reward_consumption_energy(i, action)
|
306
297
|
reward_dispersion_remaining_energy = self.compute_reward_dispersion_remaining_energy()
|
307
298
|
reward_number_of_packets = self.compute_reward_number_of_packets(action)
|
@@ -329,6 +320,39 @@ class WSNRoutingEnv(gym.Env):
|
|
329
320
|
normalized_total_energy = total_energy / max_total_energy
|
330
321
|
|
331
322
|
return np.clip(1 - normalized_total_energy, 0, 1)
|
323
|
+
|
324
|
+
def compute_reward_packet_delivery_ratio(self):
|
325
|
+
'''
|
326
|
+
Compute the reward based on the packet delivery ratio
|
327
|
+
'''
|
328
|
+
return np.clip(self.packet_delivery_ratio, 0, 1)
|
329
|
+
|
330
|
+
def compute_reward_latency(self):
|
331
|
+
'''
|
332
|
+
Compute the reward based on the average latency
|
333
|
+
'''
|
334
|
+
# Normalize the average latency
|
335
|
+
max_latency = self.n_sensors * self.steps
|
336
|
+
normalized_latency = self.total_latency / max_latency
|
337
|
+
|
338
|
+
return np.clip(1 - normalized_latency, 0, 1)
|
339
|
+
|
340
|
+
|
341
|
+
def compute_sum_rewards(self, rewards):
|
342
|
+
'''
|
343
|
+
Compute the sum of the rewards
|
344
|
+
'''
|
345
|
+
return np.sum(rewards)
|
346
|
+
|
347
|
+
def compute_attention_rewards(self, rewards):
|
348
|
+
'''
|
349
|
+
Compute the attention-based rewards
|
350
|
+
'''
|
351
|
+
rewards = torch.tensor(rewards, dtype=torch.double)
|
352
|
+
net = Attention(len(rewards), output_dim)
|
353
|
+
net = net.double() # Convert the weights to Double
|
354
|
+
final_reward = net(rewards)
|
355
|
+
return final_reward
|
332
356
|
|
333
357
|
def integrate_mobility(self):
|
334
358
|
'''
|
@@ -0,0 +1,7 @@
|
|
1
|
+
gym_examples/__init__.py,sha256=wE0UbTYKFzfps8WaV0sJVNYVtrIOFHjFfECrlT0DlmE,193
|
2
|
+
gym_examples/envs/__init__.py,sha256=lgMe4pyOuUTgTBUddM0iwMlETsYTwFShny6ifm8PGM8,53
|
3
|
+
gym_examples/envs/wsn_env.py,sha256=uV5c1RRIyTkA6KMmKuZTLsDWjFNVfALOUoUMLDuwdz0,18917
|
4
|
+
gym_examples-3.0.33.dist-info/METADATA,sha256=ebgEj1_GpRVmSw5xzyC88RYQNQ7H47LBmLkdQa__jtA,411
|
5
|
+
gym_examples-3.0.33.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
6
|
+
gym_examples-3.0.33.dist-info/top_level.txt,sha256=rJRksoAF32M6lTLBEwYzRdo4PgtejceaNnnZ3HeY_Rk,13
|
7
|
+
gym_examples-3.0.33.dist-info/RECORD,,
|
@@ -1,7 +0,0 @@
|
|
1
|
-
gym_examples/__init__.py,sha256=augD2S6JxvSYGCFGUz5j2KohuhHRrqFqekAH7LtUdZ4,193
|
2
|
-
gym_examples/envs/__init__.py,sha256=lgMe4pyOuUTgTBUddM0iwMlETsYTwFShny6ifm8PGM8,53
|
3
|
-
gym_examples/envs/wsn_env.py,sha256=HDLGNMvbhGuweGA0IcJkhw1VuzdSiMTN0Ip-6rdvXUQ,18132
|
4
|
-
gym_examples-3.0.31.dist-info/METADATA,sha256=IQDb4-6MmoLEdaGk9c2paIu5VrOp31WLclbjjW9M2mM,411
|
5
|
-
gym_examples-3.0.31.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
6
|
-
gym_examples-3.0.31.dist-info/top_level.txt,sha256=rJRksoAF32M6lTLBEwYzRdo4PgtejceaNnnZ3HeY_Rk,13
|
7
|
-
gym_examples-3.0.31.dist-info/RECORD,,
|
File without changes
|
File without changes
|