-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_envs.py
98 lines (77 loc) · 3.34 KB
/
test_envs.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
from make_env import make_env
import numpy as np
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Base init and setup for training or display')
parser.add_argument('-scen_name', type=str, help='Choose scenarios for training or display')
args = parser.parse_args()
name = args.scen_name
print('Choose scen:{}'.format(name))
MAX_EPOCH = 1000
render_flag = True
env = make_env('simple_speaker_listener')
obs_ls = env.reset()
for i in range(MAX_EPOCH):
obs = env.reset()
score_ls = np.array([0. for _ in range(env.n)]) # n个代理的回合得分表
while True:
if render_flag:
env.render()
# 动作序列
action_ls = []
for i, agent in enumerate(env.world.agents):
agent_action_space = env.action_space[i]
action = agent_action_space.sample()
action_vec = np.zeros(agent_action_space.n)
action_vec[action] = 1
action_ls.append(action_vec)
obs_next_ls, reward_ls, done_ls, info_ls = env.step(action_ls)
score_ls += reward_ls
obs_ls = obs_next_ls
# ******* 打印回合结果 ******** #
# print()
# from make_env import make_env
# import numpy as np
# env = make_env('simple_tag')
# for i_episode in range(20):
# observation = env.reset()
# for t in range(100):
# env.render()
# agent_actions = []
# for i, agent in enumerate(env.world.agents):
# # This is a Discrete
# # https://github.com/openai/gym/blob/master/gym/spaces/discrete.py
# agent_action_space = env.action_space[i]
# # Sample returns an int from 0 to agent_action_space.n
# action = agent_action_space.sample()
# # Environment expects a vector with length == agent_action_space.n
# # containing 0 or 1 for each action, 1 meaning take this action
# action_vec = np.zeros(agent_action_space.n)
# action_vec[action] = 1
# agent_actions.append(action_vec)
# # Each of these is a vector parallel to env.world.agents, as is agent_actions
# observation, reward, done, info = env.step(agent_actions)
# print (observation)
# print (reward)
# print (done)
# print (info)
# print()
class Solution(object):
def rob(self, root):
res = []
def search_node(node, depth):
if not node:
return
if len(res) <= depth:
res.append([])
res[depth].append(node.val)
search_node(node.left, depth + 1)
search_node(node.right, depth + 1)
search_node(root, 0)
res = [sum(i) for i in res]
dp = [0] * len(res)
dp[0] = res[0]
dp[1] = max(res[0], res[1])
for i in range(2, len(dp)):
dp[i] = max(dp[i - 2] + res[i], dp[i - 1])
return dp[-1]