-
Notifications
You must be signed in to change notification settings - Fork 0
/
beb_tests.py
144 lines (120 loc) · 3.85 KB
/
beb_tests.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
#%%
from ns3gym import ns3env
from comet_ml import Experiment, Optimizer
import tqdm
import subprocess
from collections import deque
import numpy as np
from preprocessor import Preprocessor
import sys
import argparse
from agents.teacher import Teacher, EnvWrapper
parser = argparse.ArgumentParser(description='Run BEB tests')
parser.add_argument('stations', metavar='N', type=int, nargs='+',
help='number of stations for the scenario (min: 5)')
parser.add_argument('--scenario', dest='scenarios', type=str, nargs='+',
help='scenarios to run (available: [basic, convergence])')
parser.add_argument('--beb', dest='beb', action='store_const',
const=True, default=False,
help='run 802.11 default instead of look-up table')
args = parser.parse_args()
for sc in args.scenarios:
if sc != "basic" and sc!="convergence":
print("Wrong scenario!")
parser.print_help()
sys.exit()
print("Stations count:", args.stations)
print("Scenarios:", args.scenarios)
if args.beb:
print("802.11 default")
else:
print("Look-up table")
class Agent:
TYPE = "continuous"
NAME = "STATIC"
# NAME = "BEB"
def __init__(self, action_space):
self.action_space = action_space
self.actor_loss = 0
self.critic_loss = 0
self.lookup = {5: 32, 10:64, 15:128, 25:256}
self.lookup = {}
for i in range(5, 10):
self.lookup[i] = 32
for i in range(10, 15):
self.lookup[i] = 64
for i in range(15, 25):
self.lookup[i] = 128
for i in range(25, 51):
self.lookup[i] = 256
self.current_cw = 32
def act(self, stations_count, *args):
# return np.random.sample(self.action_space)\
stations_count = int(stations_count)
if stations_count in self.lookup.keys():
self.current_cw = self.lookup[stations_count]
res = np.array([[np.log2(self.current_cw)-4]])
return res
def step(self, *args):
pass
def reset(self):
pass
def get_loss(self):
return {"loss": 0}
def __getattribute__(self, attr):
try:
return object.__getattribute__(self, attr)
except AttributeError:
def foo(*args):
pass
return foo
#%%
# scenario = "convergence"
simTime = 60 # seconds
stepTime = 0.01 # seconds
history_length = 300
EPISODE_COUNT = 1
steps_per_ep = int(simTime/stepTime)
if args.beb:
agent_name = "BEB"
else:
agent_name = Agent.NAME
rng = 0
for scenario in args.scenarios:
nwifi = args.stations
if scenario=="convergence":
if nwifi[0] == 5:
nwifi[0] = 6
for nw in nwifi:
sim_args = {
"simTime": simTime,
"envStepTime": stepTime,
"historyLength": history_length,
"agentType": Agent.TYPE,
"scenario": scenario,
"nWifi": nw,
"seed": np.random.randint(2**16),
"rng": rng
}
rng += 1
if args.beb:
sim_args["dryRun"] = True
print("Steps per episode:", steps_per_ep)
threads_no = 1
env = EnvWrapper(threads_no, **sim_args)
#%%
env.reset()
ob_space = env.observation_space
ac_space = env.action_space
print("Observation space shape:", ob_space)
print("Action space shape:", ac_space)
assert ob_space is not None
tags = [f"{agent_name}",
"Final",
sim_args['scenario'],
f"Station count: {sim_args['nWifi']}",
*[f"{key}: {sim_args[key]}" for key in list(sim_args)[:3]]]
#%%
teacher = Teacher(env, 1, Preprocessor(False))
agent = Agent(env.action_space)
logger = teacher.eval(agent, simTime, stepTime, history_length, tags)