-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-constrained-linear-solver.cc
254 lines (227 loc) · 11.7 KB
/
example-constrained-linear-solver.cc
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <iostream>
#include <iomanip>
#include <chrono>
#include <LazyMath/ConjugateGradient.h>
using std::cout;
using std::string;
using std::complex;
using namespace std::complex_literals;
using std::size_t;
using namespace LazyMath;
int main()
{
{
using T = double;
std::cout << "\n*** Example cs.1 *** : Constrained solver with heap-allocated non-symmetric real matrix.\n";
// Minimize |A*x - b| subject to C*x = d.
// Dimensions:
const size_t N = 6;
const size_t M = 3;
const size_t P = 2;
Matrix<T> matA(N, M);
matA = { {1,2,3},
{-6,-5,-4},
{9,7,8},
{1,0,0},
{0,1,0},
{0,0,1} };
Vector<T> vecB(N);
vecB = { -3, 2, -1, 1.85, -2.85, 0.285 };
// ... with constraints C*x = d.
Matrix<T> matC(P, M);
matC = { {1, 1, 0},
{0, 1, 1} };
Vector<T> vecD(P);
vecD = { 2, -1 };
std::cout << "Non-symmetric Linear system size: " << matA.rows() << " x " << matA.cols() << "\n";
std::cout << "Constraint matrix size: " << matC.rows() << " x " << matC.cols() << "\n";
double normAxMinusB = 0, normConstraintViolation = 0;
Vector<T> vecX = constrainedSolver(matA, vecB, matC, vecD, SymmetricMode::NonSymmetricMatrix,
0, // Default relative error tolerance
0, // Default maximum number of iterations
&normAxMinusB, // Residual |A*x - b|
&normConstraintViolation); // Residual |C*x - d|
std::cout << "Solution x to min |Ax-b| subject to Cx=d: " << vecX << std::endl;
std::cout << "Object function residual |Ax-b| = " << normAxMinusB
<< ", constraint violation norm |Cx - d| = " << normConstraintViolation << "\n";
}
{
using T = std::complex<double>;
std::cout << "\n*** Example cs.2 *** : Constrained solver with stack-allocated non-symmetric complex matrices.\n";
// Minimize |A*x - b| subject to C*x = d.
// Dimensions:
constexpr size_t N = 6;
constexpr size_t M = 3;
constexpr size_t P = 2;
Matrix<T, N, M> matA;
matA[0] = {1.0+7.0i, 2.0+6.0i, 3.0+5.0i};
matA[1] = {-6.0-7.0i, -5.0-6.0i, -4.0-5.0i};
matA[2] = {9.0-2.0i, 7.0+3.0i, 8.0+4.0i};
matA[3] = {1,0,0};
matA[4] = {0,1,0};
matA[5] = {0,0,1};
Vector<T, N> vecB;
vecB = { -3.0+1.0i, 2.0-1.0i, -1.0+1.0i, 1.85, -2.85, 0.285 }; // dim = N
// ... with constraints C*x = d.
Matrix<T, P, M> matC;
matC[0] = {1.0i, 1., 0.};
matC[1] = {0., 1.0i, 1.};
Vector<T, P> vecD;
vecD = { 2.0+2.0i, -1.0-1.0i }; // dim = P
std::cout << "Non-symmetric Linear system size: " << matA.rows() << " x " << matA.cols() << "\n";
std::cout << "Constraint matrix size: " << matC.rows() << " x " << matC.cols() << "\n";
// Solve and request residual errors of the object function and the constraint.
double normAxMinusB = 0, normConstraintViolation = 0;
Vector<T, M> vecX = constrainedSolver(matA, vecB, matC, vecD, SymmetricMode::NonSymmetricMatrix,
0, // Default relative error tolerance
0, // Default maximum number of iterations
&normAxMinusB, // Residual |A*x - b|
&normConstraintViolation); // Residual |C*x - d|
std::cout << "Solution x to min |Ax-b| subject to Cx=d (real,img): " << vecX << std::endl;
std::cout << "Object function residual |Ax-b| = " << normAxMinusB
<< ", constraint violation norm |Cx - d| = " << normConstraintViolation << "\n";
}
{
using T = double;
std::cout << "\n*** Example cs.3 *** : Constrained solver with stack-allocated real symmetric matrix.\n";
// Minimize |A*x - b| subject to C*x = d.
// Dimensions:
constexpr size_t N = 3;
constexpr size_t M = 3;
constexpr size_t P = 2;
Matrix<T, N, M> matA;
matA[0] = {119, 95, 99};
matA[1] = {95, 79, 82};
matA[2] = {99, 82, 90};
Vector<T, N> vecB;
vecB = { 1, 2, 3 };
// ... with constraints C*x = d.
Matrix<T, P, M> matC;
matC[0] = {1, 0, 0};
matC[1] = {0, 1, 0};
Vector<T, P> vecD;
vecD = { 2, -1 };
std::cout << "Symmetric Linear system size: " << matA.rows() << " x " << matA.cols() << "\n";
std::cout << "Constraint matrix size: " << matC.rows() << " x " << matC.cols() << "\n";
// Solve and request residual errors of the object function and the constraint.
double normAxMinusB = 0, normConstraintViolation = 0;
Vector<T, M> vecX = constrainedSolver(matA, vecB, matC, vecD, SymmetricMode::SymmetricMatrix,
0, // Default relative error tolerance
0, // Default maximum number of iterations
&normAxMinusB, // Residual |A*x - b|
&normConstraintViolation); // Residual |C*x - d|
std::cout << "Solution x to min |Ax-b| subject to Cx=d: " << vecX << std::endl;
std::cout << "Object function residual |Ax-b| = " << normAxMinusB
<< ", constraint violation norm |Cx - d| = " << normConstraintViolation << "\n";
}
{
using T = std::complex<double>;
std::cout << "\n*** Example cs.4 *** : Constrained solver with heap-allocated complex symmetric matrix.\n";
// Minimize |A*x - b| subject to C*x = d.
// Dimensions:
const size_t N = 3;
const size_t M = 3;
const size_t P = 2;
Matrix<T> matA(N, M);
matA[0] = {221.0, 173.0+34.0i, 161.0+38.0i};
matA[1] = {173.0-34.0i, 160.0, 154.0-3.0i};
matA[2] = {161.0-38.0i, 154.0+3.0i, 156.0};
Vector<T> vecB(M);
vecB = { 1.0+1.0i, 2.0-1.0i, 3.0+1.0i };
// ... with constraints C*x = d.
Matrix<T> matC(P, M);
matC[0] = {1.0i, 0., 0.};
matC[1] = {0., 1.0i, 0.};
Vector<T> vecD(P);
vecD = { 2.0+2.0i, -1.0-1.0i };
std::cout << "Non-symmetric Linear system size: " << matA.rows() << " x " << matA.cols() << "\n";
std::cout << "Constraint matrix size: " << matC.rows() << " x " << matC.cols() << "\n";
// Solve and request residual errors of the object function and the constraint.
double normAxMinusB = 0, normConstraintViolation = 0;
Vector<T> vecX = constrainedSolver(matA, vecB, matC, vecD, SymmetricMode::SymmetricMatrix,
0, // Default relative error tolerance
0, // Default maximum number of iterations
&normAxMinusB, // Residual |A*x - b|
&normConstraintViolation); // Residual |C*x - d|
std::cout << "Solution x to min |Ax-b| subject to Cx=d (real, imag): " << vecX << std::endl;
std::cout << "Object function residual |Ax-b| = " << normAxMinusB
<< ", constraint violation norm |Cx - d| = " << normConstraintViolation << "\n";
}
{
using T = double;
std::cout << "\n*** Example cs.5 *** : Measure time vs accuracy tradeoff with a huge heap-allocated matrices.\n";
std::cout << " (May take a while. Prints elapsed time for each relative error tolerance.)\n";
const size_t M = 1000; // Linear system size
const size_t P = 1; // Number of constraints
// Solve A*x = b ....
Matrix<T> matA(M, M);
Vector<T> vecB(M);
// ... with constraints C*x = d.
Matrix<T> matC(P, M);
Vector<T> vecD(P);
// Solve a 1000-by-1000 linear system A*x = b with a constraint
// which requires that the sum of all elements in vector x is 1.
// Fill matrix A and vector b with random numbers.
for (int i = 0; i < M; ++i) {
vecB[i] = (std::rand() % 2048) - 1024;
for (int j = 0; j < M; ++j)
matA[i][j] = (std::rand() % 2048) - 1024;
}
// Set constraint C = [1, 1, ..., 1] and d = 1, so C*x = 1.
// So the sum of the elements of x should be 1.
for (int i = 0; i < M; ++i)
matC[0][i] = 1;
vecD[0] = 1;
std::cout << "Case A: Non-symmetric Linear system, size: " << matA.rows() << " x " << matA.cols() << "\n";
std::cout << " Constraint: sum of elements of x should be 1\n";
// Residual error tolerances for conjugate gradient solver.
std::vector<T> aErrorTol = {1e-2, 1e-3, 1e-6, 1e-9};
std::vector<double> aResidual(aErrorTol.size());
std::vector<double> aConstraintViolation(aErrorTol.size());
std::vector<double> aRunTime(aErrorTol.size());
for (int iTol = 0; iTol < aErrorTol.size(); ++iTol) {
std::cout << "Tolerance = " << aErrorTol[iTol] << ": " << std::flush;
auto start = std::chrono::system_clock::now();
Vector<T> vecX = constrainedSolver(matA, vecB, matC, vecD, SymmetricMode::NonSymmetricMatrix,
aErrorTol[iTol],
0, // Use default maximum number of iterations.
&aResidual[iTol],
&aConstraintViolation[iTol]);
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> diff = end-start;
// Double check: the sum of all elements in X should be 1:
double sum = 0;
for (auto x : vecX)
sum += x;
std::cout << "residual |Ax-b| = " << aResidual[iTol]
<< ", constraint violation |Cx-d| = " << aConstraintViolation[iTol]
<< ", sum of elements of x = " << sum
<< ", time = " << diff.count() << "\n";
}
// Replace b with A'*b and A with A'*A to get a symmetric system.
// It should be much faster to solve.
vecB = multiplyTranspose(matA, vecB);
matA = inner(matA);
std::cout << "\nCase B: Symmetric Linear system, size: " << matA.rows() << " x " << matA.cols() << "\n";
std::cout << " Constraint: sum of elements of x should be 1\n";
for (int iTol = 0; iTol < aErrorTol.size(); ++iTol) {
std::cout << "Tolerance = " << aErrorTol[iTol] << ": " << std::flush;
auto start = std::chrono::system_clock::now();
Vector<T> vecX = constrainedSolver(matA, vecB, matC, vecD, SymmetricMode::SymmetricMatrix,
aErrorTol[iTol],
0, // Use default maximum number of iterations.
&aResidual[iTol],
&aConstraintViolation[iTol]);
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> diff = end-start;
// Double check: the sum of all elements in X should be 1:
double sum = 0;
for (auto x : vecX)
sum += x;
std::cout << "residual |Ax-b| = " << aResidual[iTol]
<< ", constraint violation |Cx-d| = " << aConstraintViolation[iTol]
<< ", sum of elements of x = " << sum
<< ", time = " << diff.count() << "\n";
}
}
}