matrix_vector_ocp.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 
00035 #include <acado_optimal_control.hpp>
00036 #include <acado_gnuplot.hpp>
00037 
00038 
00039 /* >>> start tutorial code >>> */
00040 int main( ){
00041 
00042     USING_NAMESPACE_ACADO
00043 
00044     // INTRODUCE THE VARIABLES:
00045     // ----------------------------
00046     DifferentialState    x("", 10, 1);    // a differential state vector with dimension 10. (vector)
00047     DifferentialState    y    ;    // another differential state y                   (scalar)
00048     Control              u("", 2, 1);    // a control input with dimension 2.              (vector)
00049     Parameter            p    ;    // a parameter (here a scalar).                   (scalar)
00050 
00051     DifferentialEquation f    ;    // the differential equation
00052 
00053     const double t_start =  0.0;
00054     const double t_end   =  1.0;
00055 
00056 
00057     // READ A MATRIX "A" FROM A FILE:
00058     // ------------------------------
00059     DMatrix A; A.read( "matrix_vector_ocp_A.txt" );
00060     DMatrix B; B.read( "matrix_vector_ocp_B.txt" );
00061 
00062 
00063     // READ A VECTOR "x0" FROM A FILE:
00064     // -------------------------------
00065     DVector x0; x0.read( "matrix_vector_ocp_x0.txt" );
00066 
00067 
00068     // DEFINE A DIFFERENTIAL EQUATION:
00069     // -------------------------------
00070     f << dot(x) == -(A*x) + B*u;                           // matrix vector notation for a linear equation
00071     f << dot(y) == x.transpose()*x + 2.0*u.transpose()*u;  // matrix vector notation:  x^x  = scalar product = ||x||_2^2
00072                                                            //                          u^u  = scalar product = ||u||_2^2
00073 
00074 
00075     // DEFINE AN OPTIMAL CONTROL PROBLEM:
00076     // ----------------------------------
00077     OCP ocp( t_start, t_end, 20 );
00078     ocp.minimizeMayerTerm( y );
00079     ocp.subjectTo( f );
00080 
00081     ocp.subjectTo( AT_START, x == x0  );
00082     ocp.subjectTo( AT_START, y == 0.0 );
00083 
00084 
00085     GnuplotWindow window;
00086         window.addSubplot( x(0),"x0" );
00087         window.addSubplot( x(6),"x6" );
00088         window.addSubplot( u(0),"u0" );
00089         window.addSubplot( u(1),"u1" );
00090 
00091 
00092     // DEFINE AN OPTIMIZATION ALGORITHM AND SOLVE THE OCP:
00093     // ---------------------------------------------------
00094     OptimizationAlgorithm algorithm(ocp);
00095 
00096     algorithm.set( MAX_NUM_ITERATIONS, 20 );
00097     algorithm.set( KKT_TOLERANCE, 1e-10 );
00098 
00099     algorithm << window;
00100     algorithm.solve();
00101 
00102     return 0;
00103 }
00104 /* <<< end tutorial code <<< */


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