Optimo  0.1.0
A C++ header library for optimization
 All Classes Functions Variables Pages
objects_ls.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_LS_H_
33 #define OPTIMO_CORE_OBJECTS_LS_H_
34 
35 #include <Eigen/Core>
36 #include <Eigen/SparseCore>
37 #include <vector>
38 
39 // m: Number of Constraints
40 // n: Number of Variables
41 typedef unsigned int uint;
42 namespace optimo {
43 using Eigen::Dynamic;
44 using Eigen::Matrix;
46 
50 template <typename Scalar>
53  virtual Scalar
54  operator()(const Matrix<Scalar, Dynamic, 1>& x) const = 0;
55 };
56 
58 
62 template <typename Scalar>
65  virtual Scalar
66  operator()(const Matrix<Scalar, Dynamic, 1>& x) const = 0;
67 };
68 
70 
74 template <typename Scalar>
75 struct ConstraintsLS {
78  operator[](const unsigned int i) = 0;
79 };
80 
82 
87 template <typename Scalar>
90  virtual void
91  operator()(const Matrix<Scalar, Dynamic, 1>& x,
92  Matrix<Scalar, Dynamic, 1>* g) const = 0;
93 };
94 
96 
103 template <typename Scalar>
106  virtual void
107  operator()(
108  const Matrix<Scalar, Dynamic, 1>& x,
109  Matrix<Scalar, Dynamic, Dynamic>* h) const = 0;
110 };
111 
113 
115 template <typename Scalar>
118  virtual Scalar
119  operator()(const Matrix<Scalar, Dynamic, 1>& x) const {
120  return static_cast<Scalar>(0.0);
121  }
122 };
123 
125 
127 template <typename Scalar>
128 class NoConstraintsLS : public ConstraintsLS<Scalar> {
129  public:
131  static NoConstraintsLS<Scalar>& getInstance(void);
132 
133  // Non copyable
134  // Copy constructor
135  NoConstraintsLS(const NoConstraintsLS<Scalar>& x) = delete;
136 
137  // Operator =
139  operator=(const NoConstraintsLS<Scalar>& rhs) = delete;
140 
142  virtual ConstraintFunctorLS<Scalar>& operator[](const unsigned int i) {
143  return *c;
144  }
145 
146  protected:
148 
149  private:
150  static void cleanUp(void);
151 
152  // Constructor
153  NoConstraintsLS(void) {
155  atexit(&cleanUp);
156  }
157 
158  // Destructor
159  ~NoConstraintsLS(void) { }
160 
161  static NoConstraintsLS<Scalar>* instance;
162 };
163 
164 template <typename Scalar>
165 NoConstraintsLS<Scalar>* NoConstraintsLS<Scalar>::instance = nullptr;
166 
167 template <typename Scalar>
168 VoidConstraintFunctorLS<Scalar>* NoConstraintsLS<Scalar>::c = nullptr;
169 
170 // Cleanup function
171 template <typename Scalar>
172 void NoConstraintsLS<Scalar>::cleanUp(void) {
173  delete c;
174  c = nullptr;
175 }
176 
177 template <typename Scalar>
179  if (!instance) {
180  instance = new NoConstraintsLS<Scalar>;
181  }
182  return *instance;
183 }
184 
186 
197 template <typename Scalar>
198 class ProblemLS {
199  public:
204  const Matrix<Scalar, Dynamic, Dynamic> A; //< Equality constraints matrix
205  const Matrix<Scalar, Dynamic, 1> b;
206 
209  const ConstraintsLS<Scalar>& con,
210  const Matrix<Scalar, Dynamic, Dynamic>& Aeq,
211  const Matrix<Scalar, Dynamic, 1>& beq,
212  const GradientFunctorLS<Scalar>& g,
213  const HessianFunctorLS<Scalar>& h) :
214  objective(obj), constraints(con), gradient(g), hessian(h), A(Aeq), b(beq)
215  { }
216 
219  const GradientFunctorLS<Scalar>& g,
220  const HessianFunctorLS<Scalar>& h) :
221  objective(obj),
222  constraints(NoConstraintsLS<Scalar>::getInstance()),
223  gradient(g), hessian(h) { }
224 
225  // Destructor
226  virtual ~ProblemLS(void) { }
227 };
228 } // optimo
229 #endif // OPTIMO_CORE_OBJECTS_LS_H_
const ConstraintsLS< Scalar > & constraints
Constraints.
Definition: objects_ls.h:201
const GradientFunctorLS< Scalar > & gradient
Gradient functor.
Definition: objects_ls.h:202
const Matrix< Scalar, Dynamic, 1 > b
Equality vector.
Definition: objects_ls.h:205
Inequality constraints functor.
Definition: objects_ls.h:75
Objective functor.
Definition: objects_ls.h:51
Dummy constraint functor for unconstrained problems.
Definition: objects_ls.h:116
virtual Scalar operator()(const Matrix< Scalar, Dynamic, 1 > &x) const =0
Evaluates constraint.
ProblemLS(const ObjectiveFunctorLS< Scalar > &obj, const ConstraintsLS< Scalar > &con, const Matrix< Scalar, Dynamic, Dynamic > &Aeq, const Matrix< Scalar, Dynamic, 1 > &beq, const GradientFunctorLS< Scalar > &g, const HessianFunctorLS< Scalar > &h)
Constrained Problem.
Definition: objects_ls.h:208
ProblemLS(const ObjectiveFunctorLS< Scalar > &obj, const GradientFunctorLS< Scalar > &g, const HessianFunctorLS< Scalar > &h)
Unconstrained problem.
Definition: objects_ls.h:218
virtual void operator()(const Matrix< Scalar, Dynamic, 1 > &x, Matrix< Scalar, Dynamic, Dynamic > *h) const =0
Computes the Hessian.
Inequality constraints functor.
Definition: objects_ls.h:63
Class defining a convex Problem.
Definition: objects_ls.h:198
virtual ConstraintFunctorLS< Scalar > & operator[](const unsigned int i)
Dummy implementation for no constraints.
Definition: objects_ls.h:142
virtual Scalar operator()(const Matrix< Scalar, Dynamic, 1 > &x) const
Dummy evaluation of a constraint functor. Returns 0 always.
Definition: objects_ls.h:119
Gradient functor.
Definition: objects_ls.h:88
Helper object for unconstrained problem.
Definition: objects_ls.h:128
virtual Scalar operator()(const Matrix< Scalar, Dynamic, 1 > &x) const =0
Evaluates objective function.
const ObjectiveFunctorLS< Scalar > & objective
Objective functor.
Definition: objects_ls.h:200
Dense Hessian functor.
Definition: objects_ls.h:104
const HessianFunctorLS< Scalar > & hessian
Hessian functor.
Definition: objects_ls.h:203
static NoConstraintsLS< Scalar > & getInstance(void)
Returns the singleton instance.
Definition: objects_ls.h:178
virtual ConstraintFunctorLS< Scalar > & operator[](const unsigned int i)=0
Returns a ConstraintFunctorLS reference.
virtual void operator()(const Matrix< Scalar, Dynamic, 1 > &x, Matrix< Scalar, Dynamic, 1 > *g) const =0
Computes the gradient.