|
| 1 | +/* |
| 2 | + Copyright 2024 Tomo Sasaki |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +#include <iostream> |
| 18 | +#include <vector> |
| 19 | +#include <cmath> |
| 20 | +#include <filesystem> |
| 21 | +#include <memory> |
| 22 | +#include <cstdlib> |
| 23 | + |
| 24 | +#include "cddp.hpp" |
| 25 | + |
| 26 | +namespace plt = matplotlibcpp; |
| 27 | +namespace fs = std::filesystem; |
| 28 | + |
| 29 | +int main() { |
| 30 | + // Problem parameters |
| 31 | + int state_dim = 3; |
| 32 | + int control_dim = 2; |
| 33 | + int horizon = 100; |
| 34 | + double timestep = 0.03; |
| 35 | + std::string integration_type = "euler"; |
| 36 | + |
| 37 | + // Create a unicycle dynamical system instance |
| 38 | + std::unique_ptr<cddp::DynamicalSystem> dyn_system = std::make_unique<cddp::Unicycle>(timestep, integration_type); |
| 39 | + |
| 40 | + // Create objective function |
| 41 | + Eigen::MatrixXd Q = Eigen::MatrixXd::Zero(state_dim, state_dim); |
| 42 | + Eigen::MatrixXd R = 0.05 * Eigen::MatrixXd::Identity(control_dim, control_dim); |
| 43 | + Eigen::MatrixXd Qf = Eigen::MatrixXd::Identity(state_dim, state_dim); |
| 44 | + Qf << 100.0, 0.0, 0.0, |
| 45 | + 0.0, 100.0, 0.0, |
| 46 | + 0.0, 0.0, 100.0; |
| 47 | + Eigen::VectorXd goal_state(state_dim); |
| 48 | + goal_state << 2.0, 2.0, M_PI/2.0; |
| 49 | + |
| 50 | + // Create an empty vector for reference states |
| 51 | + std::vector<Eigen::VectorXd> empty_reference_states; |
| 52 | + auto objective = std::make_unique<cddp::QuadraticObjective>(Q, R, Qf, goal_state, empty_reference_states, timestep); |
| 53 | + |
| 54 | + // Define initial state |
| 55 | + Eigen::VectorXd initial_state(state_dim); |
| 56 | + initial_state << 0.0, 0.0, M_PI/4.0; |
| 57 | + |
| 58 | + // Set up common CDDP options |
| 59 | + cddp::CDDPOptions options; |
| 60 | + options.max_iterations = 20; |
| 61 | + options.verbose = false; |
| 62 | + options.debug = false; |
| 63 | + options.cost_tolerance = 1e-5; |
| 64 | + options.grad_tolerance = 1e-4; |
| 65 | + options.regularization_type = "none"; |
| 66 | + |
| 67 | + // Define control box constraint bounds |
| 68 | + Eigen::VectorXd control_lower_bound(control_dim); |
| 69 | + control_lower_bound << -2.0, -M_PI; |
| 70 | + Eigen::VectorXd control_upper_bound(control_dim); |
| 71 | + control_upper_bound << 2.0, M_PI; |
| 72 | + |
| 73 | + // -------------------------- |
| 74 | + // Solve the CDDP problem without the ball constraint |
| 75 | + cddp::CDDP solver_baseline( |
| 76 | + initial_state, |
| 77 | + goal_state, |
| 78 | + horizon, |
| 79 | + timestep, |
| 80 | + std::make_unique<cddp::Unicycle>(timestep, integration_type), |
| 81 | + std::make_unique<cddp::QuadraticObjective>(Q, R, Qf, goal_state, empty_reference_states, timestep), |
| 82 | + options |
| 83 | + ); |
| 84 | + solver_baseline.setDynamicalSystem(std::make_unique<cddp::Unicycle>(timestep, integration_type)); |
| 85 | + solver_baseline.setObjective(std::make_unique<cddp::QuadraticObjective>(Q, R, Qf, goal_state, empty_reference_states, timestep)); |
| 86 | + solver_baseline.addConstraint("ControlBoxConstraint", std::make_unique<cddp::ControlBoxConstraint>(control_lower_bound, control_upper_bound)); |
| 87 | + |
| 88 | + // Set an initial trajectory (all states equal to the initial state) |
| 89 | + std::vector<Eigen::VectorXd> X_baseline(horizon + 1, Eigen::VectorXd::Zero(state_dim)); |
| 90 | + std::vector<Eigen::VectorXd> U_baseline(horizon, Eigen::VectorXd::Zero(control_dim)); |
| 91 | + for (int i = 0; i < horizon + 1; ++i) { |
| 92 | + X_baseline[i] = initial_state; |
| 93 | + } |
| 94 | + solver_baseline.setInitialTrajectory(X_baseline, U_baseline); |
| 95 | + |
| 96 | + cddp::CDDPSolution solution_baseline = solver_baseline.solve("ASCDDP"); |
| 97 | + auto X_baseline_sol = solution_baseline.state_sequence; |
| 98 | + |
| 99 | + // -------------------------- |
| 100 | + // 2. Solve with BallConstraint |
| 101 | + cddp::CDDP solver_ball( |
| 102 | + initial_state, |
| 103 | + goal_state, |
| 104 | + horizon, |
| 105 | + timestep, |
| 106 | + std::make_unique<cddp::Unicycle>(timestep, integration_type), |
| 107 | + std::make_unique<cddp::QuadraticObjective>(Q, R, Qf, goal_state, empty_reference_states, timestep), |
| 108 | + options |
| 109 | + ); |
| 110 | + solver_ball.setDynamicalSystem(std::make_unique<cddp::Unicycle>(timestep, integration_type)); |
| 111 | + solver_ball.setObjective(std::make_unique<cddp::QuadraticObjective>(Q, R, Qf, goal_state, empty_reference_states, timestep)); |
| 112 | + solver_ball.addConstraint("ControlBoxConstraint", std::make_unique<cddp::ControlBoxConstraint>(control_lower_bound, control_upper_bound)); |
| 113 | + double radius = 0.4; |
| 114 | + Eigen::Vector2d center(1.0, 1.0); |
| 115 | + solver_ball.addConstraint("BallConstraint", std::make_unique<cddp::BallConstraint>(radius, center)); |
| 116 | + |
| 117 | + // Set an initial trajectory for the ball-constrained solver |
| 118 | + std::vector<Eigen::VectorXd> X_ball(horizon + 1, Eigen::VectorXd::Zero(state_dim)); |
| 119 | + std::vector<Eigen::VectorXd> U_ball(horizon, Eigen::VectorXd::Zero(control_dim)); |
| 120 | + for (int i = 0; i < horizon + 1; ++i) { |
| 121 | + X_ball[i] = initial_state; |
| 122 | + } |
| 123 | + solver_ball.setInitialTrajectory(X_ball, U_ball); |
| 124 | + |
| 125 | + cddp::CDDPSolution solution_ball = solver_ball.solve("ASCDDP"); |
| 126 | + auto X_ball_sol = solution_ball.state_sequence; |
| 127 | + |
| 128 | + // -------------------------- |
| 129 | + // Plot the trajectories |
| 130 | + std::vector<double> x_baseline, y_baseline; |
| 131 | + std::vector<double> x_ball, y_ball; |
| 132 | + for (const auto &state : X_baseline_sol) { |
| 133 | + x_baseline.push_back(state(0)); |
| 134 | + y_baseline.push_back(state(1)); |
| 135 | + } |
| 136 | + for (const auto &state : X_ball_sol) { |
| 137 | + x_ball.push_back(state(0)); |
| 138 | + y_ball.push_back(state(1)); |
| 139 | + } |
| 140 | + |
| 141 | + // Plot trajectories on the same figure |
| 142 | + plt::figure(); |
| 143 | + plt::plot(x_baseline, y_baseline, {{"color", "b"}, {"linestyle", "-"}, {"label", "Without Ball Constraint"}}); |
| 144 | + plt::plot(x_ball, y_ball, {{"color", "r"}, {"linestyle", "-"}, {"label", "With Ball Constraint"}}); |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + // Also plot the ball for reference |
| 149 | + std::vector<double> t_ball, x_ball_circle, y_ball_circle; |
| 150 | + for (double t = 0.0; t < 2 * M_PI; t += 0.01) { |
| 151 | + t_ball.push_back(t); |
| 152 | + x_ball_circle.push_back(center(0) + radius * cos(t)); |
| 153 | + y_ball_circle.push_back(center(1) + radius * sin(t)); |
| 154 | + }plt::plot(x_ball_circle, y_ball_circle, {{"color", "g"}, {"linestyle", "--"}, {"label", "Ball Constraint"}}); |
| 155 | + |
| 156 | + plt::xlabel("x"); |
| 157 | + plt::ylabel("y"); |
| 158 | + plt::title("Trajectory Comparison: With vs. Without BallConstraint"); |
| 159 | + plt::legend(); |
| 160 | + |
| 161 | + // Save the comparison plot |
| 162 | + std::string plotDirectory = "../results/tests"; |
| 163 | + if (!fs::exists(plotDirectory)) { |
| 164 | + fs::create_directory(plotDirectory); |
| 165 | + } |
| 166 | + plt::save(plotDirectory + "/trajectory_comparison.png"); |
| 167 | + std::cout << "Trajectory comparison saved to " << plotDirectory + "/trajectory_comparison.png" << std::endl; |
| 168 | + |
| 169 | + return 0; |
| 170 | +} |
0 commit comments