44 #include <ilqgames/cost/curvature_cost.h> 45 #include <ilqgames/cost/final_time_cost.h> 46 #include <ilqgames/cost/locally_convex_proximity_cost.h> 47 #include <ilqgames/cost/nominal_path_length_cost.h> 48 #include <ilqgames/cost/orientation_cost.h> 49 #include <ilqgames/cost/proximity_cost.h> 50 #include <ilqgames/cost/quadratic_cost.h> 51 #include <ilqgames/cost/quadratic_polyline2_cost.h> 52 #include <ilqgames/cost/semiquadratic_cost.h> 53 #include <ilqgames/cost/semiquadratic_polyline2_cost.h> 54 #include <ilqgames/cost/weighted_convex_proximity_cost.h> 55 #include <ilqgames/dynamics/concatenated_dynamical_system.h> 56 #include <ilqgames/dynamics/single_player_car_6d.h> 57 #include <ilqgames/dynamics/single_player_unicycle_4d.h> 58 #include <ilqgames/examples/three_player_overtaking_example.h> 59 #include <ilqgames/geometry/polyline2.h> 60 #include <ilqgames/solver/ilq_solver.h> 61 #include <ilqgames/solver/lq_feedback_solver.h> 62 #include <ilqgames/solver/problem.h> 63 #include <ilqgames/solver/solver_params.h> 64 #include <ilqgames/utils/solver_log.h> 65 #include <ilqgames/utils/strategy.h> 66 #include <ilqgames/utils/types.h> 77 static constexpr
float kInterAxleLength = 4.0;
80 static constexpr
float kOmegaCostWeight = 500000.0;
81 static constexpr
float kJerkCostWeight = 500.0;
83 static constexpr
float kACostWeight = 50.0;
84 static constexpr
float kP1NominalVCostWeight = 10.0;
85 static constexpr
float kP2NominalVCostWeight = 1.0;
86 static constexpr
float kP3NominalVCostWeight = 1.0;
88 static constexpr
float kLaneCostWeight = 25.0;
89 static constexpr
float kLaneBoundaryCostWeight = 100.0;
91 static constexpr
float kMinProximity = 5.0;
92 static constexpr
float kP1ProximityCostWeight = 100.0;
93 static constexpr
float kP2ProximityCostWeight = 100.0;
94 static constexpr
float kP3ProximityCostWeight = 100.0;
95 using ProxCost = ProximityCost;
98 static constexpr
float kNominalHeadingCostWeight = 150.0;
100 static constexpr
bool kOrientedRight =
true;
103 static constexpr
float kLaneHalfWidth = 2.5;
106 static constexpr
float kP1NominalV = 15.0;
107 static constexpr
float kP2NominalV = 10.0;
108 static constexpr
float kP3NominalV = 10.0;
111 static constexpr
float kP1NominalHeading = M_PI_2;
114 static constexpr
float kP1InitialX = 2.5;
115 static constexpr
float kP1InitialY = -10.0;
117 static constexpr
float kP2InitialX = -1.0;
118 static constexpr
float kP2InitialY = -10.0;
120 static constexpr
float kP3InitialX = 2.5;
121 static constexpr
float kP3InitialY = 10.0;
123 static constexpr
float kP1InitialHeading = M_PI_2;
124 static constexpr
float kP2InitialHeading = M_PI_2;
125 static constexpr
float kP3InitialHeading = M_PI_2;
127 static constexpr
float kP1InitialSpeed = 10.0;
128 static constexpr
float kP2InitialSpeed = 2.0;
129 static constexpr
float kP3InitialSpeed = 2.0;
132 using P1 = SinglePlayerCar6D;
133 using P2 = SinglePlayerCar6D;
134 using P3 = SinglePlayerCar6D;
137 static const Dimension kP1XIdx = P1::kPxIdx;
138 static const Dimension kP1YIdx = P1::kPyIdx;
139 static const Dimension kP1HeadingIdx = P1::kThetaIdx;
140 static const Dimension kP1PhiIdx = P1::kPhiIdx;
141 static const Dimension kP1VIdx = P1::kVIdx;
142 static const Dimension kP1AIdx = P1::kAIdx;
144 static const Dimension kP2XIdx = P1::kNumXDims + P2::kPxIdx;
145 static const Dimension kP2YIdx = P1::kNumXDims + P2::kPyIdx;
146 static const Dimension kP2HeadingIdx = P1::kNumXDims + P2::kThetaIdx;
147 static const Dimension kP2PhiIdx = P1::kNumXDims + P2::kPhiIdx;
148 static const Dimension kP2VIdx = P1::kNumXDims + P2::kVIdx;
149 static const Dimension kP2AIdx = P1::kNumXDims + P2::kAIdx;
159 static const Dimension kP3XIdx = P1::kNumXDims + P2::kNumXDims + P3::kPxIdx;
160 static const Dimension kP3YIdx = P1::kNumXDims + P2::kNumXDims + P3::kPyIdx;
161 static const Dimension kP3HeadingIdx =
162 P1::kNumXDims + P2::kNumXDims + P3::kThetaIdx;
163 static const Dimension kP3VIdx = P1::kNumXDims + P2::kNumXDims + P3::kVIdx;
166 static const Dimension kP1OmegaIdx = 0;
167 static const Dimension kP1JerkIdx = 1;
168 static const Dimension kP2OmegaIdx = 0;
169 static const Dimension kP2JerkIdx = 1;
170 static const Dimension kP3OmegaIdx = 0;
171 static const Dimension kP3JerkIdx = 1;
174 void ThreePlayerOvertakingExample::ConstructDynamics() {
175 dynamics_.reset(
new ConcatenatedDynamicalSystem(
176 {std::make_shared<SinglePlayerCar6D>(kInterAxleLength),
177 std::make_shared<SinglePlayerCar6D>(kInterAxleLength),
178 std::make_shared<SinglePlayerCar6D>(kInterAxleLength)}));
181 void ThreePlayerOvertakingExample::ConstructInitialState() {
182 x0_ = VectorXf::Zero(dynamics_->XDim());
183 x0_(kP1XIdx) = kP1InitialX;
184 x0_(kP1YIdx) = kP1InitialY;
185 x0_(kP1HeadingIdx) = kP1InitialHeading;
186 x0_(kP1VIdx) = kP1InitialSpeed;
187 x0_(kP2XIdx) = kP2InitialX;
188 x0_(kP2YIdx) = kP2InitialY;
189 x0_(kP2HeadingIdx) = kP2InitialHeading;
190 x0_(kP2VIdx) = kP2InitialSpeed;
191 x0_(kP3XIdx) = kP3InitialX;
192 x0_(kP3YIdx) = kP3InitialY;
193 x0_(kP3HeadingIdx) = kP3InitialHeading;
194 x0_(kP3VIdx) = kP3InitialSpeed;
197 void ThreePlayerOvertakingExample::ConstructPlayerCosts() {
199 player_costs_.emplace_back(
"P1");
200 player_costs_.emplace_back(
"P2");
201 player_costs_.emplace_back(
"P3");
202 auto& p1_cost = player_costs_[0];
203 auto& p2_cost = player_costs_[1];
204 auto& p3_cost = player_costs_[2];
207 const Polyline2 lane1(
208 {Point2(kP2InitialX, -1000.0), Point2(kP2InitialX, 1000.0)});
209 const Polyline2 lane2(
210 {Point2(kP3InitialX, -1000.0), Point2(kP3InitialX, 1000.0)});
212 const std::shared_ptr<QuadraticPolyline2Cost> p1_lane_cost(
213 new QuadraticPolyline2Cost(kLaneCostWeight, lane1, {kP1XIdx, kP1YIdx},
215 const std::shared_ptr<SemiquadraticPolyline2Cost> p1_lane_r_cost(
216 new SemiquadraticPolyline2Cost(kLaneBoundaryCostWeight, lane1,
217 {kP1XIdx, kP1YIdx}, kLaneHalfWidth,
218 kOrientedRight,
"LaneRightBoundary"));
219 const std::shared_ptr<SemiquadraticPolyline2Cost> p1_lane_l_cost(
220 new SemiquadraticPolyline2Cost(kLaneBoundaryCostWeight, lane1,
221 {kP1XIdx, kP1YIdx}, -kLaneHalfWidth,
222 !kOrientedRight,
"LaneLeftBoundary"));
223 p1_cost.AddStateCost(p1_lane_cost);
224 p1_cost.AddStateCost(p1_lane_r_cost);
225 p1_cost.AddStateCost(p1_lane_l_cost);
227 const std::shared_ptr<QuadraticPolyline2Cost> p2_lane_cost(
228 new QuadraticPolyline2Cost(kLaneCostWeight, lane1, {kP2XIdx, kP2YIdx},
230 const std::shared_ptr<SemiquadraticPolyline2Cost> p2_lane_r_cost(
231 new SemiquadraticPolyline2Cost(kLaneBoundaryCostWeight, lane1,
232 {kP2XIdx, kP2YIdx}, kLaneHalfWidth,
233 kOrientedRight,
"LaneRightBoundary"));
234 const std::shared_ptr<SemiquadraticPolyline2Cost> p2_lane_l_cost(
235 new SemiquadraticPolyline2Cost(kLaneBoundaryCostWeight, lane1,
236 {kP2XIdx, kP2YIdx}, -kLaneHalfWidth,
237 !kOrientedRight,
"LaneLeftBoundary"));
238 p2_cost.AddStateCost(p2_lane_cost);
239 p2_cost.AddStateCost(p2_lane_r_cost);
240 p2_cost.AddStateCost(p2_lane_l_cost);
242 const std::shared_ptr<QuadraticPolyline2Cost> p3_lane_cost(
243 new QuadraticPolyline2Cost(kLaneCostWeight, lane2, {kP3XIdx, kP3YIdx},
245 const std::shared_ptr<SemiquadraticPolyline2Cost> p3_lane_r_cost(
246 new SemiquadraticPolyline2Cost(kLaneBoundaryCostWeight, lane2,
247 {kP3XIdx, kP3YIdx}, kLaneHalfWidth,
248 kOrientedRight,
"LaneRightBoundary"));
249 const std::shared_ptr<SemiquadraticPolyline2Cost> p3_lane_l_cost(
250 new SemiquadraticPolyline2Cost(kLaneBoundaryCostWeight, lane2,
251 {kP3XIdx, kP3YIdx}, -kLaneHalfWidth,
252 !kOrientedRight,
"LaneLeftBoundary"));
253 p3_cost.AddStateCost(p3_lane_cost);
254 p3_cost.AddStateCost(p3_lane_r_cost);
255 p3_cost.AddStateCost(p3_lane_l_cost);
262 const auto p1_nominal_v_cost = std::make_shared<QuadraticCost>(
263 kP1NominalVCostWeight, kP1VIdx, kP1NominalV,
"NominalV");
266 p1_cost.AddStateCost(p1_nominal_v_cost);
272 const auto p2_nominal_v_cost = std::make_shared<QuadraticCost>(
273 kP2NominalVCostWeight, kP2VIdx, kP2NominalV,
"NominalV");
276 p2_cost.AddStateCost(p2_nominal_v_cost);
282 const auto p3_nominal_v_cost = std::make_shared<QuadraticCost>(
283 kP3NominalVCostWeight, kP3VIdx, kP3NominalV,
"NominalV");
286 p3_cost.AddStateCost(p3_nominal_v_cost);
289 const auto p1_omega_cost = std::make_shared<QuadraticCost>(
290 kOmegaCostWeight, kP1OmegaIdx, 0.0,
"Steering");
291 const auto p1_jerk_cost =
292 std::make_shared<QuadraticCost>(kJerkCostWeight, kP1JerkIdx, 0.0,
"Jerk");
293 p1_cost.AddControlCost(0, p1_omega_cost);
294 p1_cost.AddControlCost(0, p1_jerk_cost);
296 const auto p2_omega_cost = std::make_shared<QuadraticCost>(
297 kOmegaCostWeight, kP2OmegaIdx, 0.0,
"Steering");
298 const auto p2_jerk_cost =
299 std::make_shared<QuadraticCost>(kJerkCostWeight, kP2JerkIdx, 0.0,
"Jerk");
300 p2_cost.AddControlCost(1, p2_omega_cost);
301 p2_cost.AddControlCost(1, p2_jerk_cost);
303 const auto p3_omega_cost = std::make_shared<QuadraticCost>(
304 kOmegaCostWeight, kP3OmegaIdx, 0.0,
"Steering");
305 const auto p3_a_cost =
306 std::make_shared<QuadraticCost>(kJerkCostWeight, kP3JerkIdx, 0.0,
"Jerk");
307 p3_cost.AddControlCost(2, p3_omega_cost);
308 p3_cost.AddControlCost(2, p3_a_cost);
311 const std::shared_ptr<ProxCost> p1p2_proximity_cost(
312 new ProxCost(kP1ProximityCostWeight, {kP1XIdx, kP1YIdx},
313 {kP2XIdx, kP2YIdx}, kMinProximity,
"ProximityP2"));
314 const std::shared_ptr<ProxCost> p1p3_proximity_cost(
315 new ProxCost(kP1ProximityCostWeight, {kP1XIdx, kP1YIdx},
316 {kP3XIdx, kP3YIdx}, kMinProximity,
"ProximityP3"));
317 p1_cost.AddStateCost(p1p2_proximity_cost);
318 p1_cost.AddStateCost(p1p3_proximity_cost);
320 const std::shared_ptr<ProxCost> p2p1_proximity_cost(
321 new ProxCost(kP2ProximityCostWeight, {kP2XIdx, kP2YIdx},
322 {kP1XIdx, kP1YIdx}, kMinProximity,
"ProximityP1"));
323 const std::shared_ptr<ProxCost> p2p3_proximity_cost(
324 new ProxCost(kP2ProximityCostWeight, {kP2XIdx, kP2YIdx},
325 {kP3XIdx, kP3YIdx}, kMinProximity,
"ProximityP3"));
326 p2_cost.AddStateCost(p2p1_proximity_cost);
327 p2_cost.AddStateCost(p2p3_proximity_cost);
329 const std::shared_ptr<ProxCost> p3p1_proximity_cost(
330 new ProxCost(kP3ProximityCostWeight, {kP3XIdx, kP3YIdx},
331 {kP1XIdx, kP1YIdx}, kMinProximity,
"ProximityP1"));
332 const std::shared_ptr<ProxCost> p3p2_proximity_cost(
333 new ProxCost(kP3ProximityCostWeight, {kP3XIdx, kP3YIdx},
334 {kP2XIdx, kP2YIdx}, kMinProximity,
"ProximityP2"));
339 inline std::vector<float> ThreePlayerOvertakingExample::Xs(
340 const VectorXf& x)
const {
341 return {x(kP1XIdx), x(kP2XIdx), x(kP3XIdx)};
344 inline std::vector<float> ThreePlayerOvertakingExample::Ys(
345 const VectorXf& x)
const {
346 return {x(kP1YIdx), x(kP2YIdx), x(kP3YIdx)};
349 inline std::vector<float> ThreePlayerOvertakingExample::Thetas(
350 const VectorXf& x)
const {
351 return {x(kP1HeadingIdx), x(kP2HeadingIdx), x(kP3HeadingIdx)};