discrete_time_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         double h = ((double*) user_data)[0];
00061 
00062         double v = x[0];
00063         double m = x[1];
00064         double u = x[2];
00065         double L = x[3];
00066         double s = x[4];
00067 
00068     f[0] = s + h*v;
00069     f[1] = v + h*(u-0.02*v*v)/(1.0+m);
00070     f[2] = m - h*0.01*u*u;
00071         f[3] = L + h*u*u;
00072 }
00073 
00074 
00075 void myObjectiveFunction( double *x, double *f, void *user_data ){
00076 
00077     f[0] = x[3];
00078 }
00079 
00080 
00081 void myInitialValueConstraint( double *x, double *f, void *user_data ){
00082 
00083     f[0] =  x[4];
00084     f[1] =  x[0];
00085     f[2] =  x[1];
00086     f[3] =  x[3];
00087 }
00088 
00089 
00090 void myEndPointConstraint( double *x, double *f, void *user_data ){
00091 
00092     f[0] =  x[4] - 10.0;
00093     f[1] =  x[0];
00094 }
00095 
00096 
00097 void myInequalityPathConstraint( double *x, double *f, void *user_data ){
00098 
00099     f[0] =  x[0];
00100 }
00101 
00102 
00103 // -------------------------------------------------------------------------
00104 //              USE THE ACADO TOOLKIT TO SOLVE THE PROBLEM:
00105 // -------------------------------------------------------------------------
00106 
00107 
00108 USING_NAMESPACE_ACADO
00109 
00110 
00111 int main( ){
00112 
00113 
00114     // INTRODUCE THE VARIABLES:
00115     // --------------------------------------------------
00116     DifferentialState     s,v,m,L;
00117     Control               u      ;
00118         
00119     const double h       =   0.01;
00120     DiscretizedDifferentialEquation  f(h);
00121 
00122 
00123     // DEFINE THE DIMENSIONS OF THE C-FUNCTIONS:
00124     // --------------------------------------------------
00125     CFunction F( NX, myDifferentialEquation     );
00126     CFunction M( NJ, myObjectiveFunction        );
00127     CFunction I( NI, myInitialValueConstraint   );
00128     CFunction E( NE, myEndPointConstraint       );
00129     CFunction H( NH, myInequalityPathConstraint );
00130 
00131         F.setUserData( (void*) &h );
00132 
00133 
00134     // DEFINE THE OPTIMIZATION VARIABLES:
00135     // --------------------------------------------------
00136 
00137     IntermediateState x(5);
00138 
00139     x(0) = v; x(1) = m; x(2) = u; x(3) = L; x(4) = s;
00140 
00141 
00142     // DEFINE AN OPTIMAL CONTROL PROBLEM:
00143     // ----------------------------------
00144     OCP ocp( 0.0, 10.0, 20 );
00145 
00146     ocp.minimizeMayerTerm( M(x) );
00147 
00148     ocp.subjectTo( f << F(x) );
00149 
00150     ocp.subjectTo( AT_START, I(x) ==  0.0 );
00151     ocp.subjectTo( AT_END  , E(x) ==  0.0 );
00152     ocp.subjectTo(           H(x) <=  1.3 );
00153 
00154 
00155     // VISUALIZE THE RESULTS IN A GNUPLOT WINDOW:
00156     // ------------------------------------------
00157     GnuplotWindow window1;
00158     window1.addSubplot( s,"DifferentialState s" );
00159     window1.addSubplot( v,"DifferentialState v" );
00160     window1.addSubplot( m,"DifferentialState m" );
00161     window1.addSubplot( u,"Control u" );
00162 
00163 
00164     // DEFINE AN OPTIMIZATION ALGORITHM AND SOLVE THE OCP:
00165     // ---------------------------------------------------
00166     OptimizationAlgorithm algorithm(ocp);
00167     algorithm.set( INTEGRATOR_TOLERANCE, 1e-6 );
00168     algorithm.set( KKT_TOLERANCE, 1e-3 );
00169     algorithm << window1;
00170     algorithm.solve();
00171 
00172 
00173     return 0;
00174 }
00175 /* <<< end tutorial code <<< */


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