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

Brief description of class still missing. More...

#include <GridSnoop.h>

List of all members.

Public Member Functions

uint checksum () const
 GridSnoop ()
bool openReport (const QString &fileName)
bool optimization ()
void setForward (AbstractForward *forward)
void setSeed (int seed)
bool setThreadCount (int nThreads)
void sleep ()
int variableParameterCount () const
void wake ()
 ~GridSnoop ()

Detailed Description

Brief description of class still missing.

Full description of class still missing


Constructor & Destructor Documentation

Description of constructor still missing

References TRACE.

{
  TRACE;
  _bestMisfit=1e99;
}

Description of destructor still missing

References TRACE.

{
  TRACE;
  delete _report;
  delete _generator;
}

Member Function Documentation

uint DinverCore::GridSnoop::checksum ( ) const [inline]

Referenced by DinverCore::BatchRun::snoopOptimization().

{return _parameterSpaceChecksum;}
bool DinverCore::GridSnoop::openReport ( const QString &  fileName)

Initialize report. Can be called at any time.

References QGpCoreTools::endl(), DinverCore::ReportWriter::open(), and QGpCoreTools::tr().

Referenced by DinverCore::BatchRun::snoopOptimization().

{
  if( !fileName.isEmpty()) {
    delete _report;
    _report=new ReportWriter(fileName);
    if( !_report->open()) {
      App::stream() << tr("Cannot open report %1 for writing").arg(fileName) << endl;
      delete _report;
      _report=0;
      return false;
    }
  }
  return true;
}

References DinverCore::ReportWriter::addModel(), QGpCoreTools::endl(), DinverCore::Parameter::gridValue(), DinverCore::RealSpace::isOk(), DinverCore::AbstractForward::misfit(), DinverCore::AbstractForward::parameterSpace(), DinverCore::Parameter::setGridValue(), QGpCoreTools::tr(), TRACE, DinverCore::AbstractForward::valueChanged(), DinverCore::RealSpace::variableParameter(), DinverCore::RealSpace::variableParameterCount(), and DinverCore::AbstractForward::writeReport().

Referenced by DinverCore::BatchRun::snoopOptimization().

{
  TRACE;

  AbstractForward * refForward=_forwards.first();

  RealSpace& parameterSpace=refForward->parameterSpace();
  int ndVar=parameterSpace.variableParameterCount();
  ASSERT(parameterSpace.isOk());
  // Perturbations
  bool ok=true;
  while(true) {
    double m;
    int bestAxis=-1, bestDirection=0;
    for(int ip=0; ip<ndVar; ip++) {
      Parameter * p=parameterSpace.variableParameter(ip);
      int currentValue=p->gridValue();
      App::stream() << tr("%1: %2 (%3)").arg(ip).arg(currentValue).arg(_bestMisfit) << endl;
      // +1
      p->setGridValue(currentValue+1);
      // Some parametrizations may need some updates before proceeding
      refForward->valueChanged(p);
      if(parameterSpace.isOk()) {
        m=refForward->misfit(ok);
        App::stream() << tr("+1 param ok, misfit %1").arg(m) << endl;
        if(ok) {
          if(m<_bestMisfit) {
            _bestMisfit=m;
            bestAxis=ip;
            bestDirection=1;
          }
        } else {
          ok=true;
        }
      }
      // -1
      p->setGridValue(currentValue-1);
      // Some parametrizations may need some updates before proceeding
      refForward->valueChanged(p);
      if(parameterSpace.isOk()) {
        m=refForward->misfit(ok);
        App::stream() << tr("-1 param ok, misfit %1").arg(m) << endl;
        if(ok) {
          if(m<_bestMisfit) {
            _bestMisfit=m;
            bestAxis=ip;
            bestDirection=-1;
          }
        } else {
          ok=true;
        }
      }
      // Reset to original point
      p->setGridValue(currentValue);
      // Some parametrizations may need some updates before proceeding
      refForward->valueChanged(p);
    }
    // Effectively move to a new better position
    if(bestDirection!=0) {
      int currentValue=parameterSpace.variableParameter(bestAxis)->gridValue();
      Parameter * p=parameterSpace.variableParameter(bestAxis);
      p->setGridValue(currentValue+bestDirection);
      // Some parametrizations may need some updates before proceeding
      refForward->valueChanged(p);
      refForward->misfit(ok);
      _report->addModel(_bestMisfit, _parameterSpaceChecksum, parameterSpace);
      refForward->writeReport(_report);
    } else {
      break;
    }
  }
  return true;
}

Set the forward object in charge of forward computation. Can be called only once.

References TRACE.

Referenced by DinverCore::BatchRun::snoopOptimization().

{
  TRACE;
  ASSERT(_forwards.count()==0);
  // There is always at least one thread, create it
  _forwards.append(forward);
}
void DinverCore::GridSnoop::setSeed ( int  seed)

If seed is null, a random number is calculated from current time.

References QGpCoreTools::endl(), and QGpCoreTools::tr().

Referenced by DinverCore::BatchRun::snoopOptimization().

{
  if(_forwards.isEmpty() || _forwards.first()->isSleeping()) {
    delete _generator;
    _generator=new UniqueRandom(seed);
    if( !_forwards.isEmpty()) _forwards.first()->setGenerator(_generator);
  } else {
    App::stream() << tr("Cannot change the random seed of a running inversion, first terminate it.") << endl;
  }
}
bool DinverCore::GridSnoop::setThreadCount ( int  nThreads)

If there is no model currently in list, it generates a random model without checking conditions between parameters limits. In a second step, the model is adjusted until matching all conditions of the parameter space.

References QGpCoreTools::endl(), QGpCoreTools::tr(), and TRACE.

Referenced by DinverCore::BatchRun::snoopOptimization().

{
  TRACE;
  //QTime chrono;
  //chrono.start();
  ASSERT(_forwards.count()==1);
  AbstractForward * refForward=_forwards.first();
  if(nThreads>refForward->maximumThreadCount()) nThreads=refForward->maximumThreadCount();
  if(nThreads<1) nThreads=1;

  // Get the checksum of the parameterization
  _parameterSpaceChecksum=refForward->parameterSpace().checksum();

  refForward->setFinishSignal(&_forwardSignal);
  refForward->setGenerator(_generator);

  if( ! refForward->firstModel()) {
    App::stream() << tr("Error generating first model") << endl;
    return false;
  }
  bool ok;
  _bestMisfit=refForward->misfit(ok);

  //_timeTotal+=chrono.elapsed();
  return true;
}

References DinverCore::AbstractForward::sleep(), and TRACE.

Referenced by DinverCore::BatchRun::snoopOptimization().

{
  TRACE;
  // Remove all but one forward thread
  while(_forwards.count()>1) {
    AbstractForward * f=_forwards.last();
    f->sleep();
    delete f;
    _forwards.pop_back();
  }
  _forwards.first()->sleep();
}

Referenced by DinverCore::BatchRun::snoopOptimization().

{
  return _forwards.first()->parameterSpace().variableParameterCount();
}

Activate this first forward thread. This is used as a semaphore to indicate if the inversion is currently running or not.

References TRACE.

Referenced by DinverCore::BatchRun::snoopOptimization().

{
  TRACE;
  ASSERT(_forwards.count()==1);
  _forwards.first()->wake();

  //_timeAdd=0;
  //_timeMainWait=0;
  //_timeTotal=0;
}

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