codac 2.0.0
Loading...
Searching...
No Matches
codac2_CtcInverse.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <map>
14#include "codac2_Ctc.h"
15#include "codac2_CtcWrapper.h"
16#include "codac2_Collection.h"
17#include "codac2_arith_mul.h"
18#include "codac2_Wrapper.h"
19
20namespace codac2
21{
22 class CtcNot;
23
24 template<typename Y_, typename X0=IntervalVector, typename... X>
25 class CtcInverse : public Ctc<CtcInverse<Y_,X0,X...>,X0,X...>
26 {
27 public:
28
29 using Y = typename Wrapper<Y_>::Domain; // Y_ is a possible Eigen expr. type
30 using OutputType = typename ExprType<Y>::Type;
31
32 template<typename C>
33 requires IsCtcBaseOrPtr<C,Y>
34 CtcInverse(const AnalyticFunction<OutputType>& f, const C& ctc_y, bool with_centered_form = true, bool is_not_in = false)
35 : Ctc<CtcInverse<Y,X0,X...>,X0,X...>(f.input_size()), _f(f), _ctc_y(ctc_y), _with_centered_form(with_centered_form), _is_not_in(is_not_in)
36 {
37 assert_release([&]() { return f.output_size() == size_of(ctc_y); }()
38 && "CtcInverse: invalid dimension of image argument ('y' or 'ctc_y')");
39 }
40
41 CtcInverse(const AnalyticFunction<OutputType>& f, const Y& y, bool with_centered_form = true, bool is_not_in = false)
42 : CtcInverse(f, CtcWrapper<Y,Y>(y), with_centered_form, is_not_in)
43 {
44 if constexpr(std::is_same_v<Y,IntervalMatrix>)
45 {
46 const auto [rows,cols] = f.output_shape();
47 assert_release(y.rows() == rows && y.cols() == cols
48 && "CtcInverse: invalid output matrix dimensions");
49 }
50 }
51
52 void contract(X0& x0, X&... x) const
53 {
54 return contract_(*_ctc_y.front(), x0, x...);
55 }
56
57 void contract_(const Y& y, X0& x0, X&... x) const
58 {
59 return contract_(CtcWrapper<Y,Y>(y), x0, x...);
60 }
61
62 void contract_(const CtcBase<Y>& ctc_y, X0& x0, X&... x) const
63 {
64 assert_release(_f.nb_args() == 1 + sizeof...(X)
65 && "CtcInverse: invalid number of input arguments");
66
67 ValuesMap v;
68 // Setting user values into a map before the tree evaluation
69 _f.fill_from_args(v, x0, x...);
70
71 // Forward/backward algorithm:
72
73 // [1/4] Forward evaluation
74 _f.expr()->fwd_eval(v, _f.input_size(), !_with_centered_form);
75 auto& val_expr = _f.expr()->value(v);
76
77 if(_is_not_in && !val_expr.def_domain)
78 return; // <-- iota: if the input x is outside the definition
79 // domain of one of the involved expressions, or their combinations,
80 // then the inner contraction is disabled.
81
82 // [2/4] Performing top contraction (from the root of the tree) with
83 // the contractor expressing the output domain Y.
84 // The underlying constraint is: f(x) \in [y]
85 ctc_y.contract(val_expr.a);
86
87 // [3/4 - optional]
88 // The contraction can be significantly improved using a centered form
89 // expression (enabled by default). This step must be processed before the
90 // backward part of the FwdBwd algorithm (the .m, .a values must not be
91 // changed before the centered evaluation).
92 if(_with_centered_form && val_expr.def_domain && !val_expr.da.is_unbounded() && val_expr.da.size() != 0)
93 {
94 // todo: the above condition !val_expr.da.is_unbounded() should not be necesary,
95 // possible bug in MulOp in case of unbounded domain?
96
97 IntervalVector x_box = cart_prod(x0,x...);
98 IntervalVector x_mid = cart_prod(x0.mid(), x.mid()...);
99 IntervalVector p = x_box - x_mid;
100 Y diffval = val_expr.a - val_expr.m;
101 IntervalVector fm = cart_prod(diffval);
102 MulOp::bwd(fm, val_expr.da, p);
103 intersect_from_flat_vector((p + x_mid).eval(), x0, x...); // updating input values
104 }
105
106 // [4/4] Backward evaluation
107 _f.expr()->bwd_eval(v); // recursive backward from the root to the leaves
108 _f.intersect_from_args(v, x0, x...); // updating input values
109 }
110
111 const AnalyticFunction<OutputType>& fnc() const
112 {
113 return _f;
114 }
115
116 private:
117
118 static void intersect_from_flat_vector(Interval& x, const IntervalVector& v, Index& i)
119 {
120 x &= v[i];
121 i++;
122 }
123
124 static void intersect_from_flat_vector(IntervalVector& x, const IntervalVector& v, Index& i)
125 {
126 x &= v.segment(i,x.size());
127 i+=x.size();
128 }
129
130 static void intersect_from_flat_vector(IntervalMatrix& x, const IntervalVector& v, Index& i)
131 {
132 x &= v.segment(i,x.size()).reshaped<Eigen::ColMajor>(x.rows(),x.cols());
133 i+=x.size();
134 }
135
136 template<typename... T>
137 static void intersect_from_flat_vector(const IntervalVector& v, T&... x)
138 {
139 Index i=0;
140 ((intersect_from_flat_vector(x,v,i)), ...);
141 }
142
143 protected:
144
145 const AnalyticFunction<OutputType> _f;
146 const Collection<CtcBase<Y>> _ctc_y;
147 bool _with_centered_form;
148 bool _is_not_in = false;
149 };
150
151
152 // Template deduction guides
153
154 // ScalarType
155
156 CtcInverse(const AnalyticFunction<ScalarType>&, std::initializer_list<double>, bool = true, bool = false) ->
157 CtcInverse<Interval,IntervalVector>;
158
159 template<typename Y>
160 CtcInverse(const AnalyticFunction<ScalarType>&, std::initializer_list<Y>, bool = true, bool = false) ->
161 CtcInverse<Interval,IntervalVector>;
162
163 CtcInverse(const AnalyticFunction<ScalarType>&, const Interval&, bool = true, bool = false) ->
164 CtcInverse<Interval,IntervalVector>;
165
166 CtcInverse(const AnalyticFunction<ScalarType>&, double, bool = true, bool = false) ->
167 CtcInverse<Interval,IntervalVector>;
168
169 template<typename C>
170 requires IsCtcBaseOrPtr<C,Interval>
171 CtcInverse(const AnalyticFunction<ScalarType>&, const C&, bool = true, bool = false) ->
172 CtcInverse<Interval,IntervalVector>;
173
174 // VectorType
175
176 CtcInverse(const AnalyticFunction<VectorType>&, std::initializer_list<double>, bool = true, bool = false) ->
177 CtcInverse<IntervalVector,IntervalVector>;
178
179 CtcInverse(const AnalyticFunction<VectorType>&, std::initializer_list<std::initializer_list<double>>, bool = true, bool = false) ->
180 CtcInverse<IntervalVector,IntervalVector>;
181
182 CtcInverse(const AnalyticFunction<VectorType>&, const Vector&, bool = true, bool = false) ->
183 CtcInverse<IntervalVector,IntervalVector>;
184
185 CtcInverse(const AnalyticFunction<VectorType>&, const IntervalVector&, bool = true, bool = false) ->
186 CtcInverse<IntervalVector,IntervalVector>;
187
188 template<typename OtherDerived>
189 requires (OtherDerived::RowsAtCompileTime == -1 && OtherDerived::ColsAtCompileTime == 1)
190 CtcInverse(const AnalyticFunction<VectorType>&, const Eigen::MatrixBase<OtherDerived>&, bool = true, bool = false) ->
191 CtcInverse<IntervalVector,IntervalVector>;
192
193 template<typename C>
194 requires IsCtcBaseOrPtr<C,IntervalVector>
195 CtcInverse(const AnalyticFunction<VectorType>&, const C&, bool = true, bool = false) ->
196 CtcInverse<IntervalVector,IntervalVector>;
197
198 // MatrixType
199
200 CtcInverse(const AnalyticFunction<MatrixType>&, std::initializer_list<std::initializer_list<double>>, bool = true, bool = false) ->
201 CtcInverse<IntervalMatrix,IntervalVector>;
202
203 CtcInverse(const AnalyticFunction<MatrixType>&, std::initializer_list<std::initializer_list<std::initializer_list<double>>>, bool = true, bool = false) ->
204 CtcInverse<IntervalMatrix,IntervalVector>;
205
206 template<typename OtherDerived>
207 requires (OtherDerived::RowsAtCompileTime == -1 && OtherDerived::ColsAtCompileTime == -1)
208 CtcInverse(const AnalyticFunction<MatrixType>&, const Eigen::MatrixBase<OtherDerived>&, bool = true, bool = false) ->
209 CtcInverse<IntervalMatrix,IntervalVector>;
210
211 template<typename C>
212 requires IsCtcBaseOrPtr<C,IntervalMatrix>
213 CtcInverse(const AnalyticFunction<MatrixType>&, const C&, bool = true, bool = false) ->
214 CtcInverse<IntervalMatrix,IntervalVector>;
215}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:50
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