pseAUT / SLMPC_OMMatlab

Application of OpenModelica-Matlab Interface to Integrated Simulation and Successive Linearization Based Model Predictive Control
BSD 3-Clause "New" or "Revised" License
1 stars 2 forks source link

Modifying the code to apply for different dynamic system #1

Closed Obaid107 closed 5 months ago

Obaid107 commented 6 months ago

Hello,

Thank you for providing this example, it is very helpful.

I am trying to modify this code to apply for a different dynamic system, e.g., nonlinear model of pendulum. Therefore, I have replaced the tank model with the pendulum model in Modelica. Now, the model has two states, two output, and one input. To be consistent, I have not changed the variable names.

Modelica code is as follow: %%%%% model tank parameter Real g=9.81; parameter Real l=0.1; parameter Real b=0.2; parameter Real h1_In; // x1_0 parameter Real h2_In; // x2_0 parameter Real eps=1e-6; output Real qo1; //x_1 output Real qo2; //x_2 input Real qi1;
Real h1; //x1 Real h2; //x2 initial equation h1=h1_In; h2=h2_In; equation qo1=h1; qo2=h2; der(h1)=h2; der(h2)=-(g/l)sin(h1)-bh2+qi1; end tank; %%%%%%

Accordingly I have modified Matlab code "main file" as follow: % hsp=[0.56;0.52;0.36] to hsp=[0.5;0]; % QQ1=diag([1;1;1]) to QQ1=diag([1;1]); % QQ2=diag([18;18;18]) to QQ2=diag([1;1]); % u_pre=[0;0;0] to u_pre=[0]; % u_lin=[I1;I2;I3] to u_lin=[I1]; % IC=[0;0;0] to IC=[0;0]; % State_names=["h1_In";"h2_In";"h3_In"] to State_names=["h1_In";"h2_In"]; % State_Fields= ["h1";"h2";"h3"] to State_Fields= ["h1";"h2"]; % Output_Fields=["qo1";"qo2";"qo3"] to Output_Fields=["qo1";"qo2"]; % Xhat=[0.1;0.1;0.1] to Xhat=[0.1;0.1]; % U=I1ones(3,ceil(Tsim/Ts)) to U=I1ones(1,ceil(Tsim/Ts));

I have some error related to dimension mismatch in MPC optimizer. So I have also modified OBJt and nlcondi functions as follows: % p=[reshape(uinp(1:NC),1,NC);reshape(uinp(NC+1:2NC),1,NC);reshape(uinp(2NC+1:end),1,NC)] to p=[uinp(1:NC),1,NC]; % p=[p,p(:,end).ones(3,NH-NC)] to p=[p,p(:,end).ones(1,NH-NC)];

Unfortunately, there is still the following error:

Error using horzcat Dimensions of arrays being concatenated are not consistent. Error in OBJt (line 9) p=[uinp(1:NC),1,NC]; Error in fmincon (line 563) initVals.f = feval(funfcn{3},X,varargin{:}); Error in Main (line 270) [p,fval,exitflag,output] = fmincon (@OBJt,U_guess,[],[],[],[],LB,UB,@nlcondi,opt_options); Caused by: Failure in initial objective function evaluation. FMINCON cannot continue.

Since, I don't have access to the input of OBJt and nlcondi functions which is "uinp", I would kindly request your assistance. Thank you in advance.

MohammadHadiAlizadeh commented 6 months ago

@Obaid107 Please provide the whole code as an attached file so I can have a look.

MohammadHadiAlizadeh commented 6 months ago

@Obaid107 For now, I can see you have defined a 2x1 matrix for QQ2. QQ2 is the control input weight, and considering the fact that you have 1 input, it must be a 1x1 (scalar) quantity. Please see the SLMPC paper for details.

Obaid107 commented 6 months ago

@MohammadHadiAlizadeh Thank you for your response. I have adjusted the dimensions of QQ2, but I am encountering the same error. Please find the modified files from the following link. pendelum.zip

MohammadHadiAlizadeh commented 6 months ago

@Obaid107 First, your lower and upper bounds of optimization must cope with the model's input number. 3xNc is incorrect and it has to be 1xNc:

LB=0*ones(1*NC,1);

UB=0.3*ones(1*NC,1);

In your OBJ function, I would keep p 's definition in this form: p=[reshape(uinp(1:NC),1,NC)];

This remarks may solve your code structure problem. But still your MPC fails at the first iteration and fmincon converges to an infeasible point. You can try to check your U and L bounds, and adjust optimizer options to see if it can handle this situation.

MohammadHadiAlizadeh commented 6 months ago

@Obaid107 In our tank model, our inputs vary between 0 and 0.3, and that's why we defined LB and UB in that way, so you probably want to adjust them. Also, we considered a nonlinear constraint nlcondi to avoid back-flow condition. In the Pendulum problem, you do not have such constraints and you can just simply replace @nlcondi with[] in the fmincon command: fmincon (@OBJt,U_guess,[],[],[],[],LB,UB,[],opt_options);
like above.