symmetricAD.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2013 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
27 
34 #include <time.h>
35 
41 
43 
44 // ---------------------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------------------
46 // SIMPLE AUXILIARY FUNCTION:
48 
49  uint dim = arg.getNumCols();
50  uint dim2 = arg.getNumRows();
51 
52  IntermediateState inter_res = zeros<double>(dim2,dim);
53  for( uint i = 0; i < dim; i++ ) {
54  for( uint k1 = 0; k1 < dim2; k1++ ) {
55  for( uint k2 = 0; k2 <= k1; k2++ ) {
56  inter_res(k1,i) += expr(k1,k2)*arg(k2,i);
57  }
58  for( uint k2 = k1+1; k2 < dim2; k2++ ) {
59  inter_res(k1,i) += expr(k2,k1)*arg(k2,i);
60  }
61  }
62  }
63 
64  Expression new_expr( "", dim, dim );
65  for( uint i = 0; i < dim; i++ ) {
66  for( uint j = 0; j <= i; j++ ) {
67  Expression new_tmp = 0;
68  for( uint k1 = 0; k1 < dim2; k1++ ) {
69  new_tmp = new_tmp+arg(k1,i)*inter_res(k1,j);
70  }
71  new_expr(i,j) = new_tmp;
72  new_expr(j,i) = new_tmp;
73  }
74  }
75  return new_expr;
76 }
77 
79  ASSERT( expr.getNumRows() == expr.getNumCols() );
80 
81  Expression new_expr;
82  for( uint i = 0; i < expr.getNumRows(); i++ ) {
83  for( uint j = 0; j <= i; j++ ) {
84  new_expr << expr(i,j);
85  }
86  }
87  return new_expr;
88 }
89 // ---------------------------------------------------------------------------------------
90 // ---------------------------------------------------------------------------------------
91 
92 
93 /* >>> start tutorial code >>> */
94 int main( ){
95 
96  DifferentialState xT; // the trolley position
97  DifferentialState vT; // the trolley velocity
98  IntermediateState aT; // the trolley acceleration
99  DifferentialState xL; // the cable length
100  DifferentialState vL; // the cable velocity
101  IntermediateState aL; // the cable acceleration
102  DifferentialState phi; // the excitation angle
103  DifferentialState omega; // the angular velocity
104 
105  DifferentialState uT; // trolley velocity control
106  DifferentialState uL; // cable velocity control
107 
108  Control duT;
109  Control duL;
110 
111  //
112  // DEFINE THE PARAMETERS:
113  //
114  const double tau1 = 0.012790605943772;
115  const double a1 = 0.047418203070092;
116  const double tau2 = 0.024695192379264;
117  const double a2 = 0.034087337273386;
118  const double g = 9.81;
119  const double c = 0.2;
120  const double m = 1318.0;
121 
122  //
123  // DEFINE THE MODEL EQUATIONS:
124  //
125  DifferentialEquation f, f2, test_expr;
126  ExportAcadoFunction fun, fun2;
127 
128  aT = -1.0 / tau1 * vT + a1 / tau1 * uT;
129  aL = -1.0 / tau2 * vL + a2 / tau2 * uL;
130 
131  Expression states;
132  states << xT;
133  states << vT;
134  states << xL;
135  states << vL;
136  states << phi;
137  states << omega;
138  states << uT;
139  states << uL;
140 
141  Expression controls;
142  controls << duT;
143  controls << duL;
144 
145  Expression arg;
146  arg << states;
147  arg << controls;
148 
149  int NX = states.getDim();
150  int NU = controls.getDim();
151 
152  IntermediateState expr(2);
153 
154  expr(0) = - 1.0/xL*(-g*sin(phi)-aT*cos(phi)-2*vL*omega-c*omega/(m*xL)) + log(duT/duL)*pow(xL,2);
155  expr(1) = - 1.0/xL*(-g*tan(phi)-aT*acos(phi)-2*atan(vL)*omega-c*asin(omega)/exp(xL)) + duT/pow(omega,3);
156  //~ expr(0) = - 1.0/xL*(-g*sin(phi));
157  //~ expr(1) = duT/pow(omega,3);
158 
159  DifferentialState lambda("", expr.getDim(),1);
160  DifferentialState Sx("", states.getDim(),states.getDim());
161  DifferentialState Su("", states.getDim(),controls.getDim());
162  Expression S = Sx;
163  S.appendCols(Su);
164 
165  // SYMMETRIC DERIVATIVES
166  Expression S_tmp = S;
167  S_tmp.appendRows(zeros<double>(NU,NX).appendCols(eye<double>(NU)));
168 
169  IntermediateState dfS,dl;
170 
171  Expression f_tmp = symmetricDerivative( expr, arg, S_tmp, lambda, &dfS, &dl );
172  f << returnLowerTriangular( f_tmp );
173 
174  fun.init(f, "symmetricDerivative", NX*(1+NX+NU)+expr.getDim(), 0, 2, 0, 0, 0);
175 
176  // ALTERNATIVE DERIVATIVES
177  IntermediateState tmp = backwardDerivative( expr, states, lambda );
178  IntermediateState tmp2 = forwardDerivative( tmp, states );
179  Expression tmp3 = backwardDerivative( expr, controls, lambda );
180  Expression tmp4 = multipleForwardDerivative( tmp3, states, Su );
181  Expression tmp5 = tmp4 + tmp4.transpose() + forwardDerivative( tmp3, controls );
182 
183  Expression f2_tmp1;
184  f2_tmp1 = symmetricDoubleProduct( tmp2, Sx ); f2_tmp1.appendCols( zeros<double>(NX,NU) );
185 
186  Expression f2_tmp2;
187  f2_tmp2 = Su.transpose()*tmp2*Sx + multipleForwardDerivative( tmp3, states, Sx );
188  f2_tmp2.appendCols( symmetricDoubleProduct( tmp2, Su ) + tmp5 );
189 
190  f2_tmp1.appendRows( f2_tmp2 );
191  f2 << returnLowerTriangular( f2_tmp1 );
192 
193 
194  fun2.init(f2, "alternativeSymmetric", NX*(1+NX+NU)+expr.getDim(), 0, 2, 0, 0, 0);
195 
196  Function f1;
197 
198  f1 << dfS;
199  f1 << dl;
200 
201  std::ofstream stream2( "ADtest/ADsymbolic_output2.c" );
202  stream2 << f1;
203  stream2.close();
204 
205  std::ofstream stream( "ADtest/ADsymbolic_output.c" );
206  fun.exportCode( stream, "double" );
207  fun2.exportCode( stream, "double" );
208 
209  test_expr << expr;
210  stream << test_expr;
211 
212  stream.close();
213 
214  return 0;
215 }
216 /* <<< end tutorial code <<< */
217 
USING_NAMESPACE_ACADO IntermediateState sin(const Expression &arg)
Expression symmetricDerivative(const Expression &arg1, const Expression &arg2, const Expression &forward_seed, const Expression &backward_seed, Expression *forward_result, Expression *backward_result)
Allows to setup and evaluate a general function based on SymbolicExpressions.
Definition: function_.hpp:59
int main()
Definition: symmetricAD.cpp:94
IntermediateState atan(const Expression &arg)
Expression backwardDerivative(const Expression &arg1, const Expression &arg2)
Allows to export code of an ACADO function.
#define USING_NAMESPACE_ACADO
Expression forwardDerivative(const Expression &arg1, const Expression &arg2)
uint getNumCols() const
uint getNumRows() const
IntermediateState asin(const Expression &arg)
IntermediateState pow(const Expression &arg1, const Expression &arg2)
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
IntermediateState tan(const Expression &arg)
Expression returnLowerTriangular(const Expression &expr)
Definition: symmetricAD.cpp:78
IntermediateState cos(const Expression &arg)
Base class for all variables within the symbolic expressions family.
Definition: expression.hpp:56
IntermediateState acos(const Expression &arg)
Expression multipleForwardDerivative(const Expression &arg1, const Expression &arg2, const Expression &seed)
Expression & appendRows(const Expression &arg)
Definition: expression.cpp:141
Expression transpose() const
Definition: expression.cpp:811
#define ASSERT(x)
#define NX
#define NU
IntermediateState exp(const Expression &arg)
USING_NAMESPACE_ACADO Expression symmetricDoubleProduct(const Expression &expr, const Expression &arg)
Definition: symmetricAD.cpp:47
virtual returnValue exportCode(std::ostream &stream, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16) const
Expression & appendCols(const Expression &arg)
Definition: expression.cpp:162
returnValue init(const Function &_f, const std::string &_name="acadoFcn", const uint _numX=0, const uint _numXA=0, const uint _numU=0, const uint _numP=0, const uint _numDX=0, const uint _numOD=0)
Allows to setup and evaluate differential equations (ODEs and DAEs) based on SymbolicExpressions.
uint getDim() const
IntermediateState log(const Expression &arg)


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:35:12