discrete_time_rocket_c.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 
35 #include <acado_gnuplot.hpp>
36 
37 
38 /* >>> start tutorial code >>> */
39 
40 
41 // -------------------------------------------------------------------------
42 // C-STYLE DEFINITION OF THE PROBLEM DIMENSIONS:
43 // -------------------------------------------------------------------------
44 
45 
46 #define NJ 1 // number of objective functions
47 #define NX 4 // number of differential states
48 #define NI 4 // number of initial value constraints
49 #define NE 2 // number of end-point / terminal constraints
50 #define NH 1 // number of inequality path constraints
51 
52 
53 // -----------------------------------------------------------------------------
54 // UGLY C-STYLE DEFINITION OF THE OBJECTIVE, MODEL AND CONSTRAINT FUNCTIONS:
55 // -----------------------------------------------------------------------------
56 
57 
58 void myDifferentialEquation( double *x, double *f, void *user_data ){
59 
60  double h = ((double*) user_data)[0];
61 
62  double v = x[0];
63  double m = x[1];
64  double u = x[2];
65  double L = x[3];
66  double s = x[4];
67 
68  f[0] = s + h*v;
69  f[1] = v + h*(u-0.02*v*v)/(1.0+m);
70  f[2] = m - h*0.01*u*u;
71  f[3] = L + h*u*u;
72 }
73 
74 
75 void myObjectiveFunction( double *x, double *f, void *user_data ){
76 
77  f[0] = x[3];
78 }
79 
80 
81 void myInitialValueConstraint( double *x, double *f, void *user_data ){
82 
83  f[0] = x[4];
84  f[1] = x[0];
85  f[2] = x[1];
86  f[3] = x[3];
87 }
88 
89 
90 void myEndPointConstraint( double *x, double *f, void *user_data ){
91 
92  f[0] = x[4] - 10.0;
93  f[1] = x[0];
94 }
95 
96 
97 void myInequalityPathConstraint( double *x, double *f, void *user_data ){
98 
99  f[0] = x[0];
100 }
101 
102 
103 // -------------------------------------------------------------------------
104 // USE THE ACADO TOOLKIT TO SOLVE THE PROBLEM:
105 // -------------------------------------------------------------------------
106 
107 
109 
110 
111 int main( ){
112 
113 
114  // INTRODUCE THE VARIABLES:
115  // --------------------------------------------------
116  DifferentialState s,v,m,L;
117  Control u ;
118 
119  const double h = 0.01;
121 
122 
123  // DEFINE THE DIMENSIONS OF THE C-FUNCTIONS:
124  // --------------------------------------------------
130 
131  F.setUserData( (void*) &h );
132 
133 
134  // DEFINE THE OPTIMIZATION VARIABLES:
135  // --------------------------------------------------
136 
137  IntermediateState x(5);
138 
139  x(0) = v; x(1) = m; x(2) = u; x(3) = L; x(4) = s;
140 
141 
142  // DEFINE AN OPTIMAL CONTROL PROBLEM:
143  // ----------------------------------
144  OCP ocp( 0.0, 10.0, 20 );
145 
146  ocp.minimizeMayerTerm( M(x) );
147 
148  ocp.subjectTo( f << F(x) );
149 
150  ocp.subjectTo( AT_START, I(x) == 0.0 );
151  ocp.subjectTo( AT_END , E(x) == 0.0 );
152  ocp.subjectTo( H(x) <= 1.3 );
153 
154 
155  // VISUALIZE THE RESULTS IN A GNUPLOT WINDOW:
156  // ------------------------------------------
157  GnuplotWindow window1;
158  window1.addSubplot( s,"DifferentialState s" );
159  window1.addSubplot( v,"DifferentialState v" );
160  window1.addSubplot( m,"DifferentialState m" );
161  window1.addSubplot( u,"Control u" );
162 
163 
164  // DEFINE AN OPTIMIZATION ALGORITHM AND SOLVE THE OCP:
165  // ---------------------------------------------------
166  OptimizationAlgorithm algorithm(ocp);
167  algorithm.set( INTEGRATOR_TOLERANCE, 1e-6 );
168  algorithm.set( KKT_TOLERANCE, 1e-3 );
169  algorithm << window1;
170  algorithm.solve();
171 
172 
173  return 0;
174 }
175 /* <<< end tutorial code <<< */
User-interface to formulate and solve optimal control problems and static NLPs.
void myEndPointConstraint(double *x, double *f, void *user_data)
#define USING_NAMESPACE_ACADO
returnValue subjectTo(const DifferentialEquation &differentialEquation_)
Definition: ocp.cpp:153
returnValue minimizeMayerTerm(const Expression &arg)
Definition: ocp.cpp:238
returnValue addSubplot(PlotWindowSubplot &_subplot)
Allows to setup and evaluate discretized differential equations based on SymbolicExpressions.
returnValue set(OptionsName name, int value)
Definition: options.cpp:126
void myObjectiveFunction(double *x, double *f, void *user_data)
void myInequalityPathConstraint(double *x, double *f, void *user_data)
USING_NAMESPACE_ACADO int main()
void myInitialValueConstraint(double *x, double *f, void *user_data)
#define E
virtual returnValue setUserData(void *user_data_)
Definition: c_function.cpp:539
Data class for defining optimal control problems.
Definition: ocp.hpp:89
#define NE
#define v
#define L
#define NX
#define NH
(no description yet)
Definition: c_function.hpp:54
#define NJ
void myDifferentialEquation(double *x, double *f, void *user_data)
Provides an interface to Gnuplot for plotting algorithmic outputs.
virtual returnValue solve()
#define NI


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