|
4 | 4 | #pragma once |
5 | 5 |
|
6 | 6 | #include <tinyopt/optimizers/optimizer.h> |
| 7 | +#include <tinyopt/optimizers/options.h> |
| 8 | + |
| 9 | +#include <tinyopt/optimizers/optimizers.h> |
7 | 10 |
|
8 | 11 | namespace tinyopt { |
9 | 12 |
|
10 | 13 | /// Simplest interface to optimize `x` and minimize residuals (loss function). |
11 | 14 | /// Internally call the optimizer and run the optimization. |
12 | | -template <typename Optimizer, typename X_t, typename Res_t> |
13 | | -inline auto Optimize(X_t &x, const Res_t &func, const typename Optimizer::Options &options = {}) { |
14 | | - Optimizer optimizer(options); |
15 | | - return optimizer(x, func); |
| 15 | +template <typename X_t, typename Res_t> |
| 16 | +inline Output Optimize(X_t &x, const Res_t &func, const Options &options = {}) { |
| 17 | + // Detect Scalar, supporting at most one nesting level |
| 18 | + using Scalar = std::conditional_t< |
| 19 | + std::is_scalar_v<typename traits::params_trait<X_t>::Scalar>, |
| 20 | + typename traits::params_trait<X_t>::Scalar, |
| 21 | + typename traits::params_trait<typename traits::params_trait<X_t>::Scalar>::Scalar>; |
| 22 | + static_assert(std::is_scalar_v<Scalar>); |
| 23 | + constexpr Index Dims = traits::params_trait<X_t>::Dims; |
| 24 | + |
| 25 | + // Detect Hessian Type, if it's dense or sparse |
| 26 | + constexpr bool isDense = |
| 27 | + std::is_invocable_v<Res_t, const X_t &> || |
| 28 | + std::is_invocable_v<Res_t, const X_t &, Vector<Scalar, Dims> &> || |
| 29 | + std::is_invocable_v<Res_t, const X_t &, Vector<Scalar, Dims> &, Matrix<Scalar, Dims, Dims> &>; |
| 30 | + |
| 31 | + using Hessian_t = std::conditional_t<isDense, Matrix<Scalar, Dims, Dims>, SparseMatrix<Scalar>>; |
| 32 | + using Gradient_t = std::conditional_t<isDense, Vector<Scalar, Dims>, SparseMatrix<Scalar>>; |
| 33 | + |
| 34 | + constexpr bool secondOrderValid = |
| 35 | + !std::is_invocable_v<Res_t, const X_t &, Vector<Scalar, Dims> &>; |
| 36 | + |
| 37 | + // Check if this is an unconstrained first order problem |
| 38 | + constexpr bool firstOrderAllowed = !secondOrderValid; |
| 39 | + |
| 40 | + switch (options.solver_type) { |
| 41 | + // Second order methods |
| 42 | + case Options::Solver::GaussNewton: |
| 43 | + if constexpr (secondOrderValid) { |
| 44 | + gn::Optimizer<Hessian_t> optimizer(options); |
| 45 | + return optimizer(x, func); |
| 46 | + } |
| 47 | + case Options::Solver::LevenbergMarquardt: |
| 48 | + if constexpr (secondOrderValid) { |
| 49 | + lm::Optimizer<Hessian_t> optimizer(options); |
| 50 | + return optimizer(x, func); |
| 51 | + } |
| 52 | + // First order methods |
| 53 | + case Options::Solver::GradientDescent: |
| 54 | + if constexpr (firstOrderAllowed) { |
| 55 | + if constexpr (std::is_invocable_v<Res_t, const X_t &>) { |
| 56 | + const auto res = Res_t(x); |
| 57 | + if constexpr (traits::is_scalar_v<typename std::decay_t<decltype(res)>>) { |
| 58 | + gd::Optimizer<Gradient_t> optimizer(options); |
| 59 | + return optimizer(x, func); |
| 60 | + } else { |
| 61 | + throw std::invalid_argument("Error: cost function must return a scalar for Gradient Descent"); |
| 62 | + } |
| 63 | + } else { |
| 64 | + gd::Optimizer<Gradient_t> optimizer(options); |
| 65 | + return optimizer(x, func); |
| 66 | + } |
| 67 | + } |
| 68 | + default: |
| 69 | + throw std::invalid_argument("Error: Unknown solver type"); |
| 70 | + } |
16 | 71 | } |
17 | 72 |
|
18 | 73 | } // namespace tinyopt |
0 commit comments