ocra-recipes
Doxygen documentation for the ocra-recipes repository
FunctionHelpers.h
Go to the documentation of this file.
1 #ifndef _OCRA_FUNCTION_HELPERS_H_
2 #define _OCRA_FUNCTION_HELPERS_H_
3 
4 #include "ocra/optim/Objective.h"
6 
7 #include <boost/shared_ptr.hpp>
8 #include <stdexcept>
9 
10 namespace ocra
11 {
12  // --- OBJECTIVE ----------------------------------------------
13 
14  template<class T>
16  {
17  public:
19  : function()
20  , objective()
21  {
22  }
23 
24  ObjectivePtr(T* f, double weight=1.)
25  : function(f)
26  , objective(new Objective<T>(f, weight))
27  {
28  }
29 
30  operator Objective<T>&() { return getObjective(); }
31  operator const Objective<T>&() const { return getObjective(); }
32 
33  operator T&() { return getFunction(); }
34  operator const T&() const { return getFunction(); }
35 
36  void set(T* f, double weight=1.)
37  {
38  function.reset(f);
39  objective.reset(new Objective<T>(f, weight));
40  }
41 
43  {
44  if(!function)
45  throw std::runtime_error("[ObjectivePtr::getFunction] objective was not set");
46  return *function;
47  }
48 
49  const T& getFunction() const
50  {
51  if(!function)
52  throw std::runtime_error("[ObjectivePtr::getFunction] objective was not set");
53  return *function;
54  }
55 
57  {
58  if(!objective)
59  throw std::runtime_error("[ObjectivePtr::getFunction] objective was not set");
60  return *objective;
61  }
62 
63  const Objective<T>& getObjective() const
64  {
65  if(!objective)
66  throw std::runtime_error("[ObjectivePtr::getFunction] objective was not set");
67  return *objective;
68  }
69 
70  private:
71  boost::shared_ptr<T> function;
72  boost::shared_ptr<Objective<T> > objective;
73  };
74 
75 
76  // --- EQUAL ZERO CONSTRAINT ----------------------------------
77 
78  template<class T>
80  {
81  public:
83  : function()
84  , constraint()
85  {
86  }
87 
89  : function(f)
90  , constraint(new Constraint<T>(f, true))
91  {
92  }
93 
94  operator Constraint<T>&() { return getConstraint(); }
95  operator const Constraint<T>&() const { return getConstraint(); }
96 
97  operator T&() { return getFunction(); }
98  operator const T&() const { return getFunction(); }
99 
100  void set(T* f)
101  {
102  function.reset(f);
103  constraint.reset(new Constraint<T>(f, true));
104  }
105 
107  {
108  if(!function)
109  throw std::runtime_error("[EqualZeroConstraintPtr::getFunction] constraint was not set");
110  return *function;
111  }
112 
113  const T& getFunction() const
114  {
115  if(!function)
116  throw std::runtime_error("[EqualZeroConstraintPtr::getFunction] constraint was not set");
117  return *function;
118  }
119 
121  {
122  if(!constraint)
123  throw std::runtime_error("[EqualZeroConstraintPtr::getFunction] constraint was not set");
124  return *constraint;
125  }
126 
128  {
129  if(!constraint)
130  throw std::runtime_error("[EqualZeroConstraintPtr::getFunction] constraint was not set");
131  return *constraint;
132  }
133 
134  private:
135  boost::shared_ptr<T> function;
136  boost::shared_ptr<Constraint<T> > constraint;
137  };
138 
139 
140  // --- LOWER THAN ZERO CONSTRAINT -----------------------------
141 
142  template<class T>
144  {
145  public:
147  : function()
148  , constraint()
149  {
150  }
151 
153  : function(f)
154  , constraint(new Constraint<T>(f, false))
155  {
156  }
157 
158  operator Constraint<T>&() { return getConstraint(); }
159  operator const Constraint<T>&() const { return getConstraint(); }
160 
161  operator T&() { return getFunction(); }
162  operator const T&() const { return getFunction(); }
163 
164  void set(T* f)
165  {
166  function.reset(f);
167  constraint.reset(new Constraint<T>(f, false));
168  }
169 
171  {
172  if(!function)
173  throw std::runtime_error("[LessThanZeroConstraintPtr::getFunction] constraint was not set");
174  return *function;
175  }
176 
177  const T& getFunction() const
178  {
179  if(!function)
180  throw std::runtime_error("[LessThanZeroConstraintPtr::getFunction] constraint was not set");
181  return *function;
182  }
183 
185  {
186  if(!constraint)
187  throw std::runtime_error("[LessThanZeroConstraintPtr::getFunction] constraint was not set");
188  return *constraint;
189  }
190 
192  {
193  if(!constraint)
194  throw std::runtime_error("[LessThanZeroConstraintPtr::getFunction] constraint was not set");
195  return *constraint;
196  }
197 
198  private:
199  boost::shared_ptr<T> function;
200  boost::shared_ptr<Constraint<T> > constraint;
201  };
202 
203 
204  // --- GREATER THAN ZERO CONSTRAINT ---------------------------
205 
206  template<class T>
208  {
209  public:
211  : function()
212  , constraint()
213  {
214  }
215 
217  : function(f)
218  , constraint(new Constraint<T>(f))
219  {
220  }
221 
222  operator Constraint<T>&() { return getConstraint(); }
223  operator const Constraint<T>&() const { return getConstraint(); }
224 
225  operator T&() { return getFunction(); }
226  operator const T&() const { return getFunction(); }
227 
228  void set(T* f)
229  {
230  function.reset(f);
231  constraint.reset(new Constraint<T>(f));
232  }
233 
235  {
236  if(!function)
237  throw std::runtime_error("[GreaterThanZeroConstraintPtr::getFunction] constraint was not set");
238  return *function;
239  }
240 
241  const T& getFunction() const
242  {
243  if(!function)
244  throw std::runtime_error("[GreaterThanZeroConstraintPtr::getFunction] constraint was not set");
245  return *function;
246  }
247 
249  {
250  if(!constraint)
251  throw std::runtime_error("[GreaterThanZeroConstraintPtr::getFunction] constraint was not set");
252  return *constraint;
253  }
254 
256  {
257  if(!constraint)
258  throw std::runtime_error("[GreaterThanZeroConstraintPtr::getFunction] constraint was not set");
259  return *constraint;
260  }
261 
262  private:
263  boost::shared_ptr<T> function;
264  boost::shared_ptr<Constraint<T> > constraint;
265  };
266 }
267 
268 #endif
269 
270 // cmake:sourcegroup=utils
Objective class.
Definition: Objective.h:44
const Objective< T > & getObjective() const
const T & getFunction() const
Constraint< T > & getConstraint()
const Constraint< T > & getConstraint() const
Objective< T > & getObjective()
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
ObjectivePtr(T *f, double weight=1.)
Constraint class.
Definition: Constraint.h:100
const T & getFunction() const
Declaration file of the Constraint class.
const Constraint< T > & getConstraint() const
Constraint< T > & getConstraint()
Declaration file of the Objective class.
const Constraint< T > & getConstraint() const