EnigmaSong / nethist

R package for Network histogram
https://enigmasong.github.io/nethist/
Other
2 stars 1 forks source link

Checking stoping criteria #22

Closed EnigmaSong closed 1 year ago

EnigmaSong commented 1 year ago

Old code

if(mm%5==0){
      normalizedBestLL = bestLL*normalizeC;
      if(verbose) Rcout<< normalizedBestLL << " LL.  Iter " << mm<< " of max " << maxNumRestarts << "; "
                       << bestCount << " global improvements; \n";

      check_bestcount_improvecount(&bestCount, &consecZeroImprovements);
      update_tolCounter(normalizedBestLL, oldNormalizedBestLL, &tolCounter);
      oldNormalizedBestLL = normalizedBestLL;

      if(check_quit_greedy(tolCounter, consecZeroImprovements,
                           tol_ZeroImprovements, verbose)) break;
}

...

void check_bestcount_improvecount(int* bestCount, int* consecZeroImprovements){
  if(*bestCount == 0){
    *consecZeroImprovements = *consecZeroImprovements + 1;
  }else{
    *bestCount = 0;
    *consecZeroImprovements = 0;
  }
}
void update_tolCounter(const double &normalizedBestLL, const double &oldNormalizedBestLL, int *tolCounter){
  *tolCounter = (normalizedBestLL - oldNormalizedBestLL < absTol ? *tolCounter+1 : 0);
}

bool check_quit_greedy(const int &tolCounter, 
                       const int &consecZeroImprovements, 
                       const int &tol_ZeroImprovements, const bool &verbose){
  if(tolCounter >=3){
    if(verbose) Rcout<<"3 consecutive likelihood improvements less than specified tolerance; quitting now\n";
    return true;
  }
  if(consecZeroImprovements == tol_ZeroImprovements){
    if(verbose) Rcout<<"Local optimum likely reached in random-ordered greedy likelihood search; quitting now\n";
    return true;
  }

  return false;
}