Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2010, 2011, 2012 Google Inc. All rights reserved. |
| 3 | // http://code.google.com/p/ceres-solver/ |
| 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: keir@google.com (Keir Mierle) |
| 30 | |
Sameer Agarwal | 4997cbc | 2012-07-02 12:44:34 -0700 | [diff] [blame] | 31 | #include <vector> |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 32 | #include "ceres/block_evaluate_preparer.h" |
| 33 | #include "ceres/block_jacobian_writer.h" |
| 34 | #include "ceres/compressed_row_jacobian_writer.h" |
Sameer Agarwal | 4997cbc | 2012-07-02 12:44:34 -0700 | [diff] [blame] | 35 | #include "ceres/compressed_row_sparse_matrix.h" |
| 36 | #include "ceres/crs_matrix.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 37 | #include "ceres/dense_jacobian_writer.h" |
Sameer Agarwal | 4997cbc | 2012-07-02 12:44:34 -0700 | [diff] [blame] | 38 | #include "ceres/evaluator.h" |
| 39 | #include "ceres/internal/port.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 40 | #include "ceres/program_evaluator.h" |
Sameer Agarwal | 4997cbc | 2012-07-02 12:44:34 -0700 | [diff] [blame] | 41 | #include "ceres/scratch_evaluate_preparer.h" |
Sameer Agarwal | 0beab86 | 2012-08-13 15:12:01 -0700 | [diff] [blame] | 42 | #include "glog/logging.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 43 | |
| 44 | namespace ceres { |
| 45 | namespace internal { |
| 46 | |
| 47 | Evaluator::~Evaluator() {} |
| 48 | |
| 49 | Evaluator* Evaluator::Create(const Evaluator::Options& options, |
| 50 | Program* program, |
| 51 | string* error) { |
| 52 | switch (options.linear_solver_type) { |
| 53 | case DENSE_QR: |
Sameer Agarwal | b9f15a5 | 2012-08-18 13:06:19 -0700 | [diff] [blame] | 54 | case DENSE_NORMAL_CHOLESKY: |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 55 | return new ProgramEvaluator<ScratchEvaluatePreparer, |
| 56 | DenseJacobianWriter>(options, |
| 57 | program); |
| 58 | case DENSE_SCHUR: |
| 59 | case SPARSE_SCHUR: |
| 60 | case ITERATIVE_SCHUR: |
Keir Mierle | e2a6cdc | 2012-05-07 06:39:56 -0700 | [diff] [blame] | 61 | case CGNR: |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 62 | return new ProgramEvaluator<BlockEvaluatePreparer, |
| 63 | BlockJacobianWriter>(options, |
| 64 | program); |
| 65 | case SPARSE_NORMAL_CHOLESKY: |
| 66 | return new ProgramEvaluator<ScratchEvaluatePreparer, |
| 67 | CompressedRowJacobianWriter>(options, |
| 68 | program); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 69 | default: |
| 70 | *error = "Invalid Linear Solver Type. Unable to create evaluator."; |
| 71 | return NULL; |
| 72 | } |
| 73 | } |
| 74 | |
Sameer Agarwal | 4997cbc | 2012-07-02 12:44:34 -0700 | [diff] [blame] | 75 | bool Evaluator::Evaluate(Program* program, |
| 76 | int num_threads, |
| 77 | double* cost, |
| 78 | vector<double>* residuals, |
| 79 | vector<double>* gradient, |
| 80 | CRSMatrix* output_jacobian) { |
| 81 | CHECK_GE(num_threads, 1) |
| 82 | << "This is a Ceres bug; please contact the developers!"; |
| 83 | CHECK_NOTNULL(cost); |
| 84 | |
| 85 | // Setup the Parameter indices and offsets before an evaluator can |
| 86 | // be constructed and used. |
| 87 | program->SetParameterOffsetsAndIndex(); |
| 88 | |
| 89 | Evaluator::Options evaluator_options; |
| 90 | evaluator_options.linear_solver_type = SPARSE_NORMAL_CHOLESKY; |
| 91 | evaluator_options.num_threads = num_threads; |
| 92 | |
| 93 | string error; |
| 94 | scoped_ptr<Evaluator> evaluator( |
| 95 | Evaluator::Create(evaluator_options, program, &error)); |
| 96 | if (evaluator.get() == NULL) { |
| 97 | LOG(ERROR) << "Unable to create an Evaluator object. " |
| 98 | << "Error: " << error |
| 99 | << "This is a Ceres bug; please contact the developers!"; |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | if (residuals !=NULL) { |
| 104 | residuals->resize(evaluator->NumResiduals()); |
| 105 | } |
| 106 | |
| 107 | if (gradient != NULL) { |
| 108 | gradient->resize(evaluator->NumEffectiveParameters()); |
| 109 | } |
| 110 | |
| 111 | scoped_ptr<CompressedRowSparseMatrix> jacobian; |
| 112 | if (output_jacobian != NULL) { |
| 113 | jacobian.reset( |
| 114 | down_cast<CompressedRowSparseMatrix*>(evaluator->CreateJacobian())); |
| 115 | } |
| 116 | |
| 117 | // Point the state pointers to the user state pointers. This is |
| 118 | // needed so that we can extract a parameter vector which is then |
| 119 | // passed to Evaluator::Evaluate. |
| 120 | program->SetParameterBlockStatePtrsToUserStatePtrs(); |
| 121 | |
| 122 | // Copy the value of the parameter blocks into a vector, since the |
| 123 | // Evaluate::Evaluate method needs its input as such. The previous |
| 124 | // call to SetParameterBlockStatePtrsToUserStatePtrs ensures that |
| 125 | // these values are the ones corresponding to the actual state of |
| 126 | // the parameter blocks, rather than the temporary state pointer |
| 127 | // used for evaluation. |
| 128 | Vector parameters(program->NumParameters()); |
| 129 | program->ParameterBlocksToStateVector(parameters.data()); |
| 130 | |
| 131 | if (!evaluator->Evaluate(parameters.data(), |
| 132 | cost, |
| 133 | residuals != NULL ? &(*residuals)[0] : NULL, |
| 134 | gradient != NULL ? &(*gradient)[0] : NULL, |
| 135 | jacobian.get())) { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | if (output_jacobian != NULL) { |
| 140 | jacobian->ToCRSMatrix(output_jacobian); |
| 141 | } |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 146 | } // namespace internal |
| 147 | } // namespace ceres |