-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_agent.py
More file actions
51 lines (43 loc) · 1.4 KB
/
Copy pathai_agent.py
File metadata and controls
51 lines (43 loc) · 1.4 KB
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
"""
AI Agent Class
"""
from dataclasses import dataclass, field
from typing import Optional, cast
import numpy as np
from constants import COLS
from game import GameState
from mcts import MCTS, Node
from temperature_scheduler import TemperatureScheduler
from piece import Turn
@dataclass
class Player:
"""
AI Agent class
"""
player: Turn
mcts: MCTS
temperature: TemperatureScheduler
train_logger: list[tuple[Turn, GameState, list[float], int]] = field(
default_factory=lambda: []
)
def run(self,
state: GameState,
action: Optional[int] = None,
step: int = 0, is_training=True) -> int:
"""
Play move give current state and previous action
"""
assert state.is_winning() is None, "Game is not over"
assert state.turn == self.player, "Wrong player"
self.mcts.run(state, action)
action_probs = [0] * COLS
# Might be better to consider the priors
root = cast(Node, self.mcts.root)
for k, v in root.children.items():
action_probs[k] = v.visit_count
action_probs = np.array(action_probs)
action_probs = action_probs / np.sum(action_probs)
action = self.mcts.root.select_action(self.temperature.temperature(step))
if is_training:
self.train_logger.append((self.player, state, action_probs, action))
return action