5 #pragma GCC system_header
8 #include <unsupported/Eigen/Polynomials>
11 template <
typename _Scalar>
12 class PolynomialSolver<_Scalar, 2> :
public PolynomialSolverBase<_Scalar, 2> {
14 using PS_Base = PolynomialSolverBase<_Scalar, 2>;
15 EIGEN_POLYNOMIAL_SOLVER_BASE_INHERITED_TYPES(
PS_Base)
20 template <
typename OtherPolynomial>
23 compute(poly, hasRealRoot);
27 template <
typename OtherPolynomial>
29 compute(
const OtherPolynomial& poly,
bool& hasRealRoot)
31 constexpr Scalar ZERO(0);
32 Scalar a2(2 * poly[2]);
33 assert(ZERO != poly[poly.size() - 1]);
34 Scalar discriminant((poly[1] * poly[1]) - (4 * poly[0] * poly[2]));
35 if (ZERO < discriminant) {
36 Scalar discriminant_root(std::sqrt(discriminant));
37 m_roots[0] = (-poly[1] - discriminant_root) / (a2);
38 m_roots[1] = (-poly[1] + discriminant_root) / (a2);
42 if (ZERO == discriminant) {
44 m_roots[0] = -poly[1] / a2;
48 Scalar discriminant_root(std::sqrt(-discriminant));
49 m_roots[0] = RootType(-poly[1] / a2, -discriminant_root / a2);
50 m_roots[1] = RootType(-poly[1] / a2, discriminant_root / a2);
56 template <
typename OtherPolynomial>
61 compute(poly, hasRealRoot);
65 using PS_Base::m_roots;
79 template <
typename _Scalar,
int NX = Eigen::Dynamic>
83 using VectorType = Eigen::Matrix<Scalar, InputsAtCompileTime, 1>;
120 template <
typename FunctorType>
123 using Scalar =
typename FunctorType::Scalar;
126 BFGS(FunctorType& _functor) : pnorm(0), g0norm(0), iter(-1), functor(_functor) {}
175 operator=(
const BFGS&);
196 checkExtremum(
const Eigen::Matrix<Scalar, 4, 1>& coefficients,
231 FunctorType& functor;
234 template <
typename FunctorType>
241 Scalar y = Eigen::poly_eval(coefficients, x);
248 template <
typename FunctorType>
252 x_alpha = x0 + alpha * p;
256 template <
typename FunctorType>
260 return (g_alpha.dot(p));
263 template <
typename FunctorType>
267 if (alpha == f_cache_key)
270 f_alpha = functor(x_alpha);
275 template <
typename FunctorType>
279 if (alpha == df_cache_key)
282 if (alpha != g_cache_key) {
283 functor.df(x_alpha, g_alpha);
287 df_cache_key = alpha;
291 template <
typename FunctorType>
295 if (alpha == f_cache_key && alpha == df_cache_key) {
301 if (alpha == f_cache_key || alpha == df_cache_key) {
308 functor.fdf(x_alpha, f_alpha, g_alpha);
312 df_cache_key = alpha;
317 template <
typename FunctorType>
325 Scalar f_alpha, df_alpha;
326 applyFDF(alpha, f_alpha, df_alpha);
334 template <
typename FunctorType>
347 template <
typename FunctorType>
353 status = minimizeOneStep(x);
359 template <
typename FunctorType>
366 functor.fdf(x, f, gradient);
370 p = gradient * -1 / g0norm;
391 template <
typename FunctorType>
395 Scalar alpha = 0.0, alpha1;
397 if (pnorm == 0.0 || g0norm == 0.0 || fp0 == 0) {
404 std::max(-delta_f, 10 * std::numeric_limits<Scalar>::epsilon() * std::abs(f0));
405 alpha1 = std::min(1.0, 2.0 * del / (-fp0));
408 alpha1 = std::abs(parameters.step_size);
422 updatePosition(alpha, x, f, gradient);
433 Scalar dxg, dgg, dxdg, dgnorm, A,
B;
441 dxg = dx0.dot(gradient);
442 dgg = dg0.dot(gradient);
448 A = -(1.0 + dgnorm * dgnorm / dxdg) *
B + dgg / dxdg;
465 Scalar dir = ((p.dot(gradient)) > 0) ? -1.0 : 1.0;
474 template <
typename FunctorType>
478 return functor.checkGradient(gradient);
481 template <
typename FunctorType>
494 Scalar y, alpha, ymin, ymax, fmin;
496 ymin = (xmin - a) / (b - a);
497 ymax = (xmax - a) / (b - a);
506 if (order > 2 && !(fpb != fpa) && fpb != std::numeric_limits<Scalar>::infinity()) {
510 Scalar eta = 3 * (fb - fa) - 2 * fpa - fpb;
511 Scalar xi = fpa + fpb - 2 * (fb - fa);
512 Scalar c0 = fa, c1 = fpa, c2 = eta, c3 = xi;
514 Eigen::Matrix<Scalar, 4, 1> coefficients;
515 coefficients << c0, c1, c2, c3;
519 fmin = Eigen::poly_eval(coefficients, ymin);
520 checkExtremum(coefficients, ymax, y, fmin);
523 Eigen::Matrix<Scalar, 3, 1> coefficients2;
524 coefficients2 << c1, 2 * c2, 3 * c3;
526 Eigen::PolynomialSolver<Scalar, 2> solver(coefficients2, real_roots);
528 if ((solver.roots()).size() == 2)
530 y0 = std::real(solver.roots()[0]);
531 y1 = std::real(solver.roots()[1]);
537 if (y0 > ymin && y0 < ymax)
538 checkExtremum(coefficients, y0, y, fmin);
539 if (y1 > ymin && y1 < ymax)
540 checkExtremum(coefficients, y1, y, fmin);
542 else if ((solver.roots()).size() == 1)
544 y0 = std::real(solver.roots()[0]);
545 if (y0 > ymin && y0 < ymax)
546 checkExtremum(coefficients, y0, y, fmin);
553 Scalar fl = fa + ymin * (fpa + ymin * (fb - fa - fpa));
554 Scalar fh = fa + ymax * (fpa + ymax * (fb - fa - fpa));
555 Scalar c = 2 * (fb - fa - fpa);
567 if (z > ymin && z < ymax) {
568 Scalar f = fa + z * (fpa + z * (fb - fa - fpa));
577 alpha = a + y * (b - a);
581 template <
typename FunctorType>
592 Scalar f0, fp0, falpha, falpha_prev, fpalpha, fpalpha_prev, delta, alpha_next;
593 Scalar alpha = alpha1, alpha_prev = 0.0;
594 Scalar a, b, fa, fb, fpa, fpb;
597 applyFDF(0.0, f0, fp0);
612 while (i++ < parameters.bracket_iters) {
613 falpha = applyF(alpha);
615 if (falpha > f0 + alpha * rho * fp0 || falpha >= falpha_prev) {
621 fpb = std::numeric_limits<Scalar>::quiet_NaN();
625 fpalpha = applyDF(alpha);
628 if (std::abs(fpalpha) <= -sigma * fp0) {
643 delta = alpha - alpha_prev;
646 Scalar lower = alpha + delta;
647 Scalar upper = alpha + tau1 * delta;
649 alpha_next = interpolate(alpha_prev,
661 falpha_prev = falpha;
662 fpalpha_prev = fpalpha;
666 while (i++ < parameters.section_iters) {
670 Scalar lower = a + tau2 * delta;
671 Scalar upper = b - tau3 * delta;
673 alpha = interpolate(a, fa, fpa, b, fb, fpb, lower, upper, order);
675 falpha = applyF(alpha);
676 if ((a - alpha) * fpa <= std::numeric_limits<Scalar>::epsilon()) {
681 if (falpha > f0 + rho * alpha * fp0 || falpha >= fa) {
685 fpb = std::numeric_limits<Scalar>::quiet_NaN();
688 fpalpha = applyDF(alpha);
690 if (std::abs(fpalpha) <= -sigma * fp0) {
695 if (((b - a) >= 0 && fpalpha >= 0) || ((b - a) <= 0 && fpalpha <= 0)) {
BFGS stands for Broyden–Fletcher–Goldfarb–Shanno (BFGS) method for solving unconstrained nonlinear op...
BFGSSpace::Status minimize(FVectorType &x)
BFGSSpace::Status testGradient()
typename FunctorType::Scalar Scalar
BFGSSpace::Status minimizeInit(FVectorType &x)
BFGSSpace::Status minimizeOneStep(FVectorType &x)
typename FunctorType::VectorType FVectorType
void resetParameters(void)
BFGS(FunctorType &_functor)
virtual ~PolynomialSolver()=default
void compute(const OtherPolynomial &poly)
void compute(const OtherPolynomial &poly, bool &hasRealRoot)
Computes the complex roots of a new polynomial.
PolynomialSolverBase< _Scalar, 2 > PS_Base
PolynomialSolver(const OtherPolynomial &poly, bool &hasRealRoot)
@ NegativeGradientEpsilon
Defines all the PCL and non-PCL macros used.
virtual void fdf(const VectorType &x, Scalar &f, VectorType &df)=0
virtual ~BFGSDummyFunctor()=default
virtual BFGSSpace::Status checkGradient(const VectorType &)
BFGSDummyFunctor(int inputs)
virtual void df(const VectorType &x, VectorType &df)=0
Eigen::Matrix< Scalar, InputsAtCompileTime, 1 > VectorType
virtual double operator()(const VectorType &x)=0