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): * 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>` * `<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. EO<FitT>::printOn(out); // Fitness.
// Trailing space already inserted. // 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): * 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>` * `<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. EO<FitT>::readFrom(in); // Fitness.
unsigned size; unsigned size;
@ -162,17 +162,25 @@ class moBinaryPartition : public EO<FitT>
and this->rejected == other.rejected; and this->rejected == other.rejected;
} }
virtual std::string className() const virtual std::string className() const override
{ {
return "moBinaryPartition"; return "moBinaryPartition";
} }
void fitness(const FitT& fit) { virtual void fitness(const FitT& fit) override
CLUTCHLOG(debug, "Fitness assignment -- solution: " << *this << " gets fitness: " << fit); {
// std::clog << "Fitness assignment -- solution: " << *this << " gets fitness: " << fit << std::endl;
EO<FitT>::fitness(fit); EO<FitT>::fitness(fit);
} }
FitT fitness() const { virtual const FitT& fitness() const override
{
return EO<FitT>::fitness(); 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> template<class EOT, class Fitness=typename EOT::Fitness>
class moBinaryPartitionSwapNeighbor : class moBinaryPartitionSwapNeighbor :
public moBackableNeighbor<EOT,double>//, public moBackableNeighbor<EOT,Fitness>//,
// public moIndexNeighbor<EOT,double> // FIXME see if we can model that. // public moIndexNeighbor<EOT,double> // FIXME see if we can model that.
{ {
public: public:
@ -100,6 +100,7 @@ class moBinaryPartitionSwapNeighbor :
solution.reject(this->reject); solution.reject(this->reject);
solution.select(this->select); solution.select(this->select);
assert(solution.selected.size() == this->selected_nb); assert(solution.selected.size() == this->selected_nb);
// this->fitness( Fitness(solution.fitness()) ); // For the cache.
solution.invalidate(); solution.invalidate();
} }
@ -112,6 +113,7 @@ class moBinaryPartitionSwapNeighbor :
solution.reject(this->select); solution.reject(this->select);
solution.select(this->reject); solution.select(this->reject);
assert(solution.selected.size() == this->selected_nb); assert(solution.selected.size() == this->selected_nb);
// this->fitness( Fitness(solution.fitness()) ); // For the cache.
solution.invalidate(); solution.invalidate();
} }
@ -153,7 +155,7 @@ class moBinaryPartitionSwapNeighbor :
} }
private: private:
// Disable access to `equals(moNeighbor<…>&)` (removes the related overloaded-virtual warning). // Disable access to `equals(moNeighbor<…>&)` (removes the related overloaded-virtual warning).
using moBackableNeighbor<EOT,double>::equals; using moBackableNeighbor<EOT,Fitness>::equals;
public: public:
@ -181,12 +183,14 @@ class moBinaryPartitionSwapNeighbor :
return this->selected_nb; return this->selected_nb;
} }
void fitness(const Fitness& fit) { virtual void fitness(const Fitness& fit) override
CLUTCHLOG(debug, "Fitness assignment -- neighbor: " << *this << " gets fitness: " << fit); {
// std::clog << "Fitness assignment -- neighbor: " << *this << " gets fitness: " << fit << std::endl;
EO<Fitness>::fitness(fit); EO<Fitness>::fitness(fit);
} }
Fitness fitness() const { virtual const Fitness& fitness() const override
{
return EO<Fitness>::fitness(); return EO<Fitness>::fitness();
} }

View file

@ -55,13 +55,13 @@ 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:"
<< " outer:" << j_reject+1 << "/" << from.selected.size() << "," // << " outer:" << j_reject+1 << "/" << from.selected.size() << ","
<< " inner:" << i_select+1 << "/" << from.rejected.size() << " =" // << " inner:" << i_select+1 << "/" << from.rejected.size() << " ="
<< " -" << rejected(from, j_reject) // << " -" << rejected(from, j_reject)
<< " +" << selected(from, i_select) // << " +" << selected(from, i_select)
<< " from: " << from // << " from: " << from
<< std::endl; // << std::endl;
// First item in both lists. // First item in both lists.
AtomType in = selected(from, i_select); AtomType in = selected(from, i_select);
@ -81,11 +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)
<< " from: " << from // << " from: " << from
<< std::endl; // << 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)) );
@ -102,25 +102,25 @@ class moBinaryPartitionSwapNeighborhood : public moNeighborhood<moBinaryPartitio
/** 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?"
<< " outer:" << j_reject+1 << "/" << from.selected.size() << "," // << " outer:" << j_reject+1 << "/" << from.selected.size() << ","
<< " inner:" << i_select+1 << "/" << from.rejected.size()// << " =" // << " inner:" << i_select+1 << "/" << from.rejected.size()// << " ="
// << " -" << rejected(from, j_reject) // // << " -" << rejected(from, j_reject)
// << " +" << selected(from, i_select) // // << " +" << selected(from, i_select)
<< " from: " << from // << " from: " << from
<< std::endl; // << 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;
} }