ilqgames
A new real-time solver for large-scale differential games.
modified_air_3d_example.cpp
1 /*
2  * Copyright (c) 2020, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: David Fridovich-Keil ( dfk@eecs.berkeley.edu )
35  */
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 //
39 // Two player Air3D example from:
40 // https://www.cs.ubc.ca/~mitchell/Papers/publishedIEEEtac05.pdf.
41 //
42 // Modified such that it does *not* use relative dynamics and instead considers
43 // the motion of each player separately.
44 //
45 ///////////////////////////////////////////////////////////////////////////////
46 
47 #include <ilqgames/constraint/single_dimension_constraint.h>
48 #include <ilqgames/cost/quadratic_cost.h>
49 #include <ilqgames/cost/quadratic_difference_cost.h>
50 #include <ilqgames/cost/relative_distance_cost.h>
51 #include <ilqgames/dynamics/concatenated_dynamical_system.h>
52 #include <ilqgames/dynamics/single_player_dubins_car.h>
53 #include <ilqgames/dynamics/single_player_point_mass_2d.h>
54 #include <ilqgames/examples/modified_air_3d_example.h>
55 #include <ilqgames/geometry/draw_shapes.h>
56 #include <ilqgames/geometry/polyline2.h>
57 #include <ilqgames/solver/ilq_solver.h>
58 #include <ilqgames/solver/problem.h>
59 #include <ilqgames/solver/solver_params.h>
60 #include <ilqgames/utils/types.h>
61 
62 #include <gflags/gflags.h>
63 #include <math.h>
64 #include <memory>
65 #include <vector>
66 
67 // Initial state command-line flags.
68 DEFINE_double(rx0, 4.0, "Initial x-position (m).");
69 DEFINE_double(ry0, 3.0, "Initial y-position (m).");
70 DEFINE_double(rtheta0, M_PI / 4.0, "Initial heading (rad).");
71 DEFINE_double(ve, 1.0, "Evader speed (m/s).");
72 DEFINE_double(vp, 1.0, "Pursuer speed (m/s).");
73 
74 namespace ilqgames {
75 
76 namespace {
77 
78 // Input constraint and cost weight.
79 static constexpr float kOmegaMax = 1.0; // rad/s
80 static constexpr float kOmegaCostWeight = 0.1;
81 
82 // State dimensions.
83 // using Dyn = SinglePlayerDubinsCar;
84 using Dyn = SinglePlayerPointMass2D;
85 
86 static const Dimension kP1PxIdx = Dyn::kPxIdx;
87 static const Dimension kP1PyIdx = Dyn::kPyIdx;
88 // static const Dimension kP1ThetaIdx = Dyn::kThetaIdx;
89 static const Dimension kP1VxIdx = Dyn::kVxIdx;
90 static const Dimension kP1VyIdx = Dyn::kVyIdx;
91 
92 static const Dimension kP2PxIdx = Dyn::kNumXDims + Dyn::kPxIdx;
93 static const Dimension kP2PyIdx = Dyn::kNumXDims + Dyn::kPyIdx;
94 // static const Dimension kP2ThetaIdx = Dyn::kNumXDims + Dyn::kThetaIdx;
95 static const Dimension kP2VxIdx = Dyn::kNumXDims + Dyn::kVxIdx;
96 static const Dimension kP2VyIdx = Dyn::kNumXDims + Dyn::kVyIdx;
97 
98 } // anonymous namespace
99 
100 void ModifiedAir3DExample::ConstructDynamics() {
101  dynamics_.reset(new ConcatenatedDynamicalSystem(
102  {std::make_shared<Dyn>(), std::make_shared<Dyn>()}));
103 }
104 
105 void ModifiedAir3DExample::ConstructInitialState() {
106  // Set up initial state.
107  x0_ = VectorXf::Zero(dynamics_->XDim());
108  x0_(kP1VxIdx) = FLAGS_ve;
109  x0_(kP2PxIdx) = FLAGS_rx0;
110  x0_(kP2PyIdx) = FLAGS_ry0;
111  x0_(kP2VxIdx) = FLAGS_vp * std::cos(FLAGS_rtheta0);
112  x0_(kP2VyIdx) = FLAGS_vp * std::sin(FLAGS_rtheta0);
113 }
114 
115 void ModifiedAir3DExample::ConstructPlayerCosts() {
116  // Set up costs for all players.
117  player_costs_.emplace_back("P1", 1.0, 0.0);
118  player_costs_.emplace_back("P2", 1.0, 0.0);
119  auto& p1_cost = player_costs_[0];
120  auto& p2_cost = player_costs_[1];
121 
122  const auto control_cost =
123  std::make_shared<QuadraticCost>(kOmegaCostWeight, -1, 0.0, "ControlCost");
124  p1_cost.AddControlCost(0, control_cost);
125  p2_cost.AddControlCost(1, control_cost);
126 
127  // Constrain control effort.
128  // const auto omega_max_constraint =
129  // std::make_shared<SingleDimensionConstraint>(
130  // Dyn::kOmegaIdx, kOmegaMax, true, "Omega Constraint
131  // (Max)");
132  // const auto omega_min_constraint =
133  // std::make_shared<SingleDimensionConstraint>(
134  // Dyn::kOmegaIdx, -kOmegaMax, false, "Omega Constraint
135  // (Min)");
136  // p1_cost.AddControlConstraint(0, omega_max_constraint);
137  // p1_cost.AddControlConstraint(0, omega_min_constraint);
138  // p2_cost.AddControlConstraint(1, omega_max_constraint);
139  // p2_cost.AddControlConstraint(1, omega_min_constraint);
140 
141  // Target cost.
142  constexpr float kEvaderWeight = -1e6;
143  constexpr float kPursuerWeight = 1e6;
144  // const std::shared_ptr<RelativeDistanceCost> p1_target_cost(
145  // new RelativeDistanceCost(kEvaderWeight, {kP1PxIdx, kP1PyIdx},
146  // {kP2PxIdx, kP2PyIdx}, "Target"));
147  // const std::shared_ptr<RelativeDistanceCost> p2_target_cost(
148  // new RelativeDistanceCost(kPursuerWeight, {kP1PxIdx, kP1PyIdx},
149  // {kP2PxIdx, kP2PyIdx}, "Target"));
150  const std::shared_ptr<QuadraticDifferenceCost> p1_target_cost(
151  new QuadraticDifferenceCost(kEvaderWeight, {kP1PxIdx, kP1PyIdx},
152  {kP2PxIdx, kP2PyIdx}, "Target"));
153  const std::shared_ptr<QuadraticDifferenceCost> p2_target_cost(
154  new QuadraticDifferenceCost(kPursuerWeight, {kP1PxIdx, kP1PyIdx},
155  {kP2PxIdx, kP2PyIdx}, "Target"));
156  p1_cost.AddStateCost(p1_target_cost);
157  p2_cost.AddStateCost(p2_target_cost);
158 
159  // Make sure evader's cost is a max-over-time and pursuer's is a
160  // min-over-time.
161  // p1_cost.SetMaxOverTime();
162  // p2_cost.SetMinOverTime();
163 }
164 
165 inline std::vector<float> ModifiedAir3DExample::Xs(const VectorXf& x) const {
166  return {x(kP1PxIdx), x(kP2PxIdx)};
167 }
168 
169 inline std::vector<float> ModifiedAir3DExample::Ys(const VectorXf& x) const {
170  return {x(kP1PyIdx), x(kP2PyIdx)};
171 }
172 
173 inline std::vector<float> ModifiedAir3DExample::Thetas(
174  const VectorXf& x) const {
175  // return {x(kP1ThetaIdx), x(kP2ThetaIdx)};
176  return {std::atan2(x(kP1VyIdx), x(kP1VxIdx)),
177  std::atan2(x(kP2VyIdx), x(kP2VxIdx))};
178 }
179 
180 } // namespace ilqgames