-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
65 lines (52 loc) · 2.6 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
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
from abc import ABC
from utilities.types_cp import Experience
class Agent(ABC):
ns: int # Number of states
na: int # Number of actions
discount_factor: float # Discount factor
def __init__(self, ns: int, na: int, discount_factor: float):
self.ns = ns
self.na = na
self.discount_factor = discount_factor
self.num_visits_state = np.zeros(self.ns)
self.num_visits_actions = np.zeros((self.ns, self.na))
self.last_visit_state = np.zeros(self.ns)
self.policy_diff_generative = []
self.policy_diff_constraints = []
def forward(self, state: int, step: int) -> int:
self.num_visits_state[state] += 1
self.last_visit_state[state] = step
action = self._forward_logic(state, step)
self.num_visits_actions[state][action] += 1
return action
def backward(self, experience: Experience):
self._backward_logic(experience)
def _forward_logic(self, state: int, step: int) -> int:
raise NotImplementedError
def _backward_logic(self, experience: Experience):
raise NotImplementedError
def greedy_action(self, state: int) -> int:
raise NotImplementedError
class QlearningAgent(Agent):
def __init__(self, ns: int, na: int, discount_factor: float, alpha: float, explorative_policy = None):
super().__init__(ns, na, discount_factor)
self.q_function = np.zeros((self.ns, self.na))
self.alpha = alpha
self.explorative_policy = explorative_policy
def _forward_logic(self, state: int, step: int) -> int:
if self.explorative_policy is not None:
state_sum = np.sum(self.explorative_policy[state] + 1/self.ns)
probs = (self.explorative_policy[state] + 1/self.ns)/state_sum
action = np.random.choice(range(self.na),1,p=probs)[0]
else:
eps = 1 if self.num_visits_state[state] <= 2 * self.na else max(0.5, 1 / (self.num_visits_state[state] - 2*self.na))
action = np.random.choice(self.na) if np.random.uniform() < eps else self.q_function[state].argmax()
return action
def greedy_action(self, state: int) -> int:
return self.q_function[state].argmax()
def _backward_logic(self, experience: Experience):
state, action, reward, next_state, done = list(experience)
target = reward + (1-done) * self.discount_factor * self.q_function[next_state].max()
lr = 1 / (self.num_visits_actions[state][action] ** self.alpha)
self.q_function[state][action] += lr * (target - self.q_function[state][action])