forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluator_base.cc
75 lines (68 loc) · 2.52 KB
/
evaluator_base.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
74
75
#include "drake/solvers/evaluator_base.h"
#include <set>
using std::make_shared;
using std::shared_ptr;
using Eigen::MatrixXd;
using Eigen::VectorXd;
namespace drake {
namespace solvers {
namespace {
// Check if each entry of gradient_sparsity_pattern is within [0, rows) and [0,
// cols), and if there are any repeated entries in gradient_sparsity_pattern.
void CheckGradientSparsityPattern(
const std::vector<std::pair<int, int>>& gradient_sparsity_pattern, int rows,
int cols) {
std::set<std::pair<int, int>> nonzero_entries;
for (const auto& nonzero_entry : gradient_sparsity_pattern) {
if (nonzero_entry.first < 0 || nonzero_entry.first >= rows) {
throw std::invalid_argument(
"Constraint::SetSparsityPattern(): row index out of range.");
}
if (nonzero_entry.second < 0 || nonzero_entry.second >= cols) {
throw std::invalid_argument(
"Constraint::SetSparsityPattern(): column index out of range.");
}
auto it = nonzero_entries.find(nonzero_entry);
if (it != nonzero_entries.end()) {
throw std::invalid_argument(
"Constraint::SetSparsityPatten(): was given entries with repeated "
"values.");
}
nonzero_entries.insert(it, nonzero_entry);
}
}
} // namespace
void EvaluatorBase::SetGradientSparsityPattern(
const std::vector<std::pair<int, int>>& gradient_sparsity_pattern) {
if (kDrakeAssertIsArmed) {
CheckGradientSparsityPattern(gradient_sparsity_pattern, num_outputs(),
num_vars());
}
gradient_sparsity_pattern_.emplace(gradient_sparsity_pattern);
}
void PolynomialEvaluator::DoEval(const Eigen::Ref<const Eigen::VectorXd>& x,
Eigen::VectorXd* y) const {
double_evaluation_point_temp_.clear();
for (size_t i = 0; i < poly_vars_.size(); i++) {
double_evaluation_point_temp_[poly_vars_[i]] = x[i];
}
y->resize(num_outputs());
for (int i = 0; i < num_outputs(); i++) {
(*y)[i] =
polynomials_[i].EvaluateMultivariate(double_evaluation_point_temp_);
}
}
void PolynomialEvaluator::DoEval(const Eigen::Ref<const AutoDiffVecXd>& x,
AutoDiffVecXd* y) const {
taylor_evaluation_point_temp_.clear();
for (size_t i = 0; i < poly_vars_.size(); i++) {
taylor_evaluation_point_temp_[poly_vars_[i]] = x[i];
}
y->resize(num_outputs());
for (int i = 0; i < num_outputs(); i++) {
(*y)[i] =
polynomials_[i].EvaluateMultivariate(taylor_evaluation_point_temp_);
}
}
} // namespace solvers
} // namespace drake