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";
}
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.
*/
moBinaryPartitionSwapNeighbor( const moBinaryPartitionSwapNeighbor<EOT>& 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<EOT>& operator=(
const moBinaryPartitionSwapNeighbor<EOT>& 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<AtomType,AtomType> 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<Fitness>::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>::fitness(fit);
}
Fitness fitness() const {
return EO<Fitness>::fitness();
}
#ifndef NDEBUG
public:
#else

View file

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