-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfa_agent.py
More file actions
49 lines (41 loc) · 1.73 KB
/
Copy pathfa_agent.py
File metadata and controls
49 lines (41 loc) · 1.73 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
import os
from agent import Agent
import gymnasium as gym
class FAAgent(Agent):
def __init__(self,
environment: gym.Env,
log,
technique: int,
version: str):
super(FAAgent, self).__init__(environment, log, technique, version)
def make_weights_normal(self):
weights_sum = sum([abs(w) for w in self.weights])
for i in range(self.features_num):
self.weights[i] /= weights_sum
def get_qvalue(self, state, action):
q_value = 0
for i in range(self.features_num):
feature_value = self.features[i](state, action)
q_value += self.weights[i] * feature_value
return q_value
def update_values(self, curr_state, action, reward, terminated, dynamic_val):
target = reward + self.discount * dynamic_val * (not terminated)
prediction = self.get_qvalue(curr_state, action)
diff = target - prediction
for i in range(self.features_num):
feature_value = self.features[i](curr_state, action)
self.calculate_weight(i, diff, feature_value)
if self.technique == 3:
self.make_weights_normal()
def save_agent(self):
directory = os.path.join(f'.\\agents\\')
if not os.path.exists(directory):
os.makedirs(directory)
with open(directory + 'FA' + '.txt', 'w') as fh:
fh.writelines([f'{self.weights[i]}\n' for i in range(len(self.weights))])
def load_agent(self):
directory = os.path.join(f'.\\agents\\')
if not os.path.exists(directory):
return
with open(directory + 'FA' + '.txt', 'r') as fh:
self.weights = [float(x) for x in fh.readlines()]