Files
kair_algorithms_draft/scripts/algorithms/common/noise.py
T
Kyunghwan Kim d2769dfa9d Convert code to python 2.7 (#35)
* Convert code format to python2.7 (SAC)

* Convert code format python2.7 (TD3, all fD)

* Remove no use import and black setting

* Change SAC param

* Change env name Reacher-v2 to v1

* Remove old version reacher training script

* Convert code format python2.7

* Modify .travis.yml

* Add install command python3.6 & black on Makefile

* Fix seperator to tab on Makefile

* Modify Makefile

* Fix little error

* Change td3 gamma parameter
2019-03-25 19:07:19 +09:00

59 lines
1.6 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, min_sigma=1.0, max_sigma=1.0, decay_period=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=0):
"""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, mu=0.0, theta=0.15, sigma=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):
"""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