🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

Reinforcement Learning for Drone Control

🟢 Free Lesson

Reinforcement Learning for Drone Control

Reinforcement learning teaches drones to fly through trial and error, much like how a bird learns to navigate by exploring its environment. Instead of hand-coding control rules, the drone discovers optimal behaviors by maximizing cumulative rewards.

Across the next sections you will meet the three algorithm families that dominate drone RL — value-based Q-learning, on-policy PPO, and off-policy SAC — plus the engineering decisions that decide whether a policy survives contact with real hardware: reward design, domain randomization, and safety constraints. Every section ships with runnable Python you can paste into an AirSim or Gazebo project.

The RL Agent-Environment Loop

Sponsored

RL operates on a simple principle: an agent takes actions in an environment, receives rewards, and learns to maximize long-term reward.

For a drone, the state s usually stacks position (x, y, z), linear velocity, attitude angles, angular rates, battery percentage, and sometimes a downsampled sensor vector. The action a is either discrete (hover, forward, left, right, up, down) or continuous (four motor thrusts, or a roll/pitch/yaw/throttle setpoint). The reward r is a scalar recomputed every control step — typically 20 to 50 Hz for a quadcopter RL task, slower for fixed-wing because the dynamics evolve more gently.

An episode ends when the drone reaches the goal, collides, depletes battery, or exceeds the step limit. The discount factor γ balances immediate versus delayed reward: γ = 0.99 gives an effective planning horizon of about 1 / (1 − γ) = 100 steps, or roughly 2 to 5 seconds at 50 Hz. Push γ toward 0.999 and variance in the advantage estimates explodes unless you collect much longer rollouts; drop it to 0.9 and the agent optimizes only the next handful of thrust commands.

The experience buffer D stores tuples (s, a, r, s', done). On-policy methods such as PPO discard the buffer after each update; off-policy methods such as SAC and Q-learning reuse every sample for tens of gradient steps, which is why they are far more sample-efficient when simulator throughput is the bottleneck.

RL Agent-Environment Interaction

Reinforcement Learning LoopAgent(Neural Network)Policy π(a|s)Value V(s)Environment(Simulator)AirSim / GazeboPhysics EngineAction aState s'Reward rExperience Buffer D(s, a, r, s', done) tuplesLearning

Q-Learning for Drones

Q-learning is a value-based method that learns the quality (Q-value) of state-action pairs. For drones, discretized states and actions make Q-learning applicable to simple tasks such as holding a fixed altitude or reaching one waypoint inside a small empty room.

The curse of dimensionality is the hard wall. With 20 bins per dimension and 6 state variables you get 20^6 = 64 million discrete states; each holds an 11-entry action vector, so the table alone would need about 2.8 GB in float64 and almost every cell is never visited. Practical Q-learning drones therefore restrict themselves to 2 or 3 state variables — for example x, y and altitude error inside a 10 m × 10 m arena — or replace the table with a neural function approximator, at which point you have moved into deep Q-networks territory.

Worked example: with learning rate α = 0.1, discount γ = 0.99, a step penalty of −0.01, and a terminal reward of +10 on goal reach, an agent that arrives in 40 steps banks about 10 × 0.99^40 ≈ 6.7 discounted from the goal plus roughly −0.3 from step penalties. Because each update pulls an existing Q-value toward the bootstrap target, a wrong early estimate decays with a time constant of about 1/α = 10 visits per state-action pair — so rare states improve slowly and deserve a higher ε-exploration budget.

Q-Learning Update Rule

Architecture Diagram
Q(s, a) ← Q(s, a) + α[r + γ·max Q(s', a') - Q(s, a)]
python
from collections import defaultdict

class QLearningDrone:
    def __init__(self, state_bins=20, action_space_size=5,
                 learning_rate=0.1, discount=0.99, epsilon=1.0):
        self.state_bins = state_bins
        self.action_space_size = action_space_size
        self.lr = learning_rate
        self.gamma = discount
        self.epsilon = epsilon
        self.epsilon_decay = 0.995
        self.epsilon_min = 0.01
        
        # Q-table
        self.q_table = defaultdict(lambda: np.zeros(action_space_size))
        
        # State discretization
        self.state_bounds = [
            (-10, 10),   # x position
            (-10, 10),   # y position
            (0, 20),     # altitude
            (-5, 5),     # x velocity
            (-5, 5),     # y velocity
            (-3, 3),     # z velocity
        ]
    
    def discretize_state(self, state):
        discrete = []
        for i, (s, (low, high)) in enumerate(zip(state, self.state_bounds)):
            s_clipped = np.clip(s, low, high)
            bins = np.linspace(low, high, self.state_bins)
            discrete.append(np.digitize(s_clipped, bins) - 1)
        return tuple(discrete)
    
    def choose_action(self, state):
        if np.random.random() < self.epsilon:
            return np.random.randint(self.action_space_size)
        
        discrete_state = self.discretize_state(state)
        return np.argmax(self.q_table[discrete_state])
    
    def learn(self, state, action, reward, next_state, done):
        s = self.discretize_state(state)
        s_next = self.discretize_state(next_state)
        
        current_q = self.q_table[s][action]
        
        if done:
            target = reward
        else:
            target = reward + self.gamma * np.max(self.q_table[s_next])
        
        self.q_table[s][action] += self.lr * (target - current_q)
    
    def decay_epsilon(self):
        self.epsilon = max(self.epsilon_min, self.epsilon * self.epsilon_decay)


class DiscreteActionSpace:
    """Discretized action space for Q-learning."""
    
    ACTIONS = {
        0: np.array([0, 0, 0]),       # Hover
        1: np.array([1, 0, 0]),       # Forward
        2: np.array([-1, 0, 0]),      # Backward
        3: np.array([0, 1, 0]),       # Left
        4: np.array([0, -1, 0]),      # Right
        5: np.array([0, 0, 1]),       # Up
        6: np.array([0, 0, -1]),      # Down
        7: np.array([1, 1, 0]),       # Forward-Left
        8: np.array([1, -1, 0]),      # Forward-Right
        9: np.array([-1, 1, 0]),      # Backward-Left
        10: np.array([-1, -1, 0]),    # Backward-Right
    }
    
    def __init__(self):
        self.n_actions = len(self.ACTIONS)
    
    def get_action(self, idx):
        return self.ACTIONS[idx]

Proximal Policy Optimization (PPO)

PPO is the most popular policy gradient method for continuous control. It's stable, sample-efficient, and works well for drone flight control. The clipped objective is the key idea: the probability ratio r(θ) = π_new(a|s) / π_old(a|s) is clamped to the band [1 − ε, 1 + ε] with ε = 0.2, so one minibatch can be reused for 3 to 10 epochs without the policy jumping away from the behavior that generated the data.

Drone-specific defaults reproduce most published results: Adam with learning rate 3e-4, a rollout batch of 2048 to 8192 steps collected at 50 Hz, GAE λ = 0.95, γ = 0.99, clip ε = 0.2, entropy coefficient 0.01, gradient-norm clip at 0.5, and hidden width 256 for both actor and critic. Expect roughly 20 to 40 million environment steps — about 4 to 8 hours of simulated flight at 50 Hz across parallel instances — before a hover-and-reach task saturates.

Two failure modes show up constantly. First, advantage normalization inside the batch is not optional: raw GAE values dominated by a −1000 collision penalty dwarf the dense progress terms and wreck the gradient signal. Second, actor and critic usually share an encoder but must keep separate output heads; fusing the final layer lets the value loss hijack the policy representation.

PPO Architecture for Drones

PPO Actor-Critic ArchitectureState InputPositionVelocityOrientationShared EncoderFC(256)FC(256)ReLU + LNFC(128)Actor (Policy)μ(s), σ(s)→ Mean & Std Dev→ Action DistributionCritic (Value)V(s)→ State ValueMotor Commands[roll, pitch, yaw, throttle]Value EstimateV(s) → AdvantagePPO Clipped Objective

L^CLIP = E[min(r(θ)·A, clip(r(θ), 1-ε, 1+ε)·A)]

r(θ) = π_new(a|s) / π_old(a|s) | ε = 0.2 (clip range)

PPO Implementation

python
from torch.distributions import Normal

class ActorCritic(nn.Module):
    def __init__(self, state_dim, action_dim, hidden_size=256):
        super().__init__()
        
        # Shared encoder
        self.encoder = nn.Sequential(
            nn.Linear(state_dim, hidden_size),
            nn.LayerNorm(hidden_size),
            nn.ReLU(),
            nn.Linear(hidden_size, hidden_size),
            nn.LayerNorm(hidden_size),
            nn.ReLU()
        )
        
        # Actor (policy)
        self.actor_mean = nn.Sequential(
            nn.Linear(hidden_size, hidden_size // 2),
            nn.ReLU(),
            nn.Linear(hidden_size // 2, action_dim)
        )
        self.actor_log_std = nn.Parameter(torch.zeros(action_dim))
        
        # Critic (value)
        self.critic = nn.Sequential(
            nn.Linear(hidden_size, hidden_size // 2),
            nn.ReLU(),
            nn.Linear(hidden_size // 2, 1)
        )
    
    def forward(self, state):
        features = self.encoder(state)
        
        # Actor
        action_mean = self.actor_mean(features)
        action_std = self.actor_log_std.exp().expand_as(action_mean)
        
        # Critic
        value = self.critic(features)
        
        return action_mean, action_std, value
    
    def get_action(self, state):
        action_mean, action_std, value = self.forward(state)
        dist = Normal(action_mean, action_std)
        action = dist.sample()
        log_prob = dist.log_prob(action).sum(dim=-1)
        return action, log_prob, value
    
    def evaluate(self, state, action):
        action_mean, action_std, value = self.forward(state)
        dist = Normal(action_mean, action_std)
        log_prob = dist.log_prob(action).sum(dim=-1)
        entropy = dist.entropy().sum(dim=-1)
        return log_prob, entropy, value


class PPOTrainer:
    def __init__(self, model, lr=3e-4, gamma=0.99, gae_lambda=0.95,
                 clip_epsilon=0.2, entropy_coef=0.01, value_coef=0.5):
        self.model = model
        self.optimizer = torch.optim.Adam(model.parameters(), lr=lr)
        self.gamma = gamma
        self.gae_lambda = gae_lambda
        self.clip_epsilon = clip_epsilon
        self.entropy_coef = entropy_coef
        self.value_coef = value_coef
    
    def compute_gae(self, rewards, values, dones, next_value):
        advantages = []
        gae = 0
        
        for t in reversed(range(len(rewards))):
            if t == len(rewards) - 1:
                next_val = next_value
            else:
                next_val = values[t + 1]
            
            delta = rewards[t] + self.gamma * next_val * (1 - dones[t]) - values[t]
            gae = delta + self.gamma * self.gae_lambda * (1 - dones[t]) * gae
            advantages.insert(0, gae)
        
        return torch.tensor(advantages, dtype=torch.float32)
    
    def update(self, rollout_buffer):
        states = torch.tensor(rollout_buffer['states'], dtype=torch.float32)
        actions = torch.tensor(rollout_buffer['actions'], dtype=torch.float32)
        old_log_probs = torch.tensor(rollout_buffer['log_probs'], dtype=torch.float32)
        rewards = rollout_buffer['rewards']
        dones = rollout_buffer['dones']
        values = rollout_buffer['values']
        
        # Compute GAE
        with torch.no_grad():
            _, _, next_value = self.model(states[-1:])
        returns = self.compute_gae(rewards, values, dones, next_value.item())
        advantages = returns - torch.tensor(values, dtype=torch.float32)
        advantages = (advantages - advantages.mean()) / (advantages.std() + 1e-8)
        
        # PPO update epochs
        total_loss = 0
        for _ in range(10):
            log_probs, entropy, new_values = self.model.evaluate(states, actions)
            
            # Policy loss with clipping
            ratio = (log_probs - old_log_probs).exp()
            surr1 = ratio * advantages
            surr2 = torch.clamp(ratio, 1 - self.clip_epsilon, 1 + self.clip_epsilon) * advantages
            policy_loss = -torch.min(surr1, surr2).mean()
            
            # Value loss
            value_loss = F.mse_loss(new_values.squeeze(), returns)
            
            # Entropy bonus
            entropy_loss = -entropy.mean()
            
            # Total loss
            loss = (policy_loss + 
                    self.value_coef * value_loss + 
                    self.entropy_coef * entropy_loss)
            
            self.optimizer.zero_grad()
            loss.backward()
            nn.utils.clip_grad_norm_(self.model.parameters(), 0.5)
            self.optimizer.step()
            
            total_loss += loss.item()
        
        return total_loss / 10

Soft Actor-Critic (SAC)

Sponsored

SAC is an off-policy algorithm that maximizes both reward and policy entropy, encouraging exploration. It's particularly effective for drone control with continuous action spaces because every simulator step feeds a replay buffer of 100,000 to 1,000,000 transitions that get resampled many times.

The entropy temperature is tuned automatically: target entropy is set to −action_dim (so −4 for a four-rotor action space), and a separate optimizer adjusts log α until the policy's entropy converges on that target. Twin Q-networks combined with a min operator suppress the overestimation bias that otherwise plagues actor-critic methods on sparse-reward collision tasks.

Choose SAC over PPO when simulator throughput is low or when the reward is sparse enough that PPO's short on-policy data reuse cannot keep exploration alive. Choose PPO over SAC when you can afford millions of parallel steps and want hyperparameters that rarely diverge.

python
from torch.distributions import Normal

class SACAgent:
    def __init__(self, state_dim, action_dim, hidden_size=256):
        # Twin Q-networks
        self.q1 = self._build_critic(state_dim, action_dim, hidden_size)
        self.q2 = self._build_critic(state_dim, action_dim, hidden_size)
        self.q1_target = self._build_critic(state_dim, action_dim, hidden_size)
        self.q2_target = self._build_critic(state_dim, action_dim, hidden_size)
        
        # Policy network
        self.policy = self._build_policy(state_dim, action_dim, hidden_size)
        
        # Automatic entropy tuning
        self.target_entropy = -action_dim
        self.log_alpha = torch.zeros(1, requires_grad=True)
        
        # Optimizers
        self.q1_optimizer = torch.optim.Adam(self.q1.parameters(), lr=3e-4)
        self.q2_optimizer = torch.optim.Adam(self.q2.parameters(), lr=3e-4)
        self.policy_optimizer = torch.optim.Adam(self.policy.parameters(), lr=3e-4)
        self.alpha_optimizer = torch.optim.Adam([self.log_alpha], lr=3e-4)
    
    def _build_critic(self, state_dim, action_dim, hidden_size):
        return nn.Sequential(
            nn.Linear(state_dim + action_dim, hidden_size),
            nn.ReLU(),
            nn.Linear(hidden_size, hidden_size),
            nn.ReLU(),
            nn.Linear(hidden_size, 1)
        )
    
    def _build_policy(self, state_dim, action_dim, hidden_size):
        return nn.Sequential(
            nn.Linear(state_dim, hidden_size),
            nn.ReLU(),
            nn.Linear(hidden_size, hidden_size),
            nn.ReLU(),
            nn.Linear(hidden_size, action_dim * 2)  # mean and log_std
        )
    
    def select_action(self, state, evaluate=False):
        state_tensor = torch.FloatTensor(state).unsqueeze(0)
        output = self.policy(state_tensor)
        mean, log_std = output.chunk(2, dim=-1)
        log_std = torch.clamp(log_std, -20, 2)
        
        if evaluate:
            action = torch.tanh(mean)
        else:
            std = log_std.exp()
            normal = Normal(mean, std)
            x_t = normal.rsample()
            action = torch.tanh(x_t)
        
        return action.detach().numpy()[0]
    
    def update(self, batch, alpha=0.2):
        states, actions, rewards, next_states, dones = batch
        
        # Update Q-networks
        with torch.no_grad():
            next_actions, next_log_probs = self.policy.sample(next_states)
            q1_next = self.q1_target(torch.cat([next_states, next_actions], dim=-1))
            q2_next = self.q2_target(torch.cat([next_states, next_actions], dim=-1))
            q_next = torch.min(q1_next, q2_next) - alpha * next_log_probs
            target_q = rewards + (1 - dones) * 0.99 * q_next
        
        q1_loss = F.mse_loss(self.q1(torch.cat([states, actions], dim=-1)), target_q)
        q2_loss = F.mse_loss(self.q2(torch.cat([states, actions], dim=-1)), target_q)
        
        self.q1_optimizer.zero_grad()
        q1_loss.backward()
        self.q1_optimizer.step()
        
        self.q2_optimizer.zero_grad()
        q2_loss.backward()
        self.q2_optimizer.step()
        
        # Update policy
        new_actions, log_probs = self.policy.sample(states)
        q1_new = self.q1(torch.cat([states, new_actions], dim=-1))
        q2_new = self.q2(torch.cat([states, new_actions], dim=-1))
        q_new = torch.min(q1_new, q2_new)
        
        policy_loss = (alpha * log_probs - q_new).mean()
        
        self.policy_optimizer.zero_grad()
        policy_loss.backward()
        self.policy_optimizer.step()
        
        # Update entropy coefficient
        alpha_loss = -(self.log_alpha * (log_probs + self.target_entropy).detach()).mean()
        self.alpha_optimizer.zero_grad()
        alpha_loss.backward()
        self.alpha_optimizer.step()
        
        return self.log_alpha.exp().item()

Reward Shaping for Drones

Reward design is critical for learning effective drone behaviors. Poorly designed rewards lead to unintended behaviors.

The classic failure is reward hacking. Hand out a +0.1 survival bonus per step with no episode time limit and the agent discovers it can hover in a corner forever: the episode never ends, cumulative return climbs, and the goal is never visited. Reward pure distance progress without collision penalties and the agent skims obstacles at centimeter clearance. A robust dense reward needs four competing terms: progress toward the goal as the dominant signal, a large one-shot bonus on goal reach (for example +100), a collision penalty an order of magnitude larger than that bonus (for example −1000), and small smoothness and altitude penalties that keep the trajectory flyable.

A practical calibration trick is episode-level return normalization: record the running mean and variance of episodic returns and divide advantages by that standard deviation before the update, instead of hand-tuning every coefficient into identical units.

Reward Components

Drone RL Reward ShapingProgress Rewards (+)Distance to goalWaypoint reachedTask completedFlight time bonusr = +10 per waypointPenalty Rewards (-)CollisionOvershootLow batteryOut of boundsr = -1000 collisionShaping RewardsAltitude keepingSmooth controlSpeed limitsEnergy efficiencyr = -0.1 per jerkExample: Reaching Goal TaskStartGoal!+0.5 (closer)-100 (collision!) -2.0 (too close)+5 (waypoint)+100 (goal!)

Reward Function Implementation

python
from typing import Dict

class DroneRewardFunction:
    def __init__(self, config: Dict = None):
        config = config or {}
        self.goal_reward = config.get('goal_reward', 100.0)
        self.collision_penalty = config.get('collision_penalty', -1000.0)
        self.distance_scale = config.get('distance_scale', 1.0)
        self.altitude_target = config.get('altitude_target', 5.0)
        self.max_speed = config.get('max_speed', 5.0)
        self.battery_threshold = config.get('battery_threshold', 20.0)
        
        self.prev_distance = None
        self.prev_altitude = None
        self.prev_velocity = None
    
    def compute_reward(self, state: Dict) -> float:
        reward = 0.0
        
        # Goal reaching
        if state.get('reached_goal', False):
            return self.goal_reward
        
        # Collision
        if state.get('collision', False):
            return self.collision_penalty
        
        # Progress toward goal (dense reward)
        current_distance = state['distance_to_goal']
        if self.prev_distance is not None:
            progress = self.prev_distance - current_distance
            reward += progress * self.distance_scale
        
        # Altitude maintenance
        altitude = state['altitude']
        altitude_error = abs(altitude - self.altitude_target)
        reward -= altitude_error * 0.1
        
        # Speed penalty (encourage moderate speed)
        speed = state['speed']
        if speed > self.max_speed:
            reward -= (speed - self.max_speed) * 0.5
        
        # Smooth control (penalize jerky movements)
        if self.prev_velocity is not None:
            velocity_change = np.linalg.norm(state['velocity'] - self.prev_velocity)
            reward -= velocity_change * 0.01
        
        # Battery warning
        battery = state.get('battery', 100)
        if battery < self.battery_threshold:
            reward -= (self.battery_threshold - battery) * 0.5
        
        # Survival bonus
        reward += 0.1
        
        # Update previous values
        self.prev_distance = current_distance
        self.prev_altitude = altitude
        self.prev_velocity = state['velocity'].copy() if 'velocity' in state else None
        
        return reward
    
    def reset(self):
        self.prev_distance = None
        self.prev_altitude = None
        self.prev_velocity = None

Sim-to-Real Transfer

Training in simulation is cheap and safe, but policies must transfer to real drones. Domain randomization helps bridge the reality gap.

Randomize the parameters your simulator models wrong — not every parameter equally. Wind mean and standard deviation, body mass ±10 %, drag coefficient ±20 %, sensor noise σ = 0.02, motor lag up to 20 ms, and gravity between 9.75 and 9.85 m/s² cover the gaps most teams actually measure on hardware. If the real motors have a 15 ms time constant while the sim assumes instantaneous thrust, no amount of reward tuning will make the policy transfer.

A cheap validation loop: train with randomization enabled, freeze the winning checkpoint, then run 20 episodes in an un-randomized simulator configured to match your calibration data. Success rate below 80 % means the randomization ranges are too narrow or a physical parameter is missing from the list.

python
from typing import Dict

class DomainRandomizer:
    def __init__(self):
        self.randomization_params = {
            'wind': {'mean': 0, 'std': 0.5},
            'mass': {'min': 0.9, 'max': 1.1},
            'drag': {'min': 0.8, 'max': 1.2},
            'sensor_noise': {'std': 0.02},
            'motor_delay': {'min': 0, 'max': 0.02},
            'gravity': {'min': 9.75, 'max': 9.85},
        }
    
    def randomize_environment(self) -> Dict:
        params = {}
        for key, values in self.randomization_params.items():
            if 'mean' in values and 'std' in values:
                params[key] = np.random.normal(values['mean'], values['std'])
            elif 'min' in values and 'max' in values:
                params[key] = np.random.uniform(values['min'], values['max'])
            elif 'std' in values:
                params[key] = np.random.normal(0, values['std'])
        return params
    
    def apply_randomization(self, sim_env, params: Dict):
        if 'wind' in params:
            sim_env.set_wind(params['wind'])
        if 'mass' in params:
            sim_env.set_drone_mass(params['mass'])
        if 'drag' in params:
            sim_env.set_drag_coefficient(params['drag'])
        if 'gravity' in params:
            sim_env.set_gravity(params['gravity'])

Training Pipeline

python
from collections import deque

class RLTrainingPipeline:
    def __init__(self, env, agent, reward_fn, randomizer=None):
        self.env = env
        self.agent = agent
        self.reward_fn = reward_fn
        self.randomizer = randomizer
        
        self.episode_rewards = deque(maxlen=100)
        self.episode_lengths = deque(maxlen=100)
        self.best_reward = -float('inf')
    
    def train(self, n_episodes=1000, max_steps=500):
        for episode in range(n_episodes):
            state = self.env.reset()
            self.reward_fn.reset()
            
            if self.randomizer:
                params = self.randomizer.randomize_environment()
                self.randomizer.apply_randomization(self.env, params)
            
            episode_reward = 0
            episode_length = 0
            
            for step in range(max_steps):
                # Select action
                action = self.agent.select_action(state)
                
                # Step environment
                next_state, done, info = self.env.step(action)
                
                # Compute reward
                reward_info = {
                    'distance_to_goal': info['distance_to_goal'],
                    'altitude': info['altitude'],
                    'velocity': info['velocity'],
                    'speed': info['speed'],
                    'battery': info['battery'],
                    'collision': info.get('collision', False),
                    'reached_goal': info.get('reached_goal', False)
                }
                reward = self.reward_fn.compute_reward(reward_info)
                
                # Store transition
                self.agent.store_transition(state, action, reward, next_state, done)
                
                # Learn
                if len(self.agent.buffer) > self.agent.batch_size:
                    self.agent.update()
                
                episode_reward += reward
                episode_length += 1
                state = next_state
                
                if done:
                    break
            
            self.episode_rewards.append(episode_reward)
            self.episode_lengths.append(episode_length)
            
            avg_reward = np.mean(self.episode_rewards)
            if episode % 10 == 0:
                print(f"Episode {episode}: Reward={episode_reward:.1f}, "
                      f"Avg={avg_reward:.1f}, Length={episode_length}")
            
            if avg_reward > self.best_reward:
                self.best_reward = avg_reward
                self.agent.save_model('best_model.pth')

Hands-On Project: Complete RL Training

Advertisement
python

class DroneFlightEnv:
    def __init__(self):
        self.position = np.zeros(3)
        self.velocity = np.zeros(3)
        self.orientation = np.zeros(3)
        self.battery = 100.0
        self.goal = np.array([10.0, 10.0, 5.0])
        self.max_steps = 500
        self.step_count = 0
        
        # Dynamics parameters
        self.dt = 0.1
        self.mass = 1.0
        self.gravity = 9.81
        self.max_force = 15.0
    
    def reset(self):
        self.position = np.zeros(3)
        self.velocity = np.zeros(3)
        self.orientation = np.zeros(3)
        self.battery = 100.0
        self.step_count = 0
        return self._get_state()
    
    def step(self, action):
        self.step_count += 1
        
        # Apply action (thrust commands for 4 rotors)
        forces = action * self.max_force
        total_thrust = np.sum(forces)
        
        # Simple dynamics
        acceleration = np.array([0, 0, total_thrust / self.mass - self.gravity])
        self.velocity += acceleration * self.dt
        self.position += self.velocity * self.dt
        
        # Battery consumption
        self.battery -= 0.05 * np.sum(np.abs(action))
        
        # Check termination
        done = False
        info = {
            'collision': self._check_collision(),
            'reached_goal': self._check_goal(),
            'distance_to_goal': np.linalg.norm(self.position - self.goal),
            'altitude': self.position[2],
            'velocity': self.velocity.copy(),
            'speed': np.linalg.norm(self.velocity),
            'battery': self.battery
        }
        
        if info['collision'] or info['reached_goal'] or self.battery <= 0:
            done = True
        
        if self.step_count >= self.max_steps:
            done = True
        
        return self._get_state(), done, info
    
    def _get_state(self):
        return np.concatenate([
            self.position,
            self.velocity,
            self.orientation,
            [self.battery],
            self.goal
        ])
    
    def _check_collision(self):
        return self.position[2] < 0 or np.linalg.norm(self.position[:2]) > 100
    
    def _check_goal(self):
        return np.linalg.norm(self.position - self.goal) < 1.0


# Complete training script
def train_drone_rl():
    env = DroneFlightEnv()
    state_dim = 13  # pos(3) + vel(3) + orient(3) + battery(1) + goal(3)
    action_dim = 4  # 4 rotor commands
    
    # Create PPO agent
    model = ActorCritic(state_dim, action_dim)
    agent = PPOTrainer(model)
    
    # Training loop
    n_episodes = 1000
    for episode in range(n_episodes):
        state = env.reset()
        episode_reward = 0
        
        for step in range(500):
            state_tensor = torch.FloatTensor(state).unsqueeze(0)
            action, log_prob, value = model.get_action(state_tensor)
            
            next_state, done, info = env.step(action.detach().numpy()[0])
            
            # Simple reward
            reward = -0.1  # Survival
            reward += info['distance_to_goal'] * 0.5  # Progress
            if info['reached_goal']:
                reward += 100
            if info['collision']:
                reward -= 100
            
            episode_reward += reward
            
            if done:
                break
            
            state = next_state
        
        if episode % 10 == 0:
            print(f"Episode {episode}: Total Reward = {episode_reward:.1f}")

if __name__ == "__main__":
    train_drone_rl()

Choosing the Right Algorithm

AlgorithmSample efficiencyStabilityContinuous actionsBest fit
Q-learningLow as a table; medium as DQNHigh for discrete tasksNo without extensionsTiny didactic problems
PPOMedium (20–40M steps)Very highYes, Gaussian actorDefault; parallel simulators
SACHigh, 2–5× fewer steps than PPOMediumYesSparse rewards; slow sim

A decision rule that works in practice: start with PPO whenever you can parallelize the simulator, because its clipped objective rarely diverges and its hyperparameters are forgiving. Switch to SAC when a single environment step costs more than about 5 ms of wall-clock time, or when the reward is so sparse that PPO's on-policy data reuse cannot keep exploration alive. Reach for tabular Q-learning only when the reachable state space is genuinely small — under roughly 10,000 states — or when you are teaching the update rule before scaling up.

Whatever you pick, log four curves during training: episode return, episode length, success rate, and collision rate. Return improving while success stays flat almost always means the agent found a loophole in the reward function.

Safety Constraints and Action Masking

Learned policies do not inherit a sense of self-preservation. Two independent layers keep them flyable.

Action masking clips the policy output into a certified envelope before it reaches the simulator or flight controller: maximum horizontal speed 5 m/s, vertical speed 2 m/s, tilt angle 25°, and a geofence box around the arena. In continuous-action algorithms, apply the mask after tanh squashing so recorded log-probabilities stay consistent with the actions that were actually executed.

A separate finite-state safety supervisor watches the raw state regardless of the policy: distance to nearest obstacle below 1 m commands hover, battery below 20 % commands return-to-home, attitude error beyond 35° commands emergency landing. During training, terminate the episode and apply the collision penalty whenever the supervisor fires, so the policy learns to avoid triggering it rather than depending on it.

Common Mistakes

Setting γ = 0.999 for a 50 Hz hover task and watching advantage estimates explode

Fix: Use γ = 0.99, an effective 100-step horizon, and normalize advantages per batch before every update

Shipping a policy trained with zero wind straight to a real airframe

Fix: Randomize wind, mass, drag and motor delay during training, then validate with 20 frozen-policy episodes in a matched sim

Adding a survival bonus with no episode time limit, so the agent learns to hover forever

Fix: Cap episode length, make the collision penalty an order of magnitude larger than the goal bonus, and track success rate instead of raw return

Key Takeaways

Key Takeaways

  • Q-learning only fits tiny discretized tasks: 20 bins across 6 dimensions already means 64 million states
  • PPO is the default for continuous drone control — lr 3e-4, clip 0.2, GAE λ 0.95, 20–40M steps
  • SAC reuses replay data and tunes entropy automatically, cutting sample needs 2–5× on sparse rewards
  • Reward hacking such as hovering or obstacle skimming shows up as rising return with flat success rate
  • Sim-to-real transfer needs domain randomization over the parameters your hardware actually varies
  • Keep an action mask and a safety supervisor between the policy and the motors at all times

Best Practices

  • Train with 8–16 parallel simulator instances; PPO's sample hunger is a throughput problem
  • Log success rate and collision rate alongside return — return alone hides reward hacking
  • Freeze the checkpoint with the best validation success rate, not the last training episode
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-10-15

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Drone AI Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement