ilqgames
A new real-time solver for large-scale differential games.
one_player_reachability_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 // One player reachability example. Single player choosing control to minimize
40 // max distance (-ve) signed distance to a ball we're outside.
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/concatenated_dynamical_system.h>
48 #include <ilqgames/dynamics/single_player_dubins_car.h>
49 #include <ilqgames/examples/one_player_reachability_example.h>
50 #include <ilqgames/geometry/draw_shapes.h>
51 #include <ilqgames/geometry/polyline2.h>
52 #include <ilqgames/solver/ilq_solver.h>
53 #include <ilqgames/solver/problem.h>
54 #include <ilqgames/solver/solver_params.h>
55 #include <ilqgames/utils/types.h>
56 
57 #include <gflags/gflags.h>
58 #include <math.h>
59 #include <memory>
60 #include <vector>
61 
62 // Initial state command-line flags.
63 DEFINE_double(px0, 1.75, "Initial x-position (m).");
64 DEFINE_double(py0, 1.75, "Initial y-position (m).");
65 DEFINE_double(theta0, 0.0, "Initial heading (rad).");
66 
67 namespace ilqgames {
68 
69 namespace {
70 
71 // Reach or avoid?
72 static constexpr bool kAvoid = true;
73 
74 // Target radius.
75 static constexpr float kTargetRadius = 2.0; // m
76 
77 // Input constraint and cost weight.
78 static constexpr float kOmegaMax = 1.0; // rad/s
79 static constexpr float kOmegaCostWeight = 0.1;
80 
81 // Speed.
82 static constexpr float kSpeed = 1.0; // m/s
83 
84 // Target position.
85 static constexpr float kP1TargetX = 0.0;
86 static constexpr float kP1TargetY = 0.0;
87 
88 // State dimensions.
89 using P1 = SinglePlayerDubinsCar;
90 
91 static const Dimension kP1XIdx = P1::kPxIdx;
92 static const Dimension kP1YIdx = P1::kPyIdx;
93 static const Dimension kP1ThetaIdx = P1::kThetaIdx;
94 
95 // Control dimensions.
96 static const Dimension kP1OmegaIdx = 0;
97 
98 } // anonymous namespace
99 
100 void OnePlayerReachabilityExample::ConstructDynamics() {
101  dynamics_.reset(
102  new ConcatenatedDynamicalSystem({std::make_shared<P1>(kSpeed)}));
103 }
104 
105 void OnePlayerReachabilityExample::ConstructInitialState() {
106  // Set up initial state.
107  x0_ = VectorXf::Zero(dynamics_->XDim());
108  x0_(kP1XIdx) = FLAGS_px0;
109  x0_(kP1YIdx) = FLAGS_py0;
110  x0_(kP1ThetaIdx) = FLAGS_theta0;
111 }
112 
113 void OnePlayerReachabilityExample::ConstructPlayerCosts() {
114  // Set up costs for all players.
115  player_costs_.emplace_back("P1");
116  auto& p1_cost = player_costs_[0];
117 
118  const auto control_cost =
119  std::make_shared<QuadraticCost>(kOmegaCostWeight, -1, 0.0, "ControlCost");
120  p1_cost.AddControlCost(0, control_cost);
121 
122  // Constrain control effort.
123  const auto p1_omega_max_constraint =
124  std::make_shared<SingleDimensionConstraint>(kP1OmegaIdx, kOmegaMax, true,
125  "Input Constraint (Max)");
126  const auto p1_omega_min_constraint =
127  std::make_shared<SingleDimensionConstraint>(
128  kP1OmegaIdx, -kOmegaMax, false, "Input Constraint (Min)");
129  p1_cost.AddControlConstraint(0, p1_omega_max_constraint);
130  p1_cost.AddControlConstraint(0, p1_omega_min_constraint);
131 
132  // Target cost.
133  const Polyline2 circle =
134  DrawCircle(Point2(kP1TargetX, kP1TargetY), kTargetRadius, 10);
135  const std::shared_ptr<Polyline2SignedDistanceCost> p1_target_cost(
136  new Polyline2SignedDistanceCost(circle, {kP1XIdx, kP1YIdx}, kAvoid,
137  "Target"));
138 
139  p1_cost.AddStateCost(p1_target_cost);
140 
141  // Make sure costs are maxima-over-time.
142  p1_cost.SetMaxOverTime();
143 }
144 
145 inline std::vector<float> OnePlayerReachabilityExample::Xs(
146  const VectorXf& x) const {
147  return {x(kP1XIdx)};
148 }
149 
150 inline std::vector<float> OnePlayerReachabilityExample::Ys(
151  const VectorXf& x) const {
152  return {x(kP1YIdx)};
153 }
154 
155 inline std::vector<float> OnePlayerReachabilityExample::Thetas(
156  const VectorXf& x) const {
157  return {x(kP1ThetaIdx)};
158 }
159 
160 } // namespace ilqgames