cyqlone develop
Fast, parallel and vectorized solver for linear systems with optimal control structure.
Loading...
Searching...
No Matches
random-ocp.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cyqlone/config.hpp>
4#include <cyqlone/ocp.hpp>
5#include <random>
6
7namespace cyqlone {
8
9/// Generates a random linear OCP with the given dimensions and random seed.
10/// @ingroup topic-ocp-formulations
11inline LinearOCPStorage generate_random_ocp(OCPDim dim, uint_fast32_t seed = 0) {
12 LinearOCPStorage ocp{dim};
13 auto [N, nx, nu, ny, ny_N] = ocp.dim;
14
15 std::mt19937 rng{seed};
16 std::uniform_real_distribution<real_t> uni{-1, 1};
17 std::bernoulli_distribution bernoulli{0.5};
18
19 // Dynamics and constraint matrices
20 for (index_t i = 0; i < N; ++i) {
21 auto Ai = ocp.A(i), Bi = ocp.B(i), Ci = ocp.C(i), Di = ocp.D(i);
22 auto Qi = ocp.Q(i), Ri = ocp.R(i), Si = ocp.S(i), Hi = ocp.H(i);
23 Ai.generate([&] { return uni(rng) * 0.9; });
24 Bi.generate([&] { return uni(rng); });
25 Ci.generate([&] { return uni(rng); });
26 Di.generate([&] { return uni(rng); });
27 Qi.generate([&] { return uni(rng); });
28 Ri.generate([&] { return uni(rng); });
29 Si.generate([&] { return uni(rng); });
30 for (index_t j = 0; j < nx; ++j)
31 Qi(j, j) += 2 * static_cast<real_t>(nx + nu);
32 for (index_t j = 0; j < nu; ++j)
33 Ri(j, j) += 2 * static_cast<real_t>(nx + nu);
34 for (index_t c = 0; c < nx + nu; ++c)
35 for (index_t r = c + 1; r < nx + nu; ++r)
36 Hi(c, r) = Hi(r, c);
37 }
38 auto Ci = ocp.C(N), Qi = ocp.Q(N);
39 Ci.generate([&] { return uni(rng); });
40 Qi.generate([&] { return uni(rng); });
41 for (index_t j = 0; j < nx; ++j)
42 Qi(j, j) += 2 * static_cast<real_t>(nx);
43 for (index_t c = 0; c < nx; ++c)
44 for (index_t r = c + 1; r < nx; ++r)
45 Qi(c, r) = Qi(r, c);
46 return ocp;
47}
48
49} // namespace cyqlone
LinearOCPStorage generate_random_ocp(OCPDim dim, uint_fast32_t seed=0)
Generates a random linear OCP with the given dimensions and random seed.
Data structure for optimal control problems.
Storage for a linear-quadratic OCP of the form.
Definition ocp.hpp:37
guanaqo::MatrixView< real_t, index_t > B(index_t i)
Definition ocp.hpp:132
guanaqo::MatrixView< real_t, index_t > D(index_t i)
Definition ocp.hpp:111
guanaqo::MatrixView< real_t, index_t > R(index_t i)
Definition ocp.hpp:80
guanaqo::MatrixView< real_t, index_t > H(index_t i)
Definition ocp.hpp:64
guanaqo::MatrixView< real_t, index_t > C(index_t i)
Definition ocp.hpp:106
guanaqo::MatrixView< real_t, index_t > S(index_t i)
Definition ocp.hpp:85
guanaqo::MatrixView< real_t, index_t > Q(index_t i)
Definition ocp.hpp:75
guanaqo::MatrixView< real_t, index_t > A(index_t i)
Definition ocp.hpp:127
Dimensions of an optimal control problem.
Definition ocp.hpp:16