symmetricAD.cpp
Go to the documentation of this file.
00001 /*
00002  *    This file is part of ACADO Toolkit.
00003  *
00004  *    ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
00005  *    Copyright (C) 2008-2013 by Boris Houska, Hans Joachim Ferreau,
00006  *    Milan Vukov, Rien Quirynen, KU Leuven.
00007  *    Developed within the Optimization in Engineering Center (OPTEC)
00008  *    under supervision of Moritz Diehl. All rights reserved.
00009  *
00010  *    ACADO Toolkit is free software; you can redistribute it and/or
00011  *    modify it under the terms of the GNU Lesser General Public
00012  *    License as published by the Free Software Foundation; either
00013  *    version 3 of the License, or (at your option) any later version.
00014  *
00015  *    ACADO Toolkit is distributed in the hope that it will be useful,
00016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *    Lesser General Public License for more details.
00019  *
00020  *    You should have received a copy of the GNU Lesser General Public
00021  *    License along with ACADO Toolkit; if not, write to the Free Software
00022  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023  *
00024  */
00025 
00026 
00027 
00034 #include <time.h>
00035 
00036 #include <acado/utils/acado_utils.hpp>
00037 #include <acado/user_interaction/user_interaction.hpp>
00038 #include <acado/symbolic_expression/symbolic_expression.hpp>
00039 #include <acado/function/function.hpp>
00040 #include <acado/code_generation/export_algorithm_factory.hpp>
00041 
00042 USING_NAMESPACE_ACADO
00043 
00044 // ---------------------------------------------------------------------------------------
00045 // ---------------------------------------------------------------------------------------
00046 // SIMPLE AUXILIARY FUNCTION:
00047 Expression symmetricDoubleProduct( const Expression& expr, const Expression& arg ) {
00048 
00049         uint dim = arg.getNumCols();
00050         uint dim2 = arg.getNumRows();
00051 
00052         IntermediateState inter_res = zeros<double>(dim2,dim);
00053         for( uint i = 0; i < dim; i++ ) {
00054                 for( uint k1 = 0; k1 < dim2; k1++ ) {
00055                         for( uint k2 = 0; k2 <= k1; k2++ ) {
00056                                 inter_res(k1,i) += expr(k1,k2)*arg(k2,i);
00057                         }
00058                         for( uint k2 = k1+1; k2 < dim2; k2++ ) {
00059                                 inter_res(k1,i) += expr(k2,k1)*arg(k2,i);
00060                         }
00061                 }
00062         }
00063 
00064         Expression new_expr( "", dim, dim );
00065         for( uint i = 0; i < dim; i++ ) {
00066                 for( uint j = 0; j <= i; j++ ) {
00067                         Expression new_tmp = 0;
00068                         for( uint k1 = 0; k1 < dim2; k1++ ) {
00069                                 new_tmp = new_tmp+arg(k1,i)*inter_res(k1,j);
00070                         }
00071                         new_expr(i,j) = new_tmp;
00072                         new_expr(j,i) = new_tmp;
00073                 }
00074         }
00075         return new_expr;
00076 }
00077 
00078 Expression returnLowerTriangular( const Expression& expr ) {
00079         ASSERT( expr.getNumRows() == expr.getNumCols() );
00080 
00081         Expression new_expr;
00082         for( uint i = 0; i < expr.getNumRows(); i++ ) {
00083                 for( uint j = 0; j <= i; j++ ) {
00084                         new_expr << expr(i,j);
00085                 }
00086         }
00087         return new_expr;
00088 }
00089 // ---------------------------------------------------------------------------------------
00090 // ---------------------------------------------------------------------------------------
00091 
00092 
00093 /* >>> start tutorial code >>> */
00094 int main( ){
00095     
00096     DifferentialState   xT;     // the trolley position
00097     DifferentialState   vT;     // the trolley velocity
00098     IntermediateState   aT;     // the trolley acceleration
00099     DifferentialState   xL;     // the cable length
00100     DifferentialState   vL;     // the cable velocity
00101     IntermediateState   aL;     // the cable acceleration
00102     DifferentialState   phi;    // the excitation angle
00103     DifferentialState   omega;  // the angular velocity
00104         
00105     DifferentialState   uT;     // trolley velocity control
00106     DifferentialState   uL;     // cable velocity control
00107 
00108     Control             duT;
00109     Control             duL;
00110 
00111     //
00112     // DEFINE THE PARAMETERS:
00113     //
00114     const double      tau1 = 0.012790605943772;
00115     const double      a1   = 0.047418203070092;
00116     const double      tau2 = 0.024695192379264;
00117     const double      a2   = 0.034087337273386;
00118     const double      g = 9.81;                 
00119     const double      c = 0.2;                  
00120     const double      m = 1318.0;               
00121 
00122     //
00123     // DEFINE THE MODEL EQUATIONS:
00124     //
00125     DifferentialEquation   f, f2, test_expr;
00126     ExportAcadoFunction    fun, fun2;
00127     
00128     aT = -1.0 / tau1 * vT + a1 / tau1 * uT;
00129     aL = -1.0 / tau2 * vL + a2 / tau2 * uL;
00130 
00131     Expression states;
00132     states << xT;
00133     states << vT;
00134     states << xL;
00135     states << vL;
00136     states << phi;
00137     states << omega;
00138     states << uT;
00139     states << uL;
00140     
00141     Expression controls;
00142     controls << duT;
00143     controls << duL;
00144     
00145     Expression arg;
00146     arg << states;
00147     arg << controls;
00148     
00149     int NX = states.getDim();
00150     int NU = controls.getDim();
00151     
00152     IntermediateState expr(2);
00153     
00154     expr(0) = - 1.0/xL*(-g*sin(phi)-aT*cos(phi)-2*vL*omega-c*omega/(m*xL)) + log(duT/duL)*pow(xL,2);
00155     expr(1) = - 1.0/xL*(-g*tan(phi)-aT*acos(phi)-2*atan(vL)*omega-c*asin(omega)/exp(xL)) + duT/pow(omega,3);
00156     //~ expr(0) = - 1.0/xL*(-g*sin(phi));
00157     //~ expr(1) = duT/pow(omega,3);
00158                                                 
00159         DifferentialState lambda("", expr.getDim(),1);
00160         DifferentialState Sx("", states.getDim(),states.getDim());
00161         DifferentialState Su("", states.getDim(),controls.getDim());
00162         Expression S = Sx;
00163         S.appendCols(Su);
00164         
00165         // SYMMETRIC DERIVATIVES
00166         Expression S_tmp = S;
00167         S_tmp.appendRows(zeros<double>(NU,NX).appendCols(eye<double>(NU)));
00168     
00169         IntermediateState dfS,dl;
00170         
00171     Expression f_tmp = symmetricDerivative( expr, arg, S_tmp, lambda, &dfS, &dl );
00172     f << returnLowerTriangular( f_tmp );
00173     
00174     fun.init(f, "symmetricDerivative", NX*(1+NX+NU)+expr.getDim(), 0, 2, 0, 0, 0);
00175     
00176         // ALTERNATIVE DERIVATIVES
00177     IntermediateState tmp = backwardDerivative( expr, states, lambda );
00178     IntermediateState tmp2 = forwardDerivative( tmp, states );
00179     Expression tmp3 = backwardDerivative( expr, controls, lambda );
00180         Expression tmp4 = multipleForwardDerivative( tmp3, states, Su );
00181         Expression tmp5 = tmp4 + tmp4.transpose() + forwardDerivative( tmp3, controls );
00182                 
00183         Expression f2_tmp1;
00184         f2_tmp1 = symmetricDoubleProduct( tmp2, Sx );  f2_tmp1.appendCols( zeros<double>(NX,NU) );
00185     
00186     Expression f2_tmp2;
00187     f2_tmp2 = Su.transpose()*tmp2*Sx + multipleForwardDerivative( tmp3, states, Sx );
00188     f2_tmp2.appendCols( symmetricDoubleProduct( tmp2, Su ) + tmp5 );
00189     
00190     f2_tmp1.appendRows( f2_tmp2 );
00191     f2 << returnLowerTriangular( f2_tmp1 );
00192     
00193     
00194     fun2.init(f2, "alternativeSymmetric", NX*(1+NX+NU)+expr.getDim(), 0, 2, 0, 0, 0);
00195     
00196     Function f1;
00197     
00198     f1 << dfS;
00199     f1 << dl;
00200     
00201     std::ofstream stream2( "ADtest/ADsymbolic_output2.c" );
00202     stream2 << f1;
00203     stream2.close();
00204     
00205     std::ofstream stream( "ADtest/ADsymbolic_output.c" );
00206     fun.exportCode( stream, "double" );
00207     fun2.exportCode( stream, "double" );
00208     
00209     test_expr << expr;
00210     stream << test_expr;
00211     
00212     stream.close();
00213 
00214     return 0;
00215 }
00216 /* <<< end tutorial code <<< */
00217 


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 12:01:10