#!/usr/bin/env python # coding=utf-8 ''' @Author: John @Email: johnjim0816@gmail.com @Date: 2020-06-11 20:58:59 @LastEditor: John @LastEditTime: 2020-06-11 20:59:20 @Discription: @Environment: python 3.7.7 ''' import numpy as np class OUNoise(object): def __init__(self, action_space, mu=0.0, theta=0.15, max_sigma=0.3, min_sigma=0.3, decay_period=100000): self.mu = mu self.theta = theta self.sigma = max_sigma self.max_sigma = max_sigma self.min_sigma = min_sigma self.decay_period = decay_period self.n_actions = action_space.shape[0] self.low = action_space.low self.high = action_space.high self.reset() def reset(self): self.obs = np.ones(self.n_actions) * self.mu def evolve_obs(self): x = self.obs dx = self.theta * (self.mu - x) + self.sigma * np.random.randn(self.n_actions) self.obs = x + dx return self.obs def get_action(self, action, t=0): ou_obs = self.evolve_obs() self.sigma = self.max_sigma - (self.max_sigma - self.min_sigma) * min(1.0, t / self.decay_period) return np.clip(action + ou_obs, self.low, self.high)