KASKADE 7 development version
Loading...
Searching...
No Matches
qp.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2017-2025 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef QP_HH
14#define QP_HH
15
16#include <memory>
17#include <tuple>
18
19#include "dune/istl/bvector.hh"
20
23
24namespace Kaskade
25{
56
57
58
60
64 enum class QPStructure { DENSE, SPARSE };
65
66 //-----------------------------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------------------------
68
77 template <class MatrixA, class MatrixB, class VectorX, class VectorB>
78 std::array<typename ScalarTraits<typename MatrixA::field_type>::Real,4>
79 checkKKT(MatrixA const& A, MatrixB const& B,VectorX const& c, VectorB const& b,
80 VectorX const& x, VectorB const& lambda);
81
82
83 //-----------------------------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------------------------
85
105 template <class Real, class Implementation>
107 {
108 public:
111
113 {}
114
132 std::tuple<Vector,int> solve(Vector const& c, Vector const& b, double tol) const;
133
152 std::tuple<Vector,int> solve(Vector const& x, Vector const& c, Vector const& b, double tol) const;
153
161 void gradientStep(Vector const& c, Vector const& b, Vector& x) const;
162
169 Implementation& setMinSteps(int count);
170
177 Implementation& setMaxSteps(int count);
178
179 protected:
185 : dimension(dim)
186 , minSteps(0)
187 , maxSteps(200)
188 {}
189
200 Vector solveSubmatrix(std::vector<int> const& idx, Vector const& b) const;
201
208 virtual void updateWithColumn(Real a, int i, Vector& s) const;
209
210 private:
211 int dimension; // size of primal variables (i.e. length of x and size of A)
212 int minSteps; // minimal number of steps to perform when solving
213 int maxSteps; // upper bound on iteration count
214
218 Real objective(Vector const& x, Vector const& c) const;
219
231 bool getProjectedGradient(Vector const& x, Vector const& c, Vector const& b, Vector& g, Vector& s) const;
232
233 void getProjectedNewton(Vector const& x, Vector const& c, Vector const& b, Vector& g, Vector& s) const;
234
244 bool projectedLineSearch(Vector& x, Vector const& c, Vector const& b, Vector& g, Vector& s) const;
245
246 void checkNaN(Vector const& v) const;
247
248 virtual void printMatrix(std::ostream& out) const = 0;
249
250 };
251
252 //-----------------------------------------------------------------------------------------------
253 //-----------------------------------------------------------------------------------------------
254
267 template <class R=double>
268 class QPBoundSolver: public QPBoundSolverBase<R,QPBoundSolver<R>>
269 {
271
272 public:
273 using Real = R;
274 using Vector = typename Base::Vector;
276
288
289 private:
290 Matrix A;
291
292 friend Base;
293 void mv(Vector const& x, Vector& Ax) const { A.mv(x,Ax); }
294 Real frobenius_norm() const { return A.frobenius_norm(); }
295 Vector solveSubmatrix(std::vector<int> const& idx, Vector const& b) const;
296
297 virtual void printMatrix(std::ostream& out) const override
298 {
299 out << "A = \n" << A;
300 }
301
302 virtual void updateWithColumn(Real a, int i, Vector& s) const override;
303 };
304
305 //-----------------------------------------------------------------------------------------------
306 //-----------------------------------------------------------------------------------------------
307
321 template <class R=double>
322 class QPDirectSparse: public QPBoundSolverBase<R,QPDirectSparse<R>>
323 {
325
326 public:
327 using Real = R;
328 using Vector = typename Base::Vector; // Dune::BlockVector<Dune::FieldVector<Real,1>>
330
342
343 private:
344 SparseMatrix A;
345 void mv(Vector const&x, Vector& Ax) const { A.mv(x,Ax); }
346 Real frobenius_norm() const;
347 friend Base;
348 Vector solveSubmatrix(std::vector<int> const& idx, Vector const& b) const;
349
350 virtual void printMatrix(std::ostream& out) const { out << "sparse matrix A in triplet format: \n" << A; }
351 };
352
353 //--------------------------------------------------------------------------------------------------------------
354
376 template <QPStructure sparsity=QPStructure::DENSE, class R=double>
377 class QPSlackSolver: public QPBoundSolverBase<R,QPSlackSolver<sparsity,R>>
378 {
380 using Entry = Dune::FieldMatrix<R,1,1>;
381
382 public:
383 using Real = R;
384 using Vector = typename Base::Vector; // Dune::BlockVector<Dune::FieldVector<Real,1>>
385 using Matrix = std::conditional_t<sparsity==QPStructure::DENSE,
388
400
401 private:
402 Matrix A;
403 Matrix B;
404 Real gamma;
405
406 friend Base;
407 void mv(Vector const& x, Vector& Ax) const;
408 Real frobenius_norm() const;
409 Vector solveSubmatrix(std::vector<int> const& idx, Vector const& b) const;
410
411 virtual void printMatrix(std::ostream& out) const
412 {
413 out << "A = \n" << A << "\nB = \n" << B << "\ngamma = " << gamma << "\n";
414 }
415 };
416
417
418 //--------------------------------------------------------------------------------------------------------------
419 //--------------------------------------------------------------------------------------------------------------
420
446 template <int d, class Real=double>
448 {
450
451 public:
457
469
470 QPSolver(Self const& qp);
471
472 Self& operator=(Self const& qp);
473
493 std::tuple<VectorX,VectorB,int> solve(VectorX const& c, VectorB const& b, double tol) const;
494
498 MatrixB const& matrixB() const
499 {
500 return B;
501 }
502
508
509 private:
510MatrixA A; // for debugging
511 MatrixA Ainv;
512 MatrixB B;
513 std::unique_ptr<QPBoundSolver<Real>> dual;
514 };
515
516 //--------------------------------------------------------------------------------------------------------------
517 //--------------------------------------------------------------------------------------------------------------
518
548 template <int d, QPStructure sparsity = QPStructure::DENSE, class Real=double>
550 {
551 using AEntry = Dune::FieldMatrix<Real,d,d>;
552 using BEntry = Dune::FieldMatrix<Real,1,d>;
553
554 public:
557 using MatrixA = std::conditional_t<sparsity==QPStructure::DENSE,
560 using MatrixB = std::conditional_t<sparsity==QPStructure::DENSE,
563
571 QPPenalizedSolver(MatrixA const& A, MatrixB const& B);
572
580 QPPenalizedSolver(MatrixA const& A, MatrixB const& B, Real gamma);
581
586
591
598
602 Real getPenalty() const { return gamma; }
603
622 std::tuple<VectorX,VectorB,int> solve(VectorX const& c, VectorB const& b, VectorB const& lambda, double tol) const;
623
624
642 std::tuple<VectorX,VectorB,int> solve(VectorX const& c, VectorB const& b,
643 VectorB lambda, double tol, VectorX const& x) const;
644
645 std::tuple<VectorX,int> solve(VectorX const& c, VectorB const& b, double tol, VectorX const& x) const;
646 std::tuple<VectorX,int> solve(VectorX const& c, VectorB const& b, double tol) const;
647
669
675 static constexpr QPStructure sparse = sparsity;
676
677 private:
678 Real gamma = 1e-3;
679 MatrixA A;
680 MatrixB B;
681
682 int minSteps = 0;
683 int maxSteps = 10000;
684
685 std::vector<int> allCols;
686 };
687
688 // ---------------------------------------------------------------------------------------------
689 // ---------------------------------------------------------------------------------------------
690
722 template <int d, QPStructure sparsity = QPStructure::DENSE, class Real=double>
724 {
725 using AEntry = Dune::FieldMatrix<Real,d,d>;
726 using BEntry = Dune::FieldMatrix<Real,1,d>;
727
728 public:
731 using MatrixA = std::conditional_t<sparsity==QPStructure::DENSE,
734 using MatrixB = std::conditional_t<sparsity==QPStructure::DENSE,
737
745 QPALSolver(MatrixA const& A, MatrixB const& B);
746
756
766
773
774 Real getPenalty() const { return qp.getPenalty(); }
775
793 std::tuple<VectorX,VectorB,int> solve(VectorX const& c, VectorB const& b, double tol) const;
794
795
812 std::tuple<VectorX,VectorB,int> solve(VectorX const& c, VectorB const& b, double tol, VectorX x) const;
813
819 static constexpr QPStructure sparse = sparsity;
820
821 private:
822 int minSteps = 0;
823 int maxSteps = 10000;
824
826 };
827
828
829}
830
831#endif
A LAPACK-compatible dense matrix class with shape specified at runtime.
A NUMA-aware compressed row storage matrix adhering mostly to the Dune ISTL interface (to complete....
QPALSolver(MatrixA const &A, MatrixB const &B)
Constructs the solver, providing the matrices and .
std::tuple< VectorX, VectorB, int > solve(VectorX const &c, VectorB const &b, double tol) const
Solves the QP approximately for the given right hand side vectors.
std::conditional_t< sparsity==QPStructure::DENSE, DynamicMatrix< AEntry >, NumaBCRSMatrix< AEntry > > MatrixA
Definition qp.hh:731
QPALSolver< d, sparsity, Real > & setPenalty(Real gamma)
Sets the penalty parameter .
std::conditional_t< sparsity==QPStructure::DENSE, DynamicMatrix< BEntry >, NumaBCRSMatrix< BEntry > > MatrixB
Definition qp.hh:734
Real getPenalty() const
Definition qp.hh:774
Dune::BlockVector< Dune::FieldVector< Real, 1 > > VectorB
Definition qp.hh:730
QPALSolver< d, sparsity, Real > & setMinSteps(int i)
Sets the minimum number of augmented Lagrangian steps to perform.
Dune::BlockVector< Dune::FieldVector< Real, d > > VectorX
Definition qp.hh:729
static constexpr QPStructure sparse
Given info on whether dense or sparse arithmetic is used (i.e. the choice of template parameter used ...
Definition qp.hh:819
std::tuple< VectorX, VectorB, int > solve(VectorX const &c, VectorB const &b, double tol, VectorX x) const
Solves the QP approximately for the given right hand side vectors.
QPALSolver< d, sparsity, Real > & setMaxSteps(int i)
Sets the maximum number of projected gradient steps to perform.
Vector solveSubmatrix(std::vector< int > const &idx, Vector const &b) const
solves a subsystem .
virtual void updateWithColumn(Real a, int i, Vector &s) const
Performs the update , where is the i-th unit vector.
std::tuple< Vector, int > solve(Vector const &c, Vector const &b, double tol) const
Solves the QP for the given right hand side vectors.
Implementation & setMinSteps(int count)
defines the minimal number of steps to perform in solving.
void gradientStep(Vector const &c, Vector const &b, Vector &x) const
Performs a single projected gradient step.
Implementation & setMaxSteps(int count)
defines the maximum allowed number of steps to perform in solving.
virtual ~QPBoundSolverBase()
Definition qp.hh:112
QPBoundSolverBase(int dim)
Constructor (to be used by derived classes only)
Definition qp.hh:184
std::tuple< Vector, int > solve(Vector const &x, Vector const &c, Vector const &b, double tol) const
Solves the QP for the given right hand side vectors.
Dune::BlockVector< Dune::FieldVector< Real, 1 > > Vector
Definition qp.hh:109
DynamicMatrix< Dune::FieldMatrix< Real, 1, 1 > > Matrix
Definition qp.hh:110
DynamicMatrix< Dune::FieldMatrix< Real, 1, 1 > > Matrix
Definition qp.hh:275
QPBoundSolver(Matrix const &A, QPConvexificationStrategy convex=QPConvexificationStrategy::DONOTHING)
Constructs the solver, providing the matrix .
typename Base::Vector Vector
Definition qp.hh:274
QPDirectSparse(SparseMatrix const &A, QPConvexificationStrategy convex=QPConvexificationStrategy::DONOTHING)
Constructs the solver, providing the matrix .
NumaBCRSMatrix< Dune::FieldMatrix< Real, 1, 1 > > SparseMatrix
Definition qp.hh:329
typename Base::Vector Vector
Definition qp.hh:328
An iterative solver for small instances of a particular class of quadratic programs.
Definition qp.hh:550
std::tuple< VectorX, VectorB, int > solve(VectorX const &c, VectorB const &b, VectorB const &lambda, double tol) const
Solves the QP approximately for the given right hand side vectors.
std::tuple< VectorX, int > solve(VectorX const &c, VectorB const &b, double tol, VectorX const &x) const
Real getPenalty() const
Returns the used penalty parameter .
Definition qp.hh:602
std::conditional_t< sparsity==QPStructure::DENSE, DynamicMatrix< AEntry >, NumaBCRSMatrix< AEntry > > MatrixA
Definition qp.hh:557
QPPenalizedSolver< d, sparsity, Real > & setMaxSteps(int i)
Sets the maximum number of projected gradient steps to perform.
std::conditional_t< sparsity==QPStructure::DENSE, DynamicMatrix< BEntry >, NumaBCRSMatrix< BEntry > > MatrixB
Definition qp.hh:560
Dune::BlockVector< Dune::FieldVector< Real, d > > VectorX
Definition qp.hh:555
QPPenalizedSolver< d, sparsity, Real > & setMinSteps(int i)
Sets the minimum number of projected gradient steps to perform.
std::tuple< VectorX, VectorB, int > solve(VectorX const &c, VectorB const &b, VectorB lambda, double tol, VectorX const &x) const
Solves the QP approximately for the given right hand side vectors.
VectorB multiplierEstimate(VectorX const &x, VectorX c) const
Computes a least squares multiplier estimate.
QPPenalizedSolver(MatrixA const &A, MatrixB const &B)
Constructs the solver, providing the matrices and .
Dune::BlockVector< Dune::FieldVector< Real, 1 > > VectorB
Definition qp.hh:556
QPPenalizedSolver< d, sparsity, Real > & setPenalty(Real gamma)
Sets the penalty parameter .
std::tuple< VectorX, int > solve(VectorX const &c, VectorB const &b, double tol) const
static constexpr QPStructure sparse
Given info on whether dense or sparse arithmetic is used (i.e. the choice of template parameter used ...
Definition qp.hh:675
QPPenalizedSolver(MatrixA const &A, MatrixB const &B, Real gamma)
Constructs the solver, providing the matrices and .
typename Base::Vector Vector
Definition qp.hh:384
std::conditional_t< sparsity==QPStructure::DENSE, DynamicMatrix< Entry >, NumaBCRSMatrix< Entry > > Matrix
Definition qp.hh:385
QPSlackSolver(Matrix const &A, Matrix const &B, Real gamma, QPConvexificationStrategy convex=QPConvexificationStrategy::DONOTHING)
Constructs the solver, providing the matrix and .
Dune::BlockVector< Dune::FieldVector< Real, d > > VectorX
Definition qp.hh:452
DynamicMatrix< Dune::FieldMatrix< Real, d, d > > MatrixA
Definition qp.hh:454
DynamicMatrix< Dune::FieldMatrix< Real, 1, d > > MatrixB
Definition qp.hh:455
static constexpr QPStructure sparse
Definition qp.hh:507
Self & operator=(Self const &qp)
Dune::BlockVector< Dune::FieldVector< Real, 1 > > VectorB
Definition qp.hh:453
QPSolver(MatrixA const &A, MatrixB const &B, QPConvexificationStrategy convex=QPConvexificationStrategy::DONOTHING)
Constructs the solver, providing the matrices and .
QPSolver(Self const &qp)
MatrixB const & matrixB() const
Provides access to the internal copy of .
Definition qp.hh:498
QPSolver< d, Real > Self
Definition qp.hh:456
std::tuple< VectorX, VectorB, int > solve(VectorX const &c, VectorB const &b, double tol) const
Solves the QP for the given right hand side vectors.
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
Definition fixdune.hh:667
std::array< typename ScalarTraits< typename MatrixA::field_type >::Real, 4 > checkKKT(MatrixA const &A, MatrixB const &B, VectorX const &c, VectorB const &b, VectorX const &x, VectorB const &lambda)
Computes the KKT residual for approximate solutions of certain QPs.
QPConvexificationStrategy
Definition qp.hh:59
QPStructure
A flag that determines whether the QP solver shall work with dense or sparse linear algebra.
Definition qp.hh:64