cyqlone develop
Fast, parallel and vectorized solver for linear systems with optimal control structure.
Loading...
Searching...
No Matches
spring-mass.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cyqlone/config.hpp>
4#include <cyqlone/example-problems/export.h>
5#include <cyqlone/ocp.hpp>
6
7#include <vector>
8
10
12 real_t friction = 0; ///< friction coefficient
13 real_t k_spring = 1; ///< spring constant between masses
14 real_t F_max = 0.5; ///< maximum actuator force
15 real_t p_max = 4; ///< maximum displacement of each mass
16 real_t p_min = -p_max; ///< minimum displacement of each mass
17 real_t p_min_f = p_min; ///< minimum displacement of each mass for the final stage
18 real_t p_max_f = p_max; ///< maximum displacement of each mass for the final stage
19 real_t v_max = 0; ///< maximum velocity of each mass (zero = no limit)
20 real_t v_max_f = v_max; ///< maximum velocity of each mass for the final stage
21 real_t width = 1; ///< width of the setup (distance between the walls)
22 index_t N_horiz = 32; ///< number of discretization steps
23 real_t T_horiz = 15; ///< time horizon
24 real_t q_vel = 1; ///< scaling factor for cost terms for the velocity
25 real_t q_pos = 1; ///< scaling factor for cost terms for the position
26 real_t q_vel_f = q_vel; ///< scaling factor for terminal cost term for the velocity
27 real_t q_pos_f = q_pos; ///< scaling factor for terminal cost term for the position
28 real_t r_act = 1; ///< scaling factor for cost terms for the actuators
29 std::vector<real_t> masses{1, 1, 1, 1, 1, 1};
30 index_t n_actuators = static_cast<index_t>(masses.size()) - 1; ///< number of actuators
37 uint64_t seed = 0; ///< random seed for actuator placement
38
39 static SpringMassParams wang_boyd_2008(index_t n_masses, index_t N_horiz = 30,
40 uint64_t seed = 0) {
41 // Notes: Setting the width to zero does not match Fig. 1 in Wang & Boyd (2008), but it
42 // does match the values for q, qf and r in Wang & Boyd (2010) Sec. V.A.
43 // We use Qf = I: it is left unspecified in Wang & Boyd (2008), and determined
44 // heuristically in Wang & Boyd (2010).
45 return {
46 .friction = 0,
47 .k_spring = 1,
48 .F_max = 0.5,
49 .p_max = 4,
50 .width = 0,
51 .N_horiz = N_horiz,
52 .T_horiz = 15, // Ts = 0.5s for horizon N = 30
53 .q_vel = 1, // Q = I
54 .q_pos = 1,
55 .q_vel_f = 1, // Qf = I
56 .q_pos_f = 1,
57 .r_act = 1, // R = I
58 .masses = std::vector<real_t>(n_masses, 1),
59 .n_actuators = (n_masses + 1) / 2,
61 .seed = seed,
62 };
63 }
64
65 static SpringMassParams wang_boyd_2008_width(index_t n_masses, index_t N_horiz = 30,
66 uint64_t seed = 0,
67 double steady_state_spring_length = 0.1) {
68 auto params = wang_boyd_2008(n_masses, N_horiz, seed);
69 params.width = steady_state_spring_length * static_cast<real_t>(n_masses + 1);
70 return params;
71 }
72
73 static SpringMassParams domahidi_2012(index_t n_masses, index_t N_horiz, uint64_t seed = 0) {
74 const real_t Ts = 0.5;
75 return {
76 .friction = 0,
77 .k_spring = 1,
78 .F_max = 0.5,
79 .p_max = 4,
80 .v_max = 4,
81 .width = 0, // q = 0, r = 0
82 .N_horiz = N_horiz,
83 .T_horiz = Ts * static_cast<real_t>(N_horiz),
84 .q_vel = 3, // Q = 3I
85 .q_pos = 3,
86 .q_vel_f = 3, // Qf = Q
87 .q_pos_f = 3,
88 .r_act = 1, // R = I
89 .masses = std::vector<real_t>(n_masses, 1),
90 .n_actuators = n_masses - 1,
91 .actuator_placement = IndividualActuators,
92 .seed = seed,
93 };
94 }
95
96 static SpringMassParams active_state_constr(index_t n_masses = 18, index_t N_horiz = 256,
97 uint64_t seed = 0) {
98 std::vector<real_t> masses(n_masses, 1.0);
99 if (n_masses >= 3) {
100 // Lighter masses for stiffer dynamics
101 masses[n_masses / 2 - 1] = 0.2;
102 masses[n_masses / 2 + 0] = 0.5;
103 masses[n_masses / 2 + 1] = 0.2;
104 }
105 return {
106 .friction = 0,
107 .k_spring = 1,
108 .F_max = 0.5,
109 .p_max = 0.01,
110 .p_min = -1,
111 .p_min_f = -1e-3,
112 .p_max_f = +1e-3,
113 .v_max = 2,
114 .v_max_f = 1e-3,
115 .width = real_t(0.1) * static_cast<real_t>(n_masses + 1),
116 .N_horiz = N_horiz,
117 .T_horiz = 15,
118 .q_vel = 1,
119 .q_pos = 1,
120 .q_vel_f = 1e2,
121 .q_pos_f = 1e1,
122 .r_act = 1,
123 .masses = std::move(masses),
124 .n_actuators = n_masses / 2,
125 .actuator_placement = RandomActuators,
126 .seed = seed,
127 };
128 }
129};
130
133 std::vector<real_t> ref;
134};
135
136} // namespace cyqlone::qpalm::problems
137
139
140SpringMassProblem CYQLONE_EXAMPLE_PROBLEMS_EXPORT spring_mass(SpringMassParams p);
141
142} // namespace CYQLONE_NS(cyqlone::qpalm::problems)
#define CYQLONE_NS(ns)
Definition config.hpp:10
Data structure for optimal control problems.
Storage for a linear-quadratic OCP of the form.
Definition ocp.hpp:37
real_t r_act
scaling factor for cost terms for the actuators
real_t p_max_f
maximum displacement of each mass for the final stage
static SpringMassParams wang_boyd_2008_width(index_t n_masses, index_t N_horiz=30, uint64_t seed=0, double steady_state_spring_length=0.1)
real_t F_max
maximum actuator force
real_t q_vel_f
scaling factor for terminal cost term for the velocity
real_t p_min
minimum displacement of each mass
real_t v_max
maximum velocity of each mass (zero = no limit)
real_t v_max_f
maximum velocity of each mass for the final stage
uint64_t seed
random seed for actuator placement
real_t p_min_f
minimum displacement of each mass for the final stage
real_t width
width of the setup (distance between the walls)
real_t q_vel
scaling factor for cost terms for the velocity
real_t k_spring
spring constant between masses
static SpringMassParams active_state_constr(index_t n_masses=18, index_t N_horiz=256, uint64_t seed=0)
enum cyqlone::qpalm::problems::SpringMassParams::ActuatorPlacement actuator_placement
real_t q_pos_f
scaling factor for terminal cost term for the position
static SpringMassParams domahidi_2012(index_t n_masses, index_t N_horiz, uint64_t seed=0)
static SpringMassParams wang_boyd_2008(index_t n_masses, index_t N_horiz=30, uint64_t seed=0)
index_t n_actuators
number of actuators
real_t p_max
maximum displacement of each mass
index_t N_horiz
number of discretization steps
real_t q_pos
scaling factor for cost terms for the position