45 #include <ilqgames/cost/proximity_cost.h> 46 #include <ilqgames/utils/types.h> 48 #include <glog/logging.h> 52 float ProximityCost::Evaluate(
const VectorXf& input)
const {
53 const float dx = input(xidx1_) - input(xidx2_);
54 const float dy = input(yidx1_) - input(yidx2_);
55 const float delta_sq = dx * dx + dy * dy;
57 if (delta_sq >= threshold_sq_)
return 0.0;
59 const float gap = threshold_ - std::sqrt(delta_sq);
60 return 0.5 * weight_ * gap * gap;
63 void ProximityCost::Quadraticize(
const VectorXf& input, MatrixXf* hess,
64 VectorXf* grad)
const {
69 CHECK_EQ(input.size(), hess->rows());
70 CHECK_EQ(input.size(), hess->cols());
71 CHECK_EQ(input.size(), grad->size());
74 const float dx = input(xidx1_) - input(xidx2_);
75 const float dy = input(yidx1_) - input(yidx2_);
76 const float delta_sq = dx * dx + dy * dy;
79 if (delta_sq >= threshold_sq_)
return;
81 const float delta = std::sqrt(delta_sq);
82 const float gap = threshold_ - delta;
83 const float weight_delta = weight_ / delta;
84 const float dx_delta = dx / delta;
85 const float dy_delta = dy / delta;
87 const float ddx1 = -weight_delta * gap * dx;
88 const float ddy1 = -weight_delta * gap * dy;
89 const float hess_x1x1 =
90 weight_delta * (dx_delta * (gap * dx_delta + dx) - gap);
91 const float hess_y1y1 =
92 weight_delta * (dy_delta * (gap * dy_delta + dy) - gap);
93 const float hess_x1y1 = weight_delta * (dx_delta * (gap * dy_delta + dy));
95 (*grad)(xidx1_) += ddx1;
96 (*grad)(xidx2_) -= ddx1;
98 (*grad)(yidx1_) += ddy1;
99 (*grad)(yidx2_) -= ddy1;
101 (*hess)(xidx1_, xidx1_) += hess_x1x1;
102 (*hess)(xidx1_, xidx2_) -= hess_x1x1;
103 (*hess)(xidx2_, xidx1_) -= hess_x1x1;
104 (*hess)(xidx2_, xidx2_) += hess_x1x1;
106 (*hess)(yidx1_, yidx1_) += hess_y1y1;
107 (*hess)(yidx1_, yidx2_) -= hess_y1y1;
108 (*hess)(yidx2_, yidx1_) -= hess_y1y1;
109 (*hess)(yidx2_, yidx2_) += hess_y1y1;
111 (*hess)(xidx1_, yidx1_) += hess_x1y1;
112 (*hess)(yidx1_, xidx1_) += hess_x1y1;
114 (*hess)(xidx1_, yidx2_) -= hess_x1y1;
115 (*hess)(yidx2_, xidx1_) -= hess_x1y1;
117 (*hess)(xidx2_, yidx1_) -= hess_x1y1;
118 (*hess)(yidx1_, xidx2_) -= hess_x1y1;
120 (*hess)(xidx2_, yidx2_) += hess_x1y1;
121 (*hess)(yidx2_, xidx2_) += hess_x1y1;