OptimizationMethod.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_OptimizationMethod_H
21 #define MIRTK_OptimizationMethod_H
22 
23 #include "mirtk/String.h"
24 
25 #include <functional>
26 
27 
28 namespace mirtk {
29 
30 
31 // -----------------------------------------------------------------------------
32 /// Enumeration of available optimization methods
34 {
35  OM_DownhillDescent,
36  OM_GradientDescent,
37  OM_GradientDescentConstrained,
38  OM_SteepestGradientDescent,
39  OM_ConjugateGradientDescent,
40  OM_ClosedForm,
41  OM_LBFGS,
42  OM_LineSearch,
43  OM_EulerMethod, ///< Explicit Euler method for deformable surface models
44  OM_EulerMethodWithDamping, ///< Explicit Euler method with momentum for deformable surface models
45  OM_EulerMethodWithMomentum ///< Explicit Euler method with momentum for deformable surface models
46 };
47 
48 // -----------------------------------------------------------------------------
49 template <>
50 inline string ToString(const OptimizationMethod &m, int w, char c, bool left)
51 {
52  const char *str;
53  switch (m) {
54  case OM_DownhillDescent: str = "DownhillDescent"; break;
55  case OM_GradientDescent: str = "GradientDescent"; break;
56  case OM_GradientDescentConstrained: str = "GradientDescentConstrained"; break;
57  case OM_SteepestGradientDescent: str = "SteepestGradientDescent"; break;
58  case OM_ConjugateGradientDescent: str = "ConjugateGradientDescent"; break;
59  case OM_ClosedForm: str = "ClosedForm"; break;
60  case OM_LBFGS: str = "LBFGS"; break;
61  case OM_LineSearch: str = "LineSearch"; break;
62  case OM_EulerMethod: str = "EulerMethod"; break;
63  case OM_EulerMethodWithDamping: str = "EulerMethodWithDamping"; break;
64  case OM_EulerMethodWithMomentum: str = "EulerMethodWithMomentum"; break;
65  default: str = "Unknown"; break;
66  }
67  return ToString(str, w, c, left);
68 }
69 
70 // -----------------------------------------------------------------------------
71 template <>
72 inline bool FromString(const char *str, OptimizationMethod &m)
73 {
74  if (strcmp(str, "DownhillDescent") == 0 ||
75  strcmp(str, "Downhill") == 0) {
76  m = OM_DownhillDescent;
77  } else if (strcmp(str, "GradientDescent") == 0 ||
78  strcmp(str, "GD") == 0) {
79  m = OM_GradientDescent;
80  } else if (strcmp(str, "ConstrainedGradientDescent") == 0 ||
81  strcmp(str, "GradientDescentConstrained") == 0) {
82  m = OM_GradientDescentConstrained;
83  } else if (strcmp(str, "SteepestGradientDescent") == 0 ||
84  strcmp(str, "SteepestGradient") == 0 ||
85  strcmp(str, "SGD") == 0) {
86  m = OM_SteepestGradientDescent;
87  } else if (strcmp(str, "ConjugateGradientDescent") == 0 ||
88  strcmp(str, "ConjugateGradient") == 0 ||
89  strcmp(str, "CGD") == 0) {
90  m = OM_ConjugateGradientDescent;
91  } else if (strcmp(str, "ClosedForm") == 0) {
92  m = OM_ClosedForm;
93  } else if (strcmp(str, "LBFGS") == 0) {
94  m = OM_LBFGS;
95  } else if (strcmp(str, "LineSearch") == 0) {
96  m = OM_LineSearch;
97  } else if (strcmp(str, "EulerMethod") == 0) {
98  m = OM_EulerMethod;
99  } else if (strcmp(str, "EulerMethodWithDamping") == 0) {
101  } else if (strcmp(str, "EulerMethodWithMomentum") == 0) {
103  } else {
104  return false;
105  }
106  return true;
107 }
108 
109 } // namespace mirtk
110 
111 
112 namespace std {
113 
114 /// Compute hash value from OptimizationMethod enumeration value
115 template<>
116 struct hash<mirtk::OptimizationMethod> {
117  size_t operator()(const mirtk::OptimizationMethod &enum_value) const {
118  return std::hash<int>()(enum_value);
119  }
120 };
121 
122 
123 } // namespace std
124 
125 
126 #endif // MIRTK_OptimizationMethod_H
STL namespace.
Definition: IOConfig.h:41
Explicit Euler method for deformable surface models.
string ToString(const EnergyMeasure &value, int w, char c, bool left)
Convert energy measure enumeration value to string.
bool FromString(const char *str, EnergyMeasure &value)
Convert energy measure string to enumeration value.
Explicit Euler method with momentum for deformable surface models.
OptimizationMethod
Enumeration of available optimization methods.
Explicit Euler method with momentum for deformable surface models.