-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
89 lines (79 loc) · 2.69 KB
/
run.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
import random
from pathlib import Path
from typing import Any
from src.pacman.keyboard_agents import KeyboardAgent
from src.pacman.multiagent.agents import MinimaxAgent, ExpectimaxAgent
from src.pacman.search.agents import FourPointAgent, AllFoodAgent
from src.pacman.rl import (
DQNAgent,
DQNAgentConfig,
ModelConfig,
DQNConfig,
EpsParams,
)
from src.pacman.ghost_agents import GreedyGhost, RandomGhost
from src.pacman.rules import GameRules
from src.graphics import display
from src.utils.layout import Layout
from src.utils.parser import get_parser
from src.consts.types import Params
def parse_args() -> tuple[Params, Params]:
parser = get_parser()
options = parser.parse_args()
layout_keys = ["width", "height", "num_food", "num_ghosts", "layout"]
layout_params = {key: getattr(options, key) for key in layout_keys}
args = {"num_games": options.num_games}
# ExpectimaxAgent() MinimaxAgent(), KeyboardAgent(), FourPointAgent(), AllFoodAgent()
agent_config = DQNAgentConfig(
model=ModelConfig(
dqn=DQNConfig(
width=layout_params["width"],
height=layout_params["height"],
in_channels=4,
out_features=4,
),
memory=10000,
lr=2e-4,
batch_size=64,
gamma=0.999,
update_step=200,
device="cpu",
train_start=2000,
model_path="src/pacman/rl/dqn/trained/checkpoint.tar.pth",
),
eps_params=EpsParams(start=0.9, end=0.05, step=10000),
train=True,
)
args["pacman_agent"] = DQNAgent(agent_config)
args["ghost_agents"] = [
GreedyGhost(idx + 1) for idx in range(options.max_num_ghosts)
]
args["display"] = display.PacmanGraphics(
zoom=options.zoom, frame_time=options.frame_time
)
if options.seed is not None:
random.seed(options.seed)
args["log_path"] = Path(options.log_path)
return args, layout_params
def generate_layout(params: dict[str, Any]) -> Layout:
layout = (
Layout.generate(
height=params["height"],
width=params["width"],
num_food=params["num_food"],
num_ghosts=params["num_ghosts"],
)
if params["layout"] is None
else Layout.from_text(params["layout"].layout)
)
if layout is None:
raise Exception("Invalid layout parameters")
return layout
if __name__ == "__main__":
args, layout_params = parse_args()
rules = GameRules()
num_games = args.pop("num_games")
for idx in range(num_games):
args["layout"] = generate_layout(layout_params)
game = rules.new_game(**args)
game.run()