-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQlearning.py
More file actions
51 lines (42 loc) · 1.97 KB
/
Qlearning.py
File metadata and controls
51 lines (42 loc) · 1.97 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
from collections import defaultdict
import numpy as np
class Agent:
""" Q-learning agent in this Multi-Agent Reinforcement Learning setting. """
def __init__(self, actions, gamma):
"""
Initializes an Agent.
Args:
actions (np array): List of available actions.
gamma (float): Discount factor for Q-learning.
"""
self.actions = actions
self.Q = defaultdict(lambda: dict(zip(self.actions, np.zeros(len(self.actions)))))
self.gamma = gamma
def learn(self, state, action, next_state, profit, next_profit, time):
"""
Updates the Q-function of the agent based on the observed transition.
Args:
state (Action object): Current state of the environment.
action (Action object): Action taken by the agent in the current state.
next_state (Action object): Next state of the environment after taking the action.
profit (float): Current profit obtained from the transition.
next_profit (float): Profit obtained from the next state.
time (int): Current time step.
"""
alpha = 0.6 - 0.5 * time / 500_000
v = max(self.Q[next_state].values())
self.Q[state][action] += alpha * (profit + self.gamma * next_profit
+ self.gamma**2 * v - self.Q[state][action])
def act(self, state, time):
"""
Determines the action that should be taken based on the epsilon-greedy policy.
Args:
state (Action object): Current state of the environment.
time (int): Current time step.
Returns:
Action object: Action to be taken by the agent.
"""
epsilon = 0.1 ** (4*time / 500_000)
random_action = np.random.choice(self.actions)
greedy_action = max(self.Q[state], key = self.Q[state].get)
return np.random.choice([random_action, greedy_action], p = [epsilon, 1 - epsilon])