forked from werner-duvaud/muzero-general
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplified_muzero.py
108 lines (102 loc) · 4.27 KB
/
simplified_muzero.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
from simplifiedMuZero.net2.models2 import MuZeroNetwork_2net
from muzero_general import MuZeroGeneral
from muzero import load_model_menu, hyperparameter_search
import json
import sys
import pathlib
import time
import nevergrad
if __name__ == "__main__":
# muzero = MuZeroWithoutRB("",models.MuZeroNetwork, save_path_ex="muzero_without_rb")
# start_time = time.time()
# muzero.train()
# end_time = time.time()
# print("耗时: {:.2f}秒".format(end_time - start_time))
model_cls = MuZeroNetwork_2net
if len(sys.argv) == 2:
# Train directly with: python muzero.py cartpole
muzero = MuZeroGeneral(sys.argv[1], model_cls=model_cls)
muzero.train()
elif len(sys.argv) == 3:
# Train directly with: python muzero.py cartpole '{"lr_init": 0.01}'
config = json.loads(sys.argv[2])
muzero = MuZeroGeneral(sys.argv[1], config, model_cls=model_cls)
muzero.train()
else:
print("\nWelcome to MuZero! Here's a list of games:")
# Let user pick a game
games = [
filename.stem
for filename in sorted(list((pathlib.Path.cwd() / "games").glob("*.py")))
if filename.name != "abstract_game.py"
]
for i in range(len(games)):
print(f"{i}. {games[i]}")
choice = input("Enter a number to choose the game: ")
valid_inputs = [str(i) for i in range(len(games))]
while choice not in valid_inputs:
choice = input("Invalid input, enter a number listed above: ")
# Initialize MuZero
choice = int(choice)
game_name = games[choice]
muzero = MuZeroGeneral(game_name, model_cls=model_cls)
while True:
# Configure running options
options = [
"Train",
"Load pretrained model",
"Diagnose model",
"Render some self play games",
"Play against MuZero",
"Test the game manually",
"Hyperparameter search",
"Exit",
]
print()
for i in range(len(options)):
print(f"{i}. {options[i]}")
choice = input("Enter a number to choose an action: ")
valid_inputs = [str(i) for i in range(len(options))]
while choice not in valid_inputs:
choice = input("Invalid input, enter a number listed above: ")
choice = int(choice)
if choice == 0:
start_time = time.time()
muzero.train()
end_time = time.time()
print("耗时: {:.2f}秒".format(end_time - start_time))
elif choice == 1:
load_model_menu(muzero, game_name)
elif choice == 2:
muzero.diagnose_model(30)
elif choice == 3:
muzero.test(render=True, opponent="self", muzero_player=None)
elif choice == 4:
muzero.test(render=True, opponent="human", muzero_player=0)
elif choice == 5:
env = muzero.Game()
env.reset()
env.render()
done = False
while not done:
action = env.human_to_action()
observation, reward, done = env.step(action)
print(f"\nAction: {env.action_to_string(action)}\nReward: {reward}")
env.render()
elif choice == 6:
# Define here the parameters to tune
# Parametrization documentation: https://facebookresearch.github.io/nevergrad/parametrization.html
muzero.terminate_workers()
del muzero
budget = 20
parallel_experiments = 2
lr_init = nevergrad.p.Log(lower=0.0001, upper=0.1)
discount = nevergrad.p.Log(lower=0.95, upper=0.9999)
parametrization = nevergrad.p.Dict(lr_init=lr_init, discount=discount)
best_hyperparameters = hyperparameter_search(
game_name, parametrization, budget, parallel_experiments, 20
)
muzero = MuZeroGeneral(game_name, best_hyperparameters, model_cls=model_cls)
else:
break
print("\nDone")