This script implements a Fully Centralized Deep Deterministic Policy Gradient (DDPG) approach for UAV obstacle avoidance. It uses an Artificial Potential Field (APF) environment where a single centralized controller receives the concatenated states of all obstacles (spheres, cylinders, and cones) to determine actions.
Key workflow:
- Initialize Environment: Create an
APF instance to manage obstacle states and dynamics. - Define Dimensions: Calculate
obs_dim and act_dim based on the number of obstacles (spheres, cylinders, and cones) provided by the APF instance. - Initialize Controller: Instantiate
DDPG with the calculated dimensions and action bounds. - Training Loop:
- Collect observations by concatenating obstacle states into a single vector.
- Generate actions using
centralizedContriller.get_action(obs, noise_scale=noise). - Decompose the action vector back into specific obstacle actions (
action_sphere, action_cylinder, action_cone). - Interact with the environment using
apf.getqNext(...). - Store transitions in the replay buffer using
centralizedContriller.replay_buffer.store(...). - Periodically update the model using
centralizedContriller.update(data=batch).
- Model Saving: If the reward exceeds the historical maximum after 2/3 of the training episodes, the actor model is saved to
TrainedModel/centralizedActor.pkl.
from DDPGModel import DDPG
from Static_obstacle_avoidance.ApfAlgorithm import APF
from Static_obstacle_avoidance.Method import getReward, setup_seed
# Setup
setup_seed(11)
apf = APF()
obs_dim = 6 * (apf.numberOfSphere + apf.numberOfCylinder + apf.numberOfCone)
act_dim = 1 * (apf.numberOfSphere + apf.numberOfCylinder + apf.numberOfCone)
act_bound = [0.1, 3]
# Initialize Controller
centralizedContriller = DDPG(obs_dim, act_dim, act_bound)
# Training loop logic (simplified)
# ... loop episodes and steps ...
# action = centralizedContriller.get_action(obs, noise_scale=noise)
# qNext = apf.getqNext(apf.epsilon0, action_sphere, action_cylinder, action_cone, q, qBefore)
# centralizedContriller.replay_buffer.store(obs, action, reward, obs_next, done)
# centralizedContriller.update(data=batch)