codac 2.0.0
Loading...
Searching...
No Matches
codac2_DiffInclusion.h
Go to the documentation of this file.
1
9
10#pragma once
11
13#include "codac2_SlicedTube.h"
14
15namespace codac2
16{
17 /* class to handle reachability steps on a specific domain,
18 has two methods : inflate (on DomA) and enclose (on DomA and DomB)
19 see codac2_ReachabilityStep.h */
20 template <typename DomA, typename DomB>
21 class ReachabilityStep;
22
23 /* DiffInclusionBase is an abstract class which considers only
24 * functions (IntervalVector,IntervalVector) -> IntervalVector.
25 * Below DiffInclusion extends DiffInclusionBase to handle other
26 * functions */
27 class DiffInclusionBase
28 {
29 public:
30 virtual IntervalVector f_eval(const EvalMode& m, const Interval &t,
31 const IntervalVector& x,
32 const IntervalVector& u) const =0;
33
34 virtual VectorType f_eval_diff(const Interval &t,
35 const IntervalVector& x,
36 const IntervalVector& u) const =0;
37
38 inline std::tuple<IntervalVector,IntervalMatrix,IntervalMatrix>
39 split_diff_matrix(const IntervalMatrix &da) const {
40 if (_time_dependant) {
41 return {
42 da.col(0),
43 da.middleCols(1,_x_shape.first*_x_shape.second),
44 da.rightCols(_u_shape.first*_u_shape.second)
45 };
46 } else {
47 return {
48 IntervalVector::Zero(_x_shape.first*_x_shape.second),
49 da.leftCols(_x_shape.first*_x_shape.second),
50 da.rightCols(_u_shape.first*_u_shape.second)
51 };
52 }
53 }
54
55 protected:
56 DiffInclusionBase(bool td) : _time_dependant(td) { }
57
58 IntervalVector approx_RK4_base(const Interval &time_step,
59 const IntervalVector& init, const IntervalVector& u,
60 bool forward, bool end_gate) const;
61
62 const bool _time_dependant = false;
63 std::pair<Index,Index> _x_shape;
64 std::pair<Index,Index> _u_shape;
65
66 private:
67 /* general templated single-step fixpoint-based integration scheme,
68 * starts with an initial set and an approximation of the reachable set
69 * 1) Increasing chain loop to get a post-fixpoint (widening process)
70 * a) intersect with current out => B
71 * b) compute guaranteed enclosing set A (function 2)
72 * c) check inclusion A <= B
73 * d) if inclusion false, enlarge and loop
74 * e) stops if inclusion is true or nb attempts too large (failure)
75 * 2) If 3) failed and unbounded out, returns false
76 * 3) Decreasing chain loop (narrowing process)
77 * a) starts with previous results if increasing succeeds, or
78 * with current out => B
79 * b) compute guaranteed enclosing set A (and gate) (function 2)
80 * c) check inclusion B <= A
81 * d) if inclusion true or nb attempts limited, stops
82 * e) otherwise, B=A and loop
83 * 4) returns true if 2 succeeded or one step of 4 succeeded,
84 * otherwise false
85 */
86 /* moved to codac2_DI_Integration */
87 };
88
89
90
91
96 template<typename XVar=VectorVar, typename UVar=VectorVar>
97 class DiffInclusion : public DiffInclusionBase
98 {
99 public:
100
101 /* Type : e.g. VectorType (for AnalyticFunction<XType>) */
102 using XType = typename ExprType<XVar>::Type;
103 /* X,U : e.g. IntervalVector */
104 using X = XType::Domain;
105 using U = typename ExprType<UVar>::Type::Domain;
106
107
108 inline DiffInclusion(const AnalyticFunction<XType>& f,
109 bool time_dependant=false) :
110 DiffInclusionBase(time_dependant), _f(f)
111 {
112 size_t x_arg=(time_dependant ? 1 : 0);
113 assert_release(f.args().size() == x_arg+2 &&
114 "DiffInclusion: wrong args size");
115 /* NdD : I don't understand why output_shape is not
116 a virtual method of VarBase but maybe some variable types
117 cannot have any "shape"... */
118 _u_shape = dynamic_cast<const UVar&>(*(f.args()[x_arg+1])).
119 output_shape();
120 if (time_dependant) {
121 assert_release((f.args()[0]->size() == 1)
122 && "DiffInclusion: wrong time size");
123 }
124 _x_shape = dynamic_cast<const XVar&>(*(f.args()[x_arg])).
125 output_shape();
126 assert_release(_x_shape == f.output_shape()
127 && "DiffInclusion: wrong output size");
128 }
129
137 template<bool forward=true, bool end_gate=false>
138 inline X approx_RK4(const Interval &time_step, const X& init, const U& u) {
139 return
140 to_X(approx_RK4_base(time_step,
141 from_X(init),from_U(u),forward,end_gate));
142 }
143
144
155 /* moved to codac2_DI_Integration.h */
156
157 inline IntervalVector f_eval(const EvalMode& m, const Interval &t,
158 const IntervalVector& x,
159 const IntervalVector& u) const override {
160 if constexpr(std::is_same_v<X,IntervalVector>
161 && std::is_same_v<U,IntervalVector>) {
162 if (_time_dependant)
163 return _f.eval(m,t,x,u);
164 else
165 return _f.eval(m,x,u);
166 } else {
167 if (_time_dependant)
168 return from_X(_f.eval(m,t,to_X(x),to_U(u)));
169 else
170 return from_X(_f.eval(m,to_X(x),to_U(u)));
171 }
172 }
173
174 inline VectorType f_eval_diff(const Interval &t,
175 const IntervalVector& x,
176 const IntervalVector& u) const override {
177 if constexpr(std::is_same_v<X,IntervalVector>
178 && std::is_same_v<U,IntervalVector>) {
179 if (_time_dependant)
180 return _f.eval_analytic(t,x,u);
181 else
182 return _f.eval_analytic(x,u);
183 } else {
184 if (_time_dependant) {
185 XType ret = _f.eval_analytic(t,to_X(x),to_U(u));
186 return { from_X(ret.m),
187 from_X(ret.a),
188 ret.da,
189 ret.def_domain };
190 }
191 else {
192 XType ret = _f.eval_analytic(to_X(x),to_U(u));
193 return { from_X(ret.m),
194 from_X(ret.a),
195 ret.da,
196 ret.def_domain };
197 }
198 }
199 }
200
201 inline IntervalVector from_X(const X& x) const {
202 if constexpr(std::is_same_v<X,Interval>) {
203 return { x };
204 } else {
205 return x.template reshaped<Eigen::ColMajor>();
206 }
207 }
208 inline Vector from_X(const XType::Scalar& x) const {
209 if constexpr(std::is_same_v<X,Interval>) {
210 return { x };
211 } else {
212 return x.template reshaped<Eigen::ColMajor>();
213 }
214 }
215 inline X to_X(const IntervalVector& x) const {
216 if constexpr(std::is_same_v<X,Interval>) {
217 return x[0];
218 } else {
219 return x.template reshaped<Eigen::ColMajor>
220 (_x_shape.first,_x_shape.second);
221 }
222 }
223
224 inline IntervalVector from_U(const U& u) const {
225 if constexpr(std::is_same_v<U,Interval>) {
226 return { u };
227 } else {
228 return u.template reshaped<Eigen::ColMajor>();
229 }
230 }
231 inline U to_U(const IntervalVector& u) const {
232 if constexpr(std::is_same_v<U,Interval>) {
233 return u[0];
234 } else {
235 return u.template reshaped<Eigen::ColMajor>
236 (_u_shape.first,_u_shape.second);
237 }
238 }
239
240 protected:
241 const AnalyticFunction<XType> _f;
242
243
244 };
245}
246
247
IntervalVector f_eval(const EvalMode &m, const Interval &t, const IntervalVector &x, const IntervalVector &u) const override
basic enclosing step
Definition codac2_DiffInclusion.h:157
X approx_RK4(const Interval &time_step, const X &init, const U &u)
approximation of IVP on a step using RK4. Does not return a guaranteed result, used for approximate e...
Definition codac2_DiffInclusion.h:138
const FunctionArgsList & args() const
Returns the arguments of the function.
Definition codac2_FunctionBase.h:93
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:50
auto & init(const Scalar &x)
Initializes all elements of the matrix with the scalar value x.
Definition codac2_Matrix_addons_Base.h:64
auto & init()
Initializes all elements of the matrix with default intervals.
Definition codac2_Matrix_addons_IntervalMatrixBase.h:113
Definition codac2_OctaSym.h:21
Eigen::Matrix< double,-1, 1 > Vector
Alias for a dynamically-sized column vector of doubles.
Definition codac2_Vector.h:24
Eigen::Matrix< Interval,-1, 1 > IntervalVector
Alias for a dynamic-size column vector of intervals.
Definition codac2_IntervalVector.h:25
Eigen::Matrix< Interval,-1,-1 > IntervalMatrix
Alias for a dynamic-size matrix of intervals.
Definition codac2_IntervalMatrix.h:25