ilqgames
A new real-time solver for large-scale differential games.
affine_vector_constraint.h
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) affine vector constraint, i.e., g(x) = ||A x - b||.
40 //
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 #ifndef ILQGAMES_CONSTRAINT_AFFINE_VECTOR_CONSTRAINT_H
44 #define ILQGAMES_CONSTRAINT_AFFINE_VECTOR_CONSTRAINT_H
45 
46 #include <ilqgames/constraint/time_invariant_constraint.h>
47 #include <ilqgames/utils/types.h>
48 
49 #include <glog/logging.h>
50 #include <memory>
51 #include <string>
52 
53 namespace ilqgames {
54 
56  public:
58  AffineVectorConstraint(const MatrixXf& A, const VectorXf& b, bool is_equality,
59  const std::string& name = "")
60  : TimeInvariantConstraint(is_equality, name),
61  A_(A),
62  b_(b),
63  ATA_(A.transpose() * A),
64  AAT_(A * A.transpose()) {
65  CHECK_EQ(A_.rows(), b_.size());
66  }
67 
68  // Evaluate this constraint value, i.e., g(x).
69  float Evaluate(const VectorXf& input) const {
70  CHECK_EQ(A_.rows(), input.size());
71  return (A_ * input - b_).norm();
72  }
73 
74  // Quadraticize the constraint value and its square, each scaled by lambda or
75  // mu, respectively (terms in the augmented Lagrangian).
76  void Quadraticize(Time t, const VectorXf& input, MatrixXf* hess,
77  VectorXf* grad) const {
78  CHECK_NOTNULL(hess);
79  CHECK_NOTNULL(grad);
80  CHECK_EQ(input.size(), b_.size());
81  CHECK_EQ(hess->rows(), input.size());
82  CHECK_EQ(hess->cols(), input.size());
83  CHECK_EQ(grad->size(), input.size());
84 
85  // Get current lambda and mu.
86  const float lambda = Lambda(t);
87  const float mu = Mu(t, input);
88 
89  // Compute value of the constraint.
90  const VectorXf delta = A_ * input - b_;
91  const float value = delta.norm();
92 
93  // Compute gradient and Hessian.
94  const VectorXf AT_delta = A_.transpose() * delta;
95  (*grad) += (mu + lambda / value) * AT_delta;
96  (*hess) += (lambda / value) *
97  (AAT_ - AT_delta * AT_delta.transpose() / (value * value)) +
98  mu * ATA_;
99  }
100 
101  private:
102  // A and b.
103  const MatrixXf A_;
104  const VectorXf b_;
105 
106  // Precompute ATA and AAT.
107  const MatrixXf ATA_;
108  const MatrixXf AAT_;
109 }; //\class AffineVectorConstraint
110 
111 } // namespace ilqgames
112 
113 #endif