ilqgames
A new real-time solver for large-scale differential games.
relative_distance_cost.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 // Distance between two state positions.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 #include <ilqgames/cost/relative_distance_cost.h>
44 #include <ilqgames/utils/types.h>
45 
46 #include <glog/logging.h>
47 
48 namespace ilqgames {
49 
50 float RelativeDistanceCost::Evaluate(const VectorXf& input) const {
51  const float diff_x = input(dims1_.first) - input(dims2_.first);
52  const float diff_y = input(dims1_.second) - input(dims2_.second);
53  return weight_ * std::hypot(diff_x, diff_y);
54 }
55 
56 void RelativeDistanceCost::Quadraticize(const VectorXf& input, MatrixXf* hess,
57  VectorXf* grad) const {
58  CHECK_NOTNULL(hess);
59 
60  // Check dimensions.
61  CHECK_EQ(input.size(), hess->rows());
62  CHECK_EQ(input.size(), hess->cols());
63 
64  if (grad) CHECK_EQ(input.size(), grad->size());
65 
66  const float diff_x = input(dims1_.first) - input(dims2_.first);
67  const float diff_y = input(dims1_.second) - input(dims2_.second);
68  const float dist = std::hypot(diff_x, diff_y);
69  const float dist_3 = dist * dist * dist;
70 
71  const float ddx = weight_ * diff_y * diff_y / dist_3;
72  const float ddy = weight_ * diff_x * diff_x / dist_3;
73  const float dxdy = -weight_ * diff_x * diff_y / dist_3;
74 
75  (*hess)(dims1_.first, dims1_.first) += ddx;
76  (*hess)(dims1_.first, dims1_.second) += dxdy;
77  (*hess)(dims1_.second, dims1_.first) += dxdy;
78  (*hess)(dims1_.second, dims1_.second) += ddy;
79 
80  (*hess)(dims2_.first, dims2_.first) += ddx;
81  (*hess)(dims2_.first, dims2_.second) += dxdy;
82  (*hess)(dims2_.second, dims2_.first) += dxdy;
83  (*hess)(dims2_.second, dims2_.second) += ddy;
84 
85  (*hess)(dims1_.first, dims2_.first) -= ddx;
86  (*hess)(dims1_.first, dims2_.second) -= dxdy;
87  (*hess)(dims1_.second, dims2_.first) -= dxdy;
88  (*hess)(dims1_.second, dims2_.second) -= ddy;
89 
90  (*hess)(dims2_.first, dims1_.first) -= ddx;
91  (*hess)(dims2_.first, dims1_.second) -= dxdy;
92  (*hess)(dims2_.second, dims1_.first) -= dxdy;
93  (*hess)(dims2_.second, dims1_.second) -= ddy;
94 
95  if (grad) {
96  const float dx = weight_ * diff_x / dist;
97  const float dy = weight_ * diff_y / dist;
98 
99  (*grad)(dims1_.first) += dx;
100  (*grad)(dims1_.second) += dy;
101  (*grad)(dims2_.first) -= dx;
102  (*grad)(dims2_.second) -= dy;
103  }
104 }
105 
106 } // namespace ilqgames