From 237426a6b40a049617eb1106bc9736ce262cfa54 Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Thu, 26 Jan 2023 11:48:44 +0100 Subject: [PATCH] refactor while hunting a bug --- mo/src/problems/partition/moBinaryPartition.h | 9 ++++ .../partition/moBinaryPartitionSwapNeighbor.h | 41 ++++++++++++++----- .../moBinaryPartitionSwapNeighborhood.h | 37 ++++++++++------- 3 files changed, 62 insertions(+), 25 deletions(-) diff --git a/mo/src/problems/partition/moBinaryPartition.h b/mo/src/problems/partition/moBinaryPartition.h index c91c2848b..768cf3bd1 100644 --- a/mo/src/problems/partition/moBinaryPartition.h +++ b/mo/src/problems/partition/moBinaryPartition.h @@ -166,4 +166,13 @@ class moBinaryPartition : public EO { return "moBinaryPartition"; } + + void fitness(const FitT& fit) { + CLUTCHLOG(debug, "Fitness assignment -- solution: " << *this << " gets fitness: " << fit); + EO::fitness(fit); + } + + FitT fitness() const { + return EO::fitness(); + } }; diff --git a/mo/src/problems/partition/moBinaryPartitionSwapNeighbor.h b/mo/src/problems/partition/moBinaryPartitionSwapNeighbor.h index 8dca43d53..dd9a93a3a 100644 --- a/mo/src/problems/partition/moBinaryPartitionSwapNeighbor.h +++ b/mo/src/problems/partition/moBinaryPartitionSwapNeighbor.h @@ -65,7 +65,9 @@ class moBinaryPartitionSwapNeighbor : /** Copy constructor. */ moBinaryPartitionSwapNeighbor( const moBinaryPartitionSwapNeighbor& other) : - selected_nb(other.selected_nb ) + selected_nb(other.selected_nb), + select(other.select), + reject(other.reject) #ifndef NDEBUG , is_set(other.is_set) #endif @@ -78,11 +80,13 @@ class moBinaryPartitionSwapNeighbor : moBinaryPartitionSwapNeighbor& operator=( const moBinaryPartitionSwapNeighbor& other) { - this->fitness(other.fitness()); this->selected_nb = other.selected_nb; + this->select = other.select; + this->reject = other.reject; #ifndef NDEBUG this->is_set = other.is_set; #endif + this->fitness(other.fitness()); return *this; } @@ -95,10 +99,7 @@ class moBinaryPartitionSwapNeighbor : // Swap the two atoms. solution.reject(this->reject); solution.select(this->select); - #ifndef NDEBUG - assert(solution.selected.size() == this->selected_nb); - #endif - + assert(solution.selected.size() == this->selected_nb); solution.invalidate(); } @@ -110,10 +111,7 @@ class moBinaryPartitionSwapNeighbor : assert(is_set); solution.reject(this->select); solution.select(this->reject); - #ifndef NDEBUG - assert(solution.selected.size() == this->selected_nb); - #endif - + assert(solution.selected.size() == this->selected_nb); solution.invalidate(); } @@ -128,6 +126,15 @@ class moBinaryPartitionSwapNeighbor : #ifndef NDEBUG is_set = true; #endif + this->invalidate(); + } + + /** Set the considered atoms. + * + * @param in_out A pair of {selected,rejected} atoms. + */ + void set(std::pair in_out) { + this->set(in_out.first, in_out.second); } /** Get the considered atom. @@ -157,7 +164,9 @@ class moBinaryPartitionSwapNeighbor : /** Fancy print. */ virtual void printOn(std::ostream& out) const override { assert(is_set); - out << selected_nb + EO::printOn(out); // Fitness. + out << " " + << selected_nb << " -" << reject << " +" << select; } @@ -165,12 +174,22 @@ class moBinaryPartitionSwapNeighbor : void size(size_t _selected_nb) { assert(_selected_nb > 0); this->selected_nb = _selected_nb; + this->invalidate(); } size_t size() const { return this->selected_nb; } + void fitness(const Fitness& fit) { + CLUTCHLOG(debug, "Fitness assignment -- neighbor: " << *this << " gets fitness: " << fit); + EO::fitness(fit); + } + + Fitness fitness() const { + return EO::fitness(); + } + #ifndef NDEBUG public: #else diff --git a/mo/src/problems/partition/moBinaryPartitionSwapNeighborhood.h b/mo/src/problems/partition/moBinaryPartitionSwapNeighborhood.h index cf4845478..50702be9a 100644 --- a/mo/src/problems/partition/moBinaryPartitionSwapNeighborhood.h +++ b/mo/src/problems/partition/moBinaryPartitionSwapNeighborhood.h @@ -55,16 +55,20 @@ class moBinaryPartitionSwapNeighborhood : public moNeighborhood