| pktools 2.6.7 Processing Kernel for geospatial data

OptFactory.h
1/**********************************************************************
2 OptFactory.h: factory class for nlopt::opt (selecting algorithm via string)
3 Copyright (C) 2008-2013 Pieter Kempeneers
4
5 This file is part of pktools
6
7 pktools is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12 pktools is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with pktools. If not, see <http://www.gnu.org/licenses/>.
19***********************************************************************/
20#ifndef _OPTFACTORY_H_
21#define _OPTFACTORY_H_
22
23#include <map>
24
25 class OptFactory
26{
27 private:
28 static void initMap(std::map<std::string, nlopt::algorithm>& m_algMap){
29 //initialize selMap
30 m_algMap["GN_DIRECT"]=nlopt::GN_DIRECT;
31 m_algMap["GN_DIRECT_L"]=nlopt::GN_DIRECT_L;
32 m_algMap["GN_DIRECT_L_RAND"]=nlopt::GN_DIRECT_L_RAND;
33 m_algMap["GN_DIRECT_NOSCAL"]=nlopt::GN_DIRECT_NOSCAL;
34 m_algMap["GN_DIRECT_L_NOSCAL"]=nlopt::GN_DIRECT_L_NOSCAL;
35 m_algMap["GN_DIRECT_L_RAND_NOSCAL"]=nlopt::GN_DIRECT_L_RAND_NOSCAL;
36 m_algMap["GN_ORIG_DIRECT"]=nlopt::GN_ORIG_DIRECT;
37 m_algMap["GN_ORIG_DIRECT_L"]=nlopt::GN_ORIG_DIRECT_L;
38 m_algMap["GD_STOGO"]=nlopt::GD_STOGO;
39 m_algMap["GD_STOGO_RAND"]=nlopt::GD_STOGO_RAND;
40 m_algMap["LD_LBFGS_NOCEDAL"]=nlopt::LD_LBFGS_NOCEDAL;
41 m_algMap["LD_LBFGS"]=nlopt::LD_LBFGS;
42 m_algMap["LN_PRAXIS"]=nlopt::LN_PRAXIS;
43 m_algMap["LD_VAR1"]=nlopt::LD_VAR1;
44 m_algMap["LD_VAR2"]=nlopt::LD_VAR2;
45 m_algMap["LD_TNEWTON"]=nlopt::LD_TNEWTON;
46 m_algMap["LD_TNEWTON_RESTART"]=nlopt::LD_TNEWTON_RESTART;
47 m_algMap["LD_TNEWTON_PRECOND"]=nlopt::LD_TNEWTON_PRECOND;
48 m_algMap["LD_TNEWTON_PRECOND_RESTART"]=nlopt::LD_TNEWTON_PRECOND_RESTART;
49 m_algMap["GN_CRS2_LM"]=nlopt::GN_CRS2_LM;
50 m_algMap["GN_MLSL"]=nlopt::GN_MLSL;
51 m_algMap["GD_MLSL"]=nlopt::GD_MLSL;
52 m_algMap["GN_MLSL_LDS"]=nlopt::GN_MLSL_LDS;
53 m_algMap["GD_MLSL_LDS"]=nlopt::GD_MLSL_LDS;
54 m_algMap["LD_MMA"]=nlopt::LD_MMA;
55 m_algMap["LN_COBYLA"]=nlopt::LN_COBYLA;
56 m_algMap["LN_NEWUOA"]=nlopt::LN_NEWUOA;
57 m_algMap["LN_NEWUOA_BOUND"]=nlopt::LN_NEWUOA_BOUND;
58 m_algMap["LN_NELDERMEAD"]=nlopt::LN_NELDERMEAD;
59 m_algMap["LN_SBPLX"]=nlopt::LN_SBPLX;
60 m_algMap["LN_AUGLAG"]=nlopt::LN_AUGLAG;
61 m_algMap["LD_AUGLAG"]=nlopt::LD_AUGLAG;
62 m_algMap["LN_AUGLAG_EQ"]=nlopt::LN_AUGLAG_EQ;
63 m_algMap["LD_AUGLAG_EQ"]=nlopt::LD_AUGLAG_EQ;
64 m_algMap["LN_BOBYQA"]=nlopt::LN_BOBYQA;
65 m_algMap["GN_ISRES"]=nlopt::GN_ISRES;
66 m_algMap["AUGLAG"]=nlopt::AUGLAG;
67 m_algMap["AUGLAG_EQ"]=nlopt::AUGLAG_EQ;
68 m_algMap["G_MLSL"]=nlopt::G_MLSL;
69 m_algMap["G_MLSL_LDS"]=nlopt::G_MLSL_LDS;
70 m_algMap["LD_SLSQP "]=nlopt::LD_SLSQP;
71 }
72 public:
73 OptFactory(){
74 };
75 ~OptFactory(){};
76 static nlopt::opt getOptimizer(const std::string& algorithmString, unsigned int npar){
77 std::map<std::string, nlopt::algorithm> m_algMap;
78 initMap(m_algMap);
79 switch(m_algMap[algorithmString]){
80 case(nlopt::GN_DIRECT):{
81 nlopt::opt theOptimizer(nlopt::GN_DIRECT,npar);
82 return(theOptimizer);
83 break;
84 }
85 case(nlopt::GN_DIRECT_L):{
86 nlopt::opt theOptimizer(nlopt::GN_DIRECT_L,npar);
87 return(theOptimizer);
88 break;
89 }
90 case(nlopt::GN_DIRECT_L_RAND):{
91 nlopt::opt theOptimizer(nlopt::GN_DIRECT_L_RAND,npar);
92 return(theOptimizer);
93 break;
94 }
95 case(nlopt::GN_DIRECT_NOSCAL):{
96 nlopt::opt theOptimizer(nlopt::GN_DIRECT_NOSCAL,npar);
97 return(theOptimizer);
98 break;
99 }
100 case(nlopt::GN_DIRECT_L_NOSCAL):{
101 nlopt::opt theOptimizer(nlopt::GN_DIRECT_L_NOSCAL,npar);
102 return(theOptimizer);
103 break;
104 }
105 case(nlopt::GN_DIRECT_L_RAND_NOSCAL):{
106 nlopt::opt theOptimizer(nlopt::GN_DIRECT_L_RAND_NOSCAL,npar);
107 return(theOptimizer);
108 break;
109 }
110 case(nlopt::GN_ORIG_DIRECT):{
111 nlopt::opt theOptimizer(nlopt::GN_ORIG_DIRECT,npar);
112 return(theOptimizer);
113 break;
114 }
115 case(nlopt::GN_ORIG_DIRECT_L):{
116 nlopt::opt theOptimizer(nlopt::GN_ORIG_DIRECT_L,npar);
117 return(theOptimizer);
118 break;
119 }
120 case(nlopt::LN_PRAXIS):{
121 nlopt::opt theOptimizer(nlopt::LN_PRAXIS,npar);
122 return(theOptimizer);
123 break;
124 }
125 case(nlopt::GN_CRS2_LM):{
126 nlopt::opt theOptimizer(nlopt::GN_CRS2_LM,npar);
127 return(theOptimizer);
128 break;
129 }
130 case(nlopt::GN_MLSL):{
131 nlopt::opt theOptimizer(nlopt::GN_MLSL,npar);
132 return(theOptimizer);
133 break;
134 }
135 case(nlopt::GN_MLSL_LDS):{
136 nlopt::opt theOptimizer(nlopt::GN_MLSL_LDS,npar);
137 return(theOptimizer);
138 break;
139 }
140 case(nlopt::LN_COBYLA):{
141 nlopt::opt theOptimizer(nlopt::LN_COBYLA,npar);
142 return(theOptimizer);
143 break;
144 }
145 case(nlopt::LN_NEWUOA):{
146 nlopt::opt theOptimizer(nlopt::LN_NEWUOA,npar);
147 return(theOptimizer);
148 break;
149 }
150 case(nlopt::LN_NEWUOA_BOUND):{
151 nlopt::opt theOptimizer(nlopt::LN_NEWUOA_BOUND,npar);
152 return(theOptimizer);
153 break;
154 }
155 case(nlopt::LN_NELDERMEAD):{
156 nlopt::opt theOptimizer(nlopt::LN_NELDERMEAD,npar);
157 return(theOptimizer);
158 break;
159 }
160 case(nlopt::LN_SBPLX):{
161 nlopt::opt theOptimizer(nlopt::LN_SBPLX,npar);
162 return(theOptimizer);
163 break;
164 }
165 case(nlopt::LN_AUGLAG):{
166 nlopt::opt theOptimizer(nlopt::LN_AUGLAG,npar);
167 return(theOptimizer);
168 break;
169 }
170 case(nlopt::LN_AUGLAG_EQ):{
171 nlopt::opt theOptimizer(nlopt::LN_AUGLAG_EQ,npar);
172 return(theOptimizer);
173 break;
174 }
175 case(nlopt::LN_BOBYQA):{
176 nlopt::opt theOptimizer(nlopt::LN_BOBYQA,npar);
177 return(theOptimizer);
178 break;
179 }
180 case(nlopt::GN_ISRES):{
181 nlopt::opt theOptimizer(nlopt::GN_ISRES,npar);
182 return(theOptimizer);
183 break;
184 }
185 case(nlopt::G_MLSL_LDS):
186 case(nlopt::AUGLAG):
187 case(nlopt::AUGLAG_EQ):
188 case(nlopt::G_MLSL):
189 case(nlopt::GD_MLSL):
190 case(nlopt::GD_MLSL_LDS):
191 case(nlopt::GD_STOGO):
192 case(nlopt::GD_STOGO_RAND):
193 case(nlopt::LD_LBFGS_NOCEDAL):
194 case(nlopt::LD_LBFGS):
195 case(nlopt::LD_VAR1):
196 case(nlopt::LD_VAR2):
197 case(nlopt::LD_TNEWTON):
198 case(nlopt::LD_TNEWTON_RESTART):
199 case(nlopt::LD_TNEWTON_PRECOND):
200 case(nlopt::LD_TNEWTON_PRECOND_RESTART):
201 case(nlopt::LD_MMA):
202 case(nlopt::LD_AUGLAG):
203 case(nlopt::LD_AUGLAG_EQ):
204 case(nlopt::LD_SLSQP):
205 default:{
206 std::string errorString="Error: derivative optimization algorithm ";
207 errorString+= algorithmString;
208 errorString+= " not supported, select derivative-free algorithm ([GL]N_*])";
209 throw(errorString);
210 break;
211 }
212 }
213 };
214};
215#endif /* _OPTFACTORY_H_ */
OptFactory
Definition: OptFactory.h:26