A sorted list (by increasing misfit) of indexes of best models. More...
#include <BestModels.h>
Public Member Functions | |
int * | activeModelMap () const |
void | add (int modelIndex) |
BestModels (ActiveModels *models) | |
BestModels (const BestModels &o) | |
void | clear () |
int | count () const |
const double * | misfit (int bestIndex) const |
const int * | model (int bestIndex) const |
int | modelIndex (int bestIndex) const |
void | print () |
void | remove (int modelIndex) |
void | reserve (int n) |
void | setIndex (int bestIndex, int modelIndex) |
void | setModels (ActiveModels *models) |
void | setNr (int nr) |
void | update () |
~BestModels () |
A sorted list (by increasing misfit) of indexes of best models.
Full description of class still missing
DinverCore::BestModels::BestModels | ( | ActiveModels * | models | ) |
Description of constructor still missing
References QGpCoreTools::IncreaseStorage::allocateVector(), models, and TRACE.
: IncreaseStorage(64) { TRACE; _models=models; _nrMax=50; _nr2=1; _indexes=(int *)allocateVector(sizeof(int)); }
DinverCore::BestModels::BestModels | ( | const BestModels & | o | ) |
References QGpCoreTools::IncreaseStorage::allocateVector(), QGpCoreTools::IncreaseStorage::size(), and TRACE.
: IncreaseStorage(o) { TRACE; _models=o._models; _nrMax=o._nrMax; _nr2=o._nr2; _indexes=(int *)allocateVector(sizeof(int)); memcpy(_indexes, o._indexes, size()*sizeof(int)); }
int * DinverCore::BestModels::activeModelMap | ( | ) | const |
Returns an index vector of size equal to active model count. Indexes are set to -1 if they do not correspond to a best model.
References DinverCore::ActiveModels::count(), count(), and TRACE.
Referenced by DinverCore::GeneratorModels::removeDeadModels(), and update().
void DinverCore::BestModels::add | ( | int | modelIndex | ) |
Checks if model with index modelIndex can be considered as a best model and if so add it to the current list.
Reimplemented from QGpCoreTools::IncreaseStorage.
References add(), misfit(), DinverCore::ActiveModels::misfit(), modelIndex(), QGpCoreTools::IncreaseStorage::size(), and DinverCore::ActiveModels::targetCount().
Referenced by add(), DinverCore::GeneratorModels::add(), DinverCore::Neighborhood::importModels(), DinverCore::GeneratorModels::removeDeadModels(), and update().
{ int i; if(_models->targetCount()==1) { // best models in the classical sense double modelMisfit=_models->misfit(modelIndex)[0]; if(size() < _nrMax) { if(size()==0) { _indexes[0]=modelIndex; add(); } else { i=indexOf(modelMisfit); if(exactIndexOf(modelIndex, modelMisfit, i-1)==-1) { if(i < size()) { memmove(( char * ) (_indexes + i + 1), (char * ) (_indexes + i), sizeof(int) * (size() - i) ); } _indexes[ i ]=modelIndex; add(); } } } else if(modelMisfit<misfit(size()-1)[0]) { i=indexOf(modelMisfit); if(exactIndexOf(modelIndex, modelMisfit, i-1)==-1) { memmove(( char * ) (_indexes + i + 1), (char * ) (_indexes + i), sizeof(int) * (size() - i) ); _indexes[i]=modelIndex; // Less than 5% difference between the very best one and the other best // Enlarge the best model ensemble. TODO: eventually reduce nr if the condition is not fulfilled /*if(misfit(0)[0]>0.95*misfit(size()-1)[0]) { add(); _nrMax=size(); }*/ } } else if(modelMisfit==misfit(size()-1)[0]) { /* Due to clipped misfit (increase the effective nr) Do some sort of Monte-Carlo inside the "good" misfit region */ _indexes[size()]=modelIndex; add(); _nrMax=size(); } } else { // best models contains the Pareto front } }
void DinverCore::BestModels::clear | ( | ) |
Reimplemented from QGpCoreTools::IncreaseStorage.
References QGpCoreTools::IncreaseStorage::allocateVector().
Referenced by DinverCore::GeneratorModels::clear(), and DinverCore::Neighborhood::clear().
{ free(_indexes); IncreaseStorage::clear(); _indexes=(int *)allocateVector(sizeof(int)); _nrMax=50; _nr2=1; }
int DinverCore::BestModels::count | ( | ) | const [inline] |
Referenced by activeModelMap(), DinverCore::Neighborhood::bestModelCount(), DinverCore::ModelRepository::bestModelCount(), DinverCore::ModelRepository::createModel(), DinverCore::Neighborhood::optimization(), DinverCore::GeneratorModels::setScaledModels(), DinverCore::Neighborhood::setThreadCount(), and DinverCore::ModelRepository::start().
{return size();}
const double* DinverCore::BestModels::misfit | ( | int | bestIndex | ) | const [inline] |
const int* DinverCore::BestModels::model | ( | int | bestIndex | ) | const [inline] |
Referenced by DinverCore::GeneratorModels::setScaledModels(), DinverCore::Neighborhood::setThreadCount(), and DinverCore::ModelRepository::start().
{return _models->model(_indexes[bestIndex]);}
int DinverCore::BestModels::modelIndex | ( | int | bestIndex | ) | const [inline] |
Referenced by add(), DinverCore::ModelRepository::createModel(), and DinverCore::Neighborhood::optimization().
{return _indexes[ bestIndex ];}
void DinverCore::BestModels::print | ( | ) |
References DinverCore::ActiveModels::print(), QGpCoreTools::IncreaseStorage::size(), and TRACE.
void DinverCore::BestModels::remove | ( | int | modelIndex | ) |
This function is called whenever a model listed in best models is generating a great number of bad models, exceding giveUp ratio or when a cell becomes atomic. This model must be removed from the best models if still present.
It uses the first misfit to quickly locate the modelIndex in the list of best models.
References QGpCoreTools::IncreaseStorage::downSize(), DinverCore::ActiveModels::misfit(), QGpCoreTools::IncreaseStorage::size(), and TRACE.
{ TRACE; // Check if index model is still listed double m= _models->misfit(modelIndex)[0]; int indexBest=exactIndexOf(modelIndex, m, indexOf(m)-1); if(indexBest!=-1) { // Effectively reduce the size by removing the indexBest item // _nr is always greater than 0, and indexBest<_nr memmove((char *) (_indexes+indexBest), (char *) (_indexes+indexBest+1), sizeof(int)*(size()-(indexBest+1))); downSize(size()-1); } }
void DinverCore::BestModels::reserve | ( | int | n | ) | [inline] |
Reimplemented from QGpCoreTools::IncreaseStorage.
Referenced by DinverCore::Neighborhood::optimization().
{IncreaseStorage::reserve(n);}
void DinverCore::BestModels::setIndex | ( | int | bestIndex, |
int | modelIndex | ||
) | [inline] |
{_indexes[bestIndex]=modelIndex;}
void DinverCore::BestModels::setModels | ( | ActiveModels * | models | ) | [inline] |
void DinverCore::BestModels::setNr | ( | int | nr | ) |
References QGpCoreTools::IncreaseStorage::downSize(), DinverCore::ActiveModels::misfit(), QGpCoreTools::IncreaseStorage::size(), TRACE, and update().
Referenced by DinverCore::GeneratorModels::setBestModelCount(), and DinverCore::Neighborhood::setNr().
{ TRACE; if(nr!=_nrMax && nr > 0) { if(nr > _nrMax) { _nrMax=nr; update(); } else { // When reducing the number of best models, make sure that the nr first best models // do not have the same misfit. If so leave best model count as it is. if(size()<=nr || _models->misfit(_indexes[ nr-1 ] )[0] > _models->misfit(_indexes[ 0 ] )[0] ) { _nrMax=nr; if(size() > _nrMax) { downSize(_nrMax); } } } } }
void DinverCore::BestModels::update | ( | ) |
If possible add models to the list of best model until reaching nr==nrMax.
References activeModelMap(), add(), DinverCore::ActiveModels::at(), DinverCore::ActiveModels::count(), DinverCore::ActiveModel::isQuarantined(), QGpCoreTools::IncreaseStorage::size(), and TRACE.
Referenced by DinverCore::Neighborhood::optimization(), and setNr().
{ TRACE; if(size()==_nrMax) return; int * map=activeModelMap(); // a quick way to check if model is already in current list for(int i=_models->count()-1; i>=0; i--) { if(map[i]==-1 && !_models->at(i).isQuarantined()) { add(i); } } delete map; }