3

Hopefully, I will be able to explain my question well.

I am working on Nonlinear model predictive control implementation.

I have got 3 files: 1). a simulink slx file which is basically a nonlinear pendulum model. 2). A function file, to get the cost function from the simulink model. 3). MPC code.

code snippet of cost function

**simOut=sim('NonlinearPendulum','StopTime', num2str(Np*Ts));**

%Linearly interpolates X to obtain sampled output states at time instants.
T=simOut.get('Tsim');
X=simOut.get('xsim');
xt=interp1(T,X,linspace(0,Np*Ts,Np+1))';

U=U(1:Nu);
%Quadratic cost function
R=0.01;
J=sum(sum((xt-repmat(r,[1 Np+1])).*(xt-repmat(r,[1 Np+1]))))+R*(U-ur)*...
(U-ur)';

Now I take this cost function and optimize it using fmincon to generate a sequence of inputs to be applied to the model, using my MPC code. A code snippet of my MPC code.

%Constraints -1<=u(t)<=1;
Acons=[eye(Nu,Nu);-eye(Nu,Nu)];
Bcons=[ones(Nu,1);ones(Nu,1)];
options = optimoptions(@fmincon,'Algorithm','active-set','MaxIter',100);

warning off
for a1=1:nf

X=[];                       %Prediction output
T=[];                       %Prediction time

Xsam=[];
Tsam=[];

%Nonlinear MPC controller
Ubreak=linspace(0,(Np-1)*Ts,Np); %Break points for 1D lookup, used to avoid 
% several calls/compilations of simulink model in fmincon.
**J=@(v) pendulumCostFunction(v,x0,ur,r(:,a1),Np,Nu,Ts);**
U=fmincon(J,U0,Acons,Bcons,[],[],[],[],[],options);
%U=fmincon(J,U0,Acons,Bcons);
U0=U;

UUsam=[UUsam;U(1)];%Apply only the first selected input

%Apply the selected input to plant.
Ubreak=[0 Ts]; %Break points for 1D lookup
U=[UUsam(end) UUsam(end)];
**simOut=sim('NonlinearPendulum','StopTime', num2str(Ts));**

In both the codes, I have marked the times we call our simulink model. Now, issue is that to run this whole simulation for just 5 seconds it takes around 7-8 minutes on my windows machine, MATLAB R2014B. Is there a way to optimize this? As, I am planning to extend this algorithm to 9th order system unlike 2nd order pendulum model.

If, anyone has suggestion on using simulink coder to generate C code: I have tried that, and the problem I face is that I don't know what to do with the several files generated. Please be as detailed as possible.

GKS
  • 427
  • 1
  • 5
  • 18
  • Is fmincon converging or hitting MaxIter? What's your prediction horizon? Run a profiler and check what takes most of the time. – remus Apr 15 '15 at 08:23
  • Check ACADO: "The ACADO Code Generation tool can automatically generate Gauss-Newton real-time iteration algorithms for fast nonlinear MPC and MHE applications [4, 5]." ACADO also has a Matlab interface and it's quite easy to set up (it has many examples including NMPC). – remus Apr 15 '15 at 08:27
  • fmincon doesn't hit maxiter. I have to try Prediction horizon fro 5 to 20. Running a profiler tells me that a lot of time (almost 90%) in getting cost function from the simulink model i.e. on last line of code snippet of cost function code – GKS Apr 15 '15 at 20:20
  • I don't really understand the overall flow of your top level algorithm from these snippets. Perhaps you're implementing a Sequential Quadratic Programming Approach using FMINCON to solve QP subproblems and then refine AP objective based on new SIMULINK evaluation? Maybe you are, or if not should be, running overall under FMINCON active-set (sqp might be better though w/ expensive SIMULINK evaluations) finite difference Quasi-Newton. KNITRO is similar to FMINCON, but may take many fewer iterations. If you show overall flow, can make better assessment. But I'm not very familiar with SIMULINK. – Mark L. Stone Jun 23 '15 at 17:24
  • typo above (too late to edit: That should be "QP objective", not "AP objective". – Mark L. Stone Jun 23 '15 at 17:31

1 Answers1

0

From the code snippets, it appears that you are solving a linear time invariant model with a quadratic objective. Here is some MATLAB (and Python) code for an overhead crane pendulum and inverted pendulum, both with state space linear models and quadratic objectives.

overhead pendulum

inverted pendulum

One of the ways to make it run faster is to avoid a Simulink interface and a shooting method for solving the MPC. A simultaneous method with orthogonal collocation on finite elements is faster and also enables higher index DAE model forms if you'd like to use a nonlinear model.

John Hedengren
  • 12,068
  • 1
  • 21
  • 25