I am working on RL model using DDPG method , where I have inhereted Gym environment to…
- User
I am working on RL model using DDPG method , where I have inhereted Gym environment to create a environment having continuous spaces and action where two element clashes to each other like replicating in Revit MEP clash , I am moming clash element from each other in such a way that euclidean distance lie in a specific range to assume it resolved clashes . My code is working fine . Now I want to make it more general where there are multiple clashes, then how to work on it.
import gymnasium as gym import tensorflow as tf import numpy as np
Define constants
num_states = 4 # Number of features defining the state num_actions = 6 # Number of possible resolution actions upper_bound = -10 # Maximum value for resolution actions lower_bound = 10 # Minimum value for resolution actions
Define the Ornstein-Uhlenbeck noise process
class OUActionNoise: def init(self, mean, std_deviation, theta=0.15, dt=1e-2, x_initial=None): self.theta = theta self.mean = mean self.std_dev = std_deviation self.dt = dt self.x_initial = x_initial self.reset()
def __call__(self): x = ( self.x_prev + self.theta * (self.mean - self.x_prev) * self.dt + self.std_dev * np.sqrt(self.dt) * np.random.normal(size=self.mean.shape) ) self.x_prev = x return x def reset(self): if self.x_initial is not None: self.x_prev = self.x_initial else: self.x_prev = np.zeros_like(self.mean)Define the Buffer class for experience replay
class Buffer: def init(self, buffer_capacity=100000, batch_size=64): self.buffer_capacity = buffer_capacity self.batch_size = batch_size self.buffer_counter = 0 self.state_buffer = np.zeros((self.buffer_capacity, num_states)) self.action_buffer = np.zeros((self.buffer_capacity, num_actions)) self.reward_buffer = np.zeros((self.buffer_capacity, 1)) self.next_state_buffer = np.zeros((self.buffer_capacity, num_states))
def record(self, obs_tuple): print('obs_tuple================================',obs_tuple) index = self.buffer_counter % self.buffer_capacity print('====buffer_counter , buffer_capicity, index========',self.buffer_counter, self.buffer_capacity, index) self.state_buffer[index] = obs_tuple[0] self.action_buffer[index] = obs_tuple[1] self.reward_buffer[index] = obs_tuple[2] self.next_state_buffer[index] = obs_tuple[3] self.buffer_counter += 1 @tf.function def update(self, state_batch, action_batch, reward_batch, next_state_batch): with tf.GradientTape() as tape: target_actions = target_actor(next_state_batch, training=True) y = reward_batch + gamma * target_critic( [next_state_batch, target_actions], training=True ) critic_value = critic_model([state_batch, action_batch], training=True) critic_loss = tf.math.reduce_mean(tf.math.square(y - critic_value)) critic_grad = tape.gradient(critic_loss, critic_model.trainable_variables) critic_optimizer.apply_gradients( zip(critic_grad, critic_model.trainable_variables) ) with tf.GradientTape() as tape: actions = actor_model(state_batch, training=True) critic_value = critic_model([state_batch, actions], training=True) actor_loss = -tf.math.reduce_mean(critic_value) actor_grad = tape.gradient(actor_loss, actor_model.trainable_variables) actor_optimizer.apply_gradients( zip(actor_grad, actor_model.trainable_variables) ) def learn(self): record_range = min(self.buffer_counter, self.buffer_capacity) batch_indices = np.random.choice(record_range, self.batch_size) state_batch = tf.convert_to_tensor(self.state_buffer[batch_indices]) action_batch = tf.convert_to_tensor(self.action_buffer[batch_indices]) reward_batch = tf.convert_to_tensor(self.reward_buffer[batch_indices]) reward_batch = tf.cast(reward_batch, dtype=tf.float32) next_state_batch = tf.convert_to_tensor(self.next_state_buffer[batch_indices]) self.update(state_batch, action_batch, reward_batch, next_state_batch) - Assistant