<commit_before>// This is core/vnl/algo/vnl_lbfgs.cxx
#ifdef VCL_NEEDS_PRAGMA_INTERFACE
#pragma implementation
#endif
//:
// \file
//
// \author <NAME>, Oxford RRG
// \date   22 Aug 99
//
//-----------------------------------------------------------------------------

#include <cmath>
#include <iostream>
#include <iomanip>
#include "vnl_lbfgs.h"
#include <vcl_compiler.h>

#include <vnl/algo/vnl_netlib.h> // lbfgs_()

//: Default constructor.
// memory is set to 5, line_search_accuracy to 0.9.
// Calls init_parameters
vnl_lbfgs::vnl_lbfgs():
  f_(VXL_NULLPTR)
{
  init_parameters();
}

//: Constructor. f is the cost function to be minimized.
// Calls init_parameters
vnl_lbfgs::vnl_lbfgs(vnl_cost_function& f):
  f_(&f)
{
  init_parameters();
}

//: Called by constructors.
// Memory is set to 5, line_search_accuracy to 0.9, default_step_length to 1.
void vnl_lbfgs::init_parameters()
{
  memory = 5;
  line_search_accuracy = 0.9;
  default_step_length = 1.0;
}

bool vnl_lbfgs::minimize(vnl_vector<double>& x)
{
  // Local variables
  // The driver for vnl_lbfgs must always declare LB2 as EXTERNAL

  long n = f_->get_number_of_unknowns();
  long m = memory; // The number of basis vectors to remember.

  // Create an instance of the lbfgs global data to pass as an
  // argument.  It must persist through all calls in this
  // minimization.
  v3p_netlib_lbfgs_global_t lbfgs_global;
  v3p_netlib_lbfgs_init(&lbfgs_global);

  long iprint[2] = {1, 0};
  vnl_vector<double> g(n);

  // Workspace
  vnl_vector<double> diag(n);

  vnl_vector<double> w(n * (2*m+1)+2*m);

  if (verbose_)
    std::cerr << "vnl_lbfgs: n = "<< n <<", memory = "<< m <<", Workspace = "
             << w.size() << "[ "<< ( w.size() / 128.0 / 1024.0) <<" MB], ErrorScale = "
             << f_->reported_error(1) <<", xnorm = "<< x.magnitude() << std::endl;

  bool we_trace = (verbose_ && !trace);

  if (we_trace)
    std::cerr << "vnl_lbfgs: ";

  double best_f = 0;
  vnl_vector<double> best_x;

  bool ok;
  this->num_evaluations_ = 0;
  this->num_iterations_ = 0;
  long iflag = 0;
  while (true) {
    // We do not wish to provide the diagonal matrices Hk0, and therefore set DIAGCO to FALSE.
    v3p_netlib_logical diagco = false;

    // Set these every iter in case user changes them to bail out
    double eps = gtol; // Gradient tolerance
    double local_xtol = 1e-16;
    lbfgs_global.gtol = line_search_accuracy; // set to 0.1 for huge problems or cheap functions
    lbfgs_global.stpinit = default_step_length;<commit_msg>BUG: Fix LBFGS crash when minimum is starting point.<commit_after>// This is core/vnl/algo/vnl_lbfgs.cxx
#ifdef VCL_NEEDS_PRAGMA_INTERFACE
#pragma implementation
#endif
//:
// \file
//
// \author <NAME>, Oxford RRG
// \date   22 Aug 99
//
//-----------------------------------------------------------------------------

#include <cmath>
#include <iostream>
#include <iomanip>
#include "vnl_lbfgs.h"
#include <vcl_compiler.h>

#include <vnl/algo/vnl_netlib.h> // lbfgs_()

//: Default constructor.
// memory is set to 5, line_search_accuracy to 0.9.
// Calls init_parameters
vnl_lbfgs::vnl_lbfgs():
  f_(VXL_NULLPTR)
{
  init_parameters();
}

//: Constructor. f is the cost function to be minimized.
// Calls init_parameters
vnl_lbfgs::vnl_lbfgs(vnl_cost_function& f):
  f_(&f)
{
  init_parameters();
}

//: Called by constructors.
// Memory is set to 5, line_search_accuracy to 0.9, default_step_length to 1.
void vnl_lbfgs::init_parameters()
{
  memory = 5;
  line_search_accuracy = 0.9;
  default_step_length = 1.0;
}

bool vnl_lbfgs::minimize(vnl_vector<double>& x)
{
  // Local variables
  // The driver for vnl_lbfgs must always declare LB2 as EXTERNAL

  long n = f_->get_number_of_unknowns();
  long m = memory; // The number of basis vectors to remember.

  // Create an instance of the lbfgs global data to pass as an
  // argument.  It must persist through all calls in this
  // minimization.
  v3p_netlib_lbfgs_global_t lbfgs_global;
  v3p_netlib_lbfgs_init(&lbfgs_global);

  long iprint[2] = {1, 0};
  vnl_vector<double> g(n);

  // Workspace
  vnl_vector<double> diag(n);

  vnl_vector<double> w(n * (2*m+1)+2*m);

  if (verbose_)
    std::cerr << "vnl_lbfgs: n = "<< n <<", memory = "<< m <<", Workspace = "
             << w.size() << "[ "<< ( w.size() / 128.0 / 1024.0) <<" MB], ErrorScale = "
             << f_->reported_error(1) <<", xnorm = "<< x.magnitude() << std::endl;

  bool we_trace = (verbose_ && !trace);

  if (we_trace)
    std::cerr << "vnl_lbfgs: ";

  double best_f = 0;
  vnl_vector<double> best_x = x;

  bool ok;
  this->num_evaluations_ = 0;
  this->num_iterations_ = 0;
  long iflag = 0;
  while (true) {
    // We do not wish to provide the diagonal matrices Hk0, and therefore set DIAGCO to FALSE.
    v3p_netlib_logical diagco = false;

    // Set these every iter in case user changes them to bail out
    double eps = gtol; // Gradient tolerance
    double local_xtol = 1e-16;
    lbfgs_global.gtol = line_search_accuracy; // set to 0.1 for huge problems or cheap functions
    lbfgs_global.stpinit = default_step_length;
{'fraction_non_alphanumeric': 0.16087212728344136, 'fraction_numerical': 0.018071105873109408, 'mean_word_length': 2.965732087227414, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3519558', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1884, 'n_words': 494}