rework
This commit is contained in:
32 files changed
+75060
-630
No files matched your search
@@ -1,79 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <autoopt/ellipse.hpp>
|
||||
#include <autoopt/optimization_problem.hpp>
|
||||
#include <autoopt/util.hpp>
|
||||
#include <autoopt/btls.hpp>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
using namespace autoopt;
|
||||
|
||||
TEST(Ellipse, Slope) {
|
||||
ellipse<double> e{100, 1000, deg2rad(1.0)}; // entrance angle 1 degree
|
||||
quadric<double> q = e.to_quadric();
|
||||
|
||||
EXPECT_NEAR(q.slope_at(-10), -0.0010305116165301856, 1e-9);
|
||||
EXPECT_NEAR(q.slope_at(0), 0.0, 1e-9);
|
||||
EXPECT_NEAR(q.slope_at(10), 0.00090001261192696272, 1e-9);
|
||||
}
|
||||
|
||||
TEST(Ellipse, ParamGradient) {
|
||||
std::vector<std::pair<double, double>> data_points = {
|
||||
{-10.0, -0.00103}, {0.0, 0.0}, {10, 0.0009}};
|
||||
|
||||
Eigen::VectorX<double> params(4);
|
||||
params << 100, 1000, deg2rad(1.0), 0.0;
|
||||
|
||||
auto loss_func = [&data_points]<typename T>(const Eigen::VectorX<T>& p) {
|
||||
ellipse<T> e{T{p(0)}, T{p(1)}, T{p(2)}};
|
||||
quadric<T> q = e.to_quadric().rotated_by(T{p(3)});
|
||||
T loss = T{0};
|
||||
for (const auto& [x, y_true] : data_points) {
|
||||
T y_pred = q.slope_at(T{x});
|
||||
T error = y_pred - T{y_true};
|
||||
loss = loss + error * error;
|
||||
}
|
||||
return loss / T(data_points.size());
|
||||
};
|
||||
|
||||
auto_diff_optimization_problem<double, decltype(loss_func)> problem(loss_func, params);
|
||||
|
||||
auto grad = problem.gradient(params);
|
||||
|
||||
std::cout << grad << std::endl;
|
||||
|
||||
EXPECT_NEAR(grad(0), -3.54845114759293e-12, 1e-15); // d/d(left_arm)
|
||||
EXPECT_NEAR(grad(1), -3.0016523630530093e-14, 1e-15); // d/d(right_arm)
|
||||
EXPECT_NEAR(grad(2), 2.0569619167404501e-08, 1e-15); // d/d(entrance_angle)
|
||||
EXPECT_NEAR(grad(3), -3.3267028547673413e-07, 1e-15); // d/d(rotation_angle)
|
||||
|
||||
auto hess = problem.hessian(params);
|
||||
|
||||
std::cout << hess << std::endl;
|
||||
|
||||
Eigen::VectorX<double> params_delta(4);
|
||||
params_delta << 10.0, 10.0, deg2rad(0.1), deg2rad(0.1);
|
||||
|
||||
|
||||
// log barrier
|
||||
log_barrier_optimization_problem<double> log_barrier_problem(
|
||||
problem,
|
||||
params_delta,
|
||||
1e-5);
|
||||
|
||||
while (log_barrier_problem._barrier_strength > 1e-20) {
|
||||
btls(log_barrier_problem);
|
||||
log_barrier_problem._barrier_strength *= 1e-2;
|
||||
}
|
||||
|
||||
std::cout << "Optimum params:" << std::endl;
|
||||
std::cout << "left_arm: " << log_barrier_problem.x()(0) << std::endl;
|
||||
std::cout << "right_arm: " << log_barrier_problem.x()(1) << std::endl;
|
||||
std::cout << "entrance_angle: " << rad2deg(log_barrier_problem.x()(2)) << std::endl;
|
||||
std::cout << "rotation_angle: " << rad2deg(log_barrier_problem.x()(3)) << std::endl;
|
||||
std::cout << "Optimum objective:" << std::endl;
|
||||
std::cout << problem.objective(problem.x()) << std::endl;
|
||||
std::cout << "Optimum grad:" << std::endl;
|
||||
std::cout << problem.gradient(problem.x()) << std::endl;
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "autoopt/quadric.hpp"
|
||||
#include <iostream>
|
||||
|
||||
using namespace autoopt;
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <autoopt/reactive.hpp>
|
||||
|
||||
TEST(Reactive, Simple) {
|
||||
using namespace autoopt;
|
||||
|
||||
source<int> s1(0);
|
||||
source<int> s2(0);
|
||||
source<int> s3(0);
|
||||
transform<int, int, int> t1([](int x, int y) { return x + y; }, s1,s2);
|
||||
transform<int, int, int> t2([](int x, int y) { return x + y; }, s2,s3);
|
||||
|
||||
ASSERT_EQ(t2.get(), 0);
|
||||
ASSERT_TRUE(s1.set(1));
|
||||
ASSERT_EQ(t2.get(), 1);
|
||||
s2.set(2);
|
||||
ASSERT_EQ(t2.get(), 3);
|
||||
s3.set(3);
|
||||
ASSERT_EQ(t2.get(), 6);
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <autoopt/ellipse.hpp>
|
||||
#include <autoopt/optimization_problem.hpp>
|
||||
#include <autoopt/util.hpp>
|
||||
#include <autoopt/btls.hpp>
|
||||
#include <fstream>
|
||||
#include <unordered_map>
|
||||
#include <autoopt/interface.hpp>
|
||||
#include <iomanip>
|
||||
|
||||
|
||||
using namespace autoopt;
|
||||
|
||||
std::vector<std::pair<double, double>> read_data(const std::string& filename) {
|
||||
std::fstream file(filename, std::ios::in);
|
||||
|
||||
std::unordered_map<double, size_t> index_map;
|
||||
std::vector<double> x_values;
|
||||
std::vector<std::vector<double>> data_points;
|
||||
|
||||
std::string line;
|
||||
|
||||
size_t index = 0;
|
||||
|
||||
double x_avg = 0.0;
|
||||
|
||||
for(;std::getline(file, line);) {
|
||||
if (line.empty() || line[0] == '#') {
|
||||
continue;
|
||||
}
|
||||
std::istringstream iss(line);
|
||||
double x, y;
|
||||
iss >> x;
|
||||
for (size_t i = 1; i < 5; ++i) {
|
||||
iss >> y; // skip unused columns
|
||||
}
|
||||
|
||||
y = arcsec2rad(-y);
|
||||
|
||||
if (index_map.find(x) == index_map.end()) {
|
||||
index_map[x] = index++;
|
||||
x_values.push_back(x);
|
||||
data_points.emplace_back();
|
||||
data_points.back().push_back(y);
|
||||
x_avg += x;
|
||||
continue;
|
||||
}
|
||||
data_points[index_map[x]].push_back(y);
|
||||
}
|
||||
x_avg /= x_values.size();
|
||||
std::vector<std::pair<double, double>> result;
|
||||
for (size_t i = 40; i < x_values.size() - 40; ++i) {
|
||||
double x = x_values[i];
|
||||
double y_avg = 0.0;
|
||||
for (double y : data_points[i]) {
|
||||
y_avg += y;
|
||||
}
|
||||
y_avg /= data_points[i].size();
|
||||
result.emplace_back(x - x_avg, y_avg);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
TEST(Fit, EllipseFit) {
|
||||
auto data_points = read_data("tests/input/ellipse.dat");
|
||||
|
||||
std::cout << "Read " << data_points.size() << " data points." << std::endl;
|
||||
|
||||
auto mid_point = data_points[data_points.size() / 2];
|
||||
|
||||
std::cout << "Mid point: (" << mid_point.first << ", " << mid_point.second << ")" << std::endl;
|
||||
|
||||
Eigen::VectorX<double> initial_params(4);
|
||||
initial_params << 6900.0, 500.0, deg2rad(2.0), mid_point.second;
|
||||
|
||||
Eigen::VectorX<double> delta(4);
|
||||
delta << 10.0, 10.0, deg2rad(0.1), deg2rad(0.1);
|
||||
|
||||
auto res = fit_ellipse(data_points, initial_params, delta);
|
||||
std::cout << res.transpose() << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <autoopt/parabola_new.hpp>
|
||||
#include <print>
|
||||
#include "autoopt/util.hpp"
|
||||
|
||||
|
||||
using namespace autoopt;
|
||||
|
||||
|
||||
TEST(New, Parabola) {
|
||||
parabola_new parab;
|
||||
parab.flip = false;
|
||||
parab.initial(0) = 50.0;
|
||||
parab.initial(1) = autoopt::deg2rad(1.0);
|
||||
parab.initial(2) = autoopt::deg2rad(0.05);
|
||||
|
||||
parab.delta(0) = 10.0;
|
||||
parab.delta(1) = autoopt::deg2rad(0.1);
|
||||
parab.delta(2) = autoopt::deg2rad(0.1);
|
||||
|
||||
test_data td;
|
||||
td._xs = Eigen::VectorXd(21);
|
||||
td._ys = Eigen::VectorXd(21);
|
||||
td._center = 10.0;
|
||||
|
||||
for (size_t i = 0; i < 21; i++) {
|
||||
td._xs(i) = static_cast<double>(i);
|
||||
}
|
||||
|
||||
auto res = parab.plot(td);
|
||||
|
||||
for (auto y : res) {
|
||||
std::println("y: {}", y);
|
||||
}
|
||||
|
||||
td._ys = res;
|
||||
|
||||
parab.initial(0) = 54.0;
|
||||
parab.initial(1) = autoopt::deg2rad(1.05);
|
||||
|
||||
auto [fit, rms] = parab.fit(td);
|
||||
|
||||
for (auto y : fit) {
|
||||
std::println("fit: {}", y);
|
||||
}
|
||||
|
||||
std::println("RMS: {}", rms);
|
||||
}
|
||||
Reference in new issue
Block a user