NLoptSolver.cxx
1 // Copyright (C) 2010 INRIA
2 // Author(s): Marc Fragu
3 //
4 // This file is part of the linear-algebra library Seldon,
5 // http://seldon.sourceforge.net/.
6 //
7 // Seldon is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 2.1 of the License, or (at your option)
10 // any later version.
11 //
12 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with Seldon. If not, see http://www.gnu.org/licenses/.
19 
20 
21 #ifndef SELDON_COMPUTATION_OPTIMIZATION_NLOPTSOLVER_CXX
22 #define SELDON_COMPUTATION_OPTIMIZATION_NLOPTSOLVER_CXX
23 
24 
25 #include "NLoptSolver.hxx"
26 
27 
28 namespace Seldon
29 {
30 
31 
34  {
35  }
36 
37 
40  {
41  }
42 
43 
45 
72  void NLoptSolver::Initialize(int Nparameter, string algorithm,
73  double parameter_tolerance,
74  double cost_function_tolerance,
75  int Niteration_max)
76  {
77  map<string, nlopt::algorithm> algorithm_map;
78  map<string, nlopt::algorithm>::iterator it;
79  algorithm_map["GN_DIRECT"] = nlopt::GN_DIRECT;
80  algorithm_map["GN_DIRECT_L"] = nlopt::GN_DIRECT_L;
81  algorithm_map["GN_DIRECT_L_RAND"] = nlopt::GN_DIRECT_L_RAND;
82  algorithm_map["GN_DIRECT_NOSCAL"] = nlopt::GN_DIRECT_NOSCAL;
83  algorithm_map["GN_DIRECT_L_NOSCAL"] = nlopt::GN_DIRECT_L_NOSCAL;
84  algorithm_map["GN_DIRECT_L_RAND_NOSCAL"]
85  = nlopt::GN_DIRECT_L_RAND_NOSCAL;
86  algorithm_map["GN_ORIG_DIRECT"] = nlopt::GN_ORIG_DIRECT;
87  algorithm_map["GN_ORIG_DIRECT_L"] = nlopt::GN_ORIG_DIRECT_L;
88  algorithm_map["GD_STOGO"] = nlopt::GD_STOGO;
89  algorithm_map["GD_STOGO_RAND"] = nlopt::GD_STOGO_RAND;
90  algorithm_map["LD_LBFGS_NOCEDAL"] = nlopt::LD_LBFGS_NOCEDAL;
91  algorithm_map["LD_LBFGS"] = nlopt::LD_LBFGS;
92  algorithm_map["LN_PRAXIS"] = nlopt::LN_PRAXIS;
93  algorithm_map["LD_VAR1"] = nlopt::LD_VAR1;
94  algorithm_map["LD_VAR2"] = nlopt::LD_VAR2;
95  algorithm_map["LD_TNEWTON"] = nlopt::LD_TNEWTON;
96  algorithm_map["LD_TNEWTON_RESTART"] = nlopt::LD_TNEWTON_RESTART;
97  algorithm_map["LD_TNEWTON_PRECOND"] = nlopt::LD_TNEWTON_PRECOND;
98  algorithm_map["LD_TNEWTON_PRECOND_RESTART"]
99  = nlopt::LD_TNEWTON_PRECOND_RESTART;
100  algorithm_map["GN_CRS2_LM"] = nlopt::GN_CRS2_LM;
101  algorithm_map["GN_MLSL"] = nlopt::GN_MLSL;
102  algorithm_map["GD_MLSL"] = nlopt::GD_MLSL;
103  algorithm_map["GN_MLSL_LDS"] = nlopt::GN_MLSL_LDS;
104  algorithm_map["GD_MLSL_LDS"] = nlopt::GD_MLSL_LDS;
105  algorithm_map["LD_MMA"] = nlopt::LD_MMA;
106  algorithm_map["LN_COBYLA"] = nlopt::LN_COBYLA;
107  algorithm_map["LN_NEWUOA"] = nlopt::LN_NEWUOA;
108  algorithm_map["LN_NEWUOA_BOUND"] = nlopt::LN_NEWUOA_BOUND;
109  algorithm_map["LN_NELDERMEAD"] = nlopt::LN_NELDERMEAD;
110  algorithm_map["LN_SBPLX"] = nlopt::LN_SBPLX;
111  algorithm_map["LN_AUGLAG"] = nlopt::LN_AUGLAG;
112  algorithm_map["LD_AUGLAG"] = nlopt::LD_AUGLAG;
113  algorithm_map["LN_AUGLAG_EQ"] = nlopt::LN_AUGLAG_EQ;
114  algorithm_map["LD_AUGLAG_EQ"] = nlopt::LD_AUGLAG_EQ;
115  algorithm_map["LN_BOBYQA"] = nlopt::LN_BOBYQA;
116  algorithm_map["GN_ISRES"] = nlopt::GN_ISRES;
117  algorithm_map["AUGLAG"] = nlopt::AUGLAG;
118  algorithm_map["AUGLAG_EQ"] = nlopt::AUGLAG_EQ;
119  algorithm_map["G_MLSL"] = nlopt::G_MLSL;
120  algorithm_map["G_MLSL_LDS"] = nlopt::G_MLSL_LDS;
121  algorithm_map["LD_SLSQP"] = nlopt::LD_SLSQP;
122 
123  it = algorithm_map.find(algorithm);
124  if (it == algorithm_map.end())
125  WrongArgument("void NLoptSolver::Initialize(int, string, double)",
126  "Unknown algorithm. Implemented algorithms are:"
127  " GN_DIRECT, "
128  "GN_DIRECT_L, GN_DIRECT_L_RAND, "
129  "GN_DIRECT_NOSCAL, "
130  "GN_DIRECT_L_NOSCAL, GN_DIRECT_L_RAND_NOSCAL,"
131  " GN_ORIG_DIRECT, "
132  "GN_ORIG_DIRECT_L, GD_STOGO, GD_STOGO_RAND, "
133  "LD_LBFGS_NOCEDAL, LD_LBFGS, LN_PRAXIS, "
134  "LD_VAR1, LD_VAR2, LD_TNEWTON, "
135  "LD_TNEWTON_RESTART, LD_TNEWTON_PRECOND, "
136  "LD_TNEWTON_PRECOND_RESTART, GN_CRS2_LM, "
137  "GN_MLSL,"
138  "GD_MLSL, GN_MLSL_LDS, GD_MLSL_LDS, "
139  "LD_MMA, LN_COBYLA, LN_NEWUOA, "
140  "LN_NEWUOA_BOUND, LN_NELDERMEAD, "
141  "LN_SBPLX, LN_AUGLAG, LD_AUGLAG, "
142  "LN_AUGLAG_EQ, LD_AUGLAG_EQ, LN_BOBYQA, "
143  "GN_ISRES, AUGLAG, AUGLAG_EQ,"
144  "G_MLSL, G_MLSL_LDS, LD_SLSQP, NUM_ALGORITHMS");
145  else
146  algorithm_ = it->second;
147 
148  opt_ = nlopt::SeldonOpt(algorithm_, Nparameter);
149 
150  parameter_tolerance_ = parameter_tolerance;
151 
152  cost_function_tolerance_ = cost_function_tolerance;
153 
154  Niteration_max_ = Niteration_max;
155  }
156 
157 
159 
163  {
164  if (lower_bound.GetSize() != 0)
165  opt_.set_lower_bounds(lower_bound);
166  }
167 
168 
170 
174  {
175  if (upper_bound.GetSize() != 0)
176  opt_.set_upper_bounds(upper_bound);
177  }
178 
179 
181 
189  void NLoptSolver::SetParameterTolerance(double tolerance)
190  {
191  parameter_tolerance_ = tolerance;
192  }
193 
194 
196 
205  {
206  cost_function_tolerance_ = tolerance;
207  }
208 
209 
211 
215  void NLoptSolver::SetNiterationMax(int Niteration_max)
216  {
217  Niteration_max_ = Niteration_max;
218  }
219 
220 
222 
228  void NLoptSolver::GetParameterTolerance(double& tolerance) const
229  {
230  tolerance = parameter_tolerance_;
231  }
232 
233 
235 
241  void NLoptSolver::GetCostFunctionTolerance(double& tolerance) const
242  {
243  tolerance = cost_function_tolerance_;
244  }
245 
246 
248 
251  void NLoptSolver::GetNiterationMax(int& Niteration_max) const
252  {
253  Niteration_max = Niteration_max_;
254  }
255 
256 
258 
262  {
263  parameter_.Reallocate(parameter.GetM());
264  Copy(parameter, parameter_);
265  }
266 
267 
269 
273  {
274  parameter.Reallocate(parameter_.GetM());
275  Copy(parameter_, parameter);
276  }
277 
278 
280 
293  void NLoptSolver::Optimize(cost_ptr cost, void* argument)
294  {
295  int Nparameter;
296  if (0 == (Nparameter = parameter_.GetM()))
297  throw WrongArgument("NLoptSolver::Optimize()",
298  "The vector of parameters to be optimized"
299  " is empty.");
300 
301  opt_.set_min_objective(cost, argument);
302  opt_.set_xtol_rel(parameter_tolerance_);
303  opt_.set_ftol_rel(cost_function_tolerance_);
304  opt_.set_maxeval(Niteration_max_);
305  nlopt::result result = opt_.optimize(parameter_, cost_);
306 
307  if (result < 0)
308  throw Error("NLoptSolver::Optimize()", "Nlopt failed.");
309  }
310 
311 
313 
317  double NLoptSolver::GetCost() const
318  {
319  return cost_;
320  }
321 
322 
323 } // namespace Seldon.
324 
325 
326 #endif
Seldon::NLoptSolver::GetParameter
void GetParameter(Vector< double > &parameter) const
Gets the parameters.
Definition: NLoptSolver.cxx:272
Seldon::NLoptSolver::GetParameterTolerance
void GetParameterTolerance(double &) const
Gets the relative tolerance on the parameters.
Definition: NLoptSolver.cxx:228
Seldon::NLoptSolver::cost_function_tolerance_
double cost_function_tolerance_
Relative tolerance on the cost function.
Definition: NLoptSolver.hxx:53
Seldon::Vector< double >
Seldon::NLoptSolver::GetCostFunctionTolerance
void GetCostFunctionTolerance(double &) const
Gets the relative tolerance on the cost function.
Definition: NLoptSolver.cxx:241
Seldon::NLoptSolver::SetCostFunctionTolerance
void SetCostFunctionTolerance(double)
Sets the relative tolerance on the cost function.
Definition: NLoptSolver.cxx:204
nlopt::SeldonOpt
Definition: NLopt.hxx:37
Seldon::Error
Definition: Errors.hxx:38
Seldon::NLoptSolver::SetLowerBound
void SetLowerBound(const Vector< double > &)
Sets lower bounds on the parameters.
Definition: NLoptSolver.cxx:162
Seldon::NLoptSolver::SetParameterTolerance
void SetParameterTolerance(double)
Sets the relative tolerance on the parameters.
Definition: NLoptSolver.cxx:189
Seldon::NLoptSolver::~NLoptSolver
~NLoptSolver()
Destructor.
Definition: NLoptSolver.cxx:39
Seldon::NLoptSolver::opt_
nlopt::SeldonOpt opt_
NLopt optimization solver.
Definition: NLoptSolver.hxx:47
Seldon::NLoptSolver::Optimize
void Optimize(cost_ptr cost, void *argument)
Optimization.
Definition: NLoptSolver.cxx:293
Seldon::NLoptSolver::GetNiterationMax
void GetNiterationMax(int &) const
Gets the maximum number of cost function evaluations.
Definition: NLoptSolver.cxx:251
Seldon::NLoptSolver::SetParameter
void SetParameter(const Vector< double > &parameter)
Sets the parameters.
Definition: NLoptSolver.cxx:261
Seldon::NLoptSolver::GetCost
double GetCost() const
Returns the value of the cost function.
Definition: NLoptSolver.cxx:317
Seldon::NLoptSolver::SetUpperBound
void SetUpperBound(const Vector< double > &)
Sets upper bounds on the parameters.
Definition: NLoptSolver.cxx:173
Seldon::WrongArgument
Definition: Errors.hxx:76
Seldon::NLoptSolver::NLoptSolver
NLoptSolver()
Default constructor.
Definition: NLoptSolver.cxx:33
Seldon::NLoptSolver::cost_
double cost_
The value of cost function for given parameter values.
Definition: NLoptSolver.hxx:66
Seldon::NLoptSolver::parameter_tolerance_
double parameter_tolerance_
Relative tolerance on the optimization parameters.
Definition: NLoptSolver.hxx:51
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::NLoptSolver::SetNiterationMax
void SetNiterationMax(int)
Sets the maximum number of cost function evaluations.
Definition: NLoptSolver.cxx:215
Seldon::NLoptSolver::parameter_
Vector< double > parameter_
The vector that stores parameters values. Before optimization, stores the initial parameter vector; a...
Definition: NLoptSolver.hxx:60
Seldon::NLoptSolver::algorithm_
nlopt::algorithm algorithm_
Optimization algorithm.
Definition: NLoptSolver.hxx:49
Seldon::NLoptSolver::Initialize
void Initialize(int Nparameter, string algorithm, double parameter_tolerance=1.e-6, double cost_function_tolerance=1.e-6, int Niteration_max=-1)
Initializations.
Definition: NLoptSolver.cxx:72
Seldon::NLoptSolver::Niteration_max_
int Niteration_max_
Maximum number of function evaluations. It is ignored if it is non-positive.
Definition: NLoptSolver.hxx:56