AbstractOptimizer.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief AbstractOptimizer
6  *
7  *
8  *
9  * \author T.Voss, T. Glasmachers, O.Krause
10  * \date 2010-2011
11  *
12  *
13  * \par Copyright 1995-2017 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://shark-ml.org/>
18  *
19  * Shark is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU Lesser General Public License as published
21  * by the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * Shark is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 //===========================================================================
34 #ifndef SHARK_OBJECTIVEFUNCTIONS_ABSTRACTOPTIMIZER_H
35 #define SHARK_OBJECTIVEFUNCTIONS_ABSTRACTOPTIMIZER_H
36 
38 
39 namespace shark {
40 
41 /// \brief An optimizer that optimizes general objective functions
42 ///
43 /// After construction and configurationg the optimizer, init() is called with the objective function
44 /// to be used. After that step() can be called until the required solution is found. The solution can be queried
45 /// using solution(). The type of the solution depends on the optimisation problem at hand.
46 /// It is allowed to add constrains on the features the objective function needs to offer
47 ///
48 /// These are:
49 /// - REQUIRES_VALUE: The function is evaluated to use the optimizer and
50 /// the HAS_VALUE-flag must be set
51 /// - REQUIRES_FIRST_DERIVATIVE: The first derivative needs to be evaluated and
52 /// - HAS_FIRST_DERIVATIVE must be set
53 /// - REQUIRES_SECOND_DERIVATIVE: The second derivative needs to be evaluated and
54 /// - HAS_SECOND_DERIVATIVE must be set
55 /// - CAN_SOLVE_CONSTRAINED: The optimizer can solve functions which are constrained and
56 /// where the IS_CONSTRAINED_FEATURE is set.
57 /// - REQUIRES_CLOSEST_FEASIBLE: If the function is constrained, it must offer a way to
58 /// construct the closest feasible point and
59 /// - CAN_PROVIDE_CLOSEST_FEASIBLE must be set
60 ///
61 /// Also when init() is called as offered by the AbstractOptimizer interface, the function
62 /// is required to have the CAN_PROPOSE_STARTING_POINT flag.
63 ///
64 /// \tparam PointType The type of search space the optimizer works upon.
65 /// \tparam ResultT The objective space the optimizer works upon.
66 /// \tparam SolutionTypeT The type of the final solution.
67 template <typename PointType, typename ResultT, typename SolutionTypeT>
68 class AbstractOptimizer : public INameable, public ISerializable {
69 public:
70  typedef PointType SearchPointType;
71  typedef ResultT ResultType;
72  typedef SolutionTypeT SolutionType;
74 
75  /// \brief Models features that the optimizer requires from the objective function.
76  /// \sa AbstractObjectiveFunction
77  enum Feature {
83  };
84 
86 
87  bool requiresValue()const{
88  return features()& REQUIRES_VALUE;
89  }
90 
93  }
96  }
97  bool canSolveConstrained()const{
99  }
102  }
103 
104  virtual ~AbstractOptimizer() {}
105 
106  /// \brief Returns the number of points this method requires for initialisation
107  ///
108  /// The number of points supplied is allowed to be smaller, however in this case
109  /// the optimizer will resort to techniques generating additional points if needed.
110  virtual std::size_t numInitPoints() const = 0;
111 
112  /// \brief Initialize the optimizer for the supplied objective function.
113  ///
114  /// Be aware that function.init() has to be called before calling this function!
115  /// This function will initialize the algorithm with a number of points proposed
116  /// by the function. Note that this must fail if the function can not propose starting point(s).
117  ///
118  /// \param [in] function The objective function to initialize for.
119  virtual void init( ObjectiveFunctionType const& function ){
120  SHARK_RUNTIME_CHECK(function.canProposeStartingPoint(), "Objective function does not propose a starting point");
121  std::vector<SearchPointType> initPoints(numInitPoints());
122  for(SearchPointType& point: initPoints){
123  point = function.proposeStartingPoint();
124  }
125  init(function,initPoints);
126  }
127 
128 
129  /// \brief Initialize the optimizer for the supplied objective function using a set of initialisation points
130  ///
131  /// Most single objective algorithms only require a single point. However multi-objective algorithms
132  /// need a set of initialisation points. The number of points required should be numInitPoints().
133  /// Otherwise the algorithm might use heuristics to generate additional points if needed.
134  ///
135  /// Be aware that function.init() has to be called before calling this function!
136  /// \param [in] function The objective function to initialize for.
137  /// \param [in] initPoints points used for initialisation. Should be at least numInitPoints().
138  virtual void init( ObjectiveFunctionType const& function, std::vector<SearchPointType> const& initPoints ) = 0;
139 
140  /// \brief Carry out one step of the optimizer for the supplied objective function.
141  /// \param [in] function The objective function to initialize for.
142  virtual void step( ObjectiveFunctionType const& function ) = 0;
143 
144  /// \brief Accesses the best solution obtained so far.
145  /// \return An immutable reference to the best solution obtained so far.
146  virtual SolutionType const& solution() const = 0; //mt_hint: try accessing this thing via solution().point and solution().value..
147 
148 protected:
149  /// \brief Convenience function that checks whether the features of the supplied objective function match with the required features of the optimizer.
150  /// \param [in] objectiveFunction The function to match with.
151  /// \throws shark::Exception
152  void checkFeatures (ObjectiveFunctionType const& objectiveFunction){
153  //test whether the function can be evaluated
154  SHARK_RUNTIME_CHECK(!requiresValue() || objectiveFunction.hasValue(), name()+" Requires value of objective function");
155  //test first derivative
156  SHARK_RUNTIME_CHECK(!requiresFirstDerivative() || objectiveFunction.hasFirstDerivative(), name()+" Requires first derivative of objective function");
157  //test second derivative
158  SHARK_RUNTIME_CHECK(!requiresSecondDerivative() || objectiveFunction.hasSecondDerivative(), name()+" Requires second derivative of objective function");
159  //test for constraints
160  if(objectiveFunction.isConstrained()){
161  SHARK_RUNTIME_CHECK(canSolveConstrained(), name()+" Can not solve constrained problems");
163  !requiresClosestFeasible() || objectiveFunction.canProvideClosestFeasible(),
164  name()+" Requires closest feasible solution for solving constrained problems"
165  );
166  }
167  }
168 };
169 
170 }
171 
172 #endif // SHARK_CORE_ABSTRACTOPTIMIZER_H