-
Notifications
You must be signed in to change notification settings - Fork 0
/
nilss_solver.cpp
93 lines (72 loc) · 2.41 KB
/
nilss_solver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <memory>
#include <cassert>
#include <iostream>
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include "nilss_solver.h"
using namespace Eigen;
namespace nilss {
std::unique_ptr<MatrixXd> assemble_kkt(
const std::vector<MatrixXd>& R, const std::vector<MatrixXd>& D)
{
int n = R.size(), m = R[0].rows();
assert(n > 0 && D.size() == n + 1);
int kktSize = (2 * n + 1) * m;
MatrixXd * pMat = new MatrixXd(kktSize, kktSize);
pMat->setZero();
for (int i = 0; i <= n; ++ i) {
pMat->block(i * m, i * m, m, m) = D[i];
}
int halfSize = (n + 1) * m;
for (int i = 0; i < n; ++ i) {
pMat->block(halfSize + i * m, (i + 1) * m, m, m) = MatrixXd::Identity(m, m);
pMat->block(halfSize + i * m, i * m, m, m) = -R[i];
pMat->block((i + 1) * m, halfSize + i * m, m, m) = MatrixXd::Identity(m, m);
pMat->block(i * m, halfSize + i * m, m, m) = -R[i].transpose();
}
return std::unique_ptr<MatrixXd>(pMat);
}
std::unique_ptr<VectorXd> assemble_rhs(
const std::vector<VectorXd>& b, const std::vector<VectorXd>& c)
{
int n = b.size(), m = b[0].size();
assert(n > 0 && c.size() == n + 1);
int kktSize = (2 * n + 1) * m;
VectorXd * pVec = new VectorXd(kktSize);
for (int i = 0; i <= n; ++ i) {
pVec->segment(i * m, m) = c[i];
}
int halfSize = (n + 1) * m;
for (int i = 0; i < n; ++ i) {
pVec->segment(halfSize + i * m, m) = b[i];
}
return std::unique_ptr<VectorXd>(pVec);
}
void nilss_solve(const std::vector<MatrixXd>& R,
const std::vector<MatrixXd>& D,
const std::vector<VectorXd>& b,
const std::vector<VectorXd>& c,
std::vector<VectorXd>& a)
{
int n = R.size();
assert(D.size() == n + 1);
assert(b.size() == n);
assert(c.size() == n + 1);
std::unique_ptr<MatrixXd> kkt = assemble_kkt(R, D);
std::unique_ptr<VectorXd> rhs = assemble_rhs(b, c);
typedef SparseMatrix<double> SpMat;
SpMat A(kkt->sparseView());
SparseLU<SparseMatrix<double>> solver;
solver.analyzePattern(A);
solver.factorize(A);
VectorXd sol = solver.solve(*rhs);
//VectorXd sol = kkt->partialPivLu().solve(*rhs);
assert(sol.size() % (2 * n + 1) == 0);
int m = sol.size() / (2 * n + 1);
a.empty();
a.reserve(n + 1);
for (int i = 0; i <= n; ++ i) {
a.push_back(sol.segment(i * m, m));
}
}
}