blob: 02219147d75f3c2b88ba0c36fff9a16119b6c267 [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: sameragarwal@google.com (Sameer Agarwal)
30
31#include "ceres/callbacks.h"
32#include "ceres/gradient_checking_cost_function.h"
33#include "ceres/line_search_preprocessor.h"
34#include "ceres/parallel_for.h"
35#include "ceres/preprocessor.h"
36#include "ceres/problem_impl.h"
37#include "ceres/solver.h"
38#include "ceres/trust_region_preprocessor.h"
39
40namespace ceres {
41namespace internal {
42
43Preprocessor* Preprocessor::Create(MinimizerType minimizer_type) {
44 if (minimizer_type == TRUST_REGION) {
45 return new TrustRegionPreprocessor;
46 }
47
48 if (minimizer_type == LINE_SEARCH) {
49 return new LineSearchPreprocessor;
50 }
51
52 LOG(FATAL) << "Unknown minimizer_type: " << minimizer_type;
53 return NULL;
54}
55
56Preprocessor::~Preprocessor() {
57}
58
59void ChangeNumThreadsIfNeeded(Solver::Options* options) {
60 const int num_threads_available = MaxNumThreadsAvailable();
61 if (options->num_threads > num_threads_available) {
62 LOG(WARNING)
63 << "Specified options.num_threads: " << options->num_threads
64 << " exceeds maximum available from the threading model Ceres "
65 << "was compiled with: " << num_threads_available
66 << ". Bounding to maximum number available.";
67 options->num_threads = num_threads_available;
68 }
69}
70
71void SetupCommonMinimizerOptions(PreprocessedProblem* pp) {
72 const Solver::Options& options = pp->options;
73 Program* program = pp->reduced_program.get();
74
75 // Assuming that the parameter blocks in the program have been
76 // reordered as needed, extract them into a contiguous vector.
77 pp->reduced_parameters.resize(program->NumParameters());
78 double* reduced_parameters = pp->reduced_parameters.data();
79 program->ParameterBlocksToStateVector(reduced_parameters);
80
81 Minimizer::Options& minimizer_options = pp->minimizer_options;
82 minimizer_options = Minimizer::Options(options);
83 minimizer_options.evaluator = pp->evaluator;
84
85 if (options.logging_type != SILENT) {
86 pp->logging_callback.reset(
87 new LoggingCallback(options.minimizer_type,
88 options.minimizer_progress_to_stdout));
89 minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(),
90 pp->logging_callback.get());
91 }
92
93 if (options.update_state_every_iteration) {
94 pp->state_updating_callback.reset(
95 new StateUpdatingCallback(program, reduced_parameters));
96 // This must get pushed to the front of the callbacks so that it
97 // is run before any of the user callbacks.
98 minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(),
99 pp->state_updating_callback.get());
100 }
101}
102
103} // namespace internal
104} // namespace ceres