ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
zonotope.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2017-2020 German Aerospace Center (DLR).
3  * Eclipse ADORe, Automated Driving Open Research https://eclipse.org/adore
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License 2.0 which is available at
7  * http://www.eclipse.org/legal/epl-2.0.
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * Daniel Heß - initial API and implementation
13  ********************************************************************************/
14 
15 #pragma once
16 #include "adoremath.h"
17 #include <vector>
18 #include <algorithm>
19 
20 namespace adore
21 {
22  namespace mad
23  {
31  template <typename T, int D, int K>
32  class Zonotope
33  {
34  private:
35  typedef adoreMatrix<T,D,1> Tvec;
36  typedef adoreMatrix<T,D,K*2+1> Tvecset;
37  typedef std::pair<int,T> Tsortpair;
38  typedef std::vector<Tsortpair> Tsortset;
44  int n_;
45 
46  public:
50  Zonotope():n_(0){sortset_.allocate(K*2+1);}
51 
55  void reduce(int n)
56  {
57  assert(n>=D && n<=K);
58  if(n_<=n)return;
60  //compute metric
61  sortset_.clear();
62  for(int i=0;i<n_;i++)
63  {
64  T value = (T)1;
65  for(int k=0;k<D;k++)
66  {
67  value *= generators_(i,k);//box volume metric
68  }
69  sortset_.push_back(std::make_pair(i,value));
70  }
71  //sort generator index
72  std::sort(sortset_.begin(),sortset_.end(),
73  [](Tsortpair a,Tsortpair b){return a.second>b.second;});
74  //copy generators from sortbuffer to generators_ in order
75  for(int i=0;i<n_;i++)
76  {
77  dlib::set_colm(generators_,i) = dlib::rowm(sortbuf_,sortset_[i].first);
78  }
79  //aggregate generators, which have to be removed
80  int remstart = n-D-1;
81  int remend = n_-1;
82  for(int i=0;i<D;i++)aggvec_(i)=(T)0;
83  for(int j=remstart;j<=remend;j++)
84  {
85  for(int i=0;i<D;i++)
86  {
87  aggvec_(i)+=generators_(i,j);
88  }
89  }
90  //fill last D generators with DxD diagonal matrix diag(aggvec_)
91  for(int i=0;i<D;i++)
92  {
93  for(int j=0;j<D;j++)
94  {
95  if(i==j)
96  {
97  generators_(i,remstart+j) = aggvec_(i);
98  }
99  else
100  {
101  generators_(i,remstart+j) = (T)0;
102  }
103  }
104  }
105  n_=n;
106  }
107 
111  template<typename T,int D, int K>
112  friend Zonotope<T,D,K>& operator<<(Zonotope<T,D,K>& left,const adoreMatrix<T,d,0>& right)
113  {
114  static_assert(d<D,"right hand side dimensions must be smaller or equal to left hand side dimensions");
115  assert(right.nc()<=K);
116  dlib::set_subm(left.generators_,
117  dlib::range(0,right.nr()-1),
118  dlib::range(left.n_,left.n_+right.nc()-1))=right;
119  for(int i=right.nr();i<D;i++)
120  {
121  for(int j=left.n_;j<left.n_+right.nc();j++)
122  {
123  left.generators_(i,j)=(T)0;
124  }
125  }
126  left.n_+=right.nc();
127  left.girard(K);
128  return *this;
129  }
133  template<typename T,int D,int K>
135  {
136  static_assert(d<D,"right hand side dimensions must be smaller or equal to left hand side dimensions");
137  static_assert(k<=K,"operator<< requires right hand side to have smaller or equal maximum number of generators");
138  dlib::set_subm(left.generators_,
139  dlib::range(0,d-1),
140  dlib::range(left.n_,left.n_+right.n_-1))=
141  dlib::colm(right.generators_,dlib::range(0,right.n_-1));
142  for(int i=d;i<D;i++)
143  {
144  for(int j=left.n_;j<left.n_+right.n_;j++)
145  {
146  left.generators_(i,j)=(T)0;
147  }
148  }
149  left.n_+=right.n_;
150  left.girard(K);
151  return *this;
152  }
157  {
158  static_assert(k<=K,"operator+= requires right hand side to have smaller or equal maximum number of generators");
159  this->center_ = 0.5*(this->center_+right.center_);
160  dlib::set_colm(this->generators_,this->n_) = right.center_-this->center_;
161  dlib::set_colm(this->generators_,dlib::range(this->n_+1,this->n_+right.n_))=
162  dlib::colm(this->generators_,dlib::range(0,right.n_-1));
163  this->n_+=right.n_+1;
164  this->girard(K);
165  return *this;
166  }
170  template<typename T,int D,int K,int k>
172  {
173  left += right;
174  return left;
175  }
179  Zonotope<T,D,K>& operator+=(const adoreMatrix<T,D,1>& offset)
180  {
181  this->center_ += offset;
182  return *this;
183  }
187  template<typename T,int D,int K>
188  friend Zonotope<T,D,K> operator+(Zonotope<T,D,K> z,const adoreMatrix<T,D,1>& offset)
189  {
190  z += offset;
191  return z;
192  }
196  template<typename T,int D,int K>
197  friend Zonotope<T,D,K> operator+(const adoreMatrix<T,D,1>& offset,Zonotope<T,D,K> z)
198  {
199  z += offset;
200  return z;
201  }
205  Zonotope<T,D,K>& operator*=(const adoreMatrix<T,D,D>& A)
206  {
207  this->center_ = A*this->center_;
208  dlib::set_colm(this->generators_,dlib::range(0,this->n_-1))=
209  A*dlib::colm(this->generators_,dlib::range(0,this->n_-1));
210  return *this;
211  }
215  template<typename T,int D,int K>
216  friend Zonotope<T,D,K> operator*(const adoreMatrix<T,D,D>& A,Zonotope<T,D,K> z)
217  {
218  z *= A;
219  return z;
220  }
221 
225  Zonotope<T,D,K>& operator*=(const adoreMatrix<T,0,0>& A)
226  {
227  assert(A.nc()==D && A.nr()==D)
228  this->center_ = A*this->center_;
229  dlib::set_colm(this->generators_,dlib::range(0,this->n_-1))=
230  A*dlib::colm(this->generators_,dlib::range(0,this->n_-1));
231  return *this;
232  }
236  template<typename T,int D,int K>
237  friend Zonotope<T,D,K> operator*(const adoreMatrix<T,0,0>& A,Zonotope<T,D,K> z)
238  {
239  z *= A;
240  return z;
241  }
245  Zonotope<T,D,K>& stack_below(const adoreMatrix<T,0,0>& A)
246  {
247  assert(A.nc()<D);
248  int rstart = A.nc();
249  int rend = D-1;
250  dlib::set_rowm(generators_,dlib::range(rstart,rend)) =
251  A * dlib::rowm(generators_,dlib::range(0,rstart-1));
252  return *this;
253  }
257  const adoreMatrix<T,D,0>& getGenerators()const
258  {
259  return dlib::colm(generators_,dlib::range(0,n_-1));
260  }
271  template<T,int nx,int nu,int nd,int K,int Km,int Kd>
272  friend void linear_step(
273  Zonotope<T,nx+nu,K>& Zxu,
274  const adoreMatrix<T,nx+nu+nd,nx+nu+nd>& AdBdCd,
275  const adoreMatrix<T,nx,nu>& Kfb,
276  const Zonotope<T,nx,Km>& Zm,
277  const Zonotope<T,nd,Kd>& Zd)
278  {
279  //AdBd
280  const adoreMatrix<T,nx+nu,nx+nu>& AdBd = dlib::subm(AdBdCd,
281  dlib::range(0,nx+nu-1),dlib::range(0,nx+nu-1));
282  //Bd
283  const adoreMatrix<T,nx,nu>& Bd = dlib::subm(AdBdCd,
284  dlib::range(0,nx-1),dlib::range(nx,nx+nu-1));
285  //Cd
286  const adoreMatrix<T,nx,nx>& Cd = dlib::subm(AdBdCd,
287  dlib::range(0,nx-1),dlib::range(nx+nu,nx+nu+nd-1));
288  Zxu.stack_below(Kfb);//define feedback
289  Zxu*=AdBd;//linear state update for each generator with discrete time matrix
290  Zxu<<(Bd*Kfb*Zm.getGenerators());//minkowski add the measurement error, mapped through feedback matrix and Bd
291  Zxu<<(Cd*Zd.getGenerators());//minkowski add the disturbance error, mapped through Cd
292  }
293 
294 
295  };
296  }
297 }
Definition: zonotope.h:33
const adoreMatrix< T, D, 0 > & getGenerators() const
Definition: zonotope.h:257
int n_
Definition: zonotope.h:44
Tvecset sortbuf_
Definition: zonotope.h:42
adoreMatrix< T, D, 1 > Tvec
Definition: zonotope.h:35
Tvec center_
Definition: zonotope.h:39
friend Zonotope< T, D, K > operator+(Zonotope< T, D, K > z, const adoreMatrix< T, D, 1 > &offset)
Definition: zonotope.h:188
Tvecset generators_
Definition: zonotope.h:40
friend Zonotope< T, D, K > operator*(const adoreMatrix< T, D, D > &A, Zonotope< T, D, K > z)
Definition: zonotope.h:216
friend Zonotope< T, D, K > & operator<<(Zonotope< T, D, K > &left, const adoreMatrix< T, d, 0 > &right)
Definition: zonotope.h:112
void reduce(int n)
Definition: zonotope.h:55
Zonotope()
Definition: zonotope.h:50
Zonotope< T, D, K > & operator+=(const adoreMatrix< T, D, 1 > &offset)
Definition: zonotope.h:179
Zonotope< T, D, K > & operator+=(const Zonotope< T, D, k > &right)
Definition: zonotope.h:156
Tsortset sortset_
Definition: zonotope.h:41
Zonotope< T, D, K > & operator*=(const adoreMatrix< T, D, D > &A)
Definition: zonotope.h:205
Zonotope< T, D, K > & stack_below(const adoreMatrix< T, 0, 0 > &A)
Definition: zonotope.h:245
std::vector< Tsortpair > Tsortset
Definition: zonotope.h:38
friend Zonotope< T, D, K > operator*(const adoreMatrix< T, 0, 0 > &A, Zonotope< T, D, K > z)
Definition: zonotope.h:237
friend Zonotope< T, D, K > operator+(Zonotope< T, D, K > left, const Zonotope< T, D, k > &right)
Definition: zonotope.h:171
friend Zonotope< T, D, K > operator+(const adoreMatrix< T, D, 1 > &offset, Zonotope< T, D, K > z)
Definition: zonotope.h:197
friend void linear_step(Zonotope< T, nx+nu, K > &Zxu, const adoreMatrix< T, nx+nu+nd, nx+nu+nd > &AdBdCd, const adoreMatrix< T, nx, nu > &Kfb, const Zonotope< T, nx, Km > &Zm, const Zonotope< T, nd, Kd > &Zd)
Definition: zonotope.h:272
friend Zonotope< T, D, K > & operator<<(Zonotope< T, D, K > &left, const Zonotope< T, d, k > &right)
Definition: zonotope.h:134
adoreMatrix< T, D, K *2+1 > Tvecset
Definition: zonotope.h:36
Zonotope< T, D, K > & operator*=(const adoreMatrix< T, 0, 0 > &A)
Definition: zonotope.h:225
Tvec aggvec_
Definition: zonotope.h:43
std::pair< int, T > Tsortpair
Definition: zonotope.h:37
@ right
Definition: indicator_hint.h:36
@ left
Definition: indicator_hint.h:35
z
Definition: adore_set_goal.py:32
Definition: areaofeffectconverter.h:20