gym-examples 3.0.725__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 CHANGED
@@ -5,4 +5,4 @@ register(
5
5
  entry_point="gym_examples.envs:WSNRoutingEnv",
6
6
  )
7
7
 
8
- __version__ = "3.0.725"
8
+ __version__ = "3.0.726"
@@ -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
- rewards = np.mean(rewards) # Average the rewards
229
- # rewards = np.sum(rewards) # Sum the rewards
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 rewards
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
- rewards = torch.tensor(rewards, dtype=torch.double)
473
- final_reward = net(rewards)
474
- return final_reward
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):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gym-examples
3
- Version: 3.0.725
3
+ Version: 3.0.726
4
4
  Summary: A custom environment for multi-agent reinforcement learning focused on WSN routing.
5
5
  Home-page: https://github.com/gedji/CODES.git
6
6
  Author: Georges Djimefo
@@ -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=-Kmeb4ubrlPSneFp-1LSMYXP3_bQgfWL4xtyJWolkzQ,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.725.dist-info/METADATA,sha256=6p_wC3Lyziuoko2sqMp47ERTpHeGmSl45hOKypTiFoQ,412
5
- gym_examples-3.0.725.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
6
- gym_examples-3.0.725.dist-info/top_level.txt,sha256=rJRksoAF32M6lTLBEwYzRdo4PgtejceaNnnZ3HeY_Rk,13
7
- gym_examples-3.0.725.dist-info/RECORD,,