-
Notifications
You must be signed in to change notification settings - Fork 0
/
envext3.py
227 lines (182 loc) · 11.8 KB
/
envext3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import gymnasium as gym
from gymnasium.spaces import Discrete, Box
from gym import Env
#from gym.spaces import Discrete, Box, Dict, MultiBinary
import numpy as np
from gym.vector.utils import spaces
from reset_ALGO2 import system1
import random
# tuple of action , obs with channel for rayllib, no normalization for obs apce
class MCS(gym.Env):
def __init__(self,config):
self.t_interval=config["t_interval"]
#self.T=config["T"]
self.T=config["T"]
self.h=config["h"]
self.K=config["K"]
self.pmax=config["pmax"]
self.W=config["W"]
self.t_sense_distribution=config["t_sense_distribution"]
self.t_sense_distribution_ideal=config["t_sense_distribution_ideal"]
self.E_sense_distribution=config["E_sense_distribution"]
self.p_sense_distribution=config["p_sense_distribution"]
self.throughput_distribution=config["throughput_distribution"]
self.required_sensors_per_task_distribution=config["required_sensors_per_task_distribution"]
self.E_harv=config["E_harv"]
self.task_deadline_distribution=config["task_deadline_distribution"]
self.task_types_distribution=config["task_types_distribution"]
self.normalized_req_sensors_per_task=config["normalized_req_sensors_per_task"]
self.normalized_deadline_distribution=config["normalized_deadline_distribution"]
self.normalized_throughput_distribution=config["normalized_throughput_distribution"]
self.initial_battery_state=config["initial_battery_state"]
self.sigma_n2=config["sigma_n2"]
#self.action_space=Box(0, 0.1, shape=(self.K, ), dtype=np.float32)
#self.continuous_action_space = Box(-1, 1, shape=(self.K,), dtype=np.float32)
#self.discrete_action_space=Discrete(2)
#self.action_space=(self.discrete_action_space, self.continuous_action_space)
# self.action_space=spaces.Tuple((MultiBinary(8), Box(-1, 1, shape=(self.K,), dtype=np.float32)))
# self.action_space = spaces.Tuple((Discrete(2), Discrete(2), Discrete(2), Discrete(2),Discrete(2), Discrete(2), Discrete(2), Discrete(2), Box(-1, 1, shape=(self.K,), dtype=np.float32)))
#self.observation_space=Box(0, 1,(, shape=(10, ), dtype=np.float32)
# self.observation_space = Box(low=np.array([0, 0, 0, 0, 0, 0, 0, 0, 10240, 0, 0.1,1.7e-9,1.7e-9,1.7e-9,1.7e-9, 1.7e-9, 1.7e-9,1.7e-9,1.7e-9]), high=np.array(
# [0.032, 0.032, 0.032, 0.032, 0.032, 0.032, 0.032, 0.032, 256000, 8, 0.2, 1.1e-6, 1.1e-6,1.1e-6,1.1e-6,1.1e-6,1.1e-6,1.1e-6,1.1e-6]), dtype=np.float32)
#self.action_space=gym.spaces.Tuple((gym.spaces.Tuple([gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Box(-1, 1, shape=(self.K,), dtype=np.float32))]))
self.action_space=gym.spaces.Tuple([ gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Box(-1, 1, shape=(self.K,), dtype=np.float32)])
#self.action_space=gym.spaces.Tuple([gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Discrete(2), gym.spaces.Box(-1, 1, shape=(self.K,), dtype=np.float32)])
# self.observation_space = Box(low=np.array([0, 0, 0, 0, 0, 0, 0, 0, 10240, 0, 0,0,0,0,0,0,0,0,0]), high=np.array(
#[0.032, 0.032, 0.032, 0.032, 0.032, 0.032, 0.032, 0.032, 256000, 8, 0.2, 1, 1,1,1,1,1,1,1]), dtype=np.float32)
#self.observation_space=Box(low=np.array([0,0,0,0,0,0,0,0,10240,0,0.098]), high=np.array([0.032,.032,0.032,0.032,0.032,0.032,0.032,0.032,256000,8, 0.19]), dtype=np.float32)
self.observation_space = Box(low=np.array([0, 0, 0, 0, 10240, 0, 0, 0, 0, 0, 0]),
high=np.array(
[10, 10, 10, 10, 256000, 8, 20, 1, 1,
1, 1]), dtype=np.float32)
self.j=1
self.rewards=0
#self.state=np.column_stack((self.initial_battery_state.copy()/0.032, self.throughput_distribution[0,self.task_types_distribution[self.j]].copy()/256000, self.required_sensors_per_task_distribution[self.j].copy()/8))
self.obs=np.column_stack((self.initial_battery_state.copy(), self.throughput_distribution[0,self.task_types_distribution[self.j]].copy(), self.required_sensors_per_task_distribution[self.j].copy(), self.task_deadline_distribution[self.j],self.h[:,self.j-1].reshape(1,self.K) ))[0]
self.rho = 10 * pow(10, -3)
self.Omega = 16
self.Bmax = self.rho * self.Omega * self.t_interval
self.i=config["i"]
self.average_channel_coeff_per_sensor=config["average_channel_coeff_per_sensor"]
self.average_channel_gain=config["average_channel_gain"]
self.avg_SNR_linear=config["avg_SNR_linear"]
#self.h=h
#self.iii=0
self.cond1=0
self.cond2=0
self.cond3=0
#self.num_envs=1
pass
def step(self, action):
sensors=self.required_sensors_per_task_distribution[self.j]
A = []
ACT=[]
A2=[]
y = []
X = []
Y = []
Z = []
rr = 0
rrr = []
alpha = 0
sensors=0
E_exec = np.zeros(self.K)
tau_exec = np.zeros(self.K)
tau_tx = np.zeros(self.K)
# take the battery from state
s = self.obs[0:self.K]
if self.task_types_distribution[self.j] == 0:
self.rewards = 0
else:
for i in range(self.K):
#check if we have task allocation and power_tx !=0 if yes so we convert action between 0 and 0.1 and calculate E_exec ....
if action[i]==1 and action[self.K][i]!=-1:
sensors=sensors+1
act = ((action[self.K][i] + 1) * 0.1) / 2
A.append(act)
# print('act', act)
#sigma_n2[i] = ((self.average_channel_gain) * act / self.avg_SNR_linear)
# print('sigma', sigma_n2[i])
alpha = np.divide(np.multiply(act, (self.h[i,self.j])** 2), self.sigma_n2)
# print('act',act)
tau_tx[i] = np.divide(self.throughput_distribution[0, self.task_types_distribution[self.j]],(self.W * np.log2(1 + alpha)))
tau_exec[i] = tau_tx[i] + self.t_sense_distribution[i][self.task_types_distribution[self.j]][self.j]
E_exec[i] = tau_tx[i] * act + self.E_sense_distribution[i][self.task_types_distribution[self.j]][self.j]
X.append(tau_exec[i])
Y.append(E_exec[i])
Z.append(s[i])
elif action[i]==1 and action[self.K][i]==-1:
# in case the sensor is selected but ptx_=0 we just reduce the battery by E_exec. no immediat zeros reward
E_exec[i]=self.E_sense_distribution[i][self.task_types_distribution[self.j]][self.j]
# print('action', A)
if not tau_tx.any():
# check if some sensors are appended == selected
self.rewards=0
else:
# if some sensors are appended , check condition for reward
if sensors == self.required_sensors_per_task_distribution[self.j]:
if ((np.array(X) - np.array(self.task_deadline_distribution[self.j])) <= 0).all():
if (np.array(Z) >= np.array(Y)).all():
rr = self.normalized_throughput_distribution[self.j] + self.normalized_deadline_distribution[
self.j] + self.normalized_req_sensors_per_task[self.j]
self.rewards = rr
else:
self.rewards = 0
self.cond3=self.cond3+1
else:
self.rewards = 0
self.cond2 = self.cond2 + 1
else:
self.rewards=0
self.cond1 = self.cond1 + 1
if self.j == self.T:
done = True
else:
# battery update
done = False
s = s - E_exec + self.E_harv[:, self.j]
s = np.clip(s, a_min=0, a_max=self.Bmax)
self.j += 1
#s = s - E_exec + self.E_harv[:, self.j]
#s = np.clip(s, a_min=0, a_max=self.Bmax)
# input1=np.interp(s, [0, 0.032], [-1, 1])
# input2=np.interp(self.throughput_distribution[0, self.task_types_distribution[self.j]], [10240, 256000], [-1, 1])
# input3=np.interp(self.required_sensors_per_task_distribution[self.j], [0, 8], [-1, 1])
# self.state=np.concatenate((input1.reshape(1, 8), input2.reshape(1,1), input3.reshape(1,1)), axis=1)
# self.state = np.concatenate(((s).reshape(1, 8),
# (self.throughput_distribution[0, self.task_types_distribution[self.j]]).reshape(1, 1),
# (self.required_sensors_per_task_distribution[self.j]).reshape(1, 1)), axis = 1)
# self.obs = [np.concatenate((s.reshape(1, 8),
# self.throughput_distribution[0, self.task_types_distribution[self.j]].reshape(1,1),
# self.required_sensors_per_task_distribution[self.j].reshape(1,1),self.task_deadline_distribution[self.j].reshape(1,1),self.h[:,self.j-1].reshape(1,8)),axis=1)[0]]
self.obs = np.column_stack((s.reshape(1, self.K), self.throughput_distribution[0, self.task_types_distribution[self.j]].copy(),self.required_sensors_per_task_distribution[self.j].copy(),self.task_deadline_distribution[self.j],self.h[:, self.j - 1].reshape(1, self.K)))[0]
# print('13', self.obs)
infos = {}
# print('state', self.state)
# self.reward+=1
# print('REWARD', self.rewards)
return self.obs, self.rewards, done, False, infos
pass
def reset(self, seed=None, options=None):
self.T=self.T
self.j=1
self.rewards=0
infos = {}
self.obs = np.column_stack((self.initial_battery_state.copy(),
self.throughput_distribution[0, self.task_types_distribution[self.j]].copy(),
self.required_sensors_per_task_distribution[self.j].copy(),
self.task_deadline_distribution[self.j], self.h[:, self.j - 1].reshape(1, self.K)))[0]
#self.obs = np.column_stack((self.initial_battery_state,
# self.throughput_distribution[0, self.task_types_distribution[self.j]].copy(),
# self.required_sensors_per_task_distribution[self.j].copy(),
# self.task_deadline_distribution[self.j], self.h[:, self.j - 1].reshape(1, 8)))
# print('e_harv', len(self.E_harv[:,self.j]))
#input1 = np.interp(self.initial_battery_state.copy(), [0, 0.032], [-1, 1])
#input2 = np.interp(self.throughput_distribution[0, self.task_types_distribution[self.j]], [10240, 256000],
# [-1, 1])self.initial_battery_state
#input3 = np.interp(self.required_sensors_per_task_distribution[self.j], [0, 8], [-1, 1])
#self.state = np.column_stack((input1,os input2, input3))
# self.state=np.column_stack((self.initial_battery_state, self.throughput_distribution[0,self.task_types_distribution[self.j]],self.required_sensors_per_task_distribution[self.j]))
# self.obs=[np.column_stack((self.initial_battery_state.copy(), self.throughput_distribution[0,self.task_types_distribution[self.j]].copy(),self.required_sensors_per_task_distribution[self.j].copy(), self.task_deadline_distribution[self.j], self.h[:,self.j-1].reshape(1,8)))[0]]
return self.obs, infos
pass