lsq_term.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 
00027 
00035 #include <acado_optimal_control.hpp>
00036 #include <acado_gnuplot.hpp>
00037 
00038 
00039 int main( ){
00040 
00041     USING_NAMESPACE_ACADO
00042 
00043 
00044     // INTRODUCE THE VARIABLES:
00045     // -------------------------
00046     const int N = 2;
00047 
00048     DifferentialState        x, y("", N, 1);
00049     Control                   u;
00050     DifferentialEquation      f;
00051 
00052     const double t_start =  0.0;
00053     const double t_end   = 10.0;
00054 
00055 
00056     // DEFINE A DIFFERENTIAL EQUATION:
00057     // -------------------------------
00058 
00059     f << dot(x) == -x + 0.9*x*x + u;
00060 
00061     int i;
00062     for( i = 0; i < N; i++ )
00063         f << dot( y(i) ) == -y(i) + 0.5*y(i)*y(i) + u;
00064 
00065 
00066     // DEFINE LEAST SQUARE FUNCTION:
00067     // -----------------------------
00068 
00069     Function h,m;
00070 
00071     h <<     x;
00072     h << 2.0*u;
00073 
00074     m << 10.0*x  ;
00075     m <<  0.1*x*x;
00076 
00077     DMatrix S(2,2);
00078     DVector r(2);
00079 
00080     S.setIdentity();
00081     r.setAll( 0.1 );
00082 
00083 
00084     // DEFINE AN OPTIMAL CONTROL PROBLEM:
00085     // ----------------------------------
00086     OCP ocp( t_start, t_end, 5 );
00087 
00088     ocp.minimizeLSQ       ( S, h, r );
00089     ocp.minimizeLSQEndTerm( S, m, r );
00090 
00091     ocp.subjectTo( f );
00092     ocp.subjectTo( AT_START, x == 1.0 );
00093 
00094     for( i = 0; i < N; i++ )
00095         ocp.subjectTo( AT_START, y(i) == 1.0 );
00096 
00097 
00098     // Additionally, flush a plotting object
00099     GnuplotWindow window;
00100         window.addSubplot( x,"DifferentialState x" );
00101         window.addSubplot( u,"Control u" );
00102 
00103 
00104     // DEFINE AN OPTIMIZATION ALGORITHM AND SOLVE THE OCP:
00105     // ---------------------------------------------------
00106     OptimizationAlgorithm algorithm(ocp);
00107     algorithm << window;
00108 
00109 //    algorithm.set( PRINT_SCP_METHOD_PROFILE, YES );
00110 //    algorithm.set( DYNAMIC_SENSITIVITY,  FORWARD_SENSITIVITY_LIFTED );
00111 //    algorithm.set( HESSIAN_APPROXIMATION, CONSTANT_HESSIAN );
00112 //    algorithm.set( HESSIAN_APPROXIMATION, FULL_BFGS_UPDATE );
00113 //    algorithm.set( HESSIAN_APPROXIMATION, BLOCK_BFGS_UPDATE );
00114     algorithm.set( HESSIAN_APPROXIMATION, GAUSS_NEWTON );
00115 //    algorithm.set( HESSIAN_APPROXIMATION, GAUSS_NEWTON_WITH_BLOCK_BFGS );
00116 //    algorithm.set( HESSIAN_APPROXIMATION, EXACT_HESSIAN );
00117 
00118     // Necessary to use with GN Hessian approximation.
00119     algorithm.set( LEVENBERG_MARQUARDT, 1e-10 );
00120 
00121     algorithm.solve();
00122 
00123     return 0;
00124 }
00125 
00126 
00127 


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