blob: 0e0df9d91b15544f3342dbb89cb8b0bee848b74d [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 Google Inc. All rights reserved.
3// 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
33#include <iostream> // NO LINT
34
Austin Schuh70cc9552019-01-21 19:46:48 -080035#include "ceres/program.h"
36#include "ceres/stringprintf.h"
37#include "glog/logging.h"
38
39namespace ceres {
40namespace internal {
41
42using std::string;
43
44StateUpdatingCallback::StateUpdatingCallback(Program* program,
45 double* parameters)
46 : program_(program), parameters_(parameters) {}
47
48StateUpdatingCallback::~StateUpdatingCallback() {}
49
50CallbackReturnType StateUpdatingCallback::operator()(
51 const IterationSummary& summary) {
52 program_->StateVectorToParameterBlocks(parameters_);
53 program_->CopyParameterBlockStateToUserState();
54 return SOLVER_CONTINUE;
55}
56
57GradientProblemSolverStateUpdatingCallback::
58 GradientProblemSolverStateUpdatingCallback(
59 int num_parameters,
60 const double* internal_parameters,
61 double* user_parameters)
62 : num_parameters_(num_parameters),
63 internal_parameters_(internal_parameters),
64 user_parameters_(user_parameters) {}
65
66GradientProblemSolverStateUpdatingCallback::
67 ~GradientProblemSolverStateUpdatingCallback() {}
68
69CallbackReturnType GradientProblemSolverStateUpdatingCallback::operator()(
70 const IterationSummary& summary) {
71 if (summary.step_is_successful) {
72 std::copy(internal_parameters_,
73 internal_parameters_ + num_parameters_,
74 user_parameters_);
75 }
76 return SOLVER_CONTINUE;
77}
78
79LoggingCallback::LoggingCallback(const MinimizerType minimizer_type,
80 const bool log_to_stdout)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080081 : minimizer_type(minimizer_type), log_to_stdout_(log_to_stdout) {}
Austin Schuh70cc9552019-01-21 19:46:48 -080082
83LoggingCallback::~LoggingCallback() {}
84
85CallbackReturnType LoggingCallback::operator()(
86 const IterationSummary& summary) {
87 string output;
88 if (minimizer_type == LINE_SEARCH) {
89 const char* kReportRowFormat =
90 "% 4d: f:% 8e d:% 3.2e g:% 3.2e h:% 3.2e "
91 "s:% 3.2e e:% 3d it:% 3.2e tt:% 3.2e";
92 output = StringPrintf(kReportRowFormat,
93 summary.iteration,
94 summary.cost,
95 summary.cost_change,
96 summary.gradient_max_norm,
97 summary.step_norm,
98 summary.step_size,
99 summary.line_search_function_evaluations,
100 summary.iteration_time_in_seconds,
101 summary.cumulative_time_in_seconds);
102 } else if (minimizer_type == TRUST_REGION) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800103 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800104 if (summary.iteration == 0) {
105 output = "iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time\n"; // NOLINT
106 }
107 const char* kReportRowFormat =
108 "% 4d % 8e % 3.2e % 3.2e % 3.2e % 3.2e % 3.2e % 4d % 3.2e % 3.2e"; // NOLINT
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800109 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800110 output += StringPrintf(kReportRowFormat,
111 summary.iteration,
112 summary.cost,
113 summary.cost_change,
114 summary.gradient_max_norm,
115 summary.step_norm,
116 summary.relative_decrease,
117 summary.trust_region_radius,
118 summary.linear_solver_iterations,
119 summary.iteration_time_in_seconds,
120 summary.cumulative_time_in_seconds);
121 } else {
122 LOG(FATAL) << "Unknown minimizer type.";
123 }
124
125 if (log_to_stdout_) {
126 std::cout << output << std::endl;
127 } else {
128 VLOG(1) << output;
129 }
130 return SOLVER_CONTINUE;
131}
132
133} // namespace internal
134} // namespace ceres