-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
53 lines (44 loc) · 1.92 KB
/
agent.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
from typing import Union, List, Tuple
import numpy as np
import random
import settings
class Agent:
def __init__(self, name: str, weight: float):
self.name: str = name
self.weight: float = weight
self.epsilon: float = settings.epsilon
self.q_table: List[float] = [0, 0, 0, 0]
self.actions: List[List[int]] = settings.actions
self.decay: float = settings.decay
self.move: List[int] = None
self.reward: float = None
self.accumulated_reward: float = 0
self.gamma: float = settings.gamma
self.learning_rate: float = settings.learning_rate
def reduce_epsilon(self) -> None:
"""Reduce the epsilon value for exploration over time."""
if self.epsilon != 0.:
self.epsilon = round((self.epsilon - self.decay), 2)
def select_move(self) -> int:
"""Select an action based on epsilon-greedy policy."""
if self.epsilon <= 0:
return np.argmax(self.q_table)
elif np.random.rand() <= self.epsilon:
return random.randrange(0, len(self.q_table))
else:
return np.argmax(self.q_table)
def make_move(self) -> List[int]:
"""Make a move by selecting an action."""
self.move = self.actions[self.select_move()]
self.move_num = self.actions.index(self.move)
return self.move
def get_reward(self, reward: float) -> None:
"""Set the reward received by the agent."""
self.reward = reward
self.accumulated_reward += reward
def update_q(self) -> None:
"""Update the Q-value based on the Q-learning algorithm."""
index = self.actions.index(self.move)
self.q_table[index] = self.q_table[index] + self.learning_rate * (self.reward + (self.gamma * np.max(self.q_table)) - self.q_table[index])
if __name__ == '__main__':
print('agent module')