JSBSim Flight Dynamics Model  1.0 (02 March 2017)
An Open Source Flight Dynamics and Control Software Library in C++
FGNelderMead.h
1 /*
2  * FGNelderMead.h
3  * Copyright (C) James Goppert 2010 <james.goppert@gmail.com>
4  *
5  * FGNelderMead.h is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * FGNelderMead.h is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef JSBSim_FGNelderMead_H
20 #define JSBSim_FGNelderMead_H
21 
22 #include <vector>
23 #include <limits>
24 #include <cstddef>
25 
26 namespace JSBSim
27 {
28 
30 {
31 public:
32  class Function
33  {
34  public:
35  virtual double eval(const std::vector<double> & v) = 0;
36  virtual ~Function() {};
37  };
38  class Callback
39  {
40  public:
41  virtual void eval(const std::vector<double> & v) = 0;
42  virtual ~Callback() {};
43  };
44 
45  FGNelderMead(Function * f, const std::vector<double> & initialGuess,
46  const std::vector<double> & lowerBound,
47  const std::vector<double> & upperBound,
48  const std::vector<double> & initialStepSize, int iterMax=2000,
49  double rtol=std::numeric_limits<float>::epsilon(),
50  double abstol=std::numeric_limits<float>::epsilon(),
51  double speed = 2.0,
52  double randomization=0.1,
53  bool showConvergeStatus=true,bool showSimplex=false,
54  bool pause=false,
55  Callback * callback=NULL);
56  std::vector<double> getSolution();
57 
58  void update();
59  int status();
60 
61 private:
62  // attributes
63  Function * m_f;
64  Callback * m_callback;
65  double m_randomization;
66  const std::vector<double> & m_lowerBound;
67  const std::vector<double> & m_upperBound;
68  size_t m_nDim, m_nVert;
69  int m_iMax, m_iNextMax, m_iMin;
70  std::vector< std::vector<double> > m_simplex;
71  std::vector<double> m_cost;
72  std::vector<double> m_elemSum;
73  int m_status;
74  const std::vector<double> & initialGuess;
75  const std::vector<double> & initialStepSize;
76  int iterMax, iter;
77  double rtol,abstol,speed;
78  bool showConvergeStatus, showSimplex, pause;
79  double rtolI, minCostPrevResize, minCost, minCostPrev,
80  maxCost, nextMaxCost;
81 
82  // methods
83  double getRandomFactor();
84  double tryStretch(double factor);
85  void contract();
86  void constructSimplex(const std::vector<double> & guess, const std::vector<double> & stepSize);
87  void boundVertex(std::vector<double> & vertex,
88  const std::vector<double> & upperBound,
89  const std::vector<double> & lowerBound);
90  double eval(const std::vector<double> & vertex, bool check = false);
91 };
92 
93 } // JSBSim
94 
95 #endif
96 
97 // vim:ts=4:sw=4