As an example of drone formation transformation discussed in the previous article, the following code demonstrates the application of Hungarian Algorithm to determine the position allocation in formation transformation.
#! /usr/bin/python
# pip install hungarian-algorithm
from hungarian_algorithm import algorithm
import numpy as np
# Source: drones in a 3×3 grid on Z=0 plane
source_positions = [
(x, y, 0)
for y in range(3)
for x in range(3)
]
# Target: drones in a single horizontal line (linear flight path), spaced 10 units apart
target_positions = [
(i * 10, 0, 0) for i in range(9)
]
# Compute cost matrix (Euclidean distance)
cost_matrix = [
[
np.linalg.norm(np.array(src) - np.array(dst))
for dst in target_positions
]
for src in source_positions
]
# Run Hungarian Algorithm to get minimum-cost assignment
assignment = algorithm.find_matching(cost_matrix, matching_type='min')
# Report matched pairs
for src_idx, dst_idx in enumerate(assignment):
print(f"Drone {src_idx} → Target Position {dst_idx}: {target_positions[dst_idx]}")
The above does not take velocity and heading into consideration but that can be adjusted as per trajectory.
#Codingexercise: https://1drv.ms/w/c/d609fb70e39b65c8/EYzGgu5Fc4dEoCUHWQYxMbUBSfvC36iKh8ESBaLtozvdqA?e=VfWGog