blob: 50a0ec19924009a1a3d81851d6255642ada77e0d [file] [log] [blame]
Sameer Agarwalf5e81b62014-05-29 14:04:59 -07001// Ceres Solver - A fast non-linear least squares minimizer
Keir Mierle7492b0d2015-03-17 22:30:16 -07002// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
Sameer Agarwalf5e81b62014-05-29 14:04:59 -07004//
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
31#include <iostream> // NO LINT
32#include "ceres/callbacks.h"
33#include "ceres/program.h"
34#include "ceres/stringprintf.h"
35#include "glog/logging.h"
36
37namespace ceres {
38namespace internal {
39
Sameer Agarwal05a07ec2015-01-07 15:10:46 -080040using std::string;
41
Sameer Agarwalf5e81b62014-05-29 14:04:59 -070042StateUpdatingCallback::StateUpdatingCallback(Program* program,
43 double* parameters)
44 : program_(program), parameters_(parameters) {}
45
46StateUpdatingCallback::~StateUpdatingCallback() {}
47
48CallbackReturnType StateUpdatingCallback::operator()(
49 const IterationSummary& summary) {
50 if (summary.step_is_successful) {
51 program_->StateVectorToParameterBlocks(parameters_);
52 program_->CopyParameterBlockStateToUserState();
53 }
54 return SOLVER_CONTINUE;
55}
56
57LoggingCallback::LoggingCallback(const MinimizerType minimizer_type,
58 const bool log_to_stdout)
59 : minimizer_type(minimizer_type),
60 log_to_stdout_(log_to_stdout) {}
61
62LoggingCallback::~LoggingCallback() {}
63
64CallbackReturnType LoggingCallback::operator()(
65 const IterationSummary& summary) {
66 string output;
67 if (minimizer_type == LINE_SEARCH) {
68 const char* kReportRowFormat =
69 "% 4d: f:% 8e d:% 3.2e g:% 3.2e h:% 3.2e "
70 "s:% 3.2e e:% 3d it:% 3.2e tt:% 3.2e";
71 output = StringPrintf(kReportRowFormat,
72 summary.iteration,
73 summary.cost,
74 summary.cost_change,
75 summary.gradient_max_norm,
76 summary.step_norm,
77 summary.step_size,
78 summary.line_search_function_evaluations,
79 summary.iteration_time_in_seconds,
80 summary.cumulative_time_in_seconds);
81 } else if (minimizer_type == TRUST_REGION) {
Sameer Agarwal14ec8492014-06-04 17:29:03 -070082 if (summary.iteration == 0) {
Sameer Agarwalbcc865f2014-12-17 07:35:09 -080083 output = "iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time\n"; // NOLINT
Sameer Agarwal14ec8492014-06-04 17:29:03 -070084 }
Sameer Agarwalf5e81b62014-05-29 14:04:59 -070085 const char* kReportRowFormat =
Sameer Agarwalbcc865f2014-12-17 07:35:09 -080086 "% 4d % 8e % 3.2e % 3.2e % 3.2e % 3.2e % 3.2e % 4d % 3.2e % 3.2e"; // NOLINT
Sameer Agarwal14ec8492014-06-04 17:29:03 -070087 output += StringPrintf(kReportRowFormat,
Sameer Agarwalbcc865f2014-12-17 07:35:09 -080088 summary.iteration,
89 summary.cost,
90 summary.cost_change,
91 summary.gradient_max_norm,
92 summary.step_norm,
93 summary.relative_decrease,
94 summary.trust_region_radius,
95 summary.linear_solver_iterations,
96 summary.iteration_time_in_seconds,
97 summary.cumulative_time_in_seconds);
Sameer Agarwalf5e81b62014-05-29 14:04:59 -070098 } else {
99 LOG(FATAL) << "Unknown minimizer type.";
100 }
101
102 if (log_to_stdout_) {
Sameer Agarwalbcc865f2014-12-17 07:35:09 -0800103 std::cout << output << std::endl;
Sameer Agarwalf5e81b62014-05-29 14:04:59 -0700104 } else {
105 VLOG(1) << output;
106 }
107 return SOLVER_CONTINUE;
108}
109
110} // namespace internal
111} // namespace ceres