How duongpm/spso works
1. This repository is a small MATLAB research codebase for Spherical Vector-based Particle Swarm Optimization (SPSO), specialized here for UAV path planning. The architecture is flat: one main driver script, a handful of helper functions, and a terrain image used as the environment input.
2. The top-level orchestration lives in SPSO_MAIN.m. It is the entry point the README says to run, and it likely coordinates the full optimization loop: model setup, swarm initialization, cost evaluation, particle updates, and final visualization.
3. The environment/model boundary is handled by CreateModel.m together with the input file ChrismasTerrain.tif. This is the repository’s main problem-definition layer: it turns the terrain/map data into whatever internal representation the optimizer uses for obstacles, feasible space, and path endpoints.
4. The optimization-state initialization is split out into CreateRandomSolution.m. This function is likely responsible for generating initial UAV paths or particle positions in the spherical-vector representation used by SPSO, so the search starts from valid or at least diverse candidate solutions.
5. The objective and constraint logic is concentrated in MyCost.m. This is the key evaluation boundary between the swarm and the environment: it scores candidate paths based on path quality and safety, and probably penalizes collisions or terrain violations using data from the model.
6. Several small geometry utilities support that cost evaluation. DistP2S.m computes point-to-segment distance, which is a typical primitive for collision checks and clearance calculations, while SphericalToCart.m converts spherical vectors to Cartesian coordinates, matching the algorithm’s spherical representation to 3D path geometry.
7. Visualization is separated from optimization. PlotModel.m renders the terrain/environment and PlotSolution.m renders candidate or final paths over that environment. This keeps the optimization loop independent from plotting, which is useful both for debugging and for reproducing paper figures.
8. The data flow is therefore: terrain image and parameters into CreateModel.m, candidate paths from CreateRandomSolution.m, repeated fitness evaluation through MyCost.m and geometry helpers, iterative swarm updates in SPSO_MAIN.m, then final rendering through the plotting functions.
9. The main architectural boundary is between “search logic” and “problem geometry.” The swarm algorithm itself appears to be generic SPSO machinery embedded in the main script, while all UAV/terrain specifics are isolated in the model, cost, and geometry helpers. That makes the code adaptable to other path-planning problems by replacing those problem-specific parts.
10. Tooling/runtime-wise, this is standard MATLAB code, with a note in the README that the Curve Fitting Toolbox may be required depending on MATLAB version. There are no web services, databases, or distributed components; the entire system runs locally as a single MATLAB optimization workflow.
Main entry points: SPSO_MAIN.m SPSO_MAIN · MyCost.m MyCost · CreateModel.m CreateModel · PlotModel.m PlotModel
Components 9
SPSO workflow
Main optimization flow, problem setup, evaluation, and plotting.
- SPSO_MAIN · Entry point that runs the SPSO-based UAV path planning workflow.
SPSO_MAIN.m - CreateModel · Builds the terrain and planning model used by the optimizer.
CreateModel.m - Terrain · Input terrain image used as the environment definition.
ChrismasTerrain.tif - CreateRandomSolution · Generates initial candidate UAV paths or particle states.
CreateRandomSolution.m - MyCost · Evaluates candidate paths and applies safety or feasibility penalties.
MyCost.m - DistP2S · Computes point-to-segment distance for clearance and collision checks.
DistP2S.m - SphericalToCart · Converts spherical-vector representation into Cartesian coordinates.
SphericalToCart.m - PlotModel · Renders the terrain and planning environment.
PlotModel.m - PlotSolution · Renders candidate or final paths over the model.
PlotSolution.m
Connections 10
- SPSO_MAIN → CreateModel: build model · Initializes the environment and planning bounds before optimization.
- CreateModel → Terrain: loads · Uses the terrain image as the problem input.
- SPSO_MAIN → CreateRandomSolution: seed swarm · Creates the initial population or candidate paths.
- SPSO_MAIN → MyCost: evaluate · Scores particles during the optimization loop.
- MyCost → DistP2S: collision check · Uses segment distance to measure clearance and violations.
- MyCost → SphericalToCart: convert · Maps spherical particle variables into Cartesian path geometry.
- SPSO_MAIN → PlotModel: render env · Produces the environment visualization.
- SPSO_MAIN → PlotSolution: render path · Draws final or intermediate solutions.
- PlotModel → Terrain: overlay · Displays the planning results on top of the terrain map.
- PlotSolution → CreateModel: overlay · Uses the model context for path rendering.