cyqlone develop
Fast, parallel and vectorized solver for linear systems with optimal control structure.
Loading...
Searching...
No Matches
csv.cpp
Go to the documentation of this file.
2#include <guanaqo/eigen/view.hpp>
3#include <guanaqo/io/csv.hpp>
4
5#include <filesystem>
6#include <fstream>
7
8namespace CYQLONE_NS(cyqlone::qpalm::problems) {
9
10using eigen_mat = Eigen::MatrixX<real_t>;
11
12LinearOCPStorage load_from_csv(const fs::path &folder, const std::string &name) {
13 using guanaqo::as_view;
14
15 std::array<index_t, 5> dims_array;
16 {
17 std::ifstream dims_file = folder / ("dim-" + name + ".csv");
18 guanaqo::io::csv_read_row(dims_file, std::span{dims_array});
19 }
20 auto [N, nx, nu, ny, ny_N] = dims_array;
21
22 LinearOCPStorage ocp{.dim{
23 .N_horiz = N,
24 .nx = nx,
25 .nu = nu,
26 .ny = ny,
27 .ny_N = ny_N,
28 }};
29
30 // Continuous-time dynamics
31 eigen_mat A = eigen_mat::Zero(nx, nx), B = eigen_mat::Zero(nx, nu), C = eigen_mat::Zero(ny, nx),
32 D = eigen_mat::Zero(ny, nu), CN = eigen_mat::Zero(ny_N, nx),
33 QN = eigen_mat::Zero(nx, nx), Q = eigen_mat::Zero(nx, nx),
34 R = eigen_mat::Zero(nu, nu);
35 {
36 std::ifstream f = folder / ("A-" + name + ".csv");
37 guanaqo::io::csv_read(f, as_view(A));
38 }
39 {
40 std::ifstream f = folder / ("B-" + name + ".csv");
41 guanaqo::io::csv_read(f, as_view(B));
42 }
43 {
44 std::ifstream f = folder / ("C-" + name + ".csv");
45 guanaqo::io::csv_read(f, as_view(C));
46 }
47 {
48 std::ifstream f = folder / ("D-" + name + ".csv");
49 guanaqo::io::csv_read(f, as_view(D));
50 }
51 {
52 std::ifstream f = folder / ("CN-" + name + ".csv");
53 guanaqo::io::csv_read(f, as_view(CN));
54 }
55 {
56 std::ifstream f = folder / ("QN-" + name + ".csv");
57 guanaqo::io::csv_read(f, as_view(QN));
58 }
59 {
60 std::ifstream f = folder / ("Q-" + name + ".csv");
61 guanaqo::io::csv_read(f, as_view(Q));
62 }
63 {
64 std::ifstream f = folder / ("R-" + name + ".csv");
65 guanaqo::io::csv_read(f, as_view(R));
66 }
67
68 // Reference states and inputs
69 std::vector<real_t> qr(N * (nx + nu) + nx);
70 {
71 std::ifstream f = folder / ("q-" + name + ".csv");
72 guanaqo::io::csv_read_row(f, std::span{qr});
73 }
74
75 // Constraints rhs
76 std::vector<real_t> eq((N + 1) * nx), lb(N * ny + ny_N), ub(N * ny + ny_N);
77 {
78 std::ifstream f = folder / ("eq-" + name + ".csv");
79 guanaqo::io::csv_read_row(f, std::span{eq});
80 }
81 {
82 std::ifstream f = folder / ("lb-" + name + ".csv");
83 guanaqo::io::csv_read_row(f, std::span{lb});
84 }
85 {
86 std::ifstream f = folder / ("ub-" + name + ".csv");
87 guanaqo::io::csv_read_row(f, std::span{ub});
88 }
89
90 // Dynamics and constraint matrices
91 for (index_t i = 0; i < N; ++i) {
92 auto Ai = ocp.A(i), Bi = ocp.B(i), Ci = ocp.C(i), Di = ocp.D(i);
93 auto Qi = ocp.Q(i), Ri = ocp.R(i);
94 Ai = as_view(A);
95 Bi = as_view(B);
96 Ci = as_view(C);
97 Di = as_view(D);
98 Qi = as_view(Q);
99 Ri = as_view(R);
100 }
101 auto Ci = ocp.C(N), Qi = ocp.Q(N);
102 Ci = as_view(CN);
103 Qi = as_view(QN);
104
105 // TODO: unnecessary copy
106 ocp.qr() = decltype(ocp.qr())::as_column(std::span{qr});
107 ocp.b() = decltype(ocp.b())::as_column(std::span{eq});
108 ocp.b_min() = decltype(ocp.b_min())::as_column(std::span{lb});
109 ocp.b_max() = decltype(ocp.b_max())::as_column(std::span{ub});
110
111 return ocp;
112}
113
114} // namespace CYQLONE_NS(cyqlone::qpalm::problems)
#define CYQLONE_NS(ns)
Definition config.hpp:10
void csv_read_row(std::istream &is, std::span< F > v, char sep)
void csv_read(std::istream &is, MatrixView< F, ptrdiff_t, ptrdiff_t > v, char sep)
auto as_view(Eigen::DenseBase< Derived > &M, with_index_type_t< I >={})
LinearOCPStorage load_from_csv(const fs::path &folder, const std::string &name)
Definition csv.cpp:12
Eigen::MatrixX< real_t > eigen_mat
Definition zoh.hpp:9
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 > b_min()
Definition ocp.hpp:267
guanaqo::MatrixView< real_t, index_t > b()
Definition ocp.hpp:251
guanaqo::MatrixView< real_t, index_t > R(index_t i)
Definition ocp.hpp:80
guanaqo::MatrixView< real_t, index_t > C(index_t i)
Definition ocp.hpp:106
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
guanaqo::MatrixView< real_t, index_t > b_max()
Definition ocp.hpp:283
guanaqo::MatrixView< real_t, index_t > qr()
Definition ocp.hpp:225