forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cost.cc
73 lines (61 loc) · 2.13 KB
/
cost.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "drake/solvers/cost.h"
#include <memory>
using Eigen::MatrixXd;
using Eigen::VectorXd;
using std::make_shared;
using std::shared_ptr;
namespace drake {
namespace solvers {
template <typename DerivedX, typename U>
void LinearCost::DoEvalGeneric(const Eigen::MatrixBase<DerivedX>& x,
VectorX<U>* y) const {
y->resize(1);
(*y)(0) = a_.dot(x) + b_;
}
void LinearCost::DoEval(const Eigen::Ref<const Eigen::VectorXd>& x,
Eigen::VectorXd* y) const {
DoEvalGeneric(x, y);
}
void LinearCost::DoEval(const Eigen::Ref<const AutoDiffVecXd>& x,
AutoDiffVecXd* y) const {
DoEvalGeneric(x, y);
}
void LinearCost::DoEval(const Eigen::Ref<const VectorX<symbolic::Variable>>& x,
VectorX<symbolic::Expression>* y) const {
DoEvalGeneric(x, y);
}
template <typename DerivedX, typename U>
void QuadraticCost::DoEvalGeneric(const Eigen::MatrixBase<DerivedX>& x,
VectorX<U>* y) const {
y->resize(1);
*y = .5 * x.transpose() * Q_ * x + b_.transpose() * x;
(*y)(0) += c_;
}
void QuadraticCost::DoEval(const Eigen::Ref<const Eigen::VectorXd>& x,
Eigen::VectorXd* y) const {
DoEvalGeneric(x, y);
}
void QuadraticCost::DoEval(const Eigen::Ref<const AutoDiffVecXd>& x,
AutoDiffVecXd* y) const {
DoEvalGeneric(x, y);
}
void QuadraticCost::DoEval(
const Eigen::Ref<const VectorX<symbolic::Variable>>& x,
VectorX<symbolic::Expression>* y) const {
DoEvalGeneric(x, y);
}
shared_ptr<QuadraticCost> MakeQuadraticErrorCost(
const Eigen::Ref<const MatrixXd>& Q,
const Eigen::Ref<const VectorXd>& x_desired) {
const double c = x_desired.dot(Q * x_desired);
return make_shared<QuadraticCost>(2 * Q, -2 * Q * x_desired, c);
}
shared_ptr<QuadraticCost> MakeL2NormCost(
const Eigen::Ref<const Eigen::MatrixXd>& A,
const Eigen::Ref<const Eigen::VectorXd>& b) {
const double c = b.dot(b);
return make_shared<QuadraticCost>(2 * A.transpose() * A,
-2 * A.transpose() * b, c);
}
} // namespace solvers
} // namespace drake