ilqgames
A new real-time solver for large-scale differential games.
signed_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 // Nominal value minus distance between two points in the given dimensions.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 #include <ilqgames/cost/signed_distance_cost.h>
44 #include <ilqgames/utils/types.h>
45 
46 #include <glog/logging.h>
47 #include <numeric>
48 
49 namespace ilqgames {
50 
51 float SignedDistanceCost::Evaluate(const VectorXf& input) const {
52  CHECK_LT(xdim1_, input.size());
53  CHECK_LT(ydim1_, input.size());
54  CHECK_LT(xdim2_, input.size());
55  CHECK_LT(ydim2_, input.size());
56 
57  // Otherwise, cost is squared 2-norm of entire input.
58  const float dx = input(xdim1_) - input(xdim2_);
59  const float dy = input(ydim1_) - input(ydim2_);
60  const float cost = nominal_ - std::hypot(dx, dy);
61 
62  return (less_is_positive_) ? cost : -cost;
63 }
64 
65 void SignedDistanceCost::Quadraticize(const VectorXf& input, MatrixXf* hess,
66  VectorXf* grad) const {
67  CHECK_LT(xdim1_, input.size());
68  CHECK_LT(ydim1_, input.size());
69  CHECK_LT(xdim2_, input.size());
70  CHECK_LT(ydim2_, input.size());
71  CHECK_NOTNULL(hess);
72  CHECK_NOTNULL(grad);
73 
74  // Check dimensions.
75  CHECK_EQ(input.size(), hess->rows());
76  CHECK_EQ(input.size(), hess->cols());
77  CHECK_EQ(input.size(), grad->size());
78 
79  // Compute gradient and Hessian.
80  const float s = (less_is_positive_) ? 1.0 : -1.0;
81  const float delta_x = input(xdim1_) - input(xdim2_);
82  const float delta_y = input(ydim1_) - input(ydim2_);
83  const float norm = std::hypot(delta_x, delta_y);
84  const float norm_3 = norm * norm * norm;
85 
86  const float dx1 = -s * delta_x / norm;
87  const float dy1 = -s * delta_y / norm;
88  const float ddx1 = -s * delta_y * delta_y / norm_3;
89  const float ddy1 = -s * delta_x * delta_x / norm_3;
90  const float dx1dy1 = s * delta_x * delta_y / norm_3;
91 
92  (*grad)(xdim1_) += dx1;
93  (*grad)(ydim1_) += dy1;
94  (*grad)(xdim2_) -= dx1;
95  (*grad)(ydim2_) -= dy1;
96 
97  (*hess)(xdim1_, xdim1_) += ddx1;
98  (*hess)(ydim1_, ydim1_) += ddy1;
99  (*hess)(xdim1_, ydim1_) += dx1dy1;
100  (*hess)(ydim1_, xdim1_) += dx1dy1;
101  (*hess)(xdim2_, xdim2_) += ddx1;
102  (*hess)(ydim2_, ydim2_) += ddy1;
103  (*hess)(xdim2_, ydim2_) += dx1dy1;
104  (*hess)(ydim2_, xdim2_) += dx1dy1;
105  (*hess)(xdim1_, xdim2_) -= ddx1;
106  (*hess)(xdim1_, ydim2_) -= dx1dy1;
107  (*hess)(ydim1_, xdim2_) -= dx1dy1;
108  (*hess)(ydim1_, ydim2_) -= ddy1;
109  (*hess)(xdim2_, xdim1_) -= ddx1;
110  (*hess)(xdim2_, ydim1_) -= dx1dy1;
111  (*hess)(ydim2_, xdim1_) -= dx1dy1;
112  (*hess)(ydim2_, ydim1_) -= ddy1;
113 }
114 
115 } // namespace ilqgames