forked from bmatthiesen/random-psd-matrices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
belief_state_ompl.cpp
431 lines (372 loc) · 13.5 KB
/
belief_state_ompl.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// belief_state_ompl.cpp
// Include OMPL headers
#include <ompl/base/SpaceInformation.h>
#include <ompl/base/ValidStateSampler.h>
#include <ompl/base/spaces/RealVectorStateSpace.h>
#include <ompl/util/RandomNumbers.h>
// Include standard headers and Eigen
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <complex>
#include <random>
#include <stdexcept>
#include <Eigen/Dense>
#include <Eigen/Eigenvalues> // For eigenvalues computation
namespace ob = ompl::base;
// Function declarations (as provided)
void randpdm(int dim, const std::vector<double>& trace, int num, const std::string& type,
const std::string& method, std::vector<Eigen::MatrixXd>& A);
Eigen::MatrixXd makeA(int dim, const std::vector<double>& phi, bool is_complex);
void zf_real(int n, std::vector<double>& h, std::vector<double>& g);
void zf_complex(int n, std::vector<double>& h, std::vector<double>& g);
void rndtrace(int dim, double lb, double ub, int num, const std::string& type,
std::vector<double>& tau);
// Custom valid state sampler
class MyValidStateSampler : public ob::ValidStateSampler
{
public:
MyValidStateSampler(const ob::SpaceInformation *si, int dimension) : ValidStateSampler(si), d(dimension)
{
name_ = "MyValidStateSampler";
// Calculate the size of the net vector: d + (d*(d+1))/2
net_vector_size = d + (d * (d + 1)) / 2;
}
// Generate a sample in the valid part of the state space
bool sample(ob::State *state) override
{
// Sample x ∈ [-1,1]^d
Eigen::VectorXd x(d);
for (int i = 0; i < d; ++i) {
x(i) = rng_.uniformReal(-1.0, 1.0);
}
// Generate a positive definite matrix using randpdm
std::vector<double> trace = {1.0}; // Trace of the matrix
int num = 1; // Number of matrices to generate
std::string type = "real"; // "real" or "complex"
std::string method = "rejection"; // "rejection" or "betadistr"
std::vector<Eigen::MatrixXd> A_list; // Output matrices
randpdm(d, trace, num, type, method, A_list);
// Extract the generated positive definite matrix
Eigen::MatrixXd A = A_list[0];
// Vectorize A by extracting upper triangular elements including the diagonal
std::vector<double> A_vectorized;
for (int i = 0; i < d; ++i) {
for (int j = i; j < d; ++j) { // j >= i
A_vectorized.push_back(A(i, j));
}
}
// Create net vector: [x; vectorized A]
std::vector<double> net_vector;
net_vector.reserve(x.size() + A_vectorized.size());
// Append x to net_vector
net_vector.insert(net_vector.end(), x.data(), x.data() + x.size());
// Append vectorized A to net_vector
net_vector.insert(net_vector.end(), A_vectorized.begin(), A_vectorized.end());
// Assign net_vector to the state
auto *rv_state = state->as<ob::RealVectorStateSpace::StateType>();
for (size_t i = 0; i < net_vector.size(); ++i) {
rv_state->values[i] = net_vector[i];
}
return true;
}
// Implement the sampleNear function
bool sampleNear(ob::State *state, const ob::State *near, double distance) override
{
// For now, we'll indicate that sampling near a state is not implemented.
return false;
}
protected:
ompl::RNG rng_;
int d; // Dimension of x and A
size_t net_vector_size; // Total size of the net vector
};
// Implement your functions here (as provided)
// randpdm function and dependencies
void randpdm(int dim, const std::vector<double>& trace, int num, const std::string& type,
const std::string& method, std::vector<Eigen::MatrixXd>& A) {
// Determine method
bool rejection = (method == "rejection");
// Determine type
bool is_complex = (type == "complex");
// Get h and g
std::vector<double> h, g;
if (is_complex) {
zf_complex(dim, h, g);
} else {
zf_real(dim, h, g);
}
int phi_n = h.size();
// Process trace
std::vector<double> tau(num);
if (trace.size() == 1) {
std::fill(tau.begin(), tau.end(), trace[0]);
} else if (trace.size() == 2) {
if (trace[0] == trace[1]) {
std::fill(tau.begin(), tau.end(), trace[0]);
} else {
rndtrace(dim, trace[0], trace[1], num, type, tau);
}
} else {
throw std::invalid_argument("Trace has to be a scalar or [lb, ub]!");
}
// Initialize A
A.resize(num);
for (int i = 0; i < num; ++i) {
A[i] = Eigen::MatrixXd::Zero(dim, dim);
}
// Random number generators
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<> normal_dist(0.0, 1.0);
std::uniform_real_distribution<> uniform_dist(0.0, 1.0);
if (rejection) {
// Rejection method
for (int ii = 0; ii < num; ++ii) {
std::vector<double> phi(phi_n, 0.0);
for (int l = 0; l < phi_n; ++l) {
double Xn;
if (h[l] == 0) {
double sigma = sqrt(1.0 / g[l]);
while (true) {
Xn = normal_dist(gen) * sigma + M_PI / 2.0;
double Un = uniform_dist(gen);
double tmp;
if (0.0 <= Xn && Xn <= M_PI) {
tmp = pow(sin(Xn), g[l]);
} else {
tmp = 0.0;
}
if (Un <= tmp * exp((Xn - M_PI / 2.0) * (Xn - M_PI / 2.0) / (2.0 / g[l]))) {
break;
}
}
} else {
double mu = atan(sqrt(g[l] / h[l]));
double sigma = 1.0 / (sqrt(h[l]) + sqrt(g[l]));
double sigmasq = sigma * sigma;
double a = sqrt(1.0 + g[l] / h[l]);
double b = sqrt(1.0 + h[l] / g[l]);
while (true) {
Xn = normal_dist(gen) * sigma + mu;
double Un = uniform_dist(gen);
double tmp;
if (0.0 <= Xn && Xn <= M_PI / 2.0) {
tmp = pow(a * cos(Xn), h[l]) * pow(b * sin(Xn), g[l]);
} else {
tmp = 0.0;
}
if (Un <= tmp * exp((Xn - mu) * (Xn - mu) / (2.0 * sigmasq))) {
break;
}
}
}
phi[l] = Xn;
}
A[ii] = tau[ii] * makeA(dim, phi, is_complex);
}
} else {
// Beta distribution method
for (int ii = 0; ii < num; ++ii) {
std::vector<double> phi(phi_n, 0.0);
// Generate random variates with beta distribution
for (int l = 0; l < phi_n; ++l) {
double gam_a_shape = (g[l] + 1.0) / 2.0;
double gam_b_shape = (h[l] + 1.0) / 2.0;
std::gamma_distribution<> gamma_a(gam_a_shape, 1.0);
std::gamma_distribution<> gamma_b(gam_b_shape, 1.0);
double gam_a = gamma_a(gen);
double gam_b = gamma_b(gen);
double y = gam_a / (gam_a + gam_b);
phi[l] = asin(sqrt(y));
}
// Bernoulli distributed random variates
for (int l = 0; l < phi_n; ++l) {
if (h[l] == 0 && uniform_dist(gen) <= 0.5) {
phi[l] = M_PI - phi[l];
}
}
A[ii] = tau[ii] * makeA(dim, phi, is_complex);
}
}
}
Eigen::MatrixXd makeA(int dim, const std::vector<double>& phi, bool is_complex) {
if (is_complex) {
// Complex case (not used in this example)
Eigen::MatrixXcd T = Eigen::MatrixXcd::Zero(dim, dim);
std::vector<double> x(phi.size() + 1, 0.0);
double l_val = 1.0;
for (size_t i = 0; i < phi.size(); ++i) {
x[i] = l_val * cos(phi[i]);
l_val *= sin(phi[i]);
}
x[phi.size()] = l_val;
int idx = 0;
for (int m = 0; m < dim; ++m) {
int idx2 = idx + 2 * m + 1;
for (int i = 0; i <= m; ++i) {
double real_part = x[idx + 2 * i];
double imag_part = (idx + 2 * i + 1 < x.size()) ? x[idx + 2 * i + 1] : 0.0;
T(i, m) = std::complex<double>(real_part, imag_part);
}
idx = idx2;
}
return (T.adjoint() * T).real();
} else {
// Real case
Eigen::MatrixXd T = Eigen::MatrixXd::Zero(dim, dim);
std::vector<double> x(phi.size() + 1, 0.0);
double l_val = 1.0;
for (size_t i = 0; i < phi.size(); ++i) {
x[i] = l_val * cos(phi[i]);
l_val *= sin(phi[i]);
}
x[phi.size()] = l_val;
int idx = 0;
for (int m = 0; m < dim; ++m) {
int idx2 = idx + m + 1;
for (int i = 0; i <= m; ++i) {
T(i, m) = x[idx + i];
}
idx = idx2;
}
return T.transpose() * T;
}
}
void zf_real(int n, std::vector<double>& h, std::vector<double>& g) {
int size = n * (n + 1) / 2 - 1;
h.assign(size, 0.0);
g.assign(size, 0.0);
std::vector<double> a(size, 0.0);
std::vector<double> b(size, 0.0);
for (int k = 1; k <= n - 1; ++k) {
int idx = k * (k + 1) / 2 - 1;
if (idx < size) {
h[idx] = n + 1 - k;
}
}
for (int ii = 1; ii <= n - 1; ++ii) {
for (int m = 0; m <= ii; ++m) {
int l = ii * (ii + 1) / 2 + m - 1;
if (l < size) {
a[l] = ii - 1;
b[l] = ii + 1 + m;
}
}
}
for (int i = 0; i < size; ++i) {
g[i] = n * n - a[i] * n - b[i];
}
}
void zf_complex(int n, std::vector<double>& h, std::vector<double>& g) {
int size = n * n - 1;
h.assign(size, 0.0);
g.assign(size, 0.0);
std::vector<double> a(size, 0.0);
std::vector<double> b(size, 0.0);
for (int k = 1; k <= n - 1; ++k) {
int idx = k * k - 1;
if (idx < size) {
h[idx] = 2 * (n - k) + 1;
}
}
for (int ii = 1; ii <= n - 1; ++ii) {
for (int m = 0; m <= 2 * ii; ++m) {
int l = ii * ii + m - 1;
if (l < size) {
a[l] = n - ii - 1;
b[l] = (ii - 1) * n + 1 + m;
}
}
}
for (int i = 0; i < size; ++i) {
g[i] = n * n + a[i] * n - b[i];
}
}
void rndtrace(int dim, double lb, double ub, int num, const std::string& type,
std::vector<double>& tau) {
double a;
if (type == "complex") {
a = dim * dim;
} else if (type == "real") {
a = (dim * dim + dim) / 2.0;
} else {
throw std::invalid_argument("Unknown type. Use either 'real' or 'complex'.");
}
if (lb < 0) {
throw std::invalid_argument("Trace may not be negative.");
}
if (ub < lb) {
throw std::invalid_argument("Upper bound must be greater than lower bound!");
}
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> uniform_dist(0.0, 1.0);
tau.resize(num);
for (int i = 0; i < num; ++i) {
double u = uniform_dist(gen);
if (lb == 0) {
tau[i] = ub * pow(u, 1.0 / a);
} else {
tau[i] = lb * pow(((pow(ub / lb, a) - 1) * u + 1), 1.0 / a);
}
}
}
int main()
{
// Define the dimension d
int d = 3;
// Create the state space of appropriate dimension
size_t net_vector_size = d + (d * (d + 1)) / 2;
ob::StateSpacePtr space(new ob::RealVectorStateSpace(net_vector_size));
// Create a SpaceInformation object
ob::SpaceInformationPtr si(new ob::SpaceInformation(space));
// Create an instance of MyValidStateSampler
MyValidStateSampler sampler(si.get(), d);
// Allocate space for a state
ob::State *state = space->allocState();
// Sample a state
sampler.sample(state);
// Retrieve the sampled net vector
auto *rv_state = state->as<ob::RealVectorStateSpace::StateType>();
std::vector<double> net_vector(net_vector_size);
for (size_t i = 0; i < net_vector_size; ++i) {
net_vector[i] = rv_state->values[i];
}
// Print the net vector
std::cout << "Net vector (size " << net_vector.size() << "):\n";
for (size_t i = 0; i < net_vector.size(); ++i) {
std::cout << net_vector[i] << " ";
}
std::cout << "\n\n";
// Print x and A separately
// Extract x
std::vector<double> x(net_vector.begin(), net_vector.begin() + d);
// Extract A_vectorized
std::vector<double> A_vectorized(net_vector.begin() + d, net_vector.end());
// Reconstruct A from A_vectorized
Eigen::MatrixXd A = Eigen::MatrixXd::Zero(d, d);
size_t idx = 0;
for (int i = 0; i < d; ++i) {
for (int j = i; j < d; ++j) {
A(i, j) = A_vectorized[idx];
if (i != j) {
A(j, i) = A_vectorized[idx]; // Since A is symmetric
}
++idx;
}
}
// Print x
std::cout << "State vector x (size " << x.size() << "):\n";
for (size_t i = 0; i < x.size(); ++i) {
std::cout << x[i] << " ";
}
std::cout << "\n\n";
// Print A
std::cout << "Positive definite matrix A (size " << d << "x" << d << "):\n";
std::cout << A << "\n\n";
// Clean up
space->freeState(state);
return 0;
}