line_search.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Michael Rabinovich
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "line_search.h"
  9. IGL_INLINE double igl::line_search(
  10. Eigen::MatrixXd& x,
  11. const Eigen::MatrixXd& d,
  12. double step_size,
  13. std::function<double(Eigen::MatrixXd&)> energy,
  14. double cur_energy)
  15. {
  16. double old_energy;
  17. if (cur_energy > 0)
  18. {
  19. old_energy = cur_energy;
  20. }
  21. else
  22. {
  23. old_energy = energy(x); // no energy was given -> need to compute the current energy
  24. }
  25. double new_energy = old_energy;
  26. int cur_iter = 0; int MAX_STEP_SIZE_ITER = 12;
  27. while (new_energy >= old_energy && cur_iter < MAX_STEP_SIZE_ITER)
  28. {
  29. Eigen::MatrixXd new_x = x + step_size * d;
  30. double cur_e = energy(new_x);
  31. if ( cur_e >= old_energy)
  32. {
  33. step_size /= 2;
  34. }
  35. else
  36. {
  37. x = new_x;
  38. new_energy = cur_e;
  39. }
  40. cur_iter++;
  41. }
  42. return new_energy;
  43. }
  44. #ifdef IGL_STATIC_LIBRARY
  45. #endif