ilqgames
A new real-time solver for large-scale differential games.
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 ///////////////////////////////////////////////////////////////////////////////
43 
44 #include <ilqgames/constraint/single_dimension_constraint.h>
45 #include <ilqgames/cost/polyline2_signed_distance_cost.h>
46 #include <ilqgames/cost/quadratic_cost.h>
47 #include <ilqgames/dynamics/air_3d.h>
48 #include <ilqgames/examples/air_3d_example.h>
49 #include <ilqgames/geometry/draw_shapes.h>
50 #include <ilqgames/geometry/polyline2.h>
51 #include <ilqgames/solver/ilq_solver.h>
52 #include <ilqgames/solver/problem.h>
53 #include <ilqgames/solver/solver_params.h>
54 #include <ilqgames/utils/types.h>
55 
56 #include <gflags/gflags.h>
57 #include <math.h>
58 #include <memory>
59 #include <vector>
60 
61 // Initial state command-line flags.
62 DEFINE_double(rx0, 4.0, "Initial x-position (m).");
63 DEFINE_double(ry0, 3.0, "Initial y-position (m).");
64 DEFINE_double(rtheta0, M_PI / 4.0, "Initial heading (rad).");
65 DEFINE_double(ve, 1.0, "Evader speed (m/s).");
66 DEFINE_double(vp, 1.0, "Pursuer speed (m/s).");
67 
68 namespace ilqgames {
69 
70 namespace {
71 
72 // Input cost and constraint.
73 static constexpr float kOmegaCostWeight = 0.1;
74 static constexpr float kOmegaMax = 1.0; // rad/s
75 
76 // State dimensions.
77 using Dyn = Air3D;
78 
79 } // anonymous namespace
80 
81 void Air3DExample::ConstructDynamics() {
82  dynamics_.reset(new Dyn(FLAGS_ve, FLAGS_vp));
83 }
84 
85 void Air3DExample::ConstructInitialState() {
86  x0_ = VectorXf::Zero(dynamics_->XDim());
87  x0_(Dyn::kRxIdx) = FLAGS_rx0;
88  x0_(Dyn::kRyIdx) = FLAGS_ry0;
89  x0_(Dyn::kRThetaIdx) = FLAGS_rtheta0;
90 }
91 
92 void Air3DExample::ConstructPlayerCosts() {
93  // Set up costs for all players.
94  player_costs_.emplace_back("P1");
95  player_costs_.emplace_back("P2");
96  auto& p1_cost = player_costs_[0];
97  auto& p2_cost = player_costs_[1];
98 
99  const auto control_cost =
100  std::make_shared<QuadraticCost>(kOmegaCostWeight, -1, 0.0, "ControlCost");
101  p1_cost.AddControlCost(0, control_cost);
102  p2_cost.AddControlCost(1, control_cost);
103 
104  // Constrain control effort.
105  const auto p1_omega_max_constraint =
106  std::make_shared<SingleDimensionConstraint>(
107  Dyn::kOmega1Idx, kOmegaMax, true, "Omega Constraint (Max)");
108  const auto p1_omega_min_constraint =
109  std::make_shared<SingleDimensionConstraint>(
110  Dyn::kOmega1Idx, -kOmegaMax, false, "Omega Constraint (Min)");
111  p1_cost.AddControlConstraint(0, p1_omega_max_constraint);
112  p1_cost.AddControlConstraint(0, p1_omega_min_constraint);
113 
114  const auto p2_omega_max_constraint =
115  std::make_shared<SingleDimensionConstraint>(
116  Dyn::kOmega1Idx, kOmegaMax, true, "Omega Constraint (Max)");
117  const auto p2_omega_min_constraint =
118  std::make_shared<SingleDimensionConstraint>(
119  Dyn::kOmega2Idx, -kOmegaMax, false, "Omega Constraint (Min)");
120  p2_cost.AddControlConstraint(1, p2_omega_max_constraint);
121  p2_cost.AddControlConstraint(1, p2_omega_min_constraint);
122 
123  // Target cost.
124  const float kTargetRadius = 5.0;
125  const Polyline2 circle = DrawCircle(Point2::Zero(), kTargetRadius, 10);
126 
127  constexpr bool kReach = true;
128  const std::shared_ptr<Polyline2SignedDistanceCost> p1_target_cost(
129  new Polyline2SignedDistanceCost(circle, {Dyn::kRxIdx, Dyn::kRyIdx},
130  !kReach, "Target"));
131  const std::shared_ptr<Polyline2SignedDistanceCost> p2_target_cost(
132  new Polyline2SignedDistanceCost(circle, {Dyn::kRxIdx, Dyn::kRyIdx},
133  kReach, "Target"));
134 
135  p1_cost.AddStateCost(p1_target_cost);
136  p2_cost.AddStateCost(p2_target_cost);
137 
138  // Make sure evader's cost is a max-over-time and pursuer's is a
139  // min-over-time.
140  p1_cost.SetMaxOverTime();
141  p2_cost.SetMinOverTime();
142 }
143 
144 inline std::vector<float> Air3DExample::Xs(const VectorXf& x) const {
145  return {0.0, x(Dyn::kRxIdx)};
146 }
147 
148 inline std::vector<float> Air3DExample::Ys(const VectorXf& x) const {
149  return {0.0, x(Dyn::kRyIdx)};
150 }
151 
152 inline std::vector<float> Air3DExample::Thetas(const VectorXf& x) const {
153  return {0.0, x(Dyn::kRThetaIdx)};
154 }
155 
156 } // namespace ilqgames