Exemple #1
0
/// Objective function: calculates the objective value (ignore gradient calculation)
/// Keep track of how many times this function has been called, and report current
/// chi^2 (or other objective-function value) every 20 calls
/// Note that parameters n and grad are unused, but required by the NLopt interface.
double myfunc_nlopt_gen( unsigned n, const double *x, double *grad, void *my_func_data )
{
  ModelObject *theModel = (ModelObject *)my_func_data;
  // following is a necessary kludge bcs theModel->GetFitStatistic() won't accept 
  // const double*
  double  *params = (double *)x;
  double  fitStatistic;
  nlopt_result  junk;
  
  fitStatistic = theModel->GetFitStatistic(params);
  
  // feedback to user
  funcCallCount++;
  if (verboseOutput > 0) {
    if ((funcCallCount % FUNCS_PER_REPORTING_STEP) == 0) {
      printf("\tN-M simplex: function call %d: objective = %f\n", funcCallCount, fitStatistic);
      if ( (verboseOutput > 1) && ((funcCallCount % (REPORT_STEPS_PER_VERBOSE_OUTPUT*FUNCS_PER_REPORTING_STEP)) == 0) ) {
        PrintParametersSimple(theModel, params);
      }
    }
  }
  
  if (isnan(fitStatistic)) {
    fprintf(stderr, "\n*** NaN-valued fit statistic detected (N-M optimization)!\n");
    fprintf(stderr, "*** Terminating the fit...\n");
    junk = nlopt_force_stop(theOptimizer);
  }

  return(fitStatistic);
}
Exemple #2
0
double ImfitSolver::EnergyFunction( double *trial, bool &bAtSolution )
{
  double  fitStatistic;
  
  fitStatistic = theModel->GetFitStatistic(trial);

  return(fitStatistic);
}