45 #include <ilqgames/cost/semiquadratic_norm_cost.h> 46 #include <ilqgames/utils/types.h> 48 #include <glog/logging.h> 52 float SemiquadraticNormCost::Evaluate(
const VectorXf& input)
const {
53 CHECK_LT(dim1_, input.size());
54 CHECK_LT(dim2_, input.size());
56 const float diff = std::hypot(input(dim1_), input(dim2_)) - threshold_;
57 if ((diff > 0.0 && oriented_right_) || (diff < 0.0 && !oriented_right_))
58 return 0.5 * weight_ * diff * diff;
63 void SemiquadraticNormCost::Quadraticize(
const VectorXf& input, MatrixXf* hess,
64 VectorXf* grad)
const {
65 CHECK_LT(dim1_, input.size());
66 CHECK_LT(dim2_, input.size());
71 CHECK_EQ(input.size(), hess->rows());
72 CHECK_EQ(input.size(), hess->cols());
73 CHECK_EQ(input.size(), grad->size());
76 const float norm = std::hypot(input(dim1_), input(dim2_));
77 if ((norm > threshold_ && !oriented_right_) ||
78 (norm < threshold_ && oriented_right_))
82 const float norm_2 = norm * norm;
83 const float norm_3 = norm * norm_2;
84 const float x = input(dim1_);
85 const float y = input(dim2_);
86 const float dx = -weight_ * x * (-1.0 + threshold_ / norm);
87 const float dy = -weight_ * y * (-1.0 + threshold_ / norm);
88 const float ddx = weight_ - (threshold_ * y * y * weight_) / norm_3;
89 const float ddy = weight_ - (threshold_ * x * x * weight_) / norm_3;
90 const float dxdy = threshold_ * x * y * weight_ / norm_3;
95 (*hess)(dim1_, dim1_) += ddx;
96 (*hess)(dim2_, dim2_) += ddy;
97 (*hess)(dim1_, dim2_) += dxdy;
98 (*hess)(dim2_, dim1_) += dxdy;