def __init__(self, actor_model, tgt_actor_model, critic_model, tgt_critic_model, action_limits, actor_lr=1e-4, critic_lr=1e-3, critic_decay=1e-2, process=None, rb_size=1e6, minibatch_size=64, tau=1e-3, gamma=0.99, warmup_episodes=None, logging=True): super(DDPGAgent, self).__init__(warmup_episodes, logging) self.actor = Actor(actor_model, critic_model, lr=actor_lr) self.tgt_actor = Actor(tgt_actor_model, tgt_critic_model, lr=actor_lr) self.tgt_actor.set_weights(self.actor.get_weights()) self.critic = Critic(critic_model, lr=critic_lr, decay=critic_decay) self.tgt_critic = Critic(tgt_critic_model, lr=critic_lr, decay=critic_decay) self.tgt_critic.set_weights(self.critic.get_weights()) self.action_limits = action_limits self.process = process self.buffer = ReplayBuffer(rb_size) self.minibatch_size = minibatch_size self.tau = tau self.gamma = gamma self.state_space = K.int_shape(critic_model.inputs[0])[1] self.action_space = K.int_shape(critic_model.inputs[1])[1] if process is None: self.process = OrnsteinUhlenbeck(x0=np.zeros(self.action_space), theta=0.15, mu=0, sigma=0.2) else: self.process = process
def __init__(self, task, exploration_mu=0, exploration_theta=0.15, exploration_sigma=0.2, tau=0.01): self.task = task self.state_size = task.state_size self.action_size = task.action_size self.action_low = task.action_low self.action_high = task.action_high # Actor (Policy) Model self.actor_local = Actor(self.state_size, self.action_size, self.action_low, self.action_high) self.actor_target = Actor(self.state_size, self.action_size, self.action_low, self.action_high) # Critic (Value) Model self.critic_local = Critic(self.state_size, self.action_size) self.critic_target = Critic(self.state_size, self.action_size) # Initialize target model parameters with local model parameters self.critic_target.model.set_weights( self.critic_local.model.get_weights()) self.actor_target.model.set_weights( self.actor_local.model.get_weights()) # Noise process self.exploration_mu = exploration_mu self.exploration_theta = exploration_theta self.exploration_sigma = exploration_sigma self.noise = OUNoise(self.action_size, self.exploration_mu, self.exploration_theta, self.exploration_sigma) # Replay memory self.buffer_size = 100000 self.batch_size = 128 # 64 self.memory = ReplayBuffer(self.buffer_size, self.batch_size) # Algorithm parameters self.gamma = 0.85 # 0.99 # discount factor self.tau = tau # for soft update of target parameters ## self.total_reward = 0 self.best_score = -np.inf self.score = 0 self.count = 0
def __init__(self, env, hparams): n_action = len(env.action_space.high) self.actor_main = Actor(n_action, hparams) self.actor_target = Actor(n_action, hparams) self.critic_main = Critic(hparams) self.critic_target = Critic(hparams) self.batch_size = 64 self.n_actions = len(env.action_space.high) self.a_opt = tf.keras.optimizers.Adam(hparams['lr']) # self.actor_target = tf.keras.optimizers.Adam(.001) self.c_opt = tf.keras.optimizers.Adam(hparams['lr']) # self.critic_target = tf.keras.optimizers.Adam(.002) self.memory = RBuffer(1_00_000, env.observation_space.shape, len(env.action_space.high)) self.trainstep = 0 self.replace = 5 self.gamma = 0.99 self.min_action = env.action_space.low[0] self.max_action = env.action_space.high[0]
class DDPGAgent(Agent): def __init__(self, actor_model, tgt_actor_model, critic_model, tgt_critic_model, action_limits, actor_lr=1e-4, critic_lr=1e-3, critic_decay=1e-2, process=None, rb_size=1e6, minibatch_size=64, tau=1e-3, gamma=0.99, warmup_episodes=None, logging=True): super(DDPGAgent, self).__init__(warmup_episodes, logging) self.actor = Actor(actor_model, critic_model, lr=actor_lr) self.tgt_actor = Actor(tgt_actor_model, tgt_critic_model, lr=actor_lr) self.tgt_actor.set_weights(self.actor.get_weights()) self.critic = Critic(critic_model, lr=critic_lr, decay=critic_decay) self.tgt_critic = Critic(tgt_critic_model, lr=critic_lr, decay=critic_decay) self.tgt_critic.set_weights(self.critic.get_weights()) self.action_limits = action_limits self.process = process self.buffer = ReplayBuffer(rb_size) self.minibatch_size = minibatch_size self.tau = tau self.gamma = gamma self.state_space = K.int_shape(critic_model.inputs[0])[1] self.action_space = K.int_shape(critic_model.inputs[1])[1] if process is None: self.process = OrnsteinUhlenbeck(x0=np.zeros(self.action_space), theta=0.15, mu=0, sigma=0.2) else: self.process = process def sense(self, s, a, r, s_new): # print(self.state_space) s = np.reshape(s, [-1, self.state_space]) s_new = np.reshape(s_new, [-1, self.state_space]) self.buffer.add((s, a, r, s_new)) def act(self, s): s = np.reshape(s, [-1, self.state_space]) a = self.actor(s) #self.tgt_actor(s) # why acting with the target_ctor instead of the actor? # Cache. self.last_state = np.copy(s) self.last_action = np.copy(a) if self.learning_phase: a = self.process(a) #a += self.process() # for OU process a = np.clip(a, self.action_limits[0], self.action_limits[1]) self.last_action_noisy = np.copy(a) return a[0] def train_step(self): minibatch = self.buffer.sample(self.minibatch_size) states = np.zeros([len(minibatch), self.state_space]) states_new = np.zeros([len(minibatch), self.state_space]) actions = np.zeros([len(minibatch), self.action_space]) r = np.zeros([len(minibatch), 1]) for i in range(len(minibatch)): states[i], actions[i], r[i], states_new[i] = minibatch[i] critic_out = self.critic(states_new, self.actor(states_new)) tgt_critic_out = self.tgt_critic(states_new, self.tgt_actor(states_new)) if self.logging: log = [('s', self.last_state), ('a', self.last_action), ('a_noisy', self.last_action_noisy), ('q', self.critic(self.last_state, self.last_action)), ('q_tgt', self.tgt_critic(self.last_state, self.last_action)), (('mse', np.mean(np.square(critic_out - tgt_critic_out))))] self.add_log(log) ys = r + self.gamma * tgt_critic_out loss = self.critic.step(states, actions, ys) # update critic by minimizing the loss self.actor.step(states) # update actor using the sampled policy gradient # Soft weight update. (update the target networks) critic_weights = self.critic.get_weights() tgt_critic_weights = self.tgt_critic.get_weights() actor_weights = self.actor.get_weights() tgt_actor_weights = self.tgt_actor.get_weights() for i in range(len(critic_weights)): tgt_critic_weights[i] = (1 - self.tau) * tgt_critic_weights[i] + \ self.tau * critic_weights[i] self.tgt_critic.set_weights(tgt_critic_weights) for i in range(len(actor_weights)): tgt_actor_weights[i] = (1 - self.tau) * tgt_actor_weights[i] + \ self.tau * actor_weights[i] self.tgt_actor.set_weights(tgt_actor_weights) return loss def new_episode(self): self.process.clear() if self.logging: self.logs.append({}) if len(self.logs) == 1: self.logs[-1]['episode'] = 1 # Initial episode. else: self.logs[-1]['episode'] = self.logs[-2]['episode'] + 1 def save_weights(self, actor_suffix, critic_suffix): self.actor.save_model_weights(actor_suffix) self.critic.save_model_weights(critic_suffix)
class Agent(): """Reinforcement Learning agent that learns using DDPG.""" def __init__(self, task): self.task = task self.state_size = task.state_size self.action_size = task.action_size self.action_low = task.action_low self.action_high = task.action_high # Actor (Policy) Model self.actor_local = Actor(self.state_size, self.action_size, self.action_low, self.action_high) self.actor_target = Actor(self.state_size, self.action_size, self.action_low, self.action_high) # Critic (Value) Model self.critic_local = Critic(self.state_size, self.action_size) self.critic_target = Critic(self.state_size, self.action_size) # Initialize target model parameters with local model parameters self.critic_target.model.set_weights( self.critic_local.model.get_weights()) self.actor_target.model.set_weights( self.actor_local.model.get_weights()) # Noise process self.exploration_mu = 0 self.exploration_theta = 0.15 self.exploration_sigma = 0.2 self.noise = OUNoise(self.action_size, self.exploration_mu, self.exploration_theta, self.exploration_sigma) # Replay memory self.buffer_size = 100000 self.batch_size = 64 self.memory = ReplayBuffer(self.buffer_size, self.batch_size) # Algorithm parameters self.gamma = 0.99 # discount factor self.tau = 0.01 # for soft update of target parameters # Score tracker self.best_score = -np.inf self.score = -np.inf def reset_episode(self): self.noise.reset() state = self.task.reset() self.last_state = state self.total_reward = 0.0 self.count = 0 return state def step(self, action, reward, next_state, done): # Save experience / reward self.memory.add(self.last_state, action, reward, next_state, done) # Learn, if enough samples are available in memory if len(self.memory) > self.batch_size: experiences = self.memory.sample() self.learn(experiences) # Roll over last state and action self.last_state = next_state # Save experience / reward self.total_reward += reward self.count += 1 def act(self, state): """Returns actions for given state(s) as per current policy.""" state = np.reshape(state, [-1, self.state_size]) action = self.actor_local.model.predict(state)[0] return list(action + self.noise.sample()) # add some noise for exploration def learn(self, experiences): """Update policy and value parameters using given batch of experience tuples.""" # Convert experience tuples to separate arrays for each element (states, actions, rewards, etc.) states = np.vstack([e.state for e in experiences if e is not None]) actions = np.array([e.action for e in experiences if e is not None]).astype(np.float32).reshape( -1, self.action_size) rewards = np.array([e.reward for e in experiences if e is not None ]).astype(np.float32).reshape(-1, 1) dones = np.array([e.done for e in experiences if e is not None]).astype(np.uint8).reshape(-1, 1) next_states = np.vstack( [e.next_state for e in experiences if e is not None]) # Get predicted next-state actions and Q values from target models # Q_targets_next = critic_target(next_state, actor_target(next_state)) actions_next = self.actor_target.model.predict_on_batch(next_states) Q_targets_next = self.critic_target.model.predict_on_batch( [next_states, actions_next]) # Compute Q targets for current states and train critic model (local) Q_targets = rewards + self.gamma * Q_targets_next * (1 - dones) self.critic_local.model.train_on_batch(x=[states, actions], y=Q_targets) # Train actor model (local) action_gradients = np.reshape( self.critic_local.get_action_gradients([states, actions, 0]), (-1, self.action_size)) self.actor_local.train_fn([states, action_gradients, 1]) # custom training function # Soft-update target models self.soft_update(self.critic_local.model, self.critic_target.model) self.soft_update(self.actor_local.model, self.actor_target.model) self.score = self.total_reward / float( self.count) if self.count else 0.0 if self.score > self.best_score: self.best_score = self.score def soft_update(self, local_model, target_model): """Soft update model parameters.""" local_weights = np.array(local_model.get_weights()) target_weights = np.array(target_model.get_weights()) assert len(local_weights) == len( target_weights ), "Local and target model parameters must have the same size" new_weights = self.tau * local_weights + (1 - self.tau) * target_weights target_model.set_weights(new_weights)
class Agent(): def __init__(self, env, hparams): n_action = len(env.action_space.high) self.actor_main = Actor(n_action, hparams) self.actor_target = Actor(n_action, hparams) self.critic_main = Critic(hparams) self.critic_target = Critic(hparams) self.batch_size = 64 self.n_actions = len(env.action_space.high) self.a_opt = tf.keras.optimizers.Adam(hparams['lr']) # self.actor_target = tf.keras.optimizers.Adam(.001) self.c_opt = tf.keras.optimizers.Adam(hparams['lr']) # self.critic_target = tf.keras.optimizers.Adam(.002) self.memory = RBuffer(1_00_000, env.observation_space.shape, len(env.action_space.high)) self.trainstep = 0 self.replace = 5 self.gamma = 0.99 self.min_action = env.action_space.low[0] self.max_action = env.action_space.high[0] def act(self, state, evaluate=False): state = tf.convert_to_tensor([state], dtype=tf.float32) actions = self.actor_main(state) if not evaluate: actions += tf.random.normal(shape=[self.n_actions], mean=0.0, stddev=0.1) actions = self.max_action * (tf.clip_by_value(actions, self.min_action, self.max_action)) # print(actions) return actions[0] def savexp(self, state, next_state, action, done, reward): self.memory.storexp(state, next_state, action, done, reward) def update_target(self): self.actor_target.set_weights(self.actor_main.get_weights()) self.critic_target.set_weights(self.critic_main.get_weights()) def train(self): if self.memory.cnt < self.batch_size: return states, next_states, rewards, actions, dones = self.memory.sample(self.batch_size) states = tf.convert_to_tensor(states, dtype=tf.float32) next_states = tf.convert_to_tensor(next_states, dtype=tf.float32) rewards = tf.convert_to_tensor(rewards, dtype=tf.float32) actions = tf.convert_to_tensor(actions, dtype=tf.float32) # dones = tf.convert_to_tensor(dones, dtype= tf.bool) with tf.GradientTape() as tape1, tf.GradientTape() as tape2: target_actions = self.actor_target(next_states) target_next_state_values = tf.squeeze(self.critic_target(next_states, target_actions), 1) critic_value = tf.squeeze(self.critic_main(states, actions), 1) target_values = rewards + self.gamma * target_next_state_values * dones critic_loss = tf.keras.losses.MSE(target_values, critic_value) new_policy_actions = self.actor_main(states) actor_loss = -self.critic_main(states, new_policy_actions) actor_loss = tf.math.reduce_mean(actor_loss) grads1 = tape1.gradient(actor_loss, self.actor_main.trainable_variables) grads2 = tape2.gradient(critic_loss, self.critic_main.trainable_variables) self.a_opt.apply_gradients(zip(grads1, self.actor_main.trainable_variables)) self.c_opt.apply_gradients(zip(grads2, self.critic_main.trainable_variables)) if self.trainstep % self.replace == 0: self.update_target() self.trainstep += 1