ilqgames
A new real-time solver for large-scale differential games.
proximity_cost.cpp
1 /*
2  * Copyright (c) 2019, 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 // Penalizes (thresh - relative distance)^2 between two pairs of state
40 // dimensions (representing two positions of vehicles whose states have been
41 // concatenated) whenever relative distance is less than thresh.
42 //
43 ///////////////////////////////////////////////////////////////////////////////
44 
45 #include <ilqgames/cost/proximity_cost.h>
46 #include <ilqgames/utils/types.h>
47 
48 #include <glog/logging.h>
49 
50 namespace ilqgames {
51 
52 float ProximityCost::Evaluate(const VectorXf& input) const {
53  const float dx = input(xidx1_) - input(xidx2_);
54  const float dy = input(yidx1_) - input(yidx2_);
55  const float delta_sq = dx * dx + dy * dy;
56 
57  if (delta_sq >= threshold_sq_) return 0.0;
58 
59  const float gap = threshold_ - std::sqrt(delta_sq);
60  return 0.5 * weight_ * gap * gap;
61 }
62 
63 void ProximityCost::Quadraticize(const VectorXf& input, MatrixXf* hess,
64  VectorXf* grad) const {
65  CHECK_NOTNULL(hess);
66  CHECK_NOTNULL(grad);
67 
68  // Check dimensions.
69  CHECK_EQ(input.size(), hess->rows());
70  CHECK_EQ(input.size(), hess->cols());
71  CHECK_EQ(input.size(), grad->size());
72 
73  // Compute Hessian and gradient.
74  const float dx = input(xidx1_) - input(xidx2_);
75  const float dy = input(yidx1_) - input(yidx2_);
76  const float delta_sq = dx * dx + dy * dy;
77 
78  // Catch cost not active.
79  if (delta_sq >= threshold_sq_) return;
80 
81  const float delta = std::sqrt(delta_sq);
82  const float gap = threshold_ - delta;
83  const float weight_delta = weight_ / delta;
84  const float dx_delta = dx / delta;
85  const float dy_delta = dy / delta;
86 
87  const float ddx1 = -weight_delta * gap * dx;
88  const float ddy1 = -weight_delta * gap * dy;
89  const float hess_x1x1 =
90  weight_delta * (dx_delta * (gap * dx_delta + dx) - gap);
91  const float hess_y1y1 =
92  weight_delta * (dy_delta * (gap * dy_delta + dy) - gap);
93  const float hess_x1y1 = weight_delta * (dx_delta * (gap * dy_delta + dy));
94 
95  (*grad)(xidx1_) += ddx1;
96  (*grad)(xidx2_) -= ddx1;
97 
98  (*grad)(yidx1_) += ddy1;
99  (*grad)(yidx2_) -= ddy1;
100 
101  (*hess)(xidx1_, xidx1_) += hess_x1x1;
102  (*hess)(xidx1_, xidx2_) -= hess_x1x1;
103  (*hess)(xidx2_, xidx1_) -= hess_x1x1;
104  (*hess)(xidx2_, xidx2_) += hess_x1x1;
105 
106  (*hess)(yidx1_, yidx1_) += hess_y1y1;
107  (*hess)(yidx1_, yidx2_) -= hess_y1y1;
108  (*hess)(yidx2_, yidx1_) -= hess_y1y1;
109  (*hess)(yidx2_, yidx2_) += hess_y1y1;
110 
111  (*hess)(xidx1_, yidx1_) += hess_x1y1;
112  (*hess)(yidx1_, xidx1_) += hess_x1y1;
113 
114  (*hess)(xidx1_, yidx2_) -= hess_x1y1;
115  (*hess)(yidx2_, xidx1_) -= hess_x1y1;
116 
117  (*hess)(xidx2_, yidx1_) -= hess_x1y1;
118  (*hess)(yidx1_, xidx2_) -= hess_x1y1;
119 
120  (*hess)(xidx2_, yidx2_) += hess_x1y1;
121  (*hess)(yidx2_, xidx2_) += hess_x1y1;
122 }
123 
124 } // namespace ilqgames