aligator  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
filter.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include "aligator/fwd.hpp"
6
7#include <functional>
8
9namespace aligator {
10
12template <typename Scalar> struct FilterTpl {
13public:
15
16 // List of all filter pairs
17 std::vector<std::pair<Scalar, Scalar>> filter_pairs_;
18
19 // Proximity parameter
20 Scalar beta_;
21 Scalar alpha_min_;
22 std::size_t max_num_steps_;
23
24 FilterTpl(const Scalar &beta, const Scalar &alpha_min,
25 const std::size_t &max_num_steps) {
26 beta_ = beta;
27 alpha_min_ = alpha_min;
28 max_num_steps_ = max_num_steps;
29 filter_pairs_.clear();
30 }
31
32 virtual ~FilterTpl() = default;
33
34 void resetFilter(const Scalar &beta, const Scalar &alpha_min,
35 const std::size_t &max_num_steps) {
36 beta_ = beta;
37 alpha_min_ = alpha_min;
38 max_num_steps_ = max_num_steps;
39 filter_pairs_.clear();
40 }
41
42 Scalar run(const std::function<std::pair<Scalar, Scalar>(Scalar)> &phi,
43 Scalar &alpha_try) {
44 alpha_try = 1;
45 std::pair<Scalar, Scalar> fpair;
46 // Try full step, backtrack if failure
47 while (true) {
48 try {
49 fpair = phi(alpha_try);
50 break;
51 } catch (const std::runtime_error &e) {
52 alpha_try *= 0.5;
53 if (alpha_try <= alpha_min_) {
54 alpha_try = alpha_min_;
55 break;
56 }
57 }
58 }
59
60 // Try to accept pair, backtrack if failure
61 for (std::size_t i = 0; i < max_num_steps_; i++) {
62 if (!accept_pair(fpair)) {
63 alpha_try *= 0.5;
64 if (alpha_try <= alpha_min_) {
65 alpha_try = alpha_min_;
66 fpair = phi(alpha_try);
67 break;
68 }
69 fpair = phi(alpha_try);
70 } else
71 break;
72 }
73
74 // TODO: else, feasilibity restauration by minimizing h
75 return fpair.first;
76 }
77
78 bool accept_pair(const std::pair<Scalar, Scalar> &fpair) {
79 // Check if pair is acceptable to the filter
80 for (auto el = filter_pairs_.begin(); el != filter_pairs_.end(); el++) {
81 std::pair<Scalar, Scalar> element = *el;
82 if (element.first + beta_ * element.second <= fpair.first and
83 element.second + beta_ * element.second <= fpair.second) {
84 return false;
85 }
86 }
87
88 // If acceptable, remove all pairs dominated by it
89 for (auto el = filter_pairs_.begin(); el != filter_pairs_.end();) {
90 std::pair<Scalar, Scalar> element = *el;
91 if (fpair.first <= element.first and fpair.second <= element.second) {
92 el = filter_pairs_.erase(el);
93 } else {
94 el++;
95 }
96 }
97
98 // Push new pair inside filter
99 filter_pairs_.push_back(fpair);
100
101 return true;
102 }
103};
104} // namespace aligator
105
106#ifdef ALIGATOR_ENABLE_TEMPLATE_INSTANTIATION
107#include "./filter.txx"
108#endif
Forward declarations.
Main package namespace.
bool accept_pair(const std::pair< Scalar, Scalar > &fpair)
Definition filter.hpp:78
ALIGATOR_DYNAMIC_TYPEDEFS(Scalar)
std::vector< std::pair< Scalar, Scalar > > filter_pairs_
Definition filter.hpp:17
Scalar run(const std::function< std::pair< Scalar, Scalar >(Scalar)> &phi, Scalar &alpha_try)
Definition filter.hpp:42
std::size_t max_num_steps_
Definition filter.hpp:22
FilterTpl(const Scalar &beta, const Scalar &alpha_min, const std::size_t &max_num_steps)
Definition filter.hpp:24
virtual ~FilterTpl()=default
void resetFilter(const Scalar &beta, const Scalar &alpha_min, const std::size_t &max_num_steps)
Definition filter.hpp:34