Kernel Quantum Probability Library
The KQP library aims at providing tools for working with quantums probabilities
coneprog.hpp
1 /*
2  This file is part of the Kernel Quantum Probability library (KQP).
3 
4  KQP is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  KQP is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with KQP. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef _KQP_CONEPROG_H_
19 #define _KQP_CONEPROG_H_
20 
21 #include <vector>
22 
23 #include <kqp/kqp.hpp>
24 #include <Eigen/Core>
25 
26 
27 namespace kqp {
28  namespace cvxopt {
30  struct Dimensions {
32  int l;
34  std::vector<int> q;
36  std::vector<int> s;
37 
38  inline Dimensions() : l(-1) {}
39  };
40 
41 
43  template<typename Scalar>
44  struct ConeQPOptions {
47 
49  bool DEBUG;
50 
52  bool correction;
53 
56 
58  int maxiters;
59 
60  Scalar abstol;
61  Scalar reltol;
62  Scalar feastol;
63 
64  int refinement;
65 
66  ConeQPOptions();
67  };
68 
70  enum Status {
72  OPTIMAL,
73 
74  // Singular KKT matrix
75  SINGULAR_KKT_MATRIX,
76 
77  // Not converged
78  NOT_CONVERGED
79  };
80 
81  template<typename Scalar>
82  struct ConeQPReturn {
83  KQP_VECTOR(Scalar) x;
84  KQP_VECTOR(Scalar) y;
85  KQP_VECTOR(Scalar) s;
86  KQP_VECTOR(Scalar) z;
87 
88  Status status;
89 
90  Scalar gap;
91  Scalar relative_gap;
92  Scalar primal_objective;
93  Scalar dual_objective;
94 
95  Scalar primal_infeasibility;
96  Scalar dual_infeasibility;
97 
98  Scalar primal_slack;
99  Scalar dual_slack;
100 
101  int iterations;
102 
103  ConeQPReturn();
104 
105  };
106 
107 
108  template<typename Scalar>
109  class QPMatrix {
110  public:
112  virtual void mult(const KQP_VECTOR(Scalar) &x, KQP_VECTOR(Scalar) &y, bool adjoint = false) const = 0;
113  virtual Eigen::MatrixXd::Index rows() const = 0;
114  virtual Eigen::MatrixXd::Index cols() const = 0;
115  };
116 
117 
118  /*
119  Nesterov-Todd scaling matrix
120  - W['dnl']: positive vector
121  - W['dnli']: componentwise inverse of W['dnl']
122  - W['d']: positive vector
123  - W['di']: componentwise inverse of W['d']
124  - W['v']: lists of 2nd order cone vectors with unit hyperbolic norms
125  - W['beta']: list of positive numbers
126  - W['r']: list of square matrices
127  - W['rti']: list of square matrices. rti[k] is the inverse adjoint
128  of r[k].
129  */
130  template<typename Scalar>
131  struct ScalingMatrix {
132  KQP_VECTOR(Scalar) d, di, dnl, dnli;
133 
134  std::vector<Scalar> beta;
135  std::vector<KQP_MATRIX(Scalar)> r, rti;
136  std::vector<KQP_MATRIX(Scalar)> v;
137  };
138 
139 
140  // The KKTSolver
141  template<typename Scalar>
142  class KKTSolver {
143  public:
144  virtual ~KKTSolver() {}
145  virtual void solve(KQP_VECTOR(Scalar) &x, KQP_VECTOR(Scalar) &y, KQP_VECTOR(Scalar) & z) const = 0;
146  };
147 
149  template<typename Scalar>
150  class KKTPreSolver {
151  public:
152  virtual KKTSolver<Scalar> *get(const ScalingMatrix<Scalar> &w) = 0;
153  };
154 
155 
459  template<typename Scalar>
460  void coneqp(const QPMatrix<Scalar> &P, KQP_VECTOR(Scalar) &q,
461  ConeQPReturn<Scalar> &result,
462  bool initVals = false,
463  Dimensions dims = Dimensions(),
464  const QPMatrix<Scalar> *G = NULL, KQP_VECTOR(Scalar)* h = NULL,
465  KQP_MATRIX(Scalar) *A = NULL, KQP_VECTOR(Scalar) *b = NULL,
466  KKTPreSolver<Scalar>* kktpresolver = NULL,
468 
469  }
470 
471 }
472 
473 #include <kqp/coneprog.inc.hpp>
474 
475 #endif