Apollo 11.0
自动驾驶开放平台
fem_pos_deviation_smoother.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
22
23#include <coin/IpIpoptApplication.hpp>
24#include <coin/IpSolveStatistics.hpp>
25
26#include "cyber/common/log.h"
30
31namespace apollo {
32namespace planning {
36
38 const std::vector<std::pair<double, double>>& raw_point2d,
39 const std::vector<double>& bounds, std::vector<double>* opt_x,
40 std::vector<double>* opt_y,
41 std::vector<std::vector<common::math::Vec2d>> point_box) {
42 if (config_.apply_curvature_constraint()) {
43 if (config_.use_sqp()) {
44 return SqpWithOsqp(raw_point2d, bounds, opt_x, opt_y, point_box);
45 } else {
46 return NlpWithIpopt(raw_point2d, bounds, opt_x, opt_y);
47 }
48 } else {
49 return QpWithOsqp(raw_point2d, bounds, opt_x, opt_y);
50 }
51 return true;
52}
53
55 const std::vector<std::pair<double, double>>& raw_point2d,
56 const std::vector<double>& bounds, std::vector<double>* opt_x,
57 std::vector<double>* opt_y) {
58 if (opt_x == nullptr || opt_y == nullptr) {
59 AERROR << "opt_x or opt_y is nullptr";
60 return false;
61 }
62
64
68
69 solver.set_max_iter(config_.max_iter());
70 solver.set_time_limit(config_.time_limit());
71 solver.set_verbose(config_.verbose());
73 solver.set_warm_start(config_.warm_start());
74
75 solver.set_ref_points(raw_point2d);
76 solver.set_bounds_around_refs(bounds);
77
78 if (!solver.Solve()) {
79 return false;
80 }
81
82 *opt_x = solver.opt_x();
83 *opt_y = solver.opt_y();
84 return true;
85}
86
88 const std::vector<std::pair<double, double>>& raw_point2d,
89 const std::vector<double>& bounds, std::vector<double>* opt_x,
90 std::vector<double>* opt_y,
91 std::vector<std::vector<common::math::Vec2d>> point_box) {
92 if (opt_x == nullptr || opt_y == nullptr) {
93 AERROR << "opt_x or opt_y is nullptr";
94 return false;
95 }
96
98
104 solver.set_point_box(point_box);
105
107
108 solver.set_sqp_sub_max_iter(config_.sqp_sub_max_iter());
109 solver.set_sqp_ftol(config_.sqp_ftol());
110 solver.set_sqp_pen_max_iter(config_.sqp_pen_max_iter());
111 solver.set_sqp_ctol(config_.sqp_ctol());
112
113 solver.set_max_iter(config_.max_iter());
114 solver.set_time_limit(config_.time_limit());
115 solver.set_verbose(config_.verbose());
117 solver.set_warm_start(config_.warm_start());
118
119 solver.set_ref_points(raw_point2d);
120 solver.set_bounds_around_refs(bounds);
121
122 if (!solver.Solve()) {
123 return false;
124 }
125
126 std::vector<std::pair<double, double>> opt_xy = solver.opt_xy();
127
128 // TODO(Jinyun): unify output data container
129 opt_x->resize(opt_xy.size());
130 opt_y->resize(opt_xy.size());
131 for (size_t i = 0; i < opt_xy.size(); ++i) {
132 (*opt_x)[i] = opt_xy[i].first;
133 (*opt_y)[i] = opt_xy[i].second;
134 }
135 return true;
136}
137
139 const std::vector<std::pair<double, double>>& raw_point2d,
140 const std::vector<double>& bounds, std::vector<double>* opt_x,
141 std::vector<double>* opt_y) {
142 if (opt_x == nullptr || opt_y == nullptr) {
143 AERROR << "opt_x or opt_y is nullptr";
144 return false;
145 }
146
148 new FemPosDeviationIpoptInterface(raw_point2d, bounds);
149
151 smoother->set_weight_path_length(config_.weight_path_length());
156
157 Ipopt::SmartPtr<Ipopt::TNLP> problem = smoother;
158
159 // Create an instance of the IpoptApplication
160 Ipopt::SmartPtr<Ipopt::IpoptApplication> app = IpoptApplicationFactory();
161
162 app->Options()->SetIntegerValue("print_level",
163 static_cast<int>(config_.print_level()));
164 app->Options()->SetIntegerValue(
165 "max_iter", static_cast<int>(config_.max_num_of_iterations()));
166 app->Options()->SetIntegerValue(
167 "acceptable_iter",
168 static_cast<int>(config_.acceptable_num_of_iterations()));
169 app->Options()->SetNumericValue("tol", config_.tol());
170 app->Options()->SetNumericValue("acceptable_tol", config_.acceptable_tol());
171
172 Ipopt::ApplicationReturnStatus status = app->Initialize();
173 if (status != Ipopt::Solve_Succeeded) {
174 AERROR << "*** Error during initialization!";
175 return false;
176 }
177
178 status = app->OptimizeTNLP(problem);
179
180 if (status == Ipopt::Solve_Succeeded ||
181 status == Ipopt::Solved_To_Acceptable_Level) {
182 // Retrieve some statistics about the solve
183 Ipopt::Index iter_count = app->Statistics()->IterationCount();
184 ADEBUG << "*** The problem solved in " << iter_count << " iterations!";
185 } else {
186 AERROR << "Solver fails with return code: " << static_cast<int>(status);
187 return false;
188 }
189 smoother->get_optimization_results(opt_x, opt_y);
190 return true;
191}
192
193} // namespace planning
194} // namespace apollo
void set_weight_fem_pos_deviation(const double weight_fem_pos_deviation)
void get_optimization_results(std::vector< double > *ptr_x, std::vector< double > *ptr_y) const
void set_weight_ref_deviation(const double weight_ref_deviation)
void set_weight_curvature_constraint_slack_var(const double weight_curvature_constraint_slack_var)
void set_curvature_constraint(const double curvature_constraint)
void set_ref_points(const std::vector< std::pair< double, double > > &ref_points)
void set_weight_fem_pos_deviation(const double weight_fem_pos_deviation)
void set_bounds_around_refs(const std::vector< double > &bounds_around_refs)
void set_weight_ref_deviation(const double weight_ref_deviation)
bool QpWithOsqp(const std::vector< std::pair< double, double > > &raw_point2d, const std::vector< double > &bounds, std::vector< double > *opt_x, std::vector< double > *opt_y)
bool NlpWithIpopt(const std::vector< std::pair< double, double > > &raw_point2d, const std::vector< double > &bounds, std::vector< double > *opt_x, std::vector< double > *opt_y)
FemPosDeviationSmoother(const FemPosDeviationSmootherConfig &config)
bool SqpWithOsqp(const std::vector< std::pair< double, double > > &raw_point2d, const std::vector< double > &bounds, std::vector< double > *opt_x, std::vector< double > *opt_y)
bool Solve(const std::vector< std::pair< double, double > > &raw_point2d, const std::vector< double > &bounds, std::vector< double > *opt_x, std::vector< double > *opt_y, std::vector< std::vector< common::math::Vec2d > > point_box={})
void set_weight_curvature_constraint_slack_var(const double weight_curvature_constraint_slack_var)
void set_weight_fem_pos_deviation(const double weight_fem_pos_deviation)
const std::vector< std::pair< double, double > > & opt_xy() const
void set_ref_points(const std::vector< std::pair< double, double > > &ref_points)
void set_point_box(std::vector< std::vector< common::math::Vec2d > > point_box)
void set_bounds_around_refs(const std::vector< double > &bounds_around_refs)
Planning module main class.
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37