Implementation of Particle Swarm Optimization (PSO) consistent with the softpy library.
Library Integration
- Extends Softpy's
FloatVectorCandidateandMetaHeuristicsAlgorithmabstract base classes - Maintains API consistency with existing Softpy optimization algorithms
- Implements required interface methods (
mutate(),recombine(),generate())
Design Patterns
- Factory Method:
ParticleCandidate.generate()creates instances with proper initialization - Template Method: Inherits optimization workflow from
MetaHeuristicsAlgorithm - State preservation through deep copy mechanics for best position tracking
- Implemented from scratch: particle dynamics, velocity updates, and swarm evolution.
- OOP & Design Patterns: subclassed
FloatVectorCandidateand applied the Factory Method. - Metaheuristics: applied swarm intelligence concepts (personal, local, global bests).
- Numerical Computing: vectorized with NumPy, handled boundaries, and ensured reproducibility.
- Algorithm Engineering: managed in-place state updates using deep copies to preserve best positions.
- Testing: verified correctness on benchmark functions (Sphere, Rosenbrock).
Visualization of PSO optimization on the Rosenbrock function:
Particles (red) converge toward the global optimum (star) as iterations progress.
pip install -r requirements.txt
python run_demo.pyUse the harness to run tests and generate optimization plots:
python test.py my_impl.py --pop 50 --nei 10 --iters 100• --pop: population size (number of particles)
• --nei: number of neighbors considered for each particle
• --iters: number of iterations (steps)
Example: sweep over multiple configurations in one run:
python test.py my_impl.py --pop 30,50 --nei 5,10 --iters 100,200This will run PSO with:
• 2 different population sizes (30, 50)
• 2 different neighborhood sizes (5, 10)
• 2 iteration counts (100, 200)
→ creating a total of 8 runs and saving plots in the pictures/ folder.
├── docs/ # Assignment specification (PDF)
├── pictures/ # Optimization plots generated by test harness
├── particle_candidate.py # Particle representation (ParticleCandidate class)
├── optimizer.py # Particle Swarm Optimizer class
├── my_impl.py # Wrapper to expose both classes to the test harness
├── run_demo.py # Minimal demo script for quick execution
├── test.py # Test harness with visualizations
├── requirements.txt
├── README.md
└── .gitignore
Done by me, Stella Andorno (ialwayslikedgrime)
