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: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 31 | #include <algorithm> |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 32 | #include <string> |
| 33 | #include "ceres/types.h" |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 34 | #include "glog/logging.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 35 | |
| 36 | namespace ceres { |
| 37 | |
| 38 | #define CASESTR(x) case x: return #x |
| 39 | |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 40 | #define STRENUM(x) if (value == #x) { *type = x; return true;} |
| 41 | |
| 42 | void UpperCase(string* input) { |
| 43 | std::transform(input->begin(), input->end(), input->begin(), ::toupper); |
| 44 | } |
| 45 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 46 | const char* LinearSolverTypeToString(LinearSolverType solver_type) { |
| 47 | switch (solver_type) { |
Sameer Agarwal | b9f15a5 | 2012-08-18 13:06:19 -0700 | [diff] [blame] | 48 | CASESTR(DENSE_NORMAL_CHOLESKY); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 49 | CASESTR(DENSE_QR); |
Sameer Agarwal | b9f15a5 | 2012-08-18 13:06:19 -0700 | [diff] [blame] | 50 | CASESTR(SPARSE_NORMAL_CHOLESKY); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 51 | CASESTR(DENSE_SCHUR); |
| 52 | CASESTR(SPARSE_SCHUR); |
| 53 | CASESTR(ITERATIVE_SCHUR); |
Keir Mierle | e2a6cdc | 2012-05-07 06:39:56 -0700 | [diff] [blame] | 54 | CASESTR(CGNR); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 55 | default: |
| 56 | return "UNKNOWN"; |
| 57 | } |
| 58 | } |
| 59 | |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 60 | bool StringToLinearSolverType(string value, LinearSolverType* type) { |
| 61 | UpperCase(&value); |
| 62 | STRENUM(DENSE_NORMAL_CHOLESKY); |
| 63 | STRENUM(DENSE_QR); |
| 64 | STRENUM(SPARSE_NORMAL_CHOLESKY); |
| 65 | STRENUM(DENSE_SCHUR); |
| 66 | STRENUM(SPARSE_SCHUR); |
| 67 | STRENUM(ITERATIVE_SCHUR); |
| 68 | STRENUM(CGNR); |
| 69 | return false; |
| 70 | } |
| 71 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 72 | const char* PreconditionerTypeToString( |
| 73 | PreconditionerType preconditioner_type) { |
| 74 | switch (preconditioner_type) { |
| 75 | CASESTR(IDENTITY); |
| 76 | CASESTR(JACOBI); |
| 77 | CASESTR(SCHUR_JACOBI); |
| 78 | CASESTR(CLUSTER_JACOBI); |
| 79 | CASESTR(CLUSTER_TRIDIAGONAL); |
| 80 | default: |
| 81 | return "UNKNOWN"; |
| 82 | } |
| 83 | } |
| 84 | |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 85 | bool StringToPreconditionerType(string value, PreconditionerType* type) { |
| 86 | UpperCase(&value); |
| 87 | STRENUM(IDENTITY); |
| 88 | STRENUM(JACOBI); |
| 89 | STRENUM(SCHUR_JACOBI); |
| 90 | STRENUM(CLUSTER_JACOBI); |
| 91 | STRENUM(CLUSTER_TRIDIAGONAL); |
| 92 | return false; |
| 93 | } |
| 94 | |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 95 | const char* SparseLinearAlgebraLibraryTypeToString( |
| 96 | SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type) { |
| 97 | switch (sparse_linear_algebra_library_type) { |
| 98 | CASESTR(SUITE_SPARSE); |
| 99 | CASESTR(CX_SPARSE); |
| 100 | default: |
| 101 | return "UNKNOWN"; |
| 102 | } |
| 103 | } |
| 104 | |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 105 | |
| 106 | bool StringToSparseLinearAlgebraLibraryType( |
| 107 | string value, |
| 108 | SparseLinearAlgebraLibraryType* type) { |
| 109 | UpperCase(&value); |
| 110 | STRENUM(SUITE_SPARSE); |
| 111 | STRENUM(CX_SPARSE); |
| 112 | return false; |
| 113 | } |
| 114 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 115 | const char* OrderingTypeToString(OrderingType ordering_type) { |
| 116 | switch (ordering_type) { |
| 117 | CASESTR(NATURAL); |
| 118 | CASESTR(USER); |
| 119 | CASESTR(SCHUR); |
| 120 | default: |
| 121 | return "UNKNOWN"; |
| 122 | } |
| 123 | } |
| 124 | |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 125 | bool StringToOrderingType(string value, OrderingType* type) { |
| 126 | UpperCase(&value); |
| 127 | STRENUM(NATURAL); |
| 128 | STRENUM(USER); |
| 129 | STRENUM(SCHUR); |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | const char* TrustRegionStrategyTypeToString( |
| 134 | TrustRegionStrategyType trust_region_strategy_type) { |
| 135 | switch (trust_region_strategy_type) { |
| 136 | CASESTR(LEVENBERG_MARQUARDT); |
| 137 | CASESTR(DOGLEG); |
| 138 | default: |
| 139 | return "UNKNOWN"; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | bool StringToTrustRegionStrategyType(string value, |
| 144 | TrustRegionStrategyType* type) { |
| 145 | UpperCase(&value); |
| 146 | STRENUM(LEVENBERG_MARQUARDT); |
| 147 | STRENUM(DOGLEG); |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | const char* DoglegTypeToString(DoglegType dogleg_type) { |
| 152 | switch (dogleg_type) { |
| 153 | CASESTR(TRADITIONAL_DOGLEG); |
| 154 | CASESTR(SUBSPACE_DOGLEG); |
| 155 | default: |
| 156 | return "UNKNOWN"; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | bool StringToDoglegType(string value, DoglegType* type) { |
| 161 | UpperCase(&value); |
| 162 | STRENUM(TRADITIONAL_DOGLEG); |
| 163 | STRENUM(SUBSPACE_DOGLEG); |
| 164 | return false; |
| 165 | } |
| 166 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 167 | const char* SolverTerminationTypeToString( |
| 168 | SolverTerminationType termination_type) { |
| 169 | switch (termination_type) { |
| 170 | CASESTR(NO_CONVERGENCE); |
| 171 | CASESTR(FUNCTION_TOLERANCE); |
| 172 | CASESTR(GRADIENT_TOLERANCE); |
| 173 | CASESTR(PARAMETER_TOLERANCE); |
| 174 | CASESTR(NUMERICAL_FAILURE); |
| 175 | CASESTR(USER_ABORT); |
| 176 | CASESTR(USER_SUCCESS); |
| 177 | CASESTR(DID_NOT_RUN); |
| 178 | default: |
| 179 | return "UNKNOWN"; |
| 180 | } |
| 181 | } |
| 182 | |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 183 | const char* LinearSolverTerminationTypeToString( |
| 184 | LinearSolverTerminationType termination_type) { |
| 185 | switch (termination_type) { |
| 186 | CASESTR(TOLERANCE); |
| 187 | CASESTR(MAX_ITERATIONS); |
| 188 | CASESTR(STAGNATION); |
| 189 | CASESTR(FAILURE); |
Sameer Agarwal | fa01519 | 2012-06-11 14:21:42 -0700 | [diff] [blame] | 190 | default: |
| 191 | return "UNKNOWN"; |
| 192 | } |
| 193 | } |
| 194 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 195 | #undef CASESTR |
Sameer Agarwal | cbae856 | 2012-09-02 13:50:43 -0700 | [diff] [blame] | 196 | #undef STRENUM |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 197 | |
| 198 | bool IsSchurType(LinearSolverType type) { |
| 199 | return ((type == SPARSE_SCHUR) || |
| 200 | (type == DENSE_SCHUR) || |
| 201 | (type == ITERATIVE_SCHUR)); |
| 202 | } |
| 203 | |
Sameer Agarwal | 14ee795 | 2012-09-06 11:05:32 -0700 | [diff] [blame] | 204 | bool IsSparseLinearAlgebraLibraryTypeAvailable( |
| 205 | SparseLinearAlgebraLibraryType type) { |
| 206 | if (type == SUITE_SPARSE) { |
| 207 | #ifdef CERES_NO_SUITESPARSE |
| 208 | return false; |
| 209 | #else |
| 210 | return true; |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | if (type == CX_SPARSE) { |
| 215 | #ifdef CERES_NO_CXSPARSE |
| 216 | return false; |
| 217 | #else |
| 218 | return true; |
| 219 | #endif |
| 220 | } |
| 221 | |
| 222 | LOG(WARNING) << "Unknown sparse linear algebra library " << type; |
| 223 | return false; |
| 224 | } |
| 225 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 226 | } // namespace ceres |