refactor while hunting a bug

This commit is contained in:
Johann Dreo 2023-01-26 11:48:44 +01:00 committed by nojhan
commit 237426a6b4
3 changed files with 62 additions and 25 deletions

View file

@ -166,4 +166,13 @@ class moBinaryPartition : public EO<FitT>
{ {
return "moBinaryPartition"; return "moBinaryPartition";
} }
void fitness(const FitT& fit) {
CLUTCHLOG(debug, "Fitness assignment -- solution: " << *this << " gets fitness: " << fit);
EO<FitT>::fitness(fit);
}
FitT fitness() const {
return EO<FitT>::fitness();
}
}; };

View file

@ -65,7 +65,9 @@ class moBinaryPartitionSwapNeighbor :
/** Copy constructor. /** Copy constructor.
*/ */
moBinaryPartitionSwapNeighbor( const moBinaryPartitionSwapNeighbor<EOT>& other) : moBinaryPartitionSwapNeighbor( const moBinaryPartitionSwapNeighbor<EOT>& other) :
selected_nb(other.selected_nb ) selected_nb(other.selected_nb),
select(other.select),
reject(other.reject)
#ifndef NDEBUG #ifndef NDEBUG
, is_set(other.is_set) , is_set(other.is_set)
#endif #endif
@ -78,11 +80,13 @@ class moBinaryPartitionSwapNeighbor :
moBinaryPartitionSwapNeighbor<EOT>& operator=( moBinaryPartitionSwapNeighbor<EOT>& operator=(
const moBinaryPartitionSwapNeighbor<EOT>& other) const moBinaryPartitionSwapNeighbor<EOT>& other)
{ {
this->fitness(other.fitness());
this->selected_nb = other.selected_nb; this->selected_nb = other.selected_nb;
this->select = other.select;
this->reject = other.reject;
#ifndef NDEBUG #ifndef NDEBUG
this->is_set = other.is_set; this->is_set = other.is_set;
#endif #endif
this->fitness(other.fitness());
return *this; return *this;
} }
@ -95,10 +99,7 @@ class moBinaryPartitionSwapNeighbor :
// Swap the two atoms. // Swap the two atoms.
solution.reject(this->reject); solution.reject(this->reject);
solution.select(this->select); solution.select(this->select);
#ifndef NDEBUG assert(solution.selected.size() == this->selected_nb);
assert(solution.selected.size() == this->selected_nb);
#endif
solution.invalidate(); solution.invalidate();
} }
@ -110,10 +111,7 @@ class moBinaryPartitionSwapNeighbor :
assert(is_set); assert(is_set);
solution.reject(this->select); solution.reject(this->select);
solution.select(this->reject); solution.select(this->reject);
#ifndef NDEBUG assert(solution.selected.size() == this->selected_nb);
assert(solution.selected.size() == this->selected_nb);
#endif
solution.invalidate(); solution.invalidate();
} }
@ -128,6 +126,15 @@ class moBinaryPartitionSwapNeighbor :
#ifndef NDEBUG #ifndef NDEBUG
is_set = true; is_set = true;
#endif #endif
this->invalidate();
}
/** Set the considered atoms.
*
* @param in_out A pair of {selected,rejected} atoms.
*/
void set(std::pair<AtomType,AtomType> in_out) {
this->set(in_out.first, in_out.second);
} }
/** Get the considered atom. /** Get the considered atom.
@ -157,7 +164,9 @@ class moBinaryPartitionSwapNeighbor :
/** Fancy print. */ /** Fancy print. */
virtual void printOn(std::ostream& out) const override { virtual void printOn(std::ostream& out) const override {
assert(is_set); assert(is_set);
out << selected_nb EO<Fitness>::printOn(out); // Fitness.
out << " "
<< selected_nb
<< " -" << reject << " -" << reject
<< " +" << select; << " +" << select;
} }
@ -165,12 +174,22 @@ class moBinaryPartitionSwapNeighbor :
void size(size_t _selected_nb) { void size(size_t _selected_nb) {
assert(_selected_nb > 0); assert(_selected_nb > 0);
this->selected_nb = _selected_nb; this->selected_nb = _selected_nb;
this->invalidate();
} }
size_t size() const { size_t size() const {
return this->selected_nb; return this->selected_nb;
} }
void fitness(const Fitness& fit) {
CLUTCHLOG(debug, "Fitness assignment -- neighbor: " << *this << " gets fitness: " << fit);
EO<Fitness>::fitness(fit);
}
Fitness fitness() const {
return EO<Fitness>::fitness();
}
#ifndef NDEBUG #ifndef NDEBUG
public: public:
#else #else

View file

@ -55,16 +55,20 @@ class moBinaryPartitionSwapNeighborhood : public moNeighborhood<moBinaryPartitio
i_select = 0; i_select = 0;
j_reject = 0; j_reject = 0;
// std::clog << "Init neighborhood:" std::clog << "Init neighborhood:"
// << " -" << rejected(from, j_reject) << " outer:" << j_reject+1 << "/" << from.selected.size() << ","
// << " +" << selected(from, i_select) << " inner:" << i_select+1 << "/" << from.rejected.size() << " ="
// << std::endl; << " -" << rejected(from, j_reject)
<< " +" << selected(from, i_select)
<< " from: " << from
<< std::endl;
// First item in both lists. // First item in both lists.
AtomType in = selected(from, i_select); AtomType in = selected(from, i_select);
AtomType out = rejected(from, j_reject); AtomType out = rejected(from, j_reject);
to.set(in, out); to.set(in, out);
to.size(from.selected.size()); to.size(from.selected.size());
to.invalidate();
} }
/** Point to the next neighbor. */ /** Point to the next neighbor. */
@ -77,10 +81,11 @@ class moBinaryPartitionSwapNeighborhood : public moNeighborhood<moBinaryPartitio
i_select++; // Next inner loop. i_select++; // Next inner loop.
} }
// std::clog << "Next in neighborhood:" std::clog << "Next in neighborhood:"
// << " -" << rejected(from, j_reject) << " -" << rejected(from, j_reject)
// << " +" << selected(from, i_select) << " +" << selected(from, i_select)
// << std::endl; << " from: " << from
<< std::endl;
assert( from.rejected.contains(selected(from,i_select)) ); assert( from.rejected.contains(selected(from,i_select)) );
assert( from.selected.contains(rejected(from,j_reject)) ); assert( from.selected.contains(rejected(from,j_reject)) );
@ -92,26 +97,30 @@ class moBinaryPartitionSwapNeighborhood : public moNeighborhood<moBinaryPartitio
rejected(from, j_reject) rejected(from, j_reject)
); );
to.size(from.selected.size()); to.size(from.selected.size());
to.invalidate();
} }
/** Returns true if there is more neighbors to be enumerated. */ /** Returns true if there is more neighbors to be enumerated. */
virtual bool cont(EOT& from) override { virtual bool cont(EOT& from) override {
// std::clog << "cont neighborhood?" std::clog << "cont neighborhood?"
// << " " << j_reject << "(-" << rejected(from, j_reject) << ")/" << from.selected.size() << " outer:" << j_reject+1 << "/" << from.selected.size() << ","
// << " " << i_select << "(-" << selected(from, i_select) << ")/" << from.rejected.size() << " inner:" << i_select+1 << "/" << from.rejected.size()// << " ="
// << std::endl; // << " -" << rejected(from, j_reject)
// << " +" << selected(from, i_select)
<< " from: " << from
<< std::endl;
// If reached the last item of the outer loop. // If reached the last item of the outer loop.
if( i_select == from.rejected.size()-1 if( i_select == from.rejected.size()-1
and j_reject == from.selected.size()-1) { and j_reject == from.selected.size()-1) {
// We should also have reached the end of the inner loop, // We should also have reached the end of the inner loop,
// and have set the inner loop to zero. // and have set the inner loop to zero.
// std::clog << "\tnope" << std::endl; std::clog << "\tnope" << std::endl;
return false; return false;
} else { // There is still some items in the outer loop. } else { // There is still some items in the outer loop.
// and thus also in the inner loop. // and thus also in the inner loop.
// std::clog << "\tyes" << std::endl; std::clog << "\tyes" << std::endl;
assert( j_reject < from.selected.size() ); assert( j_reject < from.selected.size() );
return true; return true;
} }