Optimo  0.1.0
A C++ header library for optimization
 All Classes Functions Variables Pages
objects.h
1 // Copyright (C) 2013 Victor Fragoso <vfragoso@cs.ucsb.edu>
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following
13 // disclaimer in the documentation and/or other materials provided
14 // with the distribution.
15 //
16 // * Neither the name of the University of California, Santa Barbara nor the
17 // names of its contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 // ARE DISCLAIMED. IN NO EVENT SHALL VICTOR FRAGOSO BE LIABLE FOR ANY DIRECT,
24 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 //
31 
32 #ifndef OPTIMO_CORE_OBJECTS_H_
33 #define OPTIMO_CORE_OBJECTS_H_
34 
35 #include <Eigen/Core>
36 #include <Eigen/SparseCore>
37 #include <vector>
38 
39 #define NO_CONSTRAINTS 1
40 
41 // m: Number of Constraints
42 // n: Number of Variables
43 typedef unsigned int uint;
44 namespace optimo {
46 // Small problems that fit in the Stack (n<16, m<16)
47 
49 
53 template <typename Scalar, uint n>
56  virtual Scalar operator()(const Eigen::Matrix<Scalar, n, 1>& x) const = 0;
57 };
58 
60 
64 template <typename Scalar, uint n>
67  virtual Scalar operator()(const Eigen::Matrix<Scalar, n, 1>& x) const = 0;
68 };
69 
71 
74 template <typename Scalar, uint m, uint n>
75 struct Constraints {
78  operator[](const unsigned int i) = 0;
79 };
80 
82 
87 template <typename Scalar, uint n>
90  virtual void
91  operator()(const Eigen::Matrix<Scalar, n, 1>& x,
92  Eigen::Matrix<Scalar, n, 1>* g) const = 0;
93 };
94 
96 
103 template <typename Scalar, uint n>
106  virtual void
107  operator()(const Eigen::Matrix<Scalar, n, 1>& x,
108  Eigen::Matrix<Scalar, n, n>* h) const = 0;
109 };
110 
112 
114 template <typename Scalar, uint n>
115 struct VoidConstraintFunctor : public ConstraintFunctor<Scalar, n> {
117  virtual Scalar operator()(const Eigen::Matrix<Scalar, n, 1>& x) const {
118  return static_cast<Scalar>(0.0);
119  }
120 };
121 
123 
125 template <typename Scalar, uint m, uint n>
126 class NoConstraints : public Constraints<Scalar, m, n> {
127  public:
130 
131  // Non copyable
132  // Copy constructor
133  explicit NoConstraints(const NoConstraints<Scalar, m, n>& x) = delete;
134 
135  // Operator =
137  operator=(const NoConstraints<Scalar, m, n>& rhs) = delete;
138 
139  // Complying with interface
140  virtual ConstraintFunctor<Scalar, n>& operator[](const unsigned int i) {
141  return *c;
142  }
143 
144  protected:
146 
147  private:
148  static void cleanUp(void);
149 
150  // Constructor
151  NoConstraints(void) {
153  atexit(&cleanUp);
154  }
155 
156  // Destructor
157  ~NoConstraints(void) { }
158 
159  static NoConstraints<Scalar, m, n>* instance;
160 };
161 
162 template <typename Scalar, uint m, uint n>
163 NoConstraints<Scalar, m, n>* NoConstraints<Scalar, m, n>::instance = nullptr;
164 
165 template <typename Scalar, uint m, uint n>
166 VoidConstraintFunctor<Scalar, n>* NoConstraints<Scalar, m, n>::c = nullptr;
167 
168 // Cleanup function
169 template <typename Scalar, uint m, uint n>
170 void NoConstraints<Scalar, m, n>::cleanUp(void) {
171  delete c;
172  c = nullptr;
173 }
174 
175 template <typename Scalar, uint m, uint n>
177  if (!instance) {
178  instance = new NoConstraints<Scalar, m, n>;
179  }
180  return *instance;
181 }
182 
184 
196 template <typename Scalar, uint m, uint n>
197 class Problem {
198  public:
199  const ObjectiveFunctor<Scalar, n>& objective; //< Objective functor
200  const Constraints<Scalar, m, n>& constraints; //< Constraints
201  const GradientFunctor<Scalar, n>& gradient; //< Gradient functor
202  const HessianFunctor<Scalar, n>& hessian; //< Hessian functor
203  const Eigen::Matrix<Scalar, m, n> A; //< Equality constraints matrix
204  const Eigen::Matrix<Scalar, m, 1> b; //< Equality constraints vector
205 
208  const Constraints<Scalar, m, n>& con,
209  const Eigen::Matrix<Scalar, m, n>& Aeq,
210  const Eigen::Matrix<Scalar, m, 1>& beq,
212  const HessianFunctor<Scalar, n>& h) :
213  objective(obj), constraints(con), gradient(g), hessian(h), A(Aeq), b(beq)
214  { }
215 
219  const HessianFunctor<Scalar, n>& h) :
220  objective(obj),
221  constraints(NoConstraints<Scalar, m, n>::getInstance()),
222  gradient(g), hessian(h) { }
223 
224  // Destructor
225  virtual ~Problem(void) { }
226 };
227 } // optimo
228 #endif // OPTIMO_CORE_OBJECTS_H_
virtual void operator()(const Eigen::Matrix< Scalar, n, 1 > &x, Eigen::Matrix< Scalar, n, 1 > *g) const =0
Computes the gradient.
virtual Scalar operator()(const Eigen::Matrix< Scalar, n, 1 > &x) const
Returns 0 always to comply with the ConstraintFunctor interface.
Definition: objects.h:117
Objective functor.
Definition: objects.h:54
Problem(const ObjectiveFunctor< Scalar, n > &obj, const GradientFunctor< Scalar, n > &g, const HessianFunctor< Scalar, n > &h)
Unconstrained problem.
Definition: objects.h:217
Constraints object.
Definition: objects.h:75
Dummy constraint functor for unconstrained problems.
Definition: objects.h:115
Class defining a convex Problem.
Definition: objects.h:197
virtual Scalar operator()(const Eigen::Matrix< Scalar, n, 1 > &x) const =0
Evaluates objective function.
Problem(const ObjectiveFunctor< Scalar, n > &obj, const Constraints< Scalar, m, n > &con, const Eigen::Matrix< Scalar, m, n > &Aeq, const Eigen::Matrix< Scalar, m, 1 > &beq, const GradientFunctor< Scalar, n > &g, const HessianFunctor< Scalar, n > &h)
Constrained problem.
Definition: objects.h:207
virtual ConstraintFunctor< Scalar, n > & operator[](const unsigned int i)=0
Returns a ConstraintFunctor.
virtual ConstraintFunctor< Scalar, n > & operator[](const unsigned int i)
Returns a ConstraintFunctor.
Definition: objects.h:140
virtual void operator()(const Eigen::Matrix< Scalar, n, 1 > &x, Eigen::Matrix< Scalar, n, n > *h) const =0
Computes the Hessian.
Helper object for unconstrained problem.
Definition: objects.h:126
Gradient functor.
Definition: objects.h:88
Dense Hessian functor.
Definition: objects.h:104
static NoConstraints< Scalar, m, n > & getInstance(void)
Returns the singleton of NoConstraints.
Definition: objects.h:176
virtual Scalar operator()(const Eigen::Matrix< Scalar, n, 1 > &x) const =0
Evaluates constraint function.
Inequality constraints functor.
Definition: objects.h:65