forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcost.cc
55 lines (46 loc) · 1.56 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
#include "drake/solvers/cost.h"
#include <memory>
using std::make_shared;
using std::shared_ptr;
using Eigen::MatrixXd;
using Eigen::VectorXd;
namespace drake {
namespace solvers {
void LinearCost::DoEval(const Eigen::Ref<const Eigen::VectorXd>& x,
Eigen::VectorXd& y) const {
y.resize(1);
y(0) = a_.dot(x) + b_;
}
void LinearCost::DoEval(const Eigen::Ref<const AutoDiffVecXd>& x,
AutoDiffVecXd& y) const {
y.resize(1);
y(0) = a_.cast<AutoDiffXd>().dot(x) + b_;
}
void QuadraticCost::DoEval(const Eigen::Ref<const Eigen::VectorXd>& x,
Eigen::VectorXd& y) const {
y.resize(1);
y = .5 * x.transpose() * Q_ * x + b_.transpose() * x;
y(0) += c_;
}
void QuadraticCost::DoEval(const Eigen::Ref<const AutoDiffVecXd>& x,
AutoDiffVecXd& y) const {
y.resize(1);
y = .5 * x.transpose() * Q_.cast<AutoDiffXd>() * x +
b_.cast<AutoDiffXd>().transpose() * x;
y(0) += c_;
}
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