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-2015 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://image.diku.dk/shark/>
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 /**
42 * \brief An optimizer that optimizes general objective functions
43 *
44 * After construction and configurationg the optimizer, init() is called with the objective function
45 * to be used. After that step() can be called until the required solution is found. The solution can be queried
46 * using solution(). The type of the solution depends on the optimisation problem at hand.
47 * It is allowed to add constrains on the features the objective function needs to offer
48 *
49 * These are:
50 * - REQUIRES_VALUE: The function is evaluated to use the optimizer and
51 * the HAS_VALUE-flag must be set
52 * - REQUIRES_FIRST_DERIVATIVE: The first derivative needs to be evaluated and
53 * - HAS_FIRST_DERIVATIVE must be set
54 * - REQUIRES_SECOND_DERIVATIVE: The second derivative needs to be evaluated and
55 * - HAS_SECOND_DERIVATIVE must be set
56 * - CAN_SOLVE_CONSTRAINED: The optimizer can solve functions which are constrained and
57 * where the IS_CONSTRAINED_FEATURE is set.
58 * - REQUIRES_CLOSEST_FEASIBLE: If the function is constrained, it must offer a way to
59 * construct the closest feasible point and
60 * - CAN_PROVIDE_CLOSEST_FEASIBLE must be set
61 *
62 * Also when init() is called as offered by the AbstractOptimizer interface, the function
63 * is required to have the CAN_PROPOSE_STARTING_POINT flag.
64 *
65 * \tparam PointType The type of search space the optimizer works upon.
66 * \tparam ResultT The objective space the optimizer works upon.
67 * \tparam SolutionTypeT The type of the final solution.
68 */
69 template <typename PointType, typename ResultT, typename SolutionTypeT>
70 class AbstractOptimizer : public INameable, public ISerializable {
71 public:
72  typedef PointType SearchPointType;
73  typedef ResultT ResultType;
74  typedef SolutionTypeT SolutionType;
76 
77  /**
78  * \brief Models features that the optimizer requires from the objective function.
79  * \sa AbstractObjectiveFunction
80  */
81  enum Feature {
87  };
88 
90 
91  bool requiresValue()const{
92  return features()& REQUIRES_VALUE;
93  }
94 
97  }
100  }
101  bool canSolveConstrained()const{
103  }
106  }
107 
108  /**
109  * \brief Empty virtual d'tor.
110  */
111  virtual ~AbstractOptimizer() {}
112 
113  /**
114  * \brief Initialize the optimizer for the supplied objective function.
115  *
116  * This will also call function->init() to reset the internal state of the function,
117  * for example the evaluation counter
118  * \param [in] function The objective function to initialize for.
119  */
120  virtual void init( ObjectiveFunctionType& function ) = 0;
121 
122  /**
123  * \brief Carry out one step of the optimizer for the supplied objective function.
124  * \param [in] function The objective function to initialize for.
125  */
126  virtual void step( ObjectiveFunctionType const& function ) = 0;
127 
128  /**
129  * \brief Accesses the best solution obtained so far.
130  * \returns An immutable reference to the best solution obtained so far.
131  */
132  virtual SolutionType const& solution() const = 0; //mt_hint: try accessing this thing via solution().point and solution().value..
133 
134 protected:
135  /**
136  * \brief Convenience function that checks whether the features of the supplied objective function match with the required features of the optimizer.
137  * \param [in] objectiveFunction The function to match with.
138  * \throws shark::Exception
139  */
140  void checkFeatures (ObjectiveFunctionType const& objectiveFunction){
141  //test first derivative
144  )throw SHARKEXCEPTION("[ "+name()+" ] requires first derivative");
145  //test second derivative
148  )throw SHARKEXCEPTION("[ "+name()+" ] requires second derivative");
149 
150  //test whether the function can be evaluated
151  if( (m_features & REQUIRES_VALUE) &
152  !(objectiveFunction.features() & ObjectiveFunctionType::HAS_VALUE)
153  )throw SHARKEXCEPTION("[ "+name()+" ] requires the value of the function");
154 
155  //test for constrains
158  )throw SHARKEXCEPTION("[ "+name()+" ] can not solve constrained functions");
159 
160  //test for closest feasible in constrained functions
161  if( (objectiveFunction.features() & ObjectiveFunctionType::IS_CONSTRAINED_FEATURE) &
164  )throw SHARKEXCEPTION("[ "+name()+" ] requires closest feasible for constrained functions");
165  }
166 };
167 
168 }
169 
170 #endif // SHARK_CORE_ABSTRACTOPTIMIZER_H