blob: a2692ccdf637bb6485065b89f90d2931cf880bbb [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 Schuhf386bf92020-12-23 22:16:19 -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 Schuhf386bf92020-12-23 22:16:19 -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) {
Austin Schuhf386bf92020-12-23 22:16:19 -080089 output = StringPrintf(
90 "% 4d: f:% 8e d:% 3.2e g:% 3.2e h:% 3.2e s:% 3.2e e:% 3d it:% 3.2e "
91 "tt:% 3.2e",
92 summary.iteration,
93 summary.cost,
94 summary.cost_change,
95 summary.gradient_max_norm,
96 summary.step_norm,
97 summary.step_size,
98 summary.line_search_function_evaluations,
99 summary.iteration_time_in_seconds,
100 summary.cumulative_time_in_seconds);
Austin Schuh70cc9552019-01-21 19:46:48 -0800101 } else if (minimizer_type == TRUST_REGION) {
Austin Schuhf386bf92020-12-23 22:16:19 -0800102 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800103 if (summary.iteration == 0) {
104 output = "iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time\n"; // NOLINT
105 }
Austin Schuhf386bf92020-12-23 22:16:19 -0800106 output += StringPrintf(
107 "% 4d % 8e % 3.2e % 3.2e % 3.2e % 3.2e % 3.2e % 4d % 3.2e % 3.2e", // NOLINT
108 // clang-format on
109 summary.iteration,
110 summary.cost,
111 summary.cost_change,
112 summary.gradient_max_norm,
113 summary.step_norm,
114 summary.relative_decrease,
115 summary.trust_region_radius,
116 summary.linear_solver_iterations,
117 summary.iteration_time_in_seconds,
118 summary.cumulative_time_in_seconds);
Austin Schuh70cc9552019-01-21 19:46:48 -0800119 } else {
120 LOG(FATAL) << "Unknown minimizer type.";
121 }
122
123 if (log_to_stdout_) {
124 std::cout << output << std::endl;
125 } else {
126 VLOG(1) << output;
127 }
128 return SOLVER_CONTINUE;
129}
130
131} // namespace internal
132} // namespace ceres