-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_env.py
132 lines (119 loc) · 3.91 KB
/
check_env.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
import os
import sys
import gym
import click
import matplotlib
import numpy as np
from typing import Dict, Any
import time
import json
from pprint import PrettyPrinter
import matplotlib.pyplot as plt
matplotlib.use("Agg")
pp = PrettyPrinter(indent=4)
# get an absolute path to the directory that contains parent files
project_dir = os.path.dirname(os.path.join(os.getcwd(), __file__))
sys.path.append(os.path.normpath(os.path.join(project_dir, '..', '..')))
from experiments.utils.constants import (
ENVSMAP,
CONFIGS_PATH,
DATA_PATH
)
from experiments.utils import (
add_path_to_config,
action_pretty_print,
config_check_env_check
)
def check_env(*, config: Dict[str, Any], type_env: str,
cluster_id: int, workload_id: int):
env_config_base = config["env_config_base"]
env_config = add_path_to_config(
config=env_config_base,
cluster_id=cluster_id,
workload_id=workload_id
)
if type_env not in ['CartPole-v0', 'Pendulum-v0']:
type_env = ENVSMAP[type_env]
env = gym.make(type_env, config=env_config)
else:
env = gym.make(type_env)
i = 1
total_timesteps = 10000
_ = env.reset()
rewards = []
rewards_cv = []
rewards_p = []
rewards_g = []
while i < total_timesteps:
action = env.action_space.sample()
# action = 1
_, reward, done, info = env.step(action)
if info['scheduling_timestep']:
print('scheudling timestep')
if info['scheduling_success']:
print('scheduling successful :)')
else:
print('scheduling unsuccessful :(')
rewards.append(reward)
rewards_cv.append(info['rewards']['cv'])
rewards_p.append(info['rewards']['p'])
rewards_g.append(info['rewards']['g'])
env.render()
if env.time % 100 == 0:
TEMP = 1
print("time: {}".format(
env.time
))
print("timestep_episode: {}".format(
env.timestep_episode
))
print("reward: {}".format(
reward
))
print("rewards: {}".format(
info['rewards']
))
if done and not env.complete_done:
_ = env.reset()
print(20*'-' + ' Done for episode! ' + 20*'-')
if done and env.complete_done:
print( + 40*'=' + ' Done for ending the simulation ' + 40*'=')
break
i += 1
x = np.arange(len(rewards))
plt.plot(x, np.array(rewards), label = "Total Rewards")
plt.plot(x, np.array(rewards_cv), label = "Variance reward")
plt.plot(x, np.array(rewards_p), label = "Consolidation reward")
plt.xlabel('Timesteps')
plt.ylabel('Reward Value')
# plt.plot(x, np.array(rewards_g), label = "G")
plt.legend()
plt.grid()
plt.savefig(f'rewards-{cluster_id}.png')
@click.command()
@click.option('--type-env', required=True,
type=click.Choice(['sim-scheduler', 'sim-binpacking',
'kube-scheduler', 'kube-binpacking',
'CartPole-v0', 'Pendulum-v0']),
default='sim-scheduler')
@click.option('--cluster-id', required=True, type=int, default=21)
@click.option('--workload-id', required=True, type=int, default=2)
def main(type_env: str, cluster_id: int, workload_id: int):
"""[summary]
Args:
type_env (str): the type of the used environment
cluster_id (int): the cluster metadata (size, #nodes, etc)
workload_id (int): the workload used in that cluster
"""
config_file_path = os.path.join(
CONFIGS_PATH, 'check',
f'check_env.json')
with open(config_file_path) as cf:
config = json.loads(cf.read())
config_check_env_check(config)
check_env(config=config,
type_env=type_env,
cluster_id=cluster_id,
workload_id=workload_id)
if __name__ == "__main__":
main()