add epsilon-greedy for dqn
This commit is contained in:
parent
a40e5aec54
commit
0bc1b63e38
@ -66,9 +66,11 @@ if __name__ == '__main__':
|
|||||||
pi.sync_weights() # TODO: automate this for policies with target network
|
pi.sync_weights() # TODO: automate this for policies with target network
|
||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
for i in range(100):
|
#TODO : repeat_num shoulde be defined in some configuration files
|
||||||
|
repeat_num = 100
|
||||||
|
for i in range(repeat_num):
|
||||||
# collect data
|
# collect data
|
||||||
data_collector.collect(num_episodes=50)
|
data_collector.collect(num_episodes=50, epsilon_greedy= (repeat_num - i + 0.0) / repeat_num)
|
||||||
|
|
||||||
# print current return
|
# print current return
|
||||||
print('Epoch {}:'.format(i))
|
print('Epoch {}:'.format(i))
|
||||||
|
@ -18,7 +18,7 @@ class DQN(PolicyBase):
|
|||||||
else:
|
else:
|
||||||
self.interaction_count = -1
|
self.interaction_count = -1
|
||||||
|
|
||||||
def act(self, observation, exploration=None):
|
def act(self, observation, my_feed_dict):
|
||||||
sess = tf.get_default_session()
|
sess = tf.get_default_session()
|
||||||
if self.weight_update > 1:
|
if self.weight_update > 1:
|
||||||
if self.interaction_count % self.weight_update == 0:
|
if self.interaction_count % self.weight_update == 0:
|
||||||
@ -30,7 +30,6 @@ class DQN(PolicyBase):
|
|||||||
if self.weight_update > 0:
|
if self.weight_update > 0:
|
||||||
self.interaction_count += 1
|
self.interaction_count += 1
|
||||||
|
|
||||||
if not exploration:
|
|
||||||
return np.squeeze(action)
|
return np.squeeze(action)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -34,7 +34,7 @@ class Batch(object):
|
|||||||
self._is_first_collect = True
|
self._is_first_collect = True
|
||||||
|
|
||||||
def collect(self, num_timesteps=0, num_episodes=0, my_feed_dict={},
|
def collect(self, num_timesteps=0, num_episodes=0, my_feed_dict={},
|
||||||
process_reward=True): # specify how many data to collect here, or fix it in __init__()
|
process_reward=True, epsilon_greedy=0): # specify how many data to collect here, or fix it in __init__()
|
||||||
assert sum(
|
assert sum(
|
||||||
[num_timesteps > 0, num_episodes > 0]) == 1, "One and only one collection number specification permitted!"
|
[num_timesteps > 0, num_episodes > 0]) == 1, "One and only one collection number specification permitted!"
|
||||||
|
|
||||||
@ -106,6 +106,10 @@ class Batch(object):
|
|||||||
episode_start_flags.append(True)
|
episode_start_flags.append(True)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
# a simple implementation of epsilon greedy
|
||||||
|
if epsilon_greedy > 0 and np.random.random() < epsilon_greedy:
|
||||||
|
ac = np.random.randint(low = 0, high = self._env.action_space.n)
|
||||||
|
else:
|
||||||
ac = self._pi.act(ob, my_feed_dict)
|
ac = self._pi.act(ob, my_feed_dict)
|
||||||
actions.append(ac)
|
actions.append(ac)
|
||||||
|
|
||||||
@ -114,9 +118,9 @@ class Batch(object):
|
|||||||
ob, reward, done, _ = self._env.step(ac)
|
ob, reward, done, _ = self._env.step(ac)
|
||||||
rewards.append(reward)
|
rewards.append(reward)
|
||||||
|
|
||||||
t_count += 1
|
#t_count += 1
|
||||||
if t_count >= 100: # force episode stop, just to test if memory still grows
|
#if t_count >= 100: # force episode stop, just to test if memory still grows
|
||||||
break
|
# break
|
||||||
|
|
||||||
if done: # end of episode, discard s_T
|
if done: # end of episode, discard s_T
|
||||||
# TODO: for num_timesteps collection, has to store terminal flag instead of start flag!
|
# TODO: for num_timesteps collection, has to store terminal flag instead of start flag!
|
||||||
|
Loading…
x
Reference in New Issue
Block a user