From df723331beb06a149f5440521eeb795a3b5103f5 Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Thu, 2 Feb 2023 14:48:02 +0100 Subject: [PATCH] refactor(fitness): store cache with the fitness to allow rollback with minimal mem footprint --- mo/src/problems/partition/moBinaryPartition.h | 20 ++++++--- .../partition/moBinaryPartitionSwapNeighbor.h | 14 ++++--- .../moBinaryPartitionSwapNeighborhood.h | 42 +++++++++---------- 3 files changed, 44 insertions(+), 32 deletions(-) diff --git a/mo/src/problems/partition/moBinaryPartition.h b/mo/src/problems/partition/moBinaryPartition.h index 768cf3bd1..b1b698ab8 100644 --- a/mo/src/problems/partition/moBinaryPartition.h +++ b/mo/src/problems/partition/moBinaryPartition.h @@ -118,7 +118,7 @@ class moBinaryPartition : public EO * Output a string of the form (spaces replaced with period here, to show their count): * `.........` */ - virtual void printOn(std::ostream& out) const + virtual void printOn(std::ostream& out) const override { EO::printOn(out); // Fitness. // Trailing space already inserted. @@ -136,7 +136,7 @@ class moBinaryPartition : public EO * Expects a string of the form (spaces replaced with period here, to show their count): * `.........` */ - virtual void readFrom(std::istream& in) + virtual void readFrom(std::istream& in) override { EO::readFrom(in); // Fitness. unsigned size; @@ -162,17 +162,25 @@ class moBinaryPartition : public EO and this->rejected == other.rejected; } - virtual std::string className() const + virtual std::string className() const override { return "moBinaryPartition"; } - void fitness(const FitT& fit) { - CLUTCHLOG(debug, "Fitness assignment -- solution: " << *this << " gets fitness: " << fit); + virtual void fitness(const FitT& fit) override + { + // std::clog << "Fitness assignment -- solution: " << *this << " gets fitness: " << fit << std::endl; EO::fitness(fit); } - FitT fitness() const { + virtual const FitT& fitness() const override + { return EO::fitness(); } + + virtual void invalidate() override + { + // this->fitness().clear(); + EO::invalidate(); + } }; diff --git a/mo/src/problems/partition/moBinaryPartitionSwapNeighbor.h b/mo/src/problems/partition/moBinaryPartitionSwapNeighbor.h index dd9a93a3a..2fe5d3a55 100644 --- a/mo/src/problems/partition/moBinaryPartitionSwapNeighbor.h +++ b/mo/src/problems/partition/moBinaryPartitionSwapNeighbor.h @@ -18,7 +18,7 @@ */ template class moBinaryPartitionSwapNeighbor : - public moBackableNeighbor//, + public moBackableNeighbor//, // public moIndexNeighbor // FIXME see if we can model that. { public: @@ -100,6 +100,7 @@ class moBinaryPartitionSwapNeighbor : solution.reject(this->reject); solution.select(this->select); assert(solution.selected.size() == this->selected_nb); + // this->fitness( Fitness(solution.fitness()) ); // For the cache. solution.invalidate(); } @@ -112,6 +113,7 @@ class moBinaryPartitionSwapNeighbor : solution.reject(this->select); solution.select(this->reject); assert(solution.selected.size() == this->selected_nb); + // this->fitness( Fitness(solution.fitness()) ); // For the cache. solution.invalidate(); } @@ -153,7 +155,7 @@ class moBinaryPartitionSwapNeighbor : } private: // Disable access to `equals(moNeighbor<…>&)` (removes the related overloaded-virtual warning). - using moBackableNeighbor::equals; + using moBackableNeighbor::equals; public: @@ -181,12 +183,14 @@ class moBinaryPartitionSwapNeighbor : return this->selected_nb; } - void fitness(const Fitness& fit) { - CLUTCHLOG(debug, "Fitness assignment -- neighbor: " << *this << " gets fitness: " << fit); + virtual void fitness(const Fitness& fit) override + { + // std::clog << "Fitness assignment -- neighbor: " << *this << " gets fitness: " << fit << std::endl; EO::fitness(fit); } - Fitness fitness() const { + virtual const Fitness& fitness() const override + { return EO::fitness(); } diff --git a/mo/src/problems/partition/moBinaryPartitionSwapNeighborhood.h b/mo/src/problems/partition/moBinaryPartitionSwapNeighborhood.h index 50702be9a..ca40fa551 100644 --- a/mo/src/problems/partition/moBinaryPartitionSwapNeighborhood.h +++ b/mo/src/problems/partition/moBinaryPartitionSwapNeighborhood.h @@ -55,13 +55,13 @@ class moBinaryPartitionSwapNeighborhood : public moNeighborhood