blob: 4aba6a39ce8933065704ff288e58a73ed404ccdf [file] [log] [blame]
Sameer Agarwalf7da4112014-08-07 14:30:33 -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 Agarwalf7da4112014-08-07 14:30:33 -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: sameragarwal@google.com (Sameer Agarwal)
30
31#include "ceres/callbacks.h"
32#include "ceres/gradient_checking_cost_function.h"
Sameer Agarwal31503212014-08-12 22:46:51 -070033#include "ceres/line_search_preprocessor.h"
Sameer Agarwalf7da4112014-08-07 14:30:33 -070034#include "ceres/preprocessor.h"
35#include "ceres/problem_impl.h"
36#include "ceres/solver.h"
37#include "ceres/trust_region_preprocessor.h"
38
39namespace ceres {
40namespace internal {
41
42Preprocessor* Preprocessor::Create(MinimizerType minimizer_type) {
43 if (minimizer_type == TRUST_REGION) {
44 return new TrustRegionPreprocessor;
45 }
46
Sameer Agarwal31503212014-08-12 22:46:51 -070047 if (minimizer_type == LINE_SEARCH) {
48 return new LineSearchPreprocessor;
49 }
Sameer Agarwalf7da4112014-08-07 14:30:33 -070050
51 LOG(FATAL) << "Unknown minimizer_type: " << minimizer_type;
52 return NULL;
53}
54
55Preprocessor::~Preprocessor() {
56}
57
58void ChangeNumThreadsIfNeeded(Solver::Options* options) {
59#ifndef CERES_USE_OPENMP
60 if (options->num_threads > 1) {
61 LOG(WARNING)
62 << "OpenMP support is not compiled into this binary; "
63 << "only options.num_threads = 1 is supported. Switching "
64 << "to single threaded mode.";
65 options->num_threads = 1;
66 }
67
68 // Only the Trust Region solver currently uses a linear solver.
69 if (options->minimizer_type == TRUST_REGION &&
70 options->num_linear_solver_threads > 1) {
71 LOG(WARNING)
72 << "OpenMP support is not compiled into this binary; "
73 << "only options.num_linear_solver_threads=1 is supported. Switching "
74 << "to single threaded mode.";
75 options->num_linear_solver_threads = 1;
76 }
77#endif // CERES_USE_OPENMP
78}
79
80void SetupCommonMinimizerOptions(PreprocessedProblem* pp) {
81 const Solver::Options& options = pp->options;
82 Program* program = pp->reduced_program.get();
83
84 // Assuming that the parameter blocks in the program have been
85 // reordered as needed, extract them into a contiguous vector.
86 pp->reduced_parameters.resize(program->NumParameters());
87 double* reduced_parameters = pp->reduced_parameters.data();
88 program->ParameterBlocksToStateVector(reduced_parameters);
89
90 Minimizer::Options& minimizer_options = pp->minimizer_options;
91 minimizer_options = Minimizer::Options(options);
92 minimizer_options.evaluator = pp->evaluator;
93
94 if (options.logging_type != SILENT) {
95 pp->logging_callback.reset(
96 new LoggingCallback(options.minimizer_type,
97 options.minimizer_progress_to_stdout));
98 minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(),
99 pp->logging_callback.get());
100 }
101
102 if (options.update_state_every_iteration) {
103 pp->state_updating_callback.reset(
104 new StateUpdatingCallback(program, reduced_parameters));
105 // This must get pushed to the front of the callbacks so that it
106 // is run before any of the user callbacks.
107 minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(),
108 pp->state_updating_callback.get());
109 }
110}
111
112} // namespace internal
113} // namespace ceres