frameworks
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import numpy as np
|
||||
from GEO_parameters import GEO_Parameters
|
||||
import torch
|
||||
|
||||
|
||||
class GEOEnv:
|
||||
def __init__(self, num_satellite = 1, num_carrier_wave = 100, num_beam = 1, num_user_dim = 32, delt_t =5, dataset = "month_2.txt", max_intervals = 8):
|
||||
self.action_space = num_carrier_wave
|
||||
self.user_space_dim = num_user_dim
|
||||
self.user_space = num_user_dim * num_user_dim
|
||||
self.delt_t = delt_t
|
||||
self.beams = num_beam
|
||||
self.satellite = num_satellite
|
||||
self.steps_before_termination = 0
|
||||
self.datas = []
|
||||
self.dataset = dataset
|
||||
self.num_data = len(self.datas)
|
||||
self.state = np.zeros((self.action_space+1, self.user_space_dim, self.user_space_dim), dtype=np.uint8)
|
||||
self.max_intervals = 8
|
||||
|
||||
def load_data(self):
|
||||
with open(self.dataset, 'r') as f:
|
||||
self.datas = f.readlines()
|
||||
self.num_data = len(self.datas)
|
||||
|
||||
def compute_state(self, index):
|
||||
# state = self.state.copy()
|
||||
self.state[-1] = np.zeros_like(self.state[-1])
|
||||
[date_t, beam_type, user, ap] = self.datas[index].split('\t')
|
||||
user = int(user)
|
||||
ap = int(ap)
|
||||
beam_type = int(beam_type)
|
||||
i = user//10
|
||||
j = user%10
|
||||
t = 0
|
||||
if ap == 0:
|
||||
t = beam_type
|
||||
else:
|
||||
t = -beam_type
|
||||
self.state[-1][i][j] = t
|
||||
return self.state
|
||||
|
||||
def compute_reward(self, action):
|
||||
assert type(action) == int
|
||||
assert action >= 0 and action < self.action_space
|
||||
# 功率暂时不用
|
||||
|
||||
# 频谱效率
|
||||
axis2_sum = np.sum(self.state, axis= 2)
|
||||
matrix_sum = np.sum(axis2_sum, axis=1)
|
||||
if (matrix_sum)
|
||||
t = 0.0
|
||||
for i in range(self.action_space):
|
||||
if matrix_sum[i] == 0:
|
||||
t += 1/self.action_space
|
||||
else:
|
||||
t += 1/self.action_space * matrix_sum[i]/self.max_intervals
|
||||
# 用户满意率,即action所在的信道的最大可用带宽的满足程度
|
||||
b = 0.0
|
||||
acquire = matrix_sum[-1]
|
||||
supply = self.max_intervals - matrix_sum[action]
|
||||
b = min(1.0, acquire/supply)
|
||||
# 如果用户被拒绝了会有一个非常大的惩罚
|
||||
if abs(b) < 1e-7 or supply <= 0:
|
||||
b = -1.0
|
||||
|
||||
# 载波共用的惩罚
|
||||
c:float = 0
|
||||
# 暂时不动
|
||||
return t + b
|
||||
|
||||
|
||||
def renew_environment():
|
||||
pass
|
||||
def step(self, action):
|
||||
self.state = self.compute_state(self.steps_before_termination)
|
||||
reward = self.compute_reward(action)
|
||||
|
||||
# self.pre
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
e = GEOEnv()
|
||||
e.load_data()
|
||||
print(len(e.datas))
|
||||
state = e.compute_state(0)
|
||||
print(state)
|
||||
state[1][1][2] = 4
|
||||
t = e.compute_reward(state, 1)
|
||||
print(t)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user