49 #ifndef FASTRACK_VALUE_MATLAB_VALUE_FUNCTION_H 50 #define FASTRACK_VALUE_MATLAB_VALUE_FUNCTION_H 59 #include <ros/assert.h> 66 template <
typename TS,
typename TC,
typename TD,
typename PS,
typename PC,
67 typename PD,
typename RS,
typename RD,
typename B>
78 double Value(
const TS& tracker_x,
const PS& planner_x)
const;
79 std::unique_ptr<RelativeState<TS, PS>>
Gradient(
const TS& tracker_x,
80 const PS& planner_x)
const;
86 double Priority(
const TS& tracker_x,
const PS& planner_x)
const;
91 ros::NodeHandle nl(n);
93 std::string file_name;
94 if (!nl.getParam(
"file_name", file_name))
return false;
96 std::cout <<
"---------------------" << std::endl;
97 std::cout << file_name << std::endl;
144 template <
typename TS,
typename TC,
typename TD,
typename PS,
typename PC,
145 typename PD,
typename RS,
typename RD,
typename B>
147 const TS& tracker_x,
const PS& planner_x)
const {
148 const VectorXd relative_x = RS(tracker_x, planner_x).ToVector();
155 double approx_value = nn_value;
157 VectorXd neighbor = relative_x;
158 for (
size_t ii = 0; ii < relative_x.size(); ii++) {
160 if (center_distance(ii) >= 0.0)
166 neighbor(ii) = relative_x(ii);
169 const double slope = (center_distance(ii) >= 0.0)
170 ? (neighbor_value - nn_value) /
cell_size_[ii]
171 : (nn_value - neighbor_value) /
cell_size_[ii];
174 approx_value += slope * center_distance(ii);
181 template <
typename TS,
typename TC,
typename TD,
typename PS,
typename PC,
182 typename PD,
typename RS,
typename RD,
typename B>
183 std::unique_ptr<RelativeState<TS, PS>>
185 const TS& tracker_x,
const PS& planner_x)
const {
186 const VectorXd relative_x = RS(tracker_x, planner_x).ToVector();
193 return std::unique_ptr<RS>(
201 template <
typename TS,
typename TC,
typename TD,
typename PS,
typename PC,
202 typename PD,
typename RS,
typename RD,
typename B>
204 const TS& tracker_x,
const PS& planner_x)
const {
205 const double value =
Value(tracker_x, planner_x);
215 template <
typename TS,
typename TC,
typename TD,
typename PS,
typename PC,
216 typename PD,
typename RS,
typename RD,
typename B>
218 const VectorXd& x)
const {
220 std::vector<size_t> quantized;
221 for (
size_t ii = 0; ii < x.size(); ii++) {
223 ROS_WARN_THROTTLE(1.0,
224 "%s: State is too small in dimension %zu: %f vs %f",
226 quantized.push_back(0);
227 }
else if (x(ii) >
upper_[ii]) {
228 ROS_WARN_THROTTLE(1.0,
229 "%s: State is too large in dimension %zu: %f vs %f",
240 size_t idx = quantized[0];
241 for (
size_t ii = 1; ii < quantized.size(); ii++) {
243 idx += quantized[ii];
250 template <
typename TS,
typename TC,
typename TD,
typename PS,
typename PC,
251 typename PD,
typename RS,
typename RD,
typename B>
257 VectorXd gradient(x.size());
258 for (
size_t ii = 0; ii < gradient.size(); ii++)
266 template <
typename TS,
typename TC,
typename TD,
typename PS,
typename PC,
267 typename PD,
typename RS,
typename RD,
typename B>
274 template <
typename TS,
typename TC,
typename TD,
typename PS,
typename PC,
275 typename PD,
typename RS,
typename RD,
typename B>
277 const VectorXd& x,
size_t idx)
const {
279 const double center =
284 return (center > x(idx)) ? center -
cell_size_[idx] : center;
288 template <
typename TS,
typename TC,
typename TD,
typename PS,
typename PC,
289 typename PD,
typename RS,
typename RD,
typename B>
292 VectorXd center(x.size());
294 for (
size_t ii = 0; ii < center.size(); ii++)
304 template <
typename TS,
typename TC,
typename TD,
typename PS,
typename PC,
305 typename PD,
typename RS,
typename RD,
typename B>
318 const double fractional_dist = (x(idx) - lower) / cell_size_[idx];
321 VectorXd x_lower = x;
322 x_lower(idx) = lower;
324 VectorXd x_upper = x;
325 x_upper(idx) = upper;
328 if (idx == x.size() - 1) {
336 (1.0 - fractional_dist);
341 template <
typename TS,
typename TC,
typename TD,
typename PS,
typename PC,
342 typename PD,
typename RS,
typename RD,
typename B>
348 if (!reader.
IsOpen())
return false;
360 ROS_ERROR(
"%s: Priority lower bound above upper bound.",
361 this->
name_.c_str());
366 ROS_ERROR(
"%s: Dimensions do not match.", this->
name_.c_str());
370 const double total_num_cells = std::accumulate(
372 if (total_num_cells == 0) {
373 ROS_ERROR(
"%s: 0 total cells.", this->
name_.c_str());
377 if (
data_.size() != total_num_cells) {
378 ROS_ERROR(
"%s: Grid data was of the wrong size.", this->
name_.c_str());
383 for (
size_t ii = 0; ii <
num_cells_.size(); ii++)
388 for (
size_t ii = 0; ii <
num_cells_.size(); ii++) {
391 if (!reader.
ReadVector(
"deriv_" + std::to_string(ii), &partial)) {
392 ROS_ERROR(
"%s: Could not read deriv_%zu.", this->
name_.c_str(), ii);
396 if (partial.size() != total_num_cells) {
397 ROS_ERROR(
"%s: Partial derivative in dimension %zu had incorrect size.",
398 this->
name_.c_str(), ii);
404 std::vector<double> params;
405 if (!reader.
ReadVector(
"tracker_params", ¶ms))
return false;
407 if (!reader.
ReadVector(
"planner_params", ¶ms))
return false;
411 if (!reader.
ReadVector(
"bound_params", ¶ms))
return false;
412 if (!this->
bound_.Initialize(params))
return false;
std::vector< double > lower_
bool InitializeFromMatFile(const std::string &file_name)
VectorXd NearestCenterPoint(const VectorXd &x) const
double LowerGridPoint(const VectorXd &x, size_t idx) const
std::unique_ptr< RelativeState< TS, PS > > Gradient(const TS &tracker_x, const PS &planner_x) const
bool ReadVector(const std::string &field_name, std::vector< double > *values)
std::vector< size_t > num_cells_
size_t StateToIndex(const VectorXd &x) const
bool ReadScalar(const std::string &field_name, double *value)
VectorXd RecursiveGradientInterpolator(const VectorXd &x, size_t idx) const
VectorXd DirectionToCenter(const VectorXd &x) const
VectorXd GradientAccessor(const VectorXd &x) const
std::vector< std::vector< double > > gradient_
std::vector< double > upper_
double Priority(const TS &tracker_x, const PS &planner_x) const
double Value(const TS &tracker_x, const PS &planner_x) const
std::unique_ptr< RelativeDynamics< TS, TC, PS, PC > > relative_dynamics_
bool LoadParameters(const ros::NodeHandle &n)
std::vector< double > cell_size_
std::vector< double > data_