tinyopt 700
Tinyopt, a lightweight, header only optimization library
Loading...
Searching...
No Matches
options.h
1// Copyright 2026 Julien Michot.
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include <cstdint>
7#include <functional>
8
9#include <tinyopt/log.h>
10#include <tinyopt/math.h>
11
12namespace tinyopt {
13
14/***
15 * @brief Common Optimization Options
16 *
17 ***/
18struct Options {
23 enum Solver {
24 LevenbergMarquardt = 0,
25 GaussNewton,
26 GradientDescent,
27 ConjugateGradient,
28 DogLeg,
29 BFGS,
30 LBFGS,
31 };
33 Solver solver_type = Solver::LevenbergMarquardt;
35
37 LinearSolverMethod linear_solver = LinearSolverMethod::LDLT;
40
41 Options(Solver type = Solver::LevenbergMarquardt) : solver_type(type) {};
42
47 struct Optimization {
49 bool check_final_cost = false;
50
53
55 float grad_clipping = 0;
56 } opt;
57
58
63
64 struct Hessian {
65 bool H_is_full = true;
66
67 float check_min_H_diag = 0;
69
70 bool save_last = true;
71 } hessian;
72
74
79 struct CostScaling {
80 bool use_squared_norm = true;
81 bool downscale_by_2 = false;
83 bool normalize = false;
84 } cost;
85
87
92 struct StopCriteria {
93 uint16_t max_iters = 50;
94 float min_error = 1e-12f;
95 float min_rerr_dec = 1e-10f;
96 float min_step_norm2 = 1e-14f;
97 float min_grad_norm2 = 1e-18f;
98 uint8_t max_total_failures = 0;
99 uint8_t max_consec_failures = 5;
100 double max_duration_ms = 0;
101
102 std::function<bool(double, double, double)> stop_callback;
103 std::function<bool(float, const VecXf &, const VecXf &)> stop_callback2;
107 std::function<bool(const VecXf &dx, bool is_rollback)> step_callback;
108 } stop;
109
110
115 struct LogOptions {
116 bool enable = true;
117 std::string e = "ε²";
118 bool print_emoji = true;
119 bool print_x = false;
120 bool print_dx = false;
121 bool print_inliers = false;
122 bool print_t = true;
123 bool print_J_jet = false;
124 bool print_max_stdev = false;
126 bool print_failure = false; // Log when a failure to solve the linear system happens
127 } log;
128
129
130 struct LM {
131 bool jacobi_scaling = false;
136 float damping_init = 1e-4f;
139 std::array<float, 2> damping_range{{1e-9f, 1e9f}};
140
141 float good_factor = 1.0f / 3.0f;
142 float bad_factor = 2.0f;
144 } lm;
145
150 struct GD {
151 float lr = 1e-3f;
152 // TODO float min_lr = 1e-6f; ///< Minimum learning rate
153 // TODO float max_lr = 1e6f; ///< Maximum learning rate
154 // TODO float decay_factor = 0.5f; ///< Factor to decay the learning rate (if adaptive)
155 // TODO bool use_adaptive_lr = false; ///< Whether to use adaptive learning rate
157 } gd;
158
159 struct CG {
160 float step_size = 0.25f;
161 float step_reduction = 0.5f;
162 } cg;
163
164 struct DL {
165 float radius_init = 1.0f;
166 float radius_max = 1e6f;
167 float shrink_factor = 0.25f;
168 float expand_factor = 2.0f;
169 } dl;
170
171 struct BFGS {
172 float step_size = 1.0f;
173 float step_reduction = 0.5f;
174 float step_growth = 1.5f;
175 float max_step_size = 1.0f;
176 float curvature_threshold = 1e-8f;
177 } bfgs;
178
179 struct LBFGS {
180 float step_size = 1.0f;
181 float step_reduction = 0.5f;
182 float step_growth = 1.5f;
183 float max_step_size = 1.0f;
184 float curvature_threshold = 1e-8f;
185 uint8_t history_size = 8;
186 } lbfgs;
187};
188
189} // namespace tinyopt
Definition options.h:171
Definition options.h:159
Definition options.h:79
bool normalize
Normalize the final error by the number of residuals (after use_squared_norm).
Definition options.h:83
bool use_squared_norm
Use squared norm instead of norm (faster).
Definition options.h:80
bool downscale_by_2
Definition options.h:81
Definition options.h:164
Definition options.h:150
float lr
Initial learning rate.
Definition options.h:151
Definition options.h:64
bool save_last
Saves the last Hessian H as part of the output results.
Definition options.h:70
bool H_is_full
Specify if H is only Upper triangularly or fully filled.
Definition options.h:65
float check_min_H_diag
threshold. Use 0 to disable the check.
Definition options.h:67
Definition options.h:179
Definition options.h:130
float damping_init
Definition options.h:136
float bad_factor
Scale to apply to the damping for bad steps.
Definition options.h:142
bool jacobi_scaling
Scale normal equations using the clamped Hessian diagonal.
Definition options.h:131
float good_factor
Scale to apply to the damping for good steps.
Definition options.h:141
Definition options.h:115
bool print_dx
Log the value of step 'dx'.
Definition options.h:120
std::string e
Symbol used when logging the error, e.g ε, ε² or √ε etc.
Definition options.h:117
bool enable
Whether to enable the logging.
Definition options.h:116
bool print_t
Log the duration (in ms).
Definition options.h:122
bool print_emoji
Whether to show the emoji or not.
Definition options.h:118
bool print_max_stdev
Definition options.h:124
bool print_x
Log the value of 'x'.
Definition options.h:119
bool print_J_jet
Log the value of 'J' from the Jet.
Definition options.h:123
bool print_inliers
Log the inliers ratio (in %).
Definition options.h:121
Definition options.h:47
bool use_step_quality_approx
Use relative error decrease as step quality; otherwise use 0.0.
Definition options.h:52
float grad_clipping
Gradient clipping to range [-v, +v], disabled if 0.
Definition options.h:55
bool check_final_cost
Recompute the final cost as a rollback safety check.
Definition options.h:49
Definition options.h:92
float min_grad_norm2
Minimum squared gradient norm.
Definition options.h:97
uint16_t max_iters
Maximum number of outer iterations.
Definition options.h:93
std::function< bool(const VecXf &dx, bool is_rollback)> step_callback
Definition options.h:107
uint8_t max_consec_failures
Maximum consecutive failures to decrease error.
Definition options.h:99
double max_duration_ms
Maximum optimization duration in milliseconds.
Definition options.h:100
float min_error
Minimum error/cost.
Definition options.h:94
float min_rerr_dec
Minimum relative error decrease.
Definition options.h:95
uint8_t max_total_failures
Overall max failures to decrease error.
Definition options.h:98
float min_step_norm2
Minimum squared step norm.
Definition options.h:96
double svd_relative_threshold
Relative singular-value cutoff for SVD; 0 uses Eigen's default threshold.
Definition options.h:39
LinearSolverMethod linear_solver
Linear system method. LDLT is enabled by default; methods require their CMake option.
Definition options.h:37
Solver solver_type
Which solver to use. Default is LevenbergMarquardt for NLLS problems.
Definition options.h:33