gym-examples 3.0.30__py3-none-any.whl → 3.0.32__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
@@ -6,4 +6,4 @@ register(
6
6
  max_episode_steps=50,
7
7
  )
8
8
 
9
- __version__ = "3.0.30"
9
+ __version__ = "3.0.32"
@@ -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 # number of individual rewards
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
@@ -69,24 +63,7 @@ class WSNRoutingEnv(gym.Env):
69
63
 
70
64
 
71
65
  def reset(self):
72
- # Save the metrics to a CSV file
73
- if self.episode_count > 0:
74
- with open('metrics.csv', mode='a') as file:
75
- writer = csv.writer(file)
76
- if self.episode_count == 1:
77
- writer.writerow(['Episode', 'Network throughput', 'Energy efficiency', 'Packet delivery ratio', 'Network lifetime', 'Average latency', 'First node dead time'])
78
- writer.writerow([self.episode_count - 1, self.network_throughput, self.energy_efficiency, self.packet_delivery_ratio, self.network_lifetime, self.average_latency, self.first_node_dead_time])
79
-
80
- if self.episode_count % 50 == 0 and self.episode_count > 0:
81
- print(f"\n========================================")
82
- print(f"Episode {self.episode_count} with previous number of steps = {self.steps}")
83
- print(f"Network throughput = {self.network_throughput}")
84
- print(f"Energy efficiency = {self.energy_efficiency}")
85
- print(f"Packet delivery ratio = {self.packet_delivery_ratio}")
86
- print(f"Network lifetime = {self.network_lifetime}")
87
- print(f"Average latency = {self.average_latency}")
88
- print(f"First node dead time = {self.first_node_dead_time}")
89
- print(f"========================================\n")
66
+
90
67
  self.sensor_positions = np.random.rand(self.n_sensors, 2) * (upper_bound - lower_bound) + lower_bound
91
68
  self.distance_to_base = np.linalg.norm(self.sensor_positions - base_station_position, axis=1)
92
69
  self.remaining_energy = np.ones(self.n_sensors) * initial_energy
@@ -146,7 +123,6 @@ class WSNRoutingEnv(gym.Env):
146
123
  self.total_latency += self.packet_latency[i] + latency_per_hop
147
124
  self.packet_latency[i] = 0
148
125
 
149
- # rewards[i] = self.compute_individual_rewards(i, action)
150
126
  rewards[i] = np.ones(input_dim) # input_dim should be equal to the number of individual rewards
151
127
  dones[i] = True
152
128
  else:
@@ -174,13 +150,7 @@ class WSNRoutingEnv(gym.Env):
174
150
  self.number_of_packets[action] += self.number_of_packets[i]
175
151
  self.number_of_packets[i] = 0 # Reset the number of packets of the sensor i
176
152
  # Calculate final reward
177
- rewards_individual = torch.tensor(rewards[i], dtype=torch.double)
178
- final_reward = net(rewards_individual)
179
- # final_reward = np.sum(rewards[i])
180
- # weights = np.ones(self.n_sensors, dtype=int)
181
- # final_reward = np.sum(reward * weight for reward, weight in zip(rewards[i], weights))
182
- rewards[i] = final_reward
183
- # rewards = np.mean(rewards)
153
+ rewards[i] = self.compute_attention_rewards(rewards[i])
184
154
  for i in range(self.n_sensors):
185
155
  if (self.remaining_energy[i] <= 0) or (self.number_of_packets[i] <= 0):
186
156
  dones[i] = True
@@ -193,12 +163,18 @@ class WSNRoutingEnv(gym.Env):
193
163
  if self.first_node_dead_time is None and np.any(self.remaining_energy <= 0):
194
164
  self.first_node_dead_time = self.steps
195
165
 
166
+ self.get_metrics()
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 for r in rewards])
173
+
196
174
  return self._get_obs(), rewards, dones, {}
197
175
 
198
176
  def _get_obs(self):
199
177
 
200
- performance = self.get_metrics()
201
-
202
178
  return [{'remaining_energy': np.array([e]),
203
179
  'consumption_energy': np.array([initial_energy - e]),
204
180
  'sensor_positions': p,
@@ -316,7 +292,7 @@ class WSNRoutingEnv(gym.Env):
316
292
  Compute the individual rewards
317
293
  '''
318
294
  reward_angle = self.compute_reward_angle(i, action)
319
- reward_distance = self.compute_reward_distance(i, action)
295
+ # reward_distance = self.compute_reward_distance(i, action)
320
296
  reward_consumption_energy = self.compute_reward_consumption_energy(i, action)
321
297
  reward_dispersion_remaining_energy = self.compute_reward_dispersion_remaining_energy()
322
298
  reward_number_of_packets = self.compute_reward_number_of_packets(action)
@@ -344,6 +320,39 @@ class WSNRoutingEnv(gym.Env):
344
320
  normalized_total_energy = total_energy / max_total_energy
345
321
 
346
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
347
356
 
348
357
  def integrate_mobility(self):
349
358
  '''
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gym-examples
3
- Version: 3.0.30
3
+ Version: 3.0.32
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=tfSANHUypx5RPlDg703z8BuizUUYBVQT5JGSL3_a1Ec,193
2
+ gym_examples/envs/__init__.py,sha256=lgMe4pyOuUTgTBUddM0iwMlETsYTwFShny6ifm8PGM8,53
3
+ gym_examples/envs/wsn_env.py,sha256=G6SiehYtP1gWLyHeskzzEJGnI-QRnUCu8B0__gIOFIk,18900
4
+ gym_examples-3.0.32.dist-info/METADATA,sha256=ZvRIjNWc1l-ElSzEC34USNseXM_-Rtcn-m4UIDaymnE,411
5
+ gym_examples-3.0.32.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
6
+ gym_examples-3.0.32.dist-info/top_level.txt,sha256=rJRksoAF32M6lTLBEwYzRdo4PgtejceaNnnZ3HeY_Rk,13
7
+ gym_examples-3.0.32.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- gym_examples/__init__.py,sha256=GPszuDx3V8PlkxDC4jh30zrtX0lVvTwIxu0nvlaZV80,193
2
- gym_examples/envs/__init__.py,sha256=lgMe4pyOuUTgTBUddM0iwMlETsYTwFShny6ifm8PGM8,53
3
- gym_examples/envs/wsn_env.py,sha256=oFnj-yRCNBywQGr8hJ06KJ5JTmhb-upzX3B3_CwlAUw,19422
4
- gym_examples-3.0.30.dist-info/METADATA,sha256=-vJtGfmrqF6cAFfuViJ2_hGQi-00tBc0yeYOl2_Jb_w,411
5
- gym_examples-3.0.30.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
6
- gym_examples-3.0.30.dist-info/top_level.txt,sha256=rJRksoAF32M6lTLBEwYzRdo4PgtejceaNnnZ3HeY_Rk,13
7
- gym_examples-3.0.30.dist-info/RECORD,,