-
Notifications
You must be signed in to change notification settings - Fork 26
/
data_handler.py
54 lines (53 loc) · 1.58 KB
/
data_handler.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
#!/usr/bin/env python
# coding=utf-8
'''
Author: JiangJi
Email: johnjim0816@gmail.com
Date: 2023-05-19 00:22:50
LastEditor: JiangJi
LastEditTime: 2023-05-19 00:55:08
Discription:
'''
import numpy as np
from algos.base.data_handler import BaseDataHandler
from algos.base.exps import Exp
class DataHandler(BaseDataHandler):
def __init__(self,cfg) -> None:
self.cfg = cfg
self.buffer = []
self.data_after_train = {}
def add_transition(self, transition):
''' add transition to buffer
'''
exp = self._create_exp(transition)
self.buffer.append(exp)
def add_data_after_learn(self, data):
''' add update data
'''
self.data_after_train = data
def sample_training_data(self):
''' sample training data from buffer
'''
exp = self.buffer.pop()[0]
if exp is not None:
return self.handle_exps_before_train(exp)
else:
return None
def _create_exp(self,transtion):
''' create experience
'''
return [Exp(**transtion)]
def handle_exps_before_train(self, exp, **kwargs):
''' convert exps to training data
'''
state = np.array(exp.state)
action = np.array(exp.action)
reward = np.array(exp.reward)
next_state = np.array(exp.next_state)
done = np.array(exp.done)
data = {'state': state, 'action': action, 'reward': reward, 'next_state': next_state, 'done': done}
return data
def handle_exps_after_train(self):
''' handle exps after train
'''
pass