|
1 | 1 | #include <iostream>
|
| 2 | +#include <chrono> |
2 | 3 | #include <vector>
|
3 | 4 | #include "Eigen/Dense"
|
4 | 5 | #include "Eigen/Sparse"
|
|
19 | 20 | namespace cddp {
|
20 | 21 |
|
21 | 22 | // Constructor
|
22 |
| -CDDPProblem::CDDPProblem(cddp::DynamicalSystem* system, const Eigen::VectorXd& initialState, int horizon, double timestep) : |
23 |
| - dynamics_(system), |
24 |
| - initial_state_(initialState), |
25 |
| - goal_state_(initialState), // Pre-allocate goal to initialState |
| 23 | +CDDPProblem::CDDPProblem(const Eigen::VectorXd& initial_state, const Eigen::VectorXd& goal_state, int horizon, double timestep) : |
| 24 | + initial_state_(initial_state), |
| 25 | + goal_state_(goal_state), // Pre-allocate goal to initialState |
26 | 26 | horizon_(horizon),
|
27 | 27 | dt_(timestep)
|
28 | 28 | {
|
29 | 29 | // Initialize Trajectory
|
30 |
| - X_.resize(horizon + 1, initial_state_); |
31 |
| - U_.resize(horizon, Eigen::VectorXd::Zero(system->control_size_)); |
32 |
| - |
33 |
| - // Initialize Intermediate Cost |
34 |
| - J_ = 0.0; |
35 |
| - |
36 |
| - k_.resize(horizon, Eigen::VectorXd::Zero(system->control_size_)); |
37 |
| - K_.resize(horizon, Eigen::MatrixXd::Zero(system->control_size_, system->state_size_)); |
38 |
| - |
39 |
| - // Initialize Intermediate value function |
40 |
| - V_.resize(horizon + 1, 0.0); |
41 |
| - V_X_.resize(horizon + 1, Eigen::VectorXd::Zero(system->state_size_)); |
42 |
| - V_XX_.resize(horizon + 1, Eigen::MatrixXd::Zero(system->state_size_, system->state_size_)); |
43 |
| - |
44 |
| - // Initialize Q-function Matrices |
45 |
| - Q_UU_.resize(horizon, Eigen::MatrixXd::Zero(system->control_size_, system->control_size_)); |
46 |
| - Q_UX_.resize(horizon, Eigen::MatrixXd::Zero(system->control_size_, system->state_size_)); |
47 |
| - Q_U_.resize(horizon, Eigen::VectorXd::Zero(system->control_size_)); |
| 30 | + // X_.resize(horizon + 1, initial_state_); |
| 31 | + // U_.resize(horizon, Eigen::VectorXd::Zero(system->control_size_)); |
48 | 32 | }
|
49 | 33 |
|
50 | 34 | // Setup Methods
|
@@ -83,6 +67,32 @@ void CDDPProblem::setOptions(const CDDPOptions& opts) {
|
83 | 67 | options_ = opts;
|
84 | 68 | }
|
85 | 69 |
|
| 70 | +void CDDPProblem::setDynamicalSystem(std::unique_ptr<DynamicalSystem> dynamics) { |
| 71 | + dynamics_ = std::move(dynamics); |
| 72 | + |
| 73 | + // Initialize Trajectory |
| 74 | + X_.resize(horizon_ + 1, initial_state_); |
| 75 | + U_.resize(horizon_, Eigen::VectorXd::Zero(dynamics_->control_size_)); |
| 76 | + |
| 77 | + // Initialize Intermediate Cost |
| 78 | + J_ = 0.0; |
| 79 | + |
| 80 | + // Initialize Control Gains |
| 81 | + k_.resize(horizon_, Eigen::VectorXd::Zero(dynamics_->control_size_)); |
| 82 | + K_.resize(horizon_, Eigen::MatrixXd::Zero(dynamics_->control_size_, dynamics_->state_size_)); |
| 83 | + |
| 84 | + // Initialize Intermediate value function |
| 85 | + V_.resize(horizon_ + 1, 0.0); |
| 86 | + V_X_.resize(horizon_ + 1, Eigen::VectorXd::Zero(dynamics_->state_size_)); |
| 87 | + V_XX_.resize(horizon_ + 1, Eigen::MatrixXd::Zero(dynamics_->state_size_, dynamics_->state_size_)); |
| 88 | + |
| 89 | + // Initialize Q-function Matrices |
| 90 | + Q_UU_.resize(horizon_, Eigen::MatrixXd::Zero(dynamics_->control_size_, dynamics_->control_size_)); |
| 91 | + Q_UX_.resize(horizon_, Eigen::MatrixXd::Zero(dynamics_->control_size_, dynamics_->state_size_)); |
| 92 | + Q_U_.resize(horizon_, Eigen::VectorXd::Zero(dynamics_->control_size_)); |
| 93 | + |
| 94 | +} |
| 95 | + |
86 | 96 | void CDDPProblem::setObjective(std::unique_ptr<Objective> objective) {
|
87 | 97 | objective_ = std::move(objective);
|
88 | 98 | }
|
@@ -160,10 +170,13 @@ std::vector<Eigen::VectorXd> CDDPProblem::solve() {
|
160 | 170 | double J = std::numeric_limits<double>::infinity();
|
161 | 171 | double gradientNorm = std::numeric_limits<double>::infinity();
|
162 | 172 |
|
| 173 | + // Start the timer |
| 174 | + auto start_time = std::chrono::high_resolution_clock::now(); |
| 175 | + |
163 | 176 | // 2. Main CDDP Iterative Loop
|
164 | 177 | for (int iter = 0; iter < options_.max_iterations; ++iter) {
|
165 | 178 | std::cout << "Iteration: " << iter << std::endl;
|
166 |
| -std::cout << "Cost " << J_ << std::endl; |
| 179 | +std::cout << "Cost " << J_ << std::endl; |
167 | 180 | double J_old = J;
|
168 | 181 | // 3. Backward
|
169 | 182 | bool backward_pass_done = solveBackwardPass();
|
@@ -200,6 +213,15 @@ std::cout << "Cost " << J_ << std::endl;
|
200 | 213 |
|
201 | 214 | }
|
202 | 215 |
|
| 216 | + // Stop the timer |
| 217 | + auto end_time = std::chrono::high_resolution_clock::now(); |
| 218 | + |
| 219 | + // Calculate the elapsed time |
| 220 | + auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time); |
| 221 | + |
| 222 | + // Print the elapsed time |
| 223 | +std::cout << "Solver time: " << duration.count() << " ms" << std::endl; |
| 224 | + |
203 | 225 | // 6. Return Optimal Control Sequence
|
204 | 226 | // place holder
|
205 | 227 | return U_;
|
|
0 commit comments