Add random initial action in ddpg (#13)

* Add random initial actions in ddpg

* Add reacher-v2 example of ddpg
This commit is contained in:
Jinwoo Park (Curt)
2019-02-18 08:57:36 +09:00
committed by GitHub
parent ecb42d30d2
commit d2b670015c
9 changed files with 272 additions and 40 deletions
+30 -3
View File
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Noise classes for baselines."""
"""Noise classes for algorithms."""
import copy
import random
@@ -7,6 +7,31 @@ import random
import numpy as np
class GaussianNoise:
"""Gaussian Noise.
Taken from https://github.com/vitchyr/rlkit
"""
def __init__(
self,
min_sigma: float = 1.0,
max_sigma: float = 1.0,
decay_period: int = 1000000,
):
"""Initialization."""
self.max_sigma = max_sigma
self.min_sigma = min_sigma
self.decay_period = decay_period
def sample(self, action_size: int, t: int = 0) -> float:
"""Get an action with gaussian noise."""
sigma = self.max_sigma - (self.max_sigma - self.min_sigma) * min(
1.0, t / self.decay_period
)
return np.random.normal(0, sigma, size=action_size)
class OUNoise:
"""Ornstein-Uhlenbeck process.
@@ -15,7 +40,9 @@ class OUNoise:
ddpg-pendulum/ddpg_agent.py
"""
def __init__(self, size, mu=0.0, theta=0.15, sigma=0.2):
def __init__(
self, size: int, mu: float = 0.0, theta: float = 0.15, sigma: float = 0.2
):
"""Initialize parameters and noise process."""
self.state = np.float64(0.0)
self.mu = mu * np.ones(size)
@@ -27,7 +54,7 @@ class OUNoise:
"""Reset the internal state (= noise) to mean (mu)."""
self.state = copy.copy(self.mu)
def sample(self):
def sample(self) -> float:
"""Update internal state and return it as a noise sample."""
x = self.state
dx = self.theta * (self.mu - x) + self.sigma * np.array(