gym-examples 3.0.724__py3-none-any.whl → 3.0.726__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 +40 -8
- {gym_examples-3.0.724.dist-info → gym_examples-3.0.726.dist-info}/METADATA +1 -1
- gym_examples-3.0.726.dist-info/RECORD +7 -0
- gym_examples-3.0.724.dist-info/RECORD +0 -7
- {gym_examples-3.0.724.dist-info → gym_examples-3.0.726.dist-info}/WHEEL +0 -0
- {gym_examples-3.0.724.dist-info → gym_examples-3.0.726.dist-info}/top_level.txt +0 -0
gym_examples/__init__.py
CHANGED
gym_examples/envs/wsn_env.py
CHANGED
@@ -211,7 +211,7 @@ class WSNRoutingEnv(gym.Env):
|
|
211
211
|
|
212
212
|
self.number_of_packets[i] = 0 # Reset the number of packets of the sensor i
|
213
213
|
# Calculate final reward
|
214
|
-
rewards[i] = self.compute_attention_rewards(rewards[i])
|
214
|
+
# rewards[i] = self.compute_attention_rewards(rewards[i])
|
215
215
|
# rewards[i] = np.mean(rewards[i])
|
216
216
|
|
217
217
|
# Integrate the mobility of the sensors
|
@@ -225,8 +225,9 @@ class WSNRoutingEnv(gym.Env):
|
|
225
225
|
self.get_metrics()
|
226
226
|
|
227
227
|
rewards = [reward.item() if isinstance(reward, torch.Tensor) else reward for reward in rewards] # Convert the reward to a float
|
228
|
-
|
229
|
-
|
228
|
+
|
229
|
+
rewards = self.compute_attention_reward(rewards)
|
230
|
+
rewards = np.sum(rewards)
|
230
231
|
|
231
232
|
for i in range(self.n_sensors):
|
232
233
|
if not dones[i]:
|
@@ -465,13 +466,44 @@ class WSNRoutingEnv(gym.Env):
|
|
465
466
|
return np.clip(normalized_throughput, 0, 1)
|
466
467
|
|
467
468
|
|
468
|
-
def compute_attention_rewards(self, rewards):
|
469
|
+
# def compute_attention_rewards(self, rewards):
|
470
|
+
# '''
|
471
|
+
# Compute the attention-based rewards
|
472
|
+
# '''
|
473
|
+
# rewards = torch.tensor(rewards, dtype=torch.double)
|
474
|
+
# final_reward = net(rewards)
|
475
|
+
# return final_reward
|
476
|
+
|
477
|
+
|
478
|
+
def compute_attention_reward(self, rewards):
|
469
479
|
'''
|
470
|
-
Compute the attention-based
|
480
|
+
Compute the attention-based reward for the network with Q, V, and K matrices
|
481
|
+
|
482
|
+
Input:
|
483
|
+
- rewards: list of rewards for each sensor
|
484
|
+
Output:
|
485
|
+
- final_reward: final reward for the network
|
471
486
|
'''
|
472
|
-
|
473
|
-
|
474
|
-
|
487
|
+
# dimension of the sensor embedding
|
488
|
+
d = len(rewards[0])
|
489
|
+
|
490
|
+
# All sensors are represented by their raw rewards
|
491
|
+
query_vector = [max_reward] * d) # Basically, the target is the base station
|
492
|
+
|
493
|
+
# Similarities between the query vector and the rewards
|
494
|
+
similarities = [np.dot(query_vector, reward) for reward in rewards]
|
495
|
+
|
496
|
+
# Similarities scaling
|
497
|
+
similarities = [similarity / np.sqrt(d) for similarity in similarities]
|
498
|
+
|
499
|
+
# Softmax operation
|
500
|
+
denominator = sum([np.exp(similarity) for similarity in similarities])
|
501
|
+
attention_weights = [np.exp(similarity) / denominator for similarity in similarities]
|
502
|
+
|
503
|
+
# Weighted sum of the rewards
|
504
|
+
final_reward = sum([attention_weight * reward for attention_weight, reward in zip(attention_weights, rewards)])
|
505
|
+
|
506
|
+
return final_reward
|
475
507
|
|
476
508
|
|
477
509
|
def integrate_mobility(self):
|
@@ -0,0 +1,7 @@
|
|
1
|
+
gym_examples/__init__.py,sha256=uumjAP8o6H1zTcsXxnS9vkZq4QJsG507NyrsYL47Ffk,166
|
2
|
+
gym_examples/envs/__init__.py,sha256=lgMe4pyOuUTgTBUddM0iwMlETsYTwFShny6ifm8PGM8,53
|
3
|
+
gym_examples/envs/wsn_env.py,sha256=Z5OS65J_qwBorJdbeGBBN56xp4hey-IQZtvzH3dGxJo,26686
|
4
|
+
gym_examples-3.0.726.dist-info/METADATA,sha256=vzWTlxeSCnU5TQSmoH1C4YxnBAJ5TPqplNLpHilLiLM,412
|
5
|
+
gym_examples-3.0.726.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
6
|
+
gym_examples-3.0.726.dist-info/top_level.txt,sha256=rJRksoAF32M6lTLBEwYzRdo4PgtejceaNnnZ3HeY_Rk,13
|
7
|
+
gym_examples-3.0.726.dist-info/RECORD,,
|
@@ -1,7 +0,0 @@
|
|
1
|
-
gym_examples/__init__.py,sha256=Lm7GIk_8Yp9w3d60B2-sbWmMPMUX5vzsZl_kZoa7Lzw,166
|
2
|
-
gym_examples/envs/__init__.py,sha256=lgMe4pyOuUTgTBUddM0iwMlETsYTwFShny6ifm8PGM8,53
|
3
|
-
gym_examples/envs/wsn_env.py,sha256=D3EaMLo38IBUfq0CCGX8kmDnwwBtyKTEaNv4iIzxyn4,25477
|
4
|
-
gym_examples-3.0.724.dist-info/METADATA,sha256=XPoX8NDFt5ZtoD8r8-7zuBANMVP5w3S3FIndfnJbhZs,412
|
5
|
-
gym_examples-3.0.724.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
6
|
-
gym_examples-3.0.724.dist-info/top_level.txt,sha256=rJRksoAF32M6lTLBEwYzRdo4PgtejceaNnnZ3HeY_Rk,13
|
7
|
-
gym_examples-3.0.724.dist-info/RECORD,,
|
File without changes
|
File without changes
|