43 #include <ilqgames/cost/relative_distance_cost.h> 44 #include <ilqgames/utils/types.h> 46 #include <glog/logging.h> 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);
56 void RelativeDistanceCost::Quadraticize(
const VectorXf& input, MatrixXf* hess,
57 VectorXf* grad)
const {
61 CHECK_EQ(input.size(), hess->rows());
62 CHECK_EQ(input.size(), hess->cols());
64 if (grad) CHECK_EQ(input.size(), grad->size());
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;
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;
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;
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;
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;
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;
96 const float dx = weight_ * diff_x / dist;
97 const float dy = weight_ * diff_y / dist;
99 (*grad)(dims1_.first) += dx;
100 (*grad)(dims1_.second) += dy;
101 (*grad)(dims2_.first) -= dx;
102 (*grad)(dims2_.second) -= dy;