Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnLPSolver.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-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnLPSolver.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/SparseCore"
20 
21 #include "drwnBase.h"
22 
23 using namespace std;
24 using namespace Eigen;
25 
26 // drwnLPSolver ------------------------------------------------------------
27 
78 class drwnLPSolver {
79  public:
80  // barrier method parameters
81  static double t0;
82  static double mu;
83  static double eps;
84  static unsigned maxiters;
85  // line search parameters
86  static double alpha;
87  static double beta;
88 
89  protected:
90  // objective
91  VectorXd _c;
92 
93  // constraints and bounds
94  MatrixXd _A;
95  VectorXd _b;
96 
97  VectorXd _lb;
98  VectorXd _ub;
99 
100  VectorXd _x;
101 
102  public:
104  drwnLPSolver(const VectorXd& c, const MatrixXd& A, const VectorXd& b);
106  drwnLPSolver(const VectorXd& c, const MatrixXd& A, const VectorXd& b,
107  const VectorXd& lb, const VectorXd& ub);
109  ~drwnLPSolver() { /* do nothing */ }
110 
112  void initialize(const VectorXd& x);
113 
115  double solve();
116 
118  inline const VectorXd& solution() const { return _x; }
119 
120  // accessors
122  inline int size() const { return _c.rows(); }
124  inline double operator[](unsigned i) const { return _x[i]; }
125 
126  protected:
128  bool isUnbounded(unsigned i) const { return ((_lb[i] == -DRWN_DBL_MAX) && (_ub[i] == DRWN_DBL_MAX)); }
130  bool isWithinBounds(const VectorXd& x) const { return (x.array() > _lb.array()).all() && (x.array() < _ub.array()).all(); }
131 
133  double barrierFunction(const VectorXd& x) const;
134 };
135 
136 // drwnSparseLPSolver ------------------------------------------------------
137 
147  protected:
148  // objective
149  VectorXd _c;
150 
151  // constraints and bounds
152  const SparseMatrix<double>& _A;
153  VectorXd _b;
154 
155  VectorXd _lb;
156  VectorXd _ub;
157 
158  VectorXd _x;
159 
160  public:
162  drwnSparseLPSolver(const VectorXd& c, const SparseMatrix<double>& A, const VectorXd& b);
164  drwnSparseLPSolver(const VectorXd& c, const SparseMatrix<double>& A, const VectorXd& b,
165  const VectorXd& lb, const VectorXd& ub);
167  ~drwnSparseLPSolver() { /* do nothing */ }
168 
170  void initialize(const VectorXd& x);
171 
173  double solve();
174 
176  inline const VectorXd& solution() const { return _x; }
177 
178  // accessors
180  inline int size() const { return _c.rows(); }
182  inline double operator[](unsigned i) const { return _x[i]; }
183 
184  protected:
186  bool isUnbounded(unsigned i) const { return ((_lb[i] == -DRWN_DBL_MAX) && (_ub[i] == DRWN_DBL_MAX)); }
188  bool isWithinBounds(const VectorXd& x) const { return (x.array() > _lb.array()).all() && (x.array() < _ub.array()).all(); }
189 
191  double barrierFunction(const VectorXd& x) const;
192 };
static double eps
stopping tolerance
Definition: drwnLPSolver.h:83
MatrixXd _A
linear equality constraint matrix
Definition: drwnLPSolver.h:94
const VectorXd & solution() const
return the current estimate of the solution
Definition: drwnLPSolver.h:118
const SparseMatrix< double > & _A
linear equality constraint matrix
Definition: drwnLPSolver.h:152
int size() const
return the number of dimensions of the state space
Definition: drwnLPSolver.h:122
VectorXd _b
linear equality constraint vector
Definition: drwnLPSolver.h:153
int size() const
return the number of dimensions of the state space
Definition: drwnLPSolver.h:180
VectorXd _x
current estimate of solution
Definition: drwnLPSolver.h:100
static double t0
initial barrier function multiplier
Definition: drwnLPSolver.h:81
VectorXd _ub
upped bound for each variable (DRWN_DBL_MAX for unbounded above)
Definition: drwnLPSolver.h:156
~drwnSparseLPSolver()
destructor
Definition: drwnLPSolver.h:167
const VectorXd & solution() const
return the current estimate of the solution
Definition: drwnLPSolver.h:176
VectorXd _c
linear term in the objective function
Definition: drwnLPSolver.h:149
~drwnLPSolver()
destructor
Definition: drwnLPSolver.h:109
static double alpha
line search stopping criterion in (0, 1/2)
Definition: drwnLPSolver.h:86
VectorXd _ub
upped bound for each variable (DRWN_DBL_MAX for unbounded above)
Definition: drwnLPSolver.h:98
bool isUnbounded(unsigned i) const
returns true if the i-th variable is unbounded
Definition: drwnLPSolver.h:186
bool isWithinBounds(const VectorXd &x) const
returns true if a point is strictly within the lower and upper bounds
Definition: drwnLPSolver.h:188
static double beta
line search backtracking parameter in (0, 1)
Definition: drwnLPSolver.h:87
VectorXd _lb
lower bound for each variable (-DRWN_DBL_MAX for unbounded below)
Definition: drwnLPSolver.h:155
bool isWithinBounds(const VectorXd &x) const
returns true if a point is strictly within the lower and upper bounds
Definition: drwnLPSolver.h:130
double operator[](unsigned i) const
access the i-th dimension of the current solution
Definition: drwnLPSolver.h:182
double operator[](unsigned i) const
access the i-th dimension of the current solution
Definition: drwnLPSolver.h:124
static double mu
barrier function multiplier update
Definition: drwnLPSolver.h:82
Solves equality constrained linear programs with positivity constraints via the log-barrier method...
Definition: drwnLPSolver.h:78
VectorXd _c
linear term in the objective function
Definition: drwnLPSolver.h:91
static unsigned maxiters
maximum number of newton steps per iteration
Definition: drwnLPSolver.h:84
VectorXd _b
linear equality constraint vector
Definition: drwnLPSolver.h:95
bool isUnbounded(unsigned i) const
returns true if the i-th variable is unbounded
Definition: drwnLPSolver.h:128
VectorXd _x
current estimate of solution
Definition: drwnLPSolver.h:158
VectorXd _lb
lower bound for each variable (-DRWN_DBL_MAX for unbounded below)
Definition: drwnLPSolver.h:97
Solves linear programs with sparse equality constraints.
Definition: drwnLPSolver.h:146