All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines
Public Member Functions
DinverCore::BestModels Class Reference

A sorted list (by increasing misfit) of indexes of best models. More...

#include <BestModels.h>

Inheritance diagram for DinverCore::BestModels:
QGpCoreTools::IncreaseStorage

List of all members.

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 ()

Detailed Description

A sorted list (by increasing misfit) of indexes of best models.

Full description of class still missing


Constructor & Destructor Documentation

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));
  }

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));
  }

Description of destructor still missing

References TRACE.

  {
    TRACE;
    free(_indexes);
  }

Member Function Documentation

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().

  {
    TRACE;
    int activeCount=_models->count();
    int * map=new int[activeCount];
    memset(map, -1, activeCount*sizeof(int));
    for(int i=count()-1; i>=0; i--) {
      map[_indexes[i]]=i;
    }
    return map;
  }
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

    }
  }

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]
const double* DinverCore::BestModels::misfit ( int  bestIndex) const [inline]

Referenced by add().

{return _models->misfit(_indexes[bestIndex]);}
const int* DinverCore::BestModels::model ( int  bestIndex) const [inline]
int DinverCore::BestModels::modelIndex ( int  bestIndex) const [inline]

References DinverCore::ActiveModels::print(), QGpCoreTools::IncreaseStorage::size(), and TRACE.

  {
    TRACE;
    printf("#\n#                 Best models\n");
    for(int i=0; i<size();i++) _models->print(_indexes[i]);
  }
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]
void DinverCore::BestModels::setIndex ( int  bestIndex,
int  modelIndex 
) [inline]
{_indexes[bestIndex]=modelIndex;}
void DinverCore::BestModels::setModels ( ActiveModels models) [inline]

References models.

Referenced by DinverCore::GeneratorModels::removeDeadModels().

{_models=models;}

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);
          }
        }
      }
    }
  }

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;
  }

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines