43 #include <ilqgames/cost/quadratic_cost.h> 44 #include <ilqgames/cost/signed_distance_cost.h> 45 #include <ilqgames/dynamics/concatenated_dynamical_system.h> 46 #include <ilqgames/dynamics/single_player_car_5d.h> 47 #include <ilqgames/examples/two_player_collision_avoidance_reachability_example.h> 48 #include <ilqgames/geometry/draw_shapes.h> 49 #include <ilqgames/geometry/polyline2.h> 50 #include <ilqgames/solver/ilq_solver.h> 51 #include <ilqgames/solver/problem.h> 52 #include <ilqgames/solver/solver_params.h> 53 #include <ilqgames/utils/types.h> 60 DEFINE_double(px0, 0.0,
"Initial x-position (m).");
61 DEFINE_double(py0, -5.0,
"Initial y-position (m).");
68 static constexpr
float kOmegaCostWeight = 0.1;
71 static constexpr
float kP1InitialHeading = 0.1;
72 static constexpr
float kP1InitialSpeed = 5.0;
73 static constexpr
float kP2InitialX = 0.0;
74 static constexpr
float kP2InitialY = 0.0;
75 static constexpr
float kP2InitialHeading = 0.0;
76 static constexpr
float kP2InitialSpeed = 5.0;
79 using P1 = SinglePlayerCar5D;
80 using P2 = SinglePlayerCar5D;
81 static constexpr
float kInterAxleDistance = 4.0;
83 static const Dimension kP1XIdx = P1::kPxIdx;
84 static const Dimension kP1YIdx = P1::kPyIdx;
85 static const Dimension kP1HeadingIdx = P1::kThetaIdx;
86 static const Dimension kP1VIdx = P1::kVIdx;
88 static const Dimension kP2XIdx = P1::kNumXDims + P2::kPxIdx;
89 static const Dimension kP2YIdx = P1::kNumXDims + P2::kPyIdx;
90 static const Dimension kP2HeadingIdx = P1::kNumXDims + P2::kThetaIdx;
91 static const Dimension kP2VIdx = P1::kNumXDims + P2::kVIdx;
95 void TwoPlayerCollisionAvoidanceReachabilityExample::ConstructDynamics() {
96 dynamics_.reset(
new ConcatenatedDynamicalSystem(
97 {std::make_shared<P1>(kInterAxleDistance),
98 std::make_shared<P2>(kInterAxleDistance)}));
101 void TwoPlayerCollisionAvoidanceReachabilityExample::ConstructInitialState() {
102 x0_ = VectorXf::Zero(dynamics_->XDim());
103 x0_(kP1XIdx) = FLAGS_px0;
104 x0_(kP1YIdx) = FLAGS_py0;
105 x0_(kP1HeadingIdx) = kP1InitialHeading;
106 x0_(kP1VIdx) = kP1InitialSpeed;
107 x0_(kP2XIdx) = kP2InitialX;
108 x0_(kP2YIdx) = kP2InitialY;
109 x0_(kP2HeadingIdx) = kP2InitialHeading;
110 x0_(kP2VIdx) = kP2InitialSpeed;
113 void TwoPlayerCollisionAvoidanceReachabilityExample::ConstructPlayerCosts() {
115 player_costs_.emplace_back(
"P1");
116 player_costs_.emplace_back(
"P2");
117 auto& p1_cost = player_costs_[0];
118 auto& p2_cost = player_costs_[1];
120 const auto control_cost =
121 std::make_shared<QuadraticCost>(kOmegaCostWeight, -1, 0.0,
"ControlCost");
122 p1_cost.AddControlCost(0, control_cost);
123 p2_cost.AddControlCost(1, control_cost);
126 auto p1_position = [](Time t) {
127 return Point2(FLAGS_px0, FLAGS_py0) +
128 t * kP1InitialSpeed *
129 Point2(std::cos(kP1InitialHeading), std::sin(kP1InitialHeading));
131 auto p2_position = [](Time t) {
132 return Point2(kP2InitialX, kP2InitialY) +
133 t * kP2InitialSpeed *
134 Point2(std::cos(kP2InitialHeading), std::sin(kP2InitialHeading));
139 const float nominal_distance = (p1_position(0.5 * time::kTimeHorizon) -
140 p2_position(0.5 * time::kTimeHorizon))
142 const std::shared_ptr<SignedDistanceCost> collision_avoidance_cost(
143 new SignedDistanceCost({kP1XIdx, kP1YIdx}, {kP2XIdx, kP2YIdx},
144 nominal_distance,
"CollisionAvoidance"));
145 p1_cost.AddStateCost(collision_avoidance_cost);
146 p2_cost.AddStateCost(collision_avoidance_cost);
149 p1_cost.SetMaxOverTime();
150 p2_cost.SetMaxOverTime();
153 inline std::vector<float> TwoPlayerCollisionAvoidanceReachabilityExample::Xs(
154 const VectorXf& x)
const {
155 return {x(kP1XIdx), x(kP2XIdx)};
158 inline std::vector<float> TwoPlayerCollisionAvoidanceReachabilityExample::Ys(
159 const VectorXf& x)
const {
160 return {x(kP1YIdx), x(kP2YIdx)};
163 inline std::vector<float>
164 TwoPlayerCollisionAvoidanceReachabilityExample::Thetas(
165 const VectorXf& x)
const {
166 return {x(kP1HeadingIdx), x(kP2HeadingIdx)};