add py module
This commit is contained in:
+18
-11
@@ -9,10 +9,10 @@ namespace autoopt {
|
||||
template <typename T>
|
||||
struct btls_parameters {
|
||||
T step_decrease = T{0.5};
|
||||
T step_increase = T{1.5};
|
||||
T step_increase = T{1.2};
|
||||
T sufficient_decrease = T{1e-2};
|
||||
T tolerance = T{1e-9};
|
||||
size_t max_iters = 1000;
|
||||
T tolerance = T{1e-10};
|
||||
size_t max_iters = 2000;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
@@ -24,20 +24,27 @@ void btls(optimization_problem<T>& problem,
|
||||
for (size_t iter = 0; iter < params.max_iters; ++iter) {
|
||||
T obj_value = problem.objective(x);
|
||||
|
||||
std::cout << "Iter " << iter << ": obj = " << obj_value
|
||||
<< ", x = " << x.transpose() << ", step_size = " << step_size
|
||||
<< std::endl;
|
||||
Eigen::VectorX<T> grad = problem.gradient(x);
|
||||
|
||||
Eigen::VectorX<T> grad = -problem.gradient(x);
|
||||
Eigen::MatrixX<T> hess = problem.hessian(x);
|
||||
Eigen::VectorX<T> step_dir = -hess.ldlt().solve(grad).normalized();
|
||||
|
||||
Eigen::VectorX<T> step_dir = grad.normalized();
|
||||
while (problem.objective(x + step_size * step_dir) >
|
||||
obj_value +
|
||||
params.sufficient_decrease * step_size * grad.dot(step_dir)) {
|
||||
// Eigen::VectorX<T> step_dir = -grad.normalized();
|
||||
|
||||
auto decrease_condition = [&] {
|
||||
T new_obj = problem.objective(x + step_size * step_dir);
|
||||
return std::isnan(new_obj) ||
|
||||
(new_obj > obj_value - std::abs(params.sufficient_decrease *
|
||||
step_size * grad.dot(step_dir)));
|
||||
};
|
||||
|
||||
while (decrease_condition()) {
|
||||
step_size *= params.step_decrease;
|
||||
}
|
||||
x += step_size * step_dir;
|
||||
|
||||
step_size = step_size * params.step_increase;
|
||||
|
||||
if (step_size < params.tolerance) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <eigen3/Eigen/Eigen>
|
||||
|
||||
namespace autoopt {
|
||||
|
||||
Eigen::VectorX<double> fit_ellipse(
|
||||
const std::vector<std::pair<double, double>>& data,
|
||||
const Eigen::VectorX<double>& inital_params,
|
||||
const Eigen::VectorX<double>& delta);
|
||||
} // namespace autoopt
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <autoopt/derivative.hpp>
|
||||
#include <cmath>
|
||||
|
||||
namespace autoopt {
|
||||
|
||||
@@ -34,6 +34,14 @@ struct quadric {
|
||||
return quadric(A_new, B_new, C_new, D_new, E_new, F_new);
|
||||
}
|
||||
|
||||
constexpr quadric translated_by(T x_o, T y_o) const {
|
||||
T D_new = _D - T{2} * _A * x_o - _B * y_o;
|
||||
T E_new = _E - _B * x_o - T{2} * _C * y_o;
|
||||
T F_new = _F + _A * x_o * x_o + _B * x_o * y_o + _C * y_o * y_o - _D * x_o -
|
||||
_E * y_o;
|
||||
return quadric(_A, _B, _C, D_new, E_new, F_new);
|
||||
}
|
||||
|
||||
constexpr T at(T x) const {
|
||||
T sign = T{-1};
|
||||
T Bx_E = _B * x + _E;
|
||||
@@ -50,10 +58,12 @@ struct quadric {
|
||||
}
|
||||
|
||||
constexpr T slope_at(T x) const {
|
||||
return derivative([&]<typename U>(U x_val) {
|
||||
quadric<U> q{U{_A}, U{_B}, U{_C}, U{_D}, U{_E}, U{_F}};
|
||||
return q.at(x_val);
|
||||
}, x);
|
||||
return derivative(
|
||||
[&]<typename U>(U x_val) {
|
||||
quadric<U> q{U{_A}, U{_B}, U{_C}, U{_D}, U{_E}, U{_F}};
|
||||
return q.at(x_val);
|
||||
},
|
||||
x);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user