00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00054 #include <acado_optimal_control.hpp>
00055 #include <acado_gnuplot.hpp>
00056 #include "../integrator/hydroscal_model.hpp"
00057
00058 #include <time.h>
00059
00060 USING_NAMESPACE_ACADO
00061
00062 void ffcn_model( double *x, double *f, void *user_data ){
00063
00064 int i;
00065 double *xd = new double[NXD];
00066 double *xa = new double[NXA];
00067 double *u = new double[ NU];
00068 double *p = new double[ NP];
00069
00070 for( i = 0; i < NXD; i++ ) xd[i] = x[ 1+i ];
00071 for( i = 0; i < NXA; i++ ) xa[i] = x[ NXD+1+i ];
00072 for( i = 0; i < NU; i++ ) u[i] = x[ NXA+NXD+1+i ];
00073 for( i = 0; i < NP; i++ ) p[i] = x[ NXA+NXD+NU+1+i ];
00074
00075 ffcn( &x[0], xd, xa, u, p, f );
00076 gfcn( &x[0], xd, xa, u, p, &(f[NXD]) );
00077
00078 delete[] xd;
00079 delete[] xa;
00080 delete[] u;
00081 delete[] p;
00082
00083 }
00084
00085
00086
00087 int main( ){
00088
00089 double clock1 = clock();
00090
00091 double t_start = 0.0;
00092 double t_end = 600.0;
00093 int intervals = 5 ;
00094 int i;
00095
00096 TIME t;
00097 DifferentialState x("", NXD, 1);
00098 AlgebraicState z("", NXA, 1);
00099 Control u("", NU, 1);
00100 Parameter p("", NP, 1);
00101 IntermediateState is(1+NXD+NXA+NU+NP);
00102
00103 is(0) = t;
00104 for (i=0; i < NXD; ++i) is(1+i) = x(i);
00105 for (i=0; i < NXA; ++i) is(1+NXD+i) = z(i);
00106 for (i=0; i < NU; ++i) is(1+NXD+NXA+i) = u(i);
00107 for (i=0; i < NP; ++i) is(1+NXD+NXA+NU+i) = p(i);
00108
00109 CFunction hydroscalModel( NXD+NXA, ffcn_model );
00110
00111
00112
00113
00114 DifferentialEquation f;
00115 f << hydroscalModel(is);
00116
00117
00118
00119
00120 double xd[NXD] = { 2.1936116177990631E-01,
00121 3.3363028623863722E-01,
00122 3.7313133250625952E-01,
00123 3.9896472354654333E-01,
00124 4.1533719381260475E-01,
00125 4.2548399372287182E-01,
00126
00127 4.3168379354213621E-01,
00128 4.3543569751236455E-01,
00129 4.3768918647214428E-01,
00130 4.3903262905928286E-01,
00131 4.3982597315656735E-01,
00132 4.4028774979047969E-01,
00133 4.4055002518902953E-01,
00134 4.4069238917008052E-01,
00135 4.4076272408112094E-01,
00136 4.4078980543461005E-01,
00137 4.4079091412311144E-01,
00138 4.4077642312834125E-01,
00139 4.4075255679998443E-01,
00140 4.4072304911231042E-01,
00141 4.4069013958173919E-01,
00142 6.7041926189645151E-01,
00143 7.3517997375758948E-01,
00144 7.8975978943631409E-01,
00145 8.3481725159539033E-01,
00146 8.7125377077380739E-01,
00147 9.0027275078767721E-01,
00148 9.2312464536394301E-01,
00149 9.4096954980798608E-01,
00150 9.5481731262797742E-01,
00151 9.6551271145368878E-01,
00152 9.7374401773010488E-01,
00153 9.8006186072166701E-01,
00154 9.8490109485675337E-01,
00155 9.8860194771099286E-01,
00156 9.9142879342008328E-01,
00157 9.9358602331847468E-01,
00158 9.9523105632238640E-01,
00159 9.9648478785701988E-01,
00160 9.9743986301741971E-01,
00161 9.9816716097314861E-01,
00162 9.9872084014280071E-01,
00163 3.8633811956730968E+00,
00164 3.9322260498028840E+00,
00165 3.9771965626392531E+00,
00166 4.0063070333869728E+00,
00167 4.0246026844143410E+00,
00168 4.0358888958821835E+00,
00169 4.0427690398786789E+00,
00170 4.0469300433477020E+00,
00171 4.0494314648020326E+00,
00172 4.0509267560029381E+00,
00173 4.0518145583397631E+00,
00174 4.0523364846379799E+00,
00175 4.0526383977460299E+00,
00176 4.0528081437632766E+00,
00177 4.0528985491134542E+00,
00178 4.0529413510270169E+00,
00179 4.0529556049324462E+00,
00180 4.0529527471448805E+00,
00181 4.0529396392278008E+00,
00182 4.0529203970496912E+00,
00183 3.6071164950918582E+00,
00184 3.7583754503438387E+00,
00185 3.8917148481441974E+00,
00186 4.0094300698741563E+00,
00187 4.1102216725798293E+00,
00188 4.1944038520620675E+00,
00189 4.2633275166560596E+00,
00190 4.3188755452109175E+00,
00191 4.3630947909857642E+00,
00192 4.3979622247841386E+00,
00193 4.4252580012497740E+00,
00194 4.4465128947193868E+00,
00195 4.4630018314791968E+00,
00196 4.4757626150015568E+00,
00197 4.4856260094946823E+00,
00198 4.4932488551808500E+00,
00199 4.4991456959629330E+00,
00200 4.5037168116896273E+00,
00201 4.5072719605639726E+00,
00202 4.5100498969782414E+00
00203 };
00204
00205 double ud[ NU] = { 4.1833910982822058E+00,
00206 2.4899344742988991E+00
00207 };
00208
00209 double pd[ NP] = { 1.5458567140000001E-01,
00210 1.7499999999999999E-01,
00211 3.4717208398678062E-01,
00212 6.1895708603484367E-01,
00213 1.6593025789999999E-01,
00214 5.0695122527590109E-01,
00215 8.5000000000000000E+00,
00216 1.7000000000000001E-01,
00217 9.3885430857029321E+04,
00218 2.5000000000000000E+02,
00219 1.4026000000000000E+01,
00220 3.2000000000000001E-01,
00221 7.1054000000000002E+01,
00222 4.7163089489100003E+01,
00223 4.1833910753991770E+00,
00224 2.4899344810136301E+00,
00225 1.8760537088149468E+02
00226 };
00227
00228 DVector x0(NXD, xd);
00229 DVector p0(NP, pd);
00230
00231
00232
00233
00234 OCP ocp( t_start, t_end, intervals );
00235
00236
00237 Function h;
00238
00239
00240
00241
00242
00243
00244 h << 0.1 * ( z(94) - 88.0 );
00245 h << 0.1 * ( z(108) - 70.0 );
00246 h << 0.01 * ( u(0) - ud[0] );
00247 h << 0.01 * ( u(1) - ud[1] );
00248 ocp.minimizeLSQ( h );
00249
00250
00251 ocp.subjectTo( f );
00252
00253
00254 ocp.subjectTo( AT_START, x == x0 );
00255
00256
00257 ocp.subjectTo( p == p0 );
00258
00259
00260 ocp.subjectTo( ud[0] - 2.0 <= u(0) <= ud[0] + 2.0 );
00261 ocp.subjectTo( ud[1] - 2.0 <= u(1) <= ud[1] + 2.0 );
00262
00263
00264
00265
00266
00267 OptimizationAlgorithm algorithm(ocp);
00268
00269 algorithm.initializeAlgebraicStates("hydroscal_algebraic_states.txt");
00270
00271 algorithm.set( INTEGRATOR_TYPE, INT_BDF );
00272 algorithm.set( MAX_NUM_ITERATIONS, 5 );
00273 algorithm.set( KKT_TOLERANCE, 1e-3 );
00274 algorithm.set( INTEGRATOR_TOLERANCE, 1e-4 );
00275 algorithm.set( ABSOLUTE_TOLERANCE , 1e-6 );
00276 algorithm.set( PRINT_SCP_METHOD_PROFILE, YES );
00277 algorithm.set( LINEAR_ALGEBRA_SOLVER, SPARSE_LU );
00278 algorithm.set( DISCRETIZATION_TYPE, MULTIPLE_SHOOTING );
00279
00280
00281
00282 algorithm.set( DYNAMIC_SENSITIVITY, FORWARD_SENSITIVITY_LIFTED );
00283
00284
00285
00286
00287
00288 algorithm.solve();
00289
00290 double clock2 = clock();
00291 printf("total computation time = %.16e \n", (clock2-clock1)/CLOCKS_PER_SEC );
00292
00293
00294
00295
00296 VariablesGrid out_states;
00297 algorithm.getDifferentialStates( out_states );
00298 out_states.print( "OUT_states.m","STATES",PS_MATLAB );
00299
00300 VariablesGrid out_controls;
00301 algorithm.getControls( out_controls );
00302 out_controls.print( "OUT_controls.m","CONTROLS",PS_MATLAB );
00303
00304 VariablesGrid out_algstates;
00305 algorithm.getAlgebraicStates( out_algstates );
00306 out_algstates.print( "OUT_algstates.m","ALGSTATES",PS_MATLAB );
00307
00308 GnuplotWindow window;
00309 window.addSubplot( out_algstates(94), "Temperature tray 14" );
00310 window.addSubplot( out_algstates(108), "Temperature tray 28" );
00311 window.addSubplot( out_controls(0), "L_vol" );
00312 window.addSubplot( out_controls(1), "Q" );
00313 window.plot( );
00314
00315 return 0;
00316 }
00317
00318
00319