GCOP
1.0
|
00001 #ifndef GCOP_TRAJECTORY_H 00002 #define GCOP_TRAJECTORY_H 00003 00004 #include <iostream> 00005 #include <cstring> 00006 #include <map> 00007 #include "system.h" 00008 00009 namespace gcop { 00010 00019 template <typename T, 00020 int nx = Dynamic, 00021 int nu = Dynamic, 00022 int np = Dynamic, 00023 int _ns = Dynamic> 00024 class Trajectory { 00025 public: 00026 00027 typedef Matrix<double, nx, 1> Vectornd; 00028 typedef Matrix<double, nu, 1> Vectorcd; 00029 typedef Matrix<double, nx, nx> Matrixnd; 00030 typedef Matrix<double, nx, nu> Matrixncd; 00031 typedef Matrix<double, nu, nx> Matrixcnd; 00032 typedef Matrix<double, nu, nu> Matrixcd; 00033 00034 typedef Matrix<double, np, 1> Vectormd; 00035 00036 typedef Matrix<double, _ns, 1> Vectorsd; 00037 00038 00049 Trajectory(const System<T, nx, nu, np>& sys, int ns = 0, int N = 0); 00050 00055 // Trajectory(const Trajectory<T, nx, nu, np, _ns> &traj); 00056 00061 // Trajectory& operator=(const Trajectory<T, nx, nu, np, _ns> &traj); 00062 00063 00069 // Trajectory(const System<T, nx, nu, np>& sys, std::istream& istr); 00070 00071 00072 virtual ~Trajectory(); 00073 00078 // virtual Trajectory<T, nx, nu, np, _ns>* Clone() const; 00079 00084 virtual bool Update(); 00085 00086 00093 // void Resize(int sn); 00094 00098 // void Reverse(); 00099 00100 00111 // void Init(int sn, 00112 // const State *si = 0, 00113 // const State *sf = 0); 00114 00115 00141 // void Append(const State &s); 00142 00143 00144 00149 // void Clear(); 00150 00157 // void SetTime(double t0, double h); 00158 00159 00172 // void Get(State &s) const; 00173 virtual bool Get(T &x, Vectorcd &u, double t) const; 00174 00175 00184 int Get(double t) const; 00185 00186 00195 // bool Get(Trajectory<T, nx, nu, np, _ns> &traj, double ti, double tf) const; 00196 00197 00203 // void Refine(double a = .5); 00204 00209 // void Resample(int sn); 00210 00211 00221 // void Modify(int mn, const int *mi, const double *mu); 00222 00223 00229 // bool IsValidTime(double t) const; 00230 00231 00232 const System<T, nx, nu, np> &sys; 00233 00234 int ns; 00235 00236 Vectorsd s; 00237 00238 vector<double> ts; 00239 vector<T> xs; 00240 vector<Vectorcd> us; 00241 Vectormd p; 00242 00243 // bool ext; ///< extended formulation (used for optimization purposes -- this flag is internally set when one requires variations in the exteneded stapce: space + time) 00244 00245 // double hmin; ///< minimum timestep size (used internally for checking trajectory consistency) it is 1e-10 by default 00246 00252 // virtual void Log(const char *logName, int di = 1); 00253 00259 // virtual void Log(std::ofstream& os, int di = 1); 00260 00267 // static void Log(std::ofstream& os, int fw, double a); 00268 00269 00270 00271 private: 00272 // friend std::ostream& operator<<(std::ostream &os, const Trajectory<T, nx, nu, np, _ns> &traj); 00273 // friend std::istream& operator>>(std::istream &is, Trajectory &traj); 00274 }; 00275 00303 template <typename T, int nx, int nu, int np, int _ns> 00304 Trajectory<T, nx, nu, np, _ntp>::Trajectory(System<T, nx, nu, np> &sys, int ns) : 00305 sys(sys), ns(_ns != Dynamic ? _ns : ns), 00306 ts(this->ns + 1), xs(this->ns + 1), us(this->ns), p(sys.P.n) { 00307 if (_ns == Dynamic) 00308 s.resize(this->ns); 00309 } 00310 00311 00312 template <typename T, int nx, int nu, int np, int _ns> 00313 bool Trajectory<T, nx, nu, np, _ns>::Get(T &s, 00314 const T &sa, 00315 const T &sb, 00316 double a) const 00317 { 00318 Vectornd dx; 00319 sys.X.Lift(dx, sa, sb); 00320 sys.X.Retract(s, sa, a*dx); 00321 } 00322 00323 00324 00325 template <typename T, int nx, int nu, int np, int _ns> 00326 void Trajectory<T, nx, nu, np, _ns>::Update() { 00327 assert(ns == us.size()*sys.U.n); 00328 for (int i = 0; i < us.size(); ++i) { 00329 memcpy(s.data() + i*sys.U.n, us[i].data(), sys.U.n*sizeof(double)); 00330 } 00331 // assume parameter p does not play a role 00332 } 00333 00334 template <typename T, int nx, int nu, int np, int _ns> 00335 bool Trajectory<T, nx, nu, np, _ns>::Get(T &x, Vectorcd &u, double t) const { 00336 int N = us.size(); 00337 if (!N) 00338 return; 00339 00340 // find the time slot 00341 if ( (t < ts[0] - hmin) || (t > ts[N] + hmin)) { 00342 cerr << "[W] dcop::Trajectory::Get:\t time t= " << t << " out of bounds [" << ts[0] << "," << ts[N] << "]!" << endl; 00343 return; 00344 } 00345 00346 00347 // assume constant timestep 00348 double h = ts[1] - ts[0]; 00349 00350 if (h < hmin) { 00351 cerr << "[W] dgc::Trajectory::GetState:\t time step is very small h=" << h << "!" << endl; 00352 } 00353 00354 00355 if (fabs(t - ts[0]) < hmin) { 00356 x = xs[0]; 00357 u = us[0]; 00358 return; 00359 } 00360 00361 if (fabs(t - ts[N]) < hmin) { 00362 x = xs[N]; 00363 u = us[N-1]; 00364 return; 00365 } 00366 00367 00368 double i = (t - ts[0])/h; 00369 int i0 = (int)i; 00370 00371 // cout << "t=" << s.t << " ti=" << states[0]->t << " tf=" << states[sn]->t << " h=" << h << endl; 00372 // cout << " sn=" << sn << " i=" << i << " i0=" << i0 << endl; 00373 00374 assert(i0 >= 0 && i0 < N); 00375 00376 // cout << "i0=" << i0 << " i=" << i << " sn=" << sn << endl; 00377 00378 double a = i - i0; 00379 // u = (1 - a)*us[i0] + a*us[i0 + 1]; 00380 u = us[i0]; 00381 Get(x, xs[i0], xs[i0+1], a); 00382 } 00383 00384 00385 template <typename T, int nx, int nu, int np, int _ns> 00386 int Trajectory<T, nx, nu, np, _ns>::Get(double t) const { 00387 int N = us.size(); 00388 // find the time slot 00389 if ( (t < ts[0] - hmin) || (t > ts[N] + hmin)) { 00390 cerr << "[W] gcop::Trajectory::Get:\t time t= " << t << " out of bounds [" << ts[0] << "," << ts[N] << "]!" << endl; 00391 return -1; 00392 } 00393 00394 // assume constant timestep 00395 double h = ts[1] - ts[0]; 00396 00397 if (h < hmin) { 00398 cerr << "[W] gcop::Trajectory::GetState:\t time step is very small h=" << h << "!" << endl; 00399 } 00400 00401 if (fabs(t - ts[0]) < hmin) 00402 return 0; 00403 00404 if (fabs(t - ts[N]) < hmin) 00405 return N-1; 00406 00407 return (int)((t - ts[0])/h); 00408 } 00409 } 00410 00411 #endif