tinyopt 700
Tinyopt, a lightweight, header only optimization library
Loading...
Searching...
No Matches
c_api_common.h
1// Copyright 2026 Julien Michot.
2// SPDX-License-Identifier: Apache-2.0
3
4#ifndef TINYOPT_C_C_API_COMMON_H
5#define TINYOPT_C_C_API_COMMON_H
6
7#include <tinyopt/c/c_api_config.h>
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13#if !TINYOPT_C_API_ENABLE_SHARED
14#define TINYOPT_C_API
15#elif defined(_WIN32)
16#if defined(TINYOPT_C_BUILD)
17#define TINYOPT_C_API __declspec(dllexport)
18#else
19#define TINYOPT_C_API __declspec(dllimport)
20#endif
21#else
22#define TINYOPT_C_API __attribute__((visibility("default")))
23#endif
24
25typedef struct tinyopt_summary_t {
26 int stop_reason; /* Tinyopt termination reason value. */
27 int num_iters; /* Number of optimizer iterations performed. */
28 int num_failures; /* Number of unsuccessful trial steps. */
29 int num_residuals; /* Number of residuals used by the problem. */
30 double final_cost; /* Final objective cost. */
31 int used_numerical_differentiation; /* Nonzero if numerical derivatives were used. */
33
34typedef enum tinyopt_status_t {
35 TINYOPT_STATUS_OK = 0,
36 TINYOPT_STATUS_INVALID_ARGUMENT = 1,
37 TINYOPT_STATUS_CALLBACK_FAILED = 2,
38 TINYOPT_STATUS_RESIDUAL_CALLBACK_FAILED = TINYOPT_STATUS_CALLBACK_FAILED,
39 TINYOPT_STATUS_OPTIMIZATION_FAILED = 3,
40 TINYOPT_STATUS_INTERNAL_ERROR = 4,
41 TINYOPT_STATUS_USER_STOPPED = 5
42} tinyopt_status_t;
43
44typedef enum tinyopt_eval_type_t {
45 TINYOPT_EVAL_COST_ONLY = 0,
46 TINYOPT_EVAL_RESIDUALS,
47 TINYOPT_EVAL_GRADIENT,
48 TINYOPT_EVAL_HESSIAN
49} tinyopt_eval_type_t;
50
51typedef enum tinyopt_solver_t {
52 TINYOPT_SOLVER_LEVENBERG_MARQUARDT = 0,
53 TINYOPT_SOLVER_GAUSS_NEWTON,
54 TINYOPT_SOLVER_GRADIENT_DESCENT,
55 TINYOPT_SOLVER_CONJUGATE_GRADIENT,
56 TINYOPT_SOLVER_DOGLEG,
57 TINYOPT_SOLVER_BFGS,
58 TINYOPT_SOLVER_LBFGS
59} tinyopt_solver_t;
60
61typedef enum tinyopt_linear_solver_t {
62 TINYOPT_LINEAR_SOLVER_LDLT = 0,
63 TINYOPT_LINEAR_SOLVER_LLT,
64 TINYOPT_LINEAR_SOLVER_LU,
65 TINYOPT_LINEAR_SOLVER_QR,
66 TINYOPT_LINEAR_SOLVER_SVD,
67 TINYOPT_LINEAR_SOLVER_SUITESPARSE
68} tinyopt_linear_solver_t;
69
70typedef int (*tinyopt_stop_callback_t)(double error, double step_norm_squared,
71 double gradient_norm_squared, void *user_data);
72typedef int (*tinyopt_stop_callback2_t)(float error, const float *x, const float *dx, int dims,
73 void *user_data);
74/* Called after every parameter update with the step `dx` added to the parameters. `is_rollback` is
75 nonzero when a rejected step is undone, `dx` being then the negated step. Summing the steps of
76 all calls tracks the current parameters. Return nonzero to stop. */
77typedef int (*tinyopt_step_callback_t)(const float *dx, int dims, int is_rollback, void *user_data);
78
79typedef struct tinyopt_options_t {
80 tinyopt_solver_t solver_type; /* Nonlinear optimizer to use. */
81 tinyopt_linear_solver_t linear_solver; /* Linear system solver to use. */
82 double svd_relative_threshold; /* Relative singular-value cutoff for SVD solvers. */
83
84 int check_final_cost; /* Re-evaluate and validate the final cost. */
85 int use_step_quality_approx; /* Approximate step quality to reduce cost evaluations. */
86 float grad_clipping; /* Maximum gradient component magnitude; zero disables clipping. */
87
88 int hessian_is_full; /* Treat the accumulated Hessian as a full matrix. */
89 float check_min_hessian_diagonal; /* Minimum accepted Hessian diagonal value. */
90 int save_last_hessian; /* Retain the final Hessian in the optimization summary. */
91
92 int use_squared_norm; /* Use squared residual norm for the objective. */
93 int downscale_cost_by_two; /* Multiply the cost by one half. */
94 int normalize_cost; /* Normalize cost by the number of residuals. */
95
96 unsigned short max_iters; /* Maximum number of optimizer iterations. */
97 float min_error; /* Stop when the objective error is below this threshold. */
98 float min_relative_error_decrease; /* Stop when relative cost decrease is below this value. */
99 float min_step_norm_squared; /* Stop when squared step norm is below this value. */
100 float min_gradient_norm_squared; /* Stop when squared gradient norm is below this value. */
101 unsigned char max_total_failures; /* Maximum total unsuccessful trial steps. */
102 unsigned char max_consecutive_failures; /* Maximum consecutive unsuccessful trial steps. */
103 double max_duration_ms; /* Maximum optimization duration in milliseconds; zero disables limit. */
104 tinyopt_stop_callback_t stop_callback; /* Optional per-iteration scalar stop callback. */
105 void *stop_callback_user_data; /* User data passed to stop_callback. */
106 tinyopt_stop_callback2_t stop_callback2; /* Optional per-iteration vector stop callback. */
107 void *stop_callback2_user_data; /* User data passed to stop_callback2. */
108 tinyopt_step_callback_t step_callback; /* Optional callback run after every parameter update. */
109 void *step_callback_user_data; /* User data passed to step_callback. */
110
111 int log_enabled; /* Enable optimizer logging. */
112 const char *log_error_symbol; /* Optional symbol used to label the logged error. */
113 int log_print_emoji; /* Include status symbols in log output. */
114 int log_print_x; /* Print parameter values. */
115 int log_print_dx; /* Print parameter updates. */
116 int log_print_inliers; /* Print inlier statistics when available. */
117 int log_print_time; /* Print iteration timing. */
118 int log_print_jacobian_jet; /* Print Jacobian automatic-differentiation details. */
119 int log_print_max_standard_deviation; /* Print maximum parameter standard deviation. */
120 int log_print_failure; /* Print details for unsuccessful trial steps. */
121
122 int lm_jacobi_scaling; /* Scale LM damping by the Hessian diagonal. */
123 float lm_damping_init; /* Initial Levenberg-Marquardt damping. */
124 float lm_damping_min; /* Minimum Levenberg-Marquardt damping. */
125 float lm_damping_max; /* Maximum Levenberg-Marquardt damping. */
126 float lm_good_factor; /* Damping multiplier after a successful step. */
127 float lm_bad_factor; /* Damping multiplier after an unsuccessful step. */
128
129 float gd_learning_rate; /* Gradient-descent learning rate. */
130 float cg_step_size; /* Initial conjugate-gradient step size. */
131 float cg_step_reduction; /* Conjugate-gradient step reduction factor. */
132 float dogleg_radius_init; /* Initial Dogleg trust-region radius. */
133 float dogleg_radius_max; /* Maximum Dogleg trust-region radius. */
134 float dogleg_shrink_factor; /* Dogleg radius multiplier after an unsuccessful step. */
135 float dogleg_expand_factor; /* Dogleg radius multiplier after a successful step. */
136 float bfgs_step_size; /* Initial BFGS step size. */
137 float bfgs_step_reduction; /* BFGS step multiplier after an unsuccessful step. */
138 float bfgs_step_growth; /* BFGS step multiplier after a successful step. */
139 float bfgs_max_step_size; /* Maximum BFGS step size. */
140 float bfgs_curvature_threshold; /* Minimum curvature for a BFGS update. */
141 float lbfgs_step_size; /* Initial L-BFGS step size. */
142 float lbfgs_step_reduction; /* L-BFGS step multiplier after an unsuccessful step. */
143 float lbfgs_step_growth; /* L-BFGS step multiplier after a successful step. */
144 float lbfgs_max_step_size; /* Maximum L-BFGS step size. */
145 float lbfgs_curvature_threshold; /* Minimum curvature for an L-BFGS update. */
146 unsigned char lbfgs_history_size; /* Number of past updates retained by L-BFGS. */
148
149TINYOPT_C_API tinyopt_status_t tinyopt_options_default(tinyopt_options_t *options);
150
151#ifdef __cplusplus
152}
153#endif
154
155#endif
Definition c_api_common.h:79
Definition c_api_common.h:25