GCOP
1.0
|
00001 // This file is part of libgcop, a library for Geometric Control, Optimization, and Planning (GCOP) 00002 // 00003 // Copyright (C) 2004-2014 Marin Kobilarov <marin(at)jhu.edu> 00004 // 00005 // This Source Code Form is subject to the terms of the Mozilla 00006 // Public License v. 2.0. If a copy of the MPL was not distributed 00007 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00008 00009 #ifndef GCOP_CONTROLLERTPARAM_H 00010 #define GCOP_CONTROLLERTPARAM_H 00011 00012 #include "tparam.h" 00013 #include "controller.h" 00014 #include "constraint.h" 00015 #include <assert.h> 00016 #include "utils.h" 00017 00018 namespace gcop { 00019 00020 using namespace std; 00021 using namespace Eigen; 00022 00030 template <typename Tx, 00031 int nx = Dynamic, 00032 int nu = Dynamic, 00033 int np = Dynamic, 00034 int _ntp = Dynamic, 00035 typename Tc = VectorXd> class ControllerTparam : public Tparam<Tx, nx, nu, np, _ntp> { 00036 00037 typedef Matrix<double, nx, 1> Vectornd; 00038 typedef Matrix<double, nu, 1> Vectorcd; 00039 typedef Matrix<double, nx, nx> Matrixnd; 00040 typedef Matrix<double, nx, nu> Matrixncd; 00041 typedef Matrix<double, nu, nx> Matrixcnd; 00042 typedef Matrix<double, nu, nu> Matrixcd; 00043 00044 typedef Matrix<double, np, 1> Vectormd; 00045 00046 typedef Matrix<double, _ntp, 1> Vectorntpd; 00047 typedef Matrix<double, _ntp, _ntp> Matrixntpd; 00048 00049 public: 00050 ControllerTparam(System<Tx, nx, nu, np> &sys, 00051 Controller<Tx, Vectorcd, Vectorntpd, Tc> &ctrl, 00052 int ntp = _ntp, 00053 Constraint<Tx, nx, nu, np, 1> *con = 0); 00054 00055 bool To(Vectorntpd &s, 00056 const vector<double> &ts, 00057 const vector<Tx> &xs, 00058 const vector<Vectorcd> &us, 00059 const Vectormd *p = 0); 00060 00061 bool From(vector<double> &ts, 00062 vector<Tx> &xs, 00063 vector<Vectorcd> &us, 00064 const Vectorntpd &s, 00065 Vectormd *p = 0); 00066 00067 virtual bool SetContext(const Tc& c); 00068 00069 Controller<Tx, Vectorcd, Vectorntpd, Tc> &ctrl; 00070 Constraint<Tx, nx, nu, np, 1> *con; 00071 00072 bool stoch; 00073 Vectornd w; 00074 }; 00075 00076 template <typename Tx, int nx, int nu, int np, int _ntp, typename Tc> 00077 ControllerTparam<Tx, nx, nu, np, _ntp, Tc>::ControllerTparam(System<Tx, nx, nu, np> &sys, Controller<Tx, Vectorcd, Vectorntpd, Tc> &ctrl, int ntp, Constraint<Tx, nx, nu, np, 1> *con) : Tparam<Tx, nx, nu, np, _ntp>(sys, ntp), ctrl(ctrl), con(con), stoch(true) { 00078 assert(ntp > 0); 00079 if (nx == Dynamic) 00080 w.resize(sys.X.n); 00081 } 00082 00083 template <typename Tx, int nx, int nu, int np, int _ntp, typename Tc> 00084 bool ControllerTparam<Tx, nx, nu, np, _ntp, Tc>::To(Vectorntpd &s, 00085 const vector<double> &ts, 00086 const vector<Tx> &xs, 00087 const vector<Vectorcd> &us, 00088 const Vectormd *p) { 00089 cout << "[W] ControllerTparam::To: calling this is useless for this type of paremetrization!" << endl; 00090 return false; 00091 } 00092 00093 template <typename Tx, int nx, int nu, int np, int _ntp, typename Tc> 00094 bool ControllerTparam<Tx, nx, nu, np, _ntp, Tc>::SetContext(const Tc &c) { 00095 return ctrl.SetContext(c); 00096 } 00097 00098 template <typename Tx, int nx, int nu, int np, int _ntp, typename Tc> 00099 bool ControllerTparam<Tx, nx, nu, np, _ntp, Tc>::From(vector<double> &ts, 00100 vector<Tx> &xs, 00101 vector<Vectorcd> &us, 00102 const Vectorntpd &s, 00103 Vectormd *p) { 00104 ctrl.SetParams(s); 00105 Matrix<double, 1, 1> g; 00106 00107 // sample noise term, assume constant along path 00108 for (int i = 0; i < us.size(); ++i) { 00109 ctrl.Set(us[i], ts[i], xs[i]); 00110 00111 if (stoch) { 00112 for (int j=0; j < w.size(); ++j) 00113 w[j] = random_normal(); 00114 00115 this->sys.Step(xs[i+1], ts[i], xs[i], us[i], ts[i+1] - ts[i], w, p); 00116 } else { 00117 this->sys.Step(xs[i+1], ts[i], xs[i], us[i], ts[i+1] - ts[i], p); 00118 } 00119 00120 if (con) { // check for feasibility 00121 (*con)(g, ts[i+1], xs[i+1], us[i], p); 00122 if (g[0] > 0) { // if infeasible then set all remaining states to current state 00123 for (int j = i+2; j < xs.size(); ++j) { 00124 xs[j] = xs[i+1]; 00125 us[j-1].setZero(); 00126 } 00127 return false; 00128 } 00129 } 00130 } 00131 return true; 00132 } 00133 } 00134 00135 #endif