refactor(fitness): store cache with the fitness

to allow rollback with minimal mem footprint
This commit is contained in:
Johann Dreo 2023-02-02 14:48:02 +01:00 committed by nojhan
commit df723331be
3 changed files with 44 additions and 32 deletions

View file

@ -118,7 +118,7 @@ class moBinaryPartition : public EO<FitT>
* Output a string of the form (spaces replaced with period here, to show their count):
* `<fitness>..<nb_selected>..<sel_0><sel_n>...<nb_rejected>..<rej_0><rej_m>`
*/
virtual void printOn(std::ostream& out) const
virtual void printOn(std::ostream& out) const override
{
EO<FitT>::printOn(out); // Fitness.
// Trailing space already inserted.
@ -136,7 +136,7 @@ class moBinaryPartition : public EO<FitT>
* Expects a string of the form (spaces replaced with period here, to show their count):
* `<fitness>..<nb_selected>..<sel_0><sel_n>...<nb_rejected>..<rej_0><rej_m>`
*/
virtual void readFrom(std::istream& in)
virtual void readFrom(std::istream& in) override
{
EO<FitT>::readFrom(in); // Fitness.
unsigned size;
@ -162,17 +162,25 @@ class moBinaryPartition : public EO<FitT>
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<FitT>::fitness(fit);
}
FitT fitness() const {
virtual const FitT& fitness() const override
{
return EO<FitT>::fitness();
}
virtual void invalidate() override
{
// this->fitness().clear();
EO<FitT>::invalidate();
}
};

View file

@ -18,7 +18,7 @@
*/
template<class EOT, class Fitness=typename EOT::Fitness>
class moBinaryPartitionSwapNeighbor :
public moBackableNeighbor<EOT,double>//,
public moBackableNeighbor<EOT,Fitness>//,
// public moIndexNeighbor<EOT,double> // 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<EOT,double>::equals;
using moBackableNeighbor<EOT,Fitness>::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>::fitness(fit);
}
Fitness fitness() const {
virtual const Fitness& fitness() const override
{
return EO<Fitness>::fitness();
}

View file

@ -55,13 +55,13 @@ class moBinaryPartitionSwapNeighborhood : public moNeighborhood<moBinaryPartitio
i_select = 0;
j_reject = 0;
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;
// 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);
@ -81,11 +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)
<< " from: " << from
<< 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)) );
@ -102,25 +102,25 @@ class moBinaryPartitionSwapNeighborhood : public moNeighborhood<moBinaryPartitio
/** Returns true if there is more neighbors to be enumerated. */
virtual bool cont(EOT& from) override {
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;
// 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;
}