Differences From Artifact [313a218248]:

To Artifact [3008238ce6]:


31
32
33
34
35
36
37
38
39
40
41
42
43



44
45
46
47
48
49
50
31
32
33
34
35
36
37

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52







-





+
+
+







  global system_name_sim i_ppp_sim x_0_sim y_sim u_sim A_u_sim simpar_sim

  ## Defaults
  if nargin<7
    extras.alpha = 0.1;
    extras.criterion = 1e-5;
    extras.emulate_timing = 0;
    extras.estimate = 1;
    extras.max_iterations = 10;
    extras.simulate = 1;
    extras.v = 1e-5;
    extras.verbose = 0;
  endif

  ##Estimate if we have some adjustable parameters
  estimating_parameters = (length(i_par)>0)
  
  ## Names
  s_system_name = sprintf("s%s", system_name);

  ## System details -- defines simulation within ol interval
  par = eval(sprintf("%s_numpar;", system_name));
  simpar = eval(sprintf("%s_simpar;", system_name));
58
59
60
61
62
63
64

65
66
67
68
69
70
71
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74







+







  x_0_model = x_0;
  [n_x,n_y,n_u] = eval(sprintf("%s_def;", system_name));

  ## Sensitivity system details -- defines moving horizon simulation
  simpars = eval(sprintf("%s_simpar;", s_system_name));
  pars = eval(sprintf("%s_numpar;", s_system_name));
  x_0s = eval(sprintf("%s_state(pars);", s_system_name));
  x_0_models = x_0s;

  ## Times
  ## -- within opt horizon
  n_Tau = round(simpars.last/simpars.dt);
  dtau = simpars.dt;
  Tau = [0:n_Tau-1]'*dtau;
  [n_tau,n_w] = size(w_s);
134
135
136
137
138
139
140
141

142
143

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161


162
163
164
165
166
167
168

169
170
171


172
173

174
175
176
177
178
179

180
181
182
183
184
185
186
187
188
189

190
191
192
193
194
195
196
137
138
139
140
141
142
143

144
145

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

164
165
166
167
168
169
170
171

172
173


174
175
176

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

194
195
196
197
198
199
200
201







-
+

-
+

















-
+
+






-
+

-
-
+
+

-
+






+









-
+







      T_ol = n_ol*dt;		# Length of ol interval
      t_open = [t_open;T_ol];

      ## Generate input to actual system
      u_star_t = ppp_ustar(A_u,1,t_ol',0,0,n_u-n_U);

      ## Tune parameters/states
      if (extras.estimate==1)
      if (estimating_parameters==1)
	## Save up the estimated parameters
	par_est = pars(i_par(:,1));
	par_est = pars(i_par(:,1))
	p = [p; par_est'];

	## Set up according to interval length
	if (T_ol>T_ol_0) ## Truncate data
	  simpar_est.last = T_ol_0;
	  y_est = y_ol(1:n_t+1,:);
	else
	  simpar_est.last = T_ol;
	  y_est = y_ol;
	endif

	simpar_pred.last = T_ol_0; # Predicted length of next interval
	pars(i_ppp(:,1)) = U_old; # Update the simulation ppp weights
	
	## Optimise
	tick = time;
	[pars,Par,Error,Y,its] = \
	    ppp_optimise(s_system_name,x_0s,pars,simpar_est,u_star_t,y_est,i_par,extras);
	    ppp_optimise(s_system_name,x_0_models,pars,simpar_est,u_star_t,y_est,i_par,extras);
II = [1:length(y_est)]; plot(II,y_est,"*", II,Y)
	est_time = time-tick;  
	t_est = [t_est;est_time];
	its_est = [its_est; its-1];
      endif

      ## Update internal model
      par(i_ppp(:,3)) = U_old; # Update the simulation ppp weights
      par(i_ppp(:,3)) = U_old; # Update the internal model ppp weights

      if (extras.estimate==1)
	par(i_par(:,3)) = pars(i_par(:,1)); # Update the simulation params
      if (estimating_parameters==1)
	par(i_par(:,3)) = pars(i_par(:,1)); # Update the internal model params
      endif
      

      simpar_model.last = T_ol;
      [y_model,x_model] = eval(sprintf("%s_sim(x_0_model, par, simpar_model, \
 					       u_star_t);",system_name));

      x_0 = x_model(n_ol+1,:)';	# Initial state of next interval
      x_0_model = x_0;
      x_0_models(1:2:(2*n_x)-1) = x_0_model;

      ## Compute U by optimisation
      tick = time;

      ## Predict state at start of next interval
      par(i_ppp(:,3)) = U;
      [y_next,x_next] = eval(sprintf("%s_sim(x_0, par, simpar, \
					     u_star_t);",system_name));
      x_next = x_next(n_t+1,:)'; # Initial state for next time
      x_nexts(1:2:(2*n_x)-1) = x_next;
      x_nexts(1:2:(2*n_x)-1) = x_next; # And for internal sensitivity model
      
      ## Optimize for next interval      
      U_old = U;		# Save previous value
      U = expm(A_u*T_ol)*U;	# Initialise from continuation trajectory
      pars(i_ppp(:,1)) = U;	# Put initial value of U into the parameter vector
      [U, U_all, Error, Y, its] = ppp_nlin(system_name,x_nexts,pars,simpars,u_star_tau,w_s,i_ppp,extras);


MTT: Model Transformation Tools
GitHub | SourceHut | Sourceforge | Fossil RSS ]