Files
kair_algorithms_draft/scripts/algorithms/common/noise.py
T
Whi Kwon 5962ff5277 Add TD3 (#10)
* Add td3

* Fix flake8

* Fix action clamping

* Increase episode max step, add detach to actor_loss

* Fix actor update freq bug

* Add per (#8)

* Add per and modify etc

* Replace pre-commit-config.yaml and add pre-commit hook in .git

* Modify .gitignore

* Modify .gitignore

* Modify buffer and code

* Modify replay buffer and per

* Modify .gitignore

* Add random initial action in ddpg (#13)

* Add random initial actions in ddpg

* Add reacher-v2 example of ddpg

* Add soft actor critic (#12)

* Add soft actor critic

* Delete unnecessary examples

* Add td3

* Fix flake8

* Fix action clamping

* Fix flake8

* Increase episode max step, add detach to actor_loss

* Fix actor update freq bug

* Fix code to reflect PR

* Resolve conflict
2019-02-24 13:44:20 +09:00

67 lines
1.8 KiB
Python

# -*- coding: utf-8 -*-
"""Noise classes for algorithms."""
import copy
import random
import numpy as np
class GaussianNoise:
"""Gaussian Noise.
Taken from https://github.com/vitchyr/rlkit
"""
def __init__(
self,
action_dim: int,
min_sigma: float = 1.0,
max_sigma: float = 1.0,
decay_period: int = 1000000,
):
"""Initialization."""
self.action_dim = action_dim
self.min_sigma = min_sigma
self.max_sigma = max_sigma
self.decay_period = decay_period
def sample(self, 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=self.action_dim)
class OUNoise:
"""Ornstein-Uhlenbeck process.
Taken from Udacity deep-reinforcement-learning github repository:
https://github.com/udacity/deep-reinforcement-learning/blob/master/
ddpg-pendulum/ddpg_agent.py
"""
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)
self.theta = theta
self.sigma = sigma
self.reset()
def reset(self):
"""Reset the internal state (= noise) to mean (mu)."""
self.state = copy.copy(self.mu)
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(
[random.random() for _ in range(len(x))]
)
self.state = x + dx
return self.state