Darwin  1.10(beta)
drwnQPSolver.h
1 /******************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2017, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnQPSolver.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <cstdlib>
16 #include <vector>
17 
18 #include "Eigen/Core"
19 #include "Eigen/Cholesky"
20 #include "Eigen/LU"
21 
22 #include "drwnBase.h"
23 
24 using namespace std;
25 using namespace Eigen;
26 
96 // drwnQPSolver -------------------------------------------------------------
97 
98 class drwnQPSolver {
99  public:
100  // line search parameters
101  static double alpha;
102  static double beta;
103 
104  protected:
105  // objective
106  MatrixXd _mP;
107  VectorXd _q;
108  double _r;
109 
110  // constraints and bounds
111  MatrixXd _mA;
112  VectorXd _b;
113  MatrixXd _mG;
114  MatrixXd _h;
115  VectorXd _l;
116  VectorXd _u;
117 
118  VectorXd _x;
119 
120  public:
122  drwnQPSolver();
124  drwnQPSolver(const MatrixXd& P, const VectorXd& q, double r = 0.0);
125  virtual ~drwnQPSolver();
126 
127  // define problem
129  void setObjective(const MatrixXd& P, const VectorXd& q, double r = 0.0);
131  void setEqConstraints(const MatrixXd& A, const VectorXd& b);
133  void setIneqConstraints(const MatrixXd& G, const VectorXd& h);
135  void setBounds(const VectorXd& lb, const VectorXd& ub);
137  void clearEqConstraints();
139  void clearIneqConstraints();
141  void clearBounds();
142 
144  virtual void initialize(const VectorXd& x);
145 
146  // solve
148  virtual double solve();
152  double objective() const;
154  double objective(const VectorXd& x) const;
156  inline VectorXd solution() const { return _x; }
157 
158  // accessors
160  inline int size() const { return _x.rows(); }
162  inline double operator[](unsigned i) const { return _x[i]; }
163 
164  protected:
165  // compute gradient: _mP x + _q
166  inline VectorXd gradient() const { return _mP * _x + _q; }
167  inline VectorXd gradient(const VectorXd& x) const { return _mP * x + _q; }
168 
169  // check strictly feasibility
170  bool isFeasiblePoint(const VectorXd& x) const;
171 
172  // special case solvers
173  double solveOnlyBounds();
174  double solveSingleEquality();
175  double solveSimplex();
176  double solveNoBounds(); // only equality constraints
177  double solveGeneral();
178 
179  // line search
180  double lineSearchNoBounds(const VectorXd& x, const VectorXd& dx,
181  const VectorXd& nu, const VectorXd& dnu) const;
182  double lineSearchGeneral(const VectorXd& x, const VectorXd& dx,
183  const VectorXd& nu, const VectorXd& dnu) const;
184 
185  // solve the system of equations:
186  // | H_x A^T | | x | = - | c |
187  // | A H_y | | y | | b |
188  void solveKKTSystem(const MatrixXd& Hx, const MatrixXd& Hy,
189  const MatrixXd& A, const VectorXd& c, const VectorXd& b,
190  VectorXd& x, VectorXd& y) const;
191 
192  // | H_x A^T | | x | = - | c |
193  // | A 0 | | y | | b |
194  void solveKKTSystem(const MatrixXd& Hx, const MatrixXd& A,
195  const VectorXd& c, const VectorXd& b,
196  VectorXd& x, VectorXd& y) const;
197 };
198 
199 // drwnLogBarrierQPSolver ---------------------------------------------------
200 
218  public:
219  // update parameters
220  static double t0; // > 0
221  static double mu; // > 1
222 
223  public:
227  drwnLogBarrierQPSolver(const MatrixXd& P, const VectorXd& q, double r = 0.0);
228  virtual ~drwnLogBarrierQPSolver();
229 
231  virtual bool findFeasibleStart();
232 
234  virtual void initialize(const VectorXd& x);
235 
236  // solve
237  virtual double solve();
238 };
double _r
constant term in the objective function
Definition: drwnQPSolver.h:108
static double alpha
line search stopping criterion in (0, 1/2)
Definition: drwnQPSolver.h:101
VectorXd _u
variable upper bounds (box constraint)
Definition: drwnQPSolver.h:116
VectorXd _l
variable lower bounds (box constraint)
Definition: drwnQPSolver.h:115
VectorXd _q
linear term in the objective function
Definition: drwnQPSolver.h:107
MatrixXd _mG
linear inequality constraint matrix
Definition: drwnQPSolver.h:113
VectorXd _x
current estimate of solution
Definition: drwnQPSolver.h:118
MatrixXd _h
linear inequality constraint vector
Definition: drwnQPSolver.h:114
VectorXd _b
linear equality constraint vector
Definition: drwnQPSolver.h:112
static double beta
line search backtracking parameter in (0, 1)
Definition: drwnQPSolver.h:102
MatrixXd _mP
positive definite quadratic term in the objective function
Definition: drwnQPSolver.h:106
Definition: drwnQPSolver.h:217
int size() const
return the number of dimensions of the state space
Definition: drwnQPSolver.h:160
VectorXd solution() const
return the current estimate of the solution
Definition: drwnQPSolver.h:156
MatrixXd _mA
linear equality constraint matrix
Definition: drwnQPSolver.h:111
double operator[](unsigned i) const
access the i-th dimension of the current solution
Definition: drwnQPSolver.h:162
Quadratic program solver.
Definition: drwnQPSolver.h:98