使用 Gnu 科学库进行多维最小化

发布于 2024-11-01 20:02:44 字数 1604 浏览 0 评论 0原文

因此,我对 GSL 中的多维最小化程序有疑问(我尝试使用的程序是gsl_multimin_fdfminimizer_vector_bfgs2,但其他程序也产生相同的问题)。我已经定义了目标函数及其梯度,并且可以开始迭代,但算法始终保持在同一点。

现在,我认为我的函数或梯度中的某个地方会出现数学错误。然而,对于一组特定的数据,我知道最佳值,并且插入这些值会产生到处都是 0 的梯度。

如果我以最佳值开始迭代,算法会正确停止并报告成功,但如果我仅最小程度地干扰起始向量,则迭代将永远运行并且永远不会找到最佳值。

我还在 R 中重新实现了函数和梯度。我检查了 C 版本和 R 版本对于相同的输入会产生相同的输出,它们确实如此。使用 R 中的 BFGS 算法,只需几次迭代即可找到最优值。

我初始化算法

 const gsl_multimin_fdfminimizer_type *T;
 gsl_multimin_fdfminimizer *s;

 gsl_multimin_function_fdf lindsey_func;
 const gsl_vector * p[] = {x, y};

 lindsey_func.n = J;
 lindsey_func.f = lindsey_loglik;
 lindsey_func.df = lindsey_gradient;
 lindsey_func.fdf = lindsey_gf;
 lindsey_func.params = p;

 T = gsl_multimin_fdfminimizer_vector_bfgs2;
 s = gsl_multimin_fdfminimizer_alloc(T, J);     

 gsl_multimin_fdfminimizer_set(s, &lindsey_func, beta0, .001, 1e-4);

,迭代如下所示:

 do
 {
  iter++;
  status = gsl_multimin_fdfminimizer_iterate(s);
  printf("%d\n", status);
  if (status)
       break;

  status = gsl_multimin_test_gradient(s->gradient, 1e-3);

  if (status == GSL_SUCCESS)
  {
     printf("Minimum found at: ");
     printf("[");
     for (j = 0; j < J; j++)
     {
      printf("%.5f, ", gsl_vector_get(s->x, j));
     }
     printf("]\n");
  }
 } while (status == GSL_CONTINUE && iter < iter_max);

我可以调整的唯一参数是初始步长和 gsl_multimin_fdfminimizer_set (gsl_multimin_fdfminimizer * s, gsl_multimin_function_fdf * fdf, const gsl_vector * x,双step_size,双tol)。在那里尝试不同的值会产生很小的影响(有时算法会移动一点),但我找不到任何实际使算法移动的值。

感谢您的任何建议!

So I have a problem with the multidimensional minimization procedures in the GSL (the one I'm trying to use is gsl_multimin_fdfminimizer_vector_bfgs2, but the others produce the same problem). I have defined my target function and its gradient and I can start the iteration, but the algorithm always stays at the same point.

Now, I thought I would have a math error in my function or gradient somewhere. However, for a specific set of data I know the optimum and plugging in those values produces a gradient that is 0 everywhere.

If I start the iteration at the optimum, the algorithm correctly stops and reports success, but if I disturb the starting vector only minimally, the iteration runs forever and never finds the optimum.

I also reimplemented the function and gradient in R. I checked that the C version and the R version produce the same output for the same input, which they do. Using the BFGS algorithm in R, the optimum is found in just a few iterations.

I initialize the algorithm with

 const gsl_multimin_fdfminimizer_type *T;
 gsl_multimin_fdfminimizer *s;

 gsl_multimin_function_fdf lindsey_func;
 const gsl_vector * p[] = {x, y};

 lindsey_func.n = J;
 lindsey_func.f = lindsey_loglik;
 lindsey_func.df = lindsey_gradient;
 lindsey_func.fdf = lindsey_gf;
 lindsey_func.params = p;

 T = gsl_multimin_fdfminimizer_vector_bfgs2;
 s = gsl_multimin_fdfminimizer_alloc(T, J);     

 gsl_multimin_fdfminimizer_set(s, &lindsey_func, beta0, .001, 1e-4);

and the iteration looks like this:

 do
 {
  iter++;
  status = gsl_multimin_fdfminimizer_iterate(s);
  printf("%d\n", status);
  if (status)
       break;

  status = gsl_multimin_test_gradient(s->gradient, 1e-3);

  if (status == GSL_SUCCESS)
  {
     printf("Minimum found at: ");
     printf("[");
     for (j = 0; j < J; j++)
     {
      printf("%.5f, ", gsl_vector_get(s->x, j));
     }
     printf("]\n");
  }
 } while (status == GSL_CONTINUE && iter < iter_max);

The only parameter that I can tweak are the initial step size and the accuracy of the line minimization in gsl_multimin_fdfminimizer_set (gsl_multimin_fdfminimizer * s, gsl_multimin_function_fdf * fdf, const gsl_vector * x, double step_size, double tol). Trying different values there has a small effect (sometimes the algorithm moves a little bit), but I can't find any values that actually make the algorithm move around.

Thanks for any suggestions!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文