rocket_c.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 
00034 #include <acado_optimal_control.hpp>
00035 #include <acado_gnuplot.hpp>
00036 
00037 
00038 /* >>> start tutorial code >>> */
00039 
00040 
00041 // -------------------------------------------------------------------------
00042 //             C-STYLE DEFINITION OF THE PROBLEM DIMENSIONS:
00043 // -------------------------------------------------------------------------
00044 
00045 
00046 #define  NJ   1    // number of objective functions
00047 #define  NX   4    // number of differential states
00048 #define  NI   4    // number of initial value constraints
00049 #define  NE   2    // number of end-point / terminal constraints
00050 #define  NH   1    // number of inequality path constraints
00051 
00052 
00053 // -----------------------------------------------------------------------------
00054 //   UGLY C-STYLE DEFINITION OF THE OBJECTIVE, MODEL AND CONSTRAINT FUNCTIONS:
00055 // -----------------------------------------------------------------------------
00056 
00057 
00058 void myDifferentialEquation( double *x, double *f, void *user_data ){
00059 
00060     f[0] =  x[0];
00061     f[1] =  (x[2]-0.02*x[0]*x[0])/(1.0+x[1]);
00062     f[2] =  -0.01*x[2]*x[2];
00063     f[3] =  x[2]*x[2];
00064 }
00065 
00066 void myObjectiveFunction( double *x, double *f, void *user_data ){
00067 
00068     f[0] = x[3];
00069 }
00070 
00071 
00072 void myInitialValueConstraint( double *x, double *f, void *user_data ){
00073 
00074     f[0] =  x[4];
00075     f[1] =  x[0];
00076     f[2] =  x[1];
00077     f[3] =  x[3];
00078 }
00079 
00080 
00081 void myEndPointConstraint( double *x, double *f, void *user_data ){
00082 
00083     f[0] =  x[4] - 10.0;
00084     f[1] =  x[0];
00085 }
00086 
00087 
00088 void myInequalityPathConstraint( double *x, double *f, void *user_data ){
00089 
00090     f[0] =  x[0];
00091 }
00092 
00093 
00094 // -------------------------------------------------------------------------
00095 //              USE THE ACADO TOOLKIT TO SOLVE THE PROBLEM:
00096 // -------------------------------------------------------------------------
00097 
00098 
00099 USING_NAMESPACE_ACADO
00100 
00101 
00102 int main( ){
00103 
00104 
00105     // INTRODUCE THE VARIABLES:
00106     // --------------------------------------------------
00107     DifferentialState     s,v,m,L;
00108     Control               u      ;
00109     DifferentialEquation  f      ;
00110 
00111 
00112     // DEFINE THE DIMENSIONS OF THE C-FUNCTIONS:
00113     // --------------------------------------------------
00114     CFunction F( NX, myDifferentialEquation     );
00115     CFunction M( NJ, myObjectiveFunction        );
00116     CFunction I( NI, myInitialValueConstraint   );
00117     CFunction E( NE, myEndPointConstraint       );
00118     CFunction H( NH, myInequalityPathConstraint );
00119 
00120 
00121     // DEFINE THE OPTIMIZATION VARIABLES:
00122     // --------------------------------------------------
00123 
00124     IntermediateState x(5);
00125 
00126     x(0) = v; x(1) = m; x(2) = u; x(3) = L; x(4) = s;
00127 
00128 
00129     // DEFINE AN OPTIMAL CONTROL PROBLEM:
00130     // ----------------------------------
00131     OCP ocp( 0.0, 10.0 );
00132 
00133     ocp.minimizeMayerTerm( M(x) );
00134 
00135     ocp.subjectTo( f << F(x) );
00136 
00137     ocp.subjectTo( AT_START, I(x) ==  0.0 );
00138     ocp.subjectTo( AT_END  , E(x) ==  0.0 );
00139     ocp.subjectTo(           H(x) <=  1.3 );
00140 
00141 
00142     // VISUALIZE THE RESULTS IN A GNUPLOT WINDOW:
00143     // ------------------------------------------
00144     GnuplotWindow window1;
00145     window1.addSubplot( s,"DifferentialState s" );
00146     window1.addSubplot( v,"DifferentialState v" );
00147     window1.addSubplot( m,"DifferentialState m" );
00148     window1.addSubplot( u,"Control u" );
00149 
00150 
00151     // DEFINE AN OPTIMIZATION ALGORITHM AND SOLVE THE OCP:
00152     // ---------------------------------------------------
00153     OptimizationAlgorithm algorithm(ocp);
00154     algorithm.set( INTEGRATOR_TOLERANCE, 1e-6 );
00155     algorithm.set( KKT_TOLERANCE, 1e-3 );
00156     algorithm << window1;
00157     algorithm.solve();
00158 
00159 
00160     return 0;
00161 }
00162 /* <<< end tutorial code <<< */


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