44 #include <ilqgames/cost/quadratic_norm_cost.h> 45 #include <ilqgames/utils/types.h> 47 #include <glog/logging.h> 51 float QuadraticNormCost::Evaluate(
const VectorXf& input)
const {
52 CHECK_LT(dim1_, input.size());
53 CHECK_LT(dim2_, input.size());
56 const float diff = std::hypot(input(dim1_), input(dim2_)) - nominal_;
57 return 0.5 * weight_ * diff * diff;
60 void QuadraticNormCost::Quadraticize(
const VectorXf& input, MatrixXf* hess,
61 VectorXf* grad)
const {
62 CHECK_LT(dim1_, input.size());
63 CHECK_LT(dim2_, input.size());
68 CHECK_EQ(input.size(), hess->rows());
69 CHECK_EQ(input.size(), hess->cols());
70 CHECK_EQ(input.size(), grad->size());
73 const float x = input(dim1_);
74 const float y = input(dim2_);
75 const float x_sq = x * x;
76 const float y_sq = y * y;
77 const float norm_sq = x_sq + y_sq;
78 const float norm = std::sqrt(norm_sq);
79 const float norm_3 = norm * norm_sq;
81 const float dx = -weight_ * x * (-1.0 + nominal_ / norm);
82 const float dy = -weight_ * y * (-1.0 + nominal_ / norm);
83 const float ddx = weight_ - (nominal_ * y_sq * weight_) / norm_3;
84 const float ddy = weight_ - (nominal_ * x_sq * weight_) / norm_3;
85 const float dxdy = nominal_ * x * y * weight_ / norm_3;
90 (*hess)(dim1_, dim1_) += ddx;
91 (*hess)(dim2_, dim2_) += ddy;
92 (*hess)(dim1_, dim2_) += dxdy;
93 (*hess)(dim2_, dim1_) += dxdy;