line_search.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. old_energy = cur_energy;
  19. } else {
  20. old_energy = energy(x); // no energy was given -> need to compute the current energy
  21. }
  22. double new_energy = old_energy;
  23. int cur_iter = 0; int MAX_STEP_SIZE_ITER = 12;
  24. while (new_energy >= old_energy && cur_iter < MAX_STEP_SIZE_ITER) {
  25. Eigen::MatrixXd new_x = x + step_size * d;
  26. double cur_e = energy(new_x);
  27. if ( cur_e >= old_energy) {
  28. step_size /= 2;
  29. } else {
  30. x = new_x;
  31. new_energy = cur_e;
  32. }
  33. cur_iter++;
  34. }
  35. return new_energy;
  36. }
  37. #ifdef IGL_STATIC_LIBRARY
  38. #endif