blob: e6e064438eee09b9e59fddbc55253d875584cc55 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// http://ceres-solver.org/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: sameeragarwal@google.com (Sameer Agarwal)
30
Austin Schuh70cc9552019-01-21 19:46:48 -080031#include "ceres/callbacks.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080032
Austin Schuh3de38b02024-06-25 18:25:10 -070033#include <algorithm>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080034#include <iostream> // NO LINT
Austin Schuh3de38b02024-06-25 18:25:10 -070035#include <string>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080036
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include "ceres/program.h"
38#include "ceres/stringprintf.h"
39#include "glog/logging.h"
40
Austin Schuh3de38b02024-06-25 18:25:10 -070041namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080042
43StateUpdatingCallback::StateUpdatingCallback(Program* program,
44 double* parameters)
45 : program_(program), parameters_(parameters) {}
46
Austin Schuh3de38b02024-06-25 18:25:10 -070047StateUpdatingCallback::~StateUpdatingCallback() = default;
Austin Schuh70cc9552019-01-21 19:46:48 -080048
49CallbackReturnType StateUpdatingCallback::operator()(
Austin Schuh3de38b02024-06-25 18:25:10 -070050 const IterationSummary& /*summary*/) {
Austin Schuh70cc9552019-01-21 19:46:48 -080051 program_->StateVectorToParameterBlocks(parameters_);
52 program_->CopyParameterBlockStateToUserState();
53 return SOLVER_CONTINUE;
54}
55
56GradientProblemSolverStateUpdatingCallback::
57 GradientProblemSolverStateUpdatingCallback(
58 int num_parameters,
59 const double* internal_parameters,
60 double* user_parameters)
61 : num_parameters_(num_parameters),
62 internal_parameters_(internal_parameters),
63 user_parameters_(user_parameters) {}
64
65GradientProblemSolverStateUpdatingCallback::
Austin Schuh3de38b02024-06-25 18:25:10 -070066 ~GradientProblemSolverStateUpdatingCallback() = default;
Austin Schuh70cc9552019-01-21 19:46:48 -080067
68CallbackReturnType GradientProblemSolverStateUpdatingCallback::operator()(
69 const IterationSummary& summary) {
70 if (summary.step_is_successful) {
Austin Schuh3de38b02024-06-25 18:25:10 -070071 std::copy_n(internal_parameters_, num_parameters_, user_parameters_);
Austin Schuh70cc9552019-01-21 19:46:48 -080072 }
73 return SOLVER_CONTINUE;
74}
75
76LoggingCallback::LoggingCallback(const MinimizerType minimizer_type,
77 const bool log_to_stdout)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080078 : minimizer_type(minimizer_type), log_to_stdout_(log_to_stdout) {}
Austin Schuh70cc9552019-01-21 19:46:48 -080079
Austin Schuh3de38b02024-06-25 18:25:10 -070080LoggingCallback::~LoggingCallback() = default;
Austin Schuh70cc9552019-01-21 19:46:48 -080081
82CallbackReturnType LoggingCallback::operator()(
83 const IterationSummary& summary) {
Austin Schuh3de38b02024-06-25 18:25:10 -070084 std::string output;
Austin Schuh70cc9552019-01-21 19:46:48 -080085 if (minimizer_type == LINE_SEARCH) {
Austin Schuh3de38b02024-06-25 18:25:10 -070086 output = StringPrintf(
87 "% 4d: f:% 8e d:% 3.2e g:% 3.2e h:% 3.2e s:% 3.2e e:% 3d it:% 3.2e "
88 "tt:% 3.2e",
89 summary.iteration,
90 summary.cost,
91 summary.cost_change,
92 summary.gradient_max_norm,
93 summary.step_norm,
94 summary.step_size,
95 summary.line_search_function_evaluations,
96 summary.iteration_time_in_seconds,
97 summary.cumulative_time_in_seconds);
Austin Schuh70cc9552019-01-21 19:46:48 -080098 } else if (minimizer_type == TRUST_REGION) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080099 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800100 if (summary.iteration == 0) {
101 output = "iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time\n"; // NOLINT
102 }
Austin Schuh3de38b02024-06-25 18:25:10 -0700103 output += StringPrintf(
104 "% 4d % 8e % 3.2e % 3.2e % 3.2e % 3.2e % 3.2e % 4d % 3.2e % 3.2e", // NOLINT
105 // clang-format on
106 summary.iteration,
107 summary.cost,
108 summary.cost_change,
109 summary.gradient_max_norm,
110 summary.step_norm,
111 summary.relative_decrease,
112 summary.trust_region_radius,
113 summary.linear_solver_iterations,
114 summary.iteration_time_in_seconds,
115 summary.cumulative_time_in_seconds);
Austin Schuh70cc9552019-01-21 19:46:48 -0800116 } else {
117 LOG(FATAL) << "Unknown minimizer type.";
118 }
119
120 if (log_to_stdout_) {
121 std::cout << output << std::endl;
122 } else {
123 VLOG(1) << output;
124 }
125 return SOLVER_CONTINUE;
126}
127
Austin Schuh3de38b02024-06-25 18:25:10 -0700128} // namespace ceres::internal