lyapunov_test.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-2014 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 
00033 #include <acado_optimal_control.hpp>
00034 #include <acado_gnuplot.hpp>
00035 
00036 
00037 #define NX 2
00038 
00039 
00040 USING_NAMESPACE_ACADO
00041 
00042 
00043 Expression model( const Expression &x, const Expression &u, const Expression &w ){
00044 
00045     IntermediateState rhs(3);
00046 
00047     rhs(0) =   x(1);
00048     rhs(1) =  -x(0) - (1.0-u*u)*x(1) + w;
00049     rhs(2) =   x(0)*x(0) + u*u;
00050 
00051 
00052     return rhs;
00053 }
00054 
00055 int main( ){
00056 
00057     // INTRODUCE THE VARIABLES:
00058     // -------------------------
00059     DifferentialState     x1(3);
00060     Control               u(1) ;
00061     Parameter             ww   ;
00062     DifferentialEquation  f    ;
00063 
00064     IntermediateState rhs1(3);
00065     rhs1 = model( x1, u, ww );
00066 
00067     f << dot(x1(0)) == rhs1(0);
00068     f << dot(x1(1)) == rhs1(1);
00069     f << dot(x1(2)) == rhs1(2);
00070 
00071     DifferentialState P(3,3);
00072     IntermediateState A = forwardDerivative( rhs1, x1 );
00073     IntermediateState B = forwardDerivative( rhs1, ww );
00074    
00075 
00076     //f << dot(P) == A*P+P*A.transpose()+B*B.transpose();
00077     f << dot(P) == Lyapunov(rhs1,A,B,P,x1,u,ww);
00078  
00079     //simulate( f );
00080 
00081      // DEFINE AN OPTIMAL CONTROL PROBLEM:
00082     // ----------------------------------
00083     OCP ocp( 0.0, 2.0*M_PI, 20 );
00084     ocp.minimizeMayerTerm( x1(2) );
00085     //ocp.minimizeLagrangeTerm( x1(0)*x1(0) + u*u );
00086 
00087 
00088     ocp.subjectTo( f );
00089 
00090     ocp.subjectTo( AT_START, x1(0) ==  0.0 );
00091     ocp.subjectTo( AT_START, x1(1) ==  1.0 );
00092     ocp.subjectTo( AT_START, x1(2) ==  0.0 );
00093     ocp.subjectTo( AT_START, ww == 0.0 );
00094     ocp.subjectTo( AT_START, P == 0.0 );
00095     //ocp.subjectTo( 0.0, P     , -P     , 0.0 );
00096     ocp.subjectTo( x1(0) <=  0.6 );
00097 
00098     OptimizationAlgorithm algorithm(ocp);
00099     algorithm.set                  ( INTEGRATOR_TYPE , INT_LYAPUNOV45 );
00100     algorithm.set          (DYNAMIC_SENSITIVITY, FORWARD_SENSITIVITY);
00101 
00102     // DEFINE A PLOT WINDOW:
00103     // ---------------------
00104     GnuplotWindow window;
00105     window.addSubplot( x1(0),"state x(0)" );
00106     window.addSubplot( x1(1),"state x(1)" );
00107     window.addSubplot( P(0),"Lyapunov matrix P(0)" );
00108     window.addSubplot( P(1),"Lyapunov matrix P(1)" );
00109     window.addSubplot( P(2),"Lyapunov matrix P(2)" );
00110     window.addSubplot( P(3),"Lyapunov matrix P(3)" );
00111  
00112     window.addSubplot( u ,"Control u" );
00113 
00114     algorithm << window;
00115 
00116     algorithm.initializeControls("controls1.txt");
00117     algorithm.initializeDifferentialStates("states1.txt");
00118     algorithm.set( KKT_TOLERANCE, 1e-10 );
00119     algorithm.solve();
00120 
00121     algorithm.getDifferentialStates("states2.txt"    );
00122     algorithm.getControls          ("controls2.txt"  );
00123  
00124   return 0;
00125 }
00126 
00127 
00128 


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 11:59:06