ilqgames
A new real-time solver for large-scale differential games.
proximity_constraint.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 // (Time-invariant) proximity (inequality) constraint between two vehicles, i.e.
40 // g(x) = (+/-) (||(px1, py1) - (px2, py2)|| - d) <= 0
41 //
42 // NOTE: The `keep_within` argument specifies the sign of g (true corresponds to
43 // positive).
44 //
45 ///////////////////////////////////////////////////////////////////////////////
46 
47 #include <ilqgames/constraint/proximity_constraint.h>
48 #include <ilqgames/utils/types.h>
49 
50 #include <glog/logging.h>
51 #include <memory>
52 #include <string>
53 
54 namespace ilqgames {
55 
56 float ProximityConstraint::Evaluate(const VectorXf& input) const {
57  const float dx = input(xidx1_) - input(xidx2_);
58  const float dy = input(yidx1_) - input(yidx2_);
59  const float value = std::hypot(dx, dy) - threshold_;
60 
61  return (keep_within_) ? value : -value;
62 }
63 
64 void ProximityConstraint::Quadraticize(Time t, const VectorXf& input,
65  MatrixXf* hess, VectorXf* grad) const {
66  CHECK_NOTNULL(hess);
67  CHECK_NOTNULL(grad);
68  CHECK_EQ(hess->rows(), input.size());
69  CHECK_EQ(hess->cols(), input.size());
70  CHECK_EQ(grad->size(), input.size());
71 
72  // Compute proximity.
73  const float dx = input(xidx1_) - input(xidx2_);
74  const float dy = input(yidx1_) - input(yidx2_);
75  const float prox = std::hypot(dx, dy);
76  const float sign = (keep_within_) ? 1.0 : -1.0;
77  const float g = sign * (prox - threshold_);
78 
79  // Compute gradient and Hessian.
80  const float rel_dx = dx / prox;
81  const float rel_dy = dy / prox;
82 
83  float grad_x1 = sign * rel_dx;
84  float grad_y1 = sign * rel_dy;
85  float hess_x1x1 = sign * (1.0 - rel_dx * rel_dx) / prox;
86  float hess_y1y1 = sign * (1.0 - rel_dy * rel_dy) / prox;
87  float hess_x1y1 = -sign * rel_dx * rel_dy / prox;
88 
89  ModifyDerivatives(t, g, &grad_x1, &hess_x1x1, &grad_y1, &hess_y1y1,
90  &hess_x1y1);
91 
92  (*grad)(xidx1_) += grad_x1;
93  (*grad)(xidx2_) -= grad_x1;
94 
95  (*grad)(yidx1_) += grad_y1;
96  (*grad)(yidx2_) -= grad_y1;
97 
98  (*hess)(xidx1_, xidx1_) += hess_x1x1;
99  (*hess)(xidx1_, xidx2_) -= hess_x1x1;
100  (*hess)(xidx2_, xidx1_) -= hess_x1x1;
101  (*hess)(xidx2_, xidx2_) += hess_x1x1;
102 
103  (*hess)(yidx1_, yidx1_) += hess_y1y1;
104  (*hess)(yidx1_, yidx2_) -= hess_y1y1;
105  (*hess)(yidx2_, yidx1_) -= hess_y1y1;
106  (*hess)(yidx2_, yidx2_) += hess_y1y1;
107 
108  (*hess)(xidx1_, yidx1_) += hess_x1y1;
109  (*hess)(xidx1_, yidx2_) -= hess_x1y1;
110  (*hess)(xidx2_, yidx1_) -= hess_x1y1;
111  (*hess)(xidx2_, yidx2_) += hess_x1y1;
112  (*hess)(yidx1_, xidx1_) += hess_x1y1;
113  (*hess)(yidx1_, xidx2_) -= hess_x1y1;
114  (*hess)(yidx2_, xidx1_) -= hess_x1y1;
115  (*hess)(yidx2_, xidx2_) += hess_x1y1;
116 }
117 
118 } // namespace ilqgames