lyapunov_test.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-2014 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 
34 #include <acado_gnuplot.hpp>
35 
36 
37 #define NX 2
38 
39 
41 
42 
43 Expression model( const Expression &x, const Expression &u, const Expression &w ){
44 
46 
47  rhs(0) = x(1);
48  rhs(1) = -x(0) - (1.0-u*u)*x(1) + w;
49  rhs(2) = x(0)*x(0) + u*u;
50 
51 
52  return rhs;
53 }
54 
55 int main( ){
56 
57  // INTRODUCE THE VARIABLES:
58  // -------------------------
59  DifferentialState x1(3);
60  Control u(1) ;
61  Parameter ww ;
63 
64  IntermediateState rhs1(3);
65  rhs1 = model( x1, u, ww );
66 
67  f << dot(x1(0)) == rhs1(0);
68  f << dot(x1(1)) == rhs1(1);
69  f << dot(x1(2)) == rhs1(2);
70 
71  DifferentialState P(3,3);
73  IntermediateState B = forwardDerivative( rhs1, ww );
74 
75 
76  //f << dot(P) == A*P+P*A.transpose()+B*B.transpose();
77  f << dot(P) == Lyapunov(rhs1,A,B,P,x1,u,ww);
78 
79  //simulate( f );
80 
81  // DEFINE AN OPTIMAL CONTROL PROBLEM:
82  // ----------------------------------
83  OCP ocp( 0.0, 2.0*M_PI, 20 );
84  ocp.minimizeMayerTerm( x1(2) );
85  //ocp.minimizeLagrangeTerm( x1(0)*x1(0) + u*u );
86 
87 
88  ocp.subjectTo( f );
89 
90  ocp.subjectTo( AT_START, x1(0) == 0.0 );
91  ocp.subjectTo( AT_START, x1(1) == 1.0 );
92  ocp.subjectTo( AT_START, x1(2) == 0.0 );
93  ocp.subjectTo( AT_START, ww == 0.0 );
94  ocp.subjectTo( AT_START, P == 0.0 );
95  //ocp.subjectTo( 0.0, P , -P , 0.0 );
96  ocp.subjectTo( x1(0) <= 0.6 );
97 
98  OptimizationAlgorithm algorithm(ocp);
99  algorithm.set ( INTEGRATOR_TYPE , INT_LYAPUNOV45 );
101 
102  // DEFINE A PLOT WINDOW:
103  // ---------------------
104  GnuplotWindow window;
105  window.addSubplot( x1(0),"state x(0)" );
106  window.addSubplot( x1(1),"state x(1)" );
107  window.addSubplot( P(0),"Lyapunov matrix P(0)" );
108  window.addSubplot( P(1),"Lyapunov matrix P(1)" );
109  window.addSubplot( P(2),"Lyapunov matrix P(2)" );
110  window.addSubplot( P(3),"Lyapunov matrix P(3)" );
111 
112  window.addSubplot( u ,"Control u" );
113 
114  algorithm << window;
115 
116  algorithm.initializeControls("controls1.txt");
117  algorithm.initializeDifferentialStates("states1.txt");
118  algorithm.set( KKT_TOLERANCE, 1e-10 );
119  algorithm.solve();
120 
121  algorithm.getDifferentialStates("states2.txt" );
122  algorithm.getControls ("controls2.txt" );
123 
124  return 0;
125 }
126 
127 
128 
returnValue initializeControls(const char *fileName)
int main()
User-interface to formulate and solve optimal control problems and static NLPs.
#define USING_NAMESPACE_ACADO
Expression forwardDerivative(const Expression &arg1, const Expression &arg2)
returnValue subjectTo(const DifferentialEquation &differentialEquation_)
Definition: ocp.cpp:153
returnValue minimizeMayerTerm(const Expression &arg)
Definition: ocp.cpp:238
returnValue addSubplot(PlotWindowSubplot &_subplot)
returnValue set(OptionsName name, int value)
Definition: options.cpp:126
Base class for all variables within the symbolic expressions family.
Definition: expression.hpp:56
#define M_PI
Definition: acado_utils.hpp:54
returnValue initializeDifferentialStates(const char *fileName, BooleanType autoinit=BT_FALSE)
Data class for defining optimal control problems.
Definition: ocp.hpp:89
void rhs(const real_t *x, real_t *f)
Expression dot(const Expression &arg)
USING_NAMESPACE_ACADO Expression model(const Expression &x, const Expression &u, const Expression &w)
returnValue getControls(VariablesGrid &p_) const
Implements a parameter.
Definition: lyapunov.hpp:54
Provides an interface to Gnuplot for plotting algorithmic outputs.
virtual returnValue solve()
returnValue getDifferentialStates(VariablesGrid &xd_) const
Allows to setup and evaluate differential equations (ODEs and DAEs) based on SymbolicExpressions.


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