From d95f385418d3be12ec5d551dee06f659d71bf623 Mon Sep 17 00:00:00 2001 From: Eremey Valetov Date: Mon, 16 Feb 2026 18:14:03 -0500 Subject: [PATCH] feat(moeo): add finalize() lifecycle hook for algorithm post-processing Add virtual finalize() and hasFinalize() to moeoAlgo/moeoPopAlgo hierarchy. NSGA-II implements finalize() to recompute fitness and diversity assignments after population modifications (e.g. immigrant integration in island model). --- moeo/src/algo/moeoAlgo.h | 8 +++++++- moeo/src/algo/moeoNSGAII.h | 11 +++++++++++ moeo/src/algo/moeoPopAlgo.h | 10 +++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/moeo/src/algo/moeoAlgo.h b/moeo/src/algo/moeoAlgo.h index 39b01f394..3458ec0c4 100644 --- a/moeo/src/algo/moeoAlgo.h +++ b/moeo/src/algo/moeoAlgo.h @@ -42,6 +42,12 @@ * Abstract class for multi-objective algorithms. */ class moeoAlgo - {}; +{ +public: + virtual ~moeoAlgo() = default; + + /** Whether this algorithm supports finalize() for post-integration updates. */ + virtual bool hasFinalize() const { return false; } +}; #endif /*MOEOALGO_H_*/ diff --git a/moeo/src/algo/moeoNSGAII.h b/moeo/src/algo/moeoNSGAII.h index 137592865..68e0cd200 100644 --- a/moeo/src/algo/moeoNSGAII.h +++ b/moeo/src/algo/moeoNSGAII.h @@ -148,6 +148,17 @@ public: while (continuator (_pop)); } + /** + * Recompute fitness and diversity assignments on the population. + * Useful after integrating immigrants from an island model. + * @param _pop the population to finalize + */ + void finalize(eoPop& _pop) override { + fitnessAssignment(_pop); + diversityAssignment(_pop); + } + + bool hasFinalize() const override { return true; } protected: diff --git a/moeo/src/algo/moeoPopAlgo.h b/moeo/src/algo/moeoPopAlgo.h index a3b948a3e..1caa40744 100644 --- a/moeo/src/algo/moeoPopAlgo.h +++ b/moeo/src/algo/moeoPopAlgo.h @@ -47,6 +47,14 @@ */ template < class MOEOT > class moeoPopAlgo : public moeoAlgo, public eoAlgo < MOEOT > - {}; +{ +public: + /** + * Recompute fitness/diversity after external population changes (e.g. immigrant integration). + * Default implementation is a no-op. Override in subclasses that need it. + * @param _pop the population to finalize + */ + virtual void finalize(eoPop&) {} +}; #endif /*MOEOPOPALGO_H_*/