-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreplaybuffer_ddpg.py
174 lines (143 loc) · 6.32 KB
/
replaybuffer_ddpg.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from __future__ import print_function
"""
"""
from collections import deque
import random
import numpy as np
import yaml
import os
import pickle
from train_model import prediction
class ReplayBuffer(object):
def __init__(self, buffer_size, random_seed, diff_sess = None):
"""
The right side of the deque contains the most recent experiences
"""
self.buffer_size = buffer_size
self.buffer_size_file = 0
self.transitions_size_file = 0
self.transitions_count = 0
self.replay_buffer_count = 0
self.transitions_buffer_count = 0
self.replay_buffer = deque()
self.transitions_buffer = deque()
self.transitions = deque()
self.transitions_save = 0
self.transitions_load = 0
self.buffer_save = 0
self.buffer_load = 0
self.diff = 0
self.save_filename = None
self.load_filename = None
self.model_filename = None
self.sess = diff_sess
random.seed(random_seed)
self.read_cfg('config.yaml')
def replay_buffer_add(self, s, a, r, t, s2, sd):
experience = (s, a, r, t, s2, sd)
if self.replay_buffer_count < self.buffer_size:
self.replay_buffer.append(experience)
self.replay_buffer_count += 1
# print ("Buffer count:", self.buffer_count)
else:
self.replay_buffer.popleft()
self.replay_buffer.append(experience)
if self.replay_buffer_count == self.buffer_size_file:
if self.buffer_save:
with open(self.save_filename, 'w') as f:
pickle.dump(self.replay_buffer, f, protocol=pickle.HIGHEST_PROTOCOL)
return False
def transitions_buffer_add(self, s, a, r, t, s2, sd):
if self.transitions_save:
experience = (s, a, r, t, s2, sd)
self.transitions_buffer.append(experience)
self.transitions_buffer_count +=1
if self.transitions_buffer_count == self.transitions_size_file:
with open(self.save_filename, 'w') as f:
pickle.dump(self.transitions_buffer, f, protocol=pickle.HIGHEST_PROTOCOL)
print (len(self.transitions_buffer))
return True
return False
def size(self):
return self.replay_buffer_count
def sample_batch(self, batch_size):
batch = []
if self.replay_buffer_count < batch_size:
batch = random.sample(self.replay_buffer, self.replay_buffer_count)
else:
batch = random.sample(self.replay_buffer, batch_size)
s_batch = np.array([_[0] for _ in batch])
a_batch = np.array([_[1] for _ in batch])
r_batch = np.array([_[2] for _ in batch])
t_batch = np.array([_[3] for _ in batch])
s2_batch = np.array([_[4] for _ in batch])
return s_batch, a_batch, r_batch, t_batch, s2_batch
def clear(self):
self.deque.clear()
self.transitions_count = 0
self.replay_buffer_count = 0
def read_cfg(self, cfg):
path = os.path.dirname(os.path.abspath(__file__))
yfile = '{}/{}'.format(path,cfg)
if not os.path.isfile(yfile):
print ("File %s not found" % yfile)
else:
# open configuration
stream = file(yfile, 'r')
conf = yaml.load(stream)
if 'transitions' in conf:
self.transitions_save = int(conf['transitions']['save'])
self.transitions_load = int(conf['transitions']['load'])
self.transitions_size_file = conf['transitions']['buffer_size']
if 'replay_buffer' in conf:
self.buffer_save = int(conf['replay_buffer']['save'])
self.buffer_load = int(conf['replay_buffer']['load'])
self.buffer_size_file = conf['replay_buffer']['buffer_size']
self.diff = int(conf['difference_model'])
print ("Transitions:", self.transitions_save, self.transitions_load, self.diff)
print ("Replay_Buffer:", self.buffer_save, self.buffer_load, self.diff)
if self.transitions_save == 1:
self.save_filename = conf['transitions']['save_filename']
elif self.buffer_save == 1:
self.save_filename = conf['replay_buffer']['save_filename']
if self.transitions_load == 1:
self.load_filename = conf['transitions']['load_filename']
with open(self.load_filename) as f:
self.transitions = pickle.load(f)
print (len(self.transitions))
f.close()
if self.buffer_load == 1:
self.load_filename = conf['replay_buffer']['load_filename']
with open(self.load_filename) as f:
self.replay_buffer = pickle.load(f)
f.close()
# self.update_replay_buffer()
self.replay_buffer_count += len(self.replay_buffer)
if self.diff == 1:
self.model_filename = conf['difference_model']['model_filename']
stream.close()
def sample_state_action(self, state, action, test, episode_start):
if self.transitions_load == 1:
if not episode_start:
self.transitions_count += 1
temp = self.transitions[self.transitions_count]
state = temp[0]
action = temp[1]
return state, action
else:
return state, action
def update_replay_buffer(self):
s_batch = np.array([_[0] for _ in self.replay_buffer])
a_batch = np.array([_[1] for _ in self.replay_buffer])
r_batch = np.array([_[2] for _ in self.replay_buffer])
t_batch = np.array([_[3] for _ in self.replay_buffer])
s2_batch = np.array([_[4] for _ in self.replay_buffer])
diff_state_old = np.array([_[5] for _ in self.replay_buffer])
input = np.concatenate((s_batch, a_batch, s2_batch-diff_state_old), axis=1)
s2_new = prediction(self.sess, input, 24, 18)
diff_state_new = s2_new - (s2_batch - diff_state_old)
self.replay_buffer.clear()
for i in range(s_batch):
self.replay_buffer.append((s_batch[i], a_batch[i], r_batch[i], t_batch[i], s2_new[i], diff_state_new[i]))