Optimization with many nonlinear Constraints

Hello,
I want to use my optimization Problem with the Optimization Toolbox and the fmincon function.
This is my Optimization problem with my linear constraints and lower and upper bounds
objective=@z;
x(1:4)=[Fx(1), Fx(2), Fx(3), Fx(4)];
x(5:8)=[alpha(1), alpha(2), alpha(3), alpha(4)];
%initial guess
x0=[0, 0, 0, 0, 0, 0, 0, 0]
%variable bounds
lb=[Fx_min(:,1), Fx_min(:,2), Fx_min(:,3), Fx_min(:,4), d_min_vorne+beta(:,1), d_min_vorne+beta(:,2), d_min_hinten+beta(:,3), d_min_hinten+beta(:,4)];
ub=[Fx_max(:,1), Fx_max(:,2), Fx_max(:,3), Fx_max(:,4), d_max_vorne-beta(:,1), d_max_vorne-beta(;,2), d_max_hinten-beta(:,3), d_max_hinten-beta(:,4)];
%linear constraints
%Definition alpha
Aeq(2:5)=[0 0 0 0 1 0 0 0; 0 0 0 0 0 1 0 0; 0 0 0 0 0 0 1 0; 0 0 0 0 0 0 0 1];
beq(2:5)=[d(1)-beta(1); d(2)-beta(2); d(3)-beta(3); d(4)-beta(4)];
%Alternative
%Folgen Solltrajektorie Fx
Aeq(:,1)=[1 1 1 1 0 0 0 0];
beq(:,1)=Fx_v;
A=[];
b=[];
%nonlinear constraints
nonlincon=@nlcon;
%optimize with fmincon
[x,fval,exitflag,output]=fmincon(objective,x0,A,b,Aeq,beq,lb,ub,@nonlcon);
I have my objective function with
i=1:4;
if Fx(:,i) < 0
FxB(:,i)=(-Fx(:,i)+c_w(:,i)*alpha(:,i)*sin(d(:,i)))/cos(d(:,i));
FxA(:,i)=0;
else
FxA(:,i)=(Fx(:,i)+c_w(:,i)*alpha(:,i)*sin(d(:,i)))/cos(d(:,i));
FxB(:,i)=0;
end
%delta Lenkwinkel
delta_d(i)=d(i,t-1)-d(i,t);
%die drei Ziele
S_1(i)=(A_i/SOC(i))*FxA(i);
S_2(i)=((B_i*M(i))/SOC(i))*(1/FxB(i));
S_3(i)= C_i*delta_d(i);
% die Summen der drei Zielfunktionen
syms i
z_1 = symsum(S_1,i,1,4);
z_2 = symsum(S_2,i,1,4);
z_3 = symsum(S_3,i,1,4);
%objektive function
create function z = z_1 + z_2 + z_3;
and my nonlinear constraints
function[C,Ceq]=nlcon(FxA,alpha);
i=1:4;
%Folgen der Solltrajektorie
Mz(i) = -w_vl*(cos(d(1))*Fx(1)-sin(d(1))*Fy(1))+w_vr*(cos(d(2))*Fx(2)-sin(d(2))*Fy(2))-w_hl*(cos(d(3))*Fx(3)-sin(d(3))*Fy(3))+w_hr*(cos(d(4))*Fx(4)-sin(d(4))*Fy(4))+l_v*(sin(d(1))*Fx(1)-cos(d(1))*Fy(1)+sin(d(2))*Fx(2)-cos(d(2))*Fy(2))-l_h*(sin(d(3))*Fx(3)-cos(d(3))*Fy(3)+sin(d(4))*Fy(4)-cos(d(4))*Fy(4));
Ceq(:,1)=Fy_v-symsum(sin(d(i))*((Fx(i)+c_w(i)*alpha(i)*sin(d(i)))/cos(d(i)))-cos(d(i))*c_w(i)*alpha(i),i,1,4);
Ceq(:,2)=Mz_v-symsum(Mz(i),i,1,4);
%maximales regeneratives Bremsmoment
if
Fx_v-symsum(Fx(i),i,1,4)=0;
Fy_v-symsum(sin(d(i))*((Fx(i)+c_w(i)*alpha(i)*sin(d(i)))/cos(d(i)))-cos(d(i))*c_w(i)*alpha(i))=0;
Mz_v-symsum(Mz(i),i,1,4)=0;
then C(:,1)=-(FxB(i)+abs(M(i)/r_dyn));
else C(:,1)=-FxB(i)-Fx_max;
%aktive Anregung
%Eingabe k=1 oder k=0,
%Eingabe j=1...4, u=1...4,
%Eingabe deltaT_aA=Differenzmoment
for k=1;
Ceq(:,3)=r_dyn*FxA(:,j)-r_dyn*FxA(:,u)-deltaT_aA;
end
%Lenkpriorisierung
for SOC(i)<=SOC_krit;
C=FxA(i);
I guess I have everything whats needed but I still get many errors back, because of the dimensions of the Vektors.
Can someone help me whats my fault?

Answers (1)

Walter Roberson
Walter Roberson on 7 Jan 2020
Edited: Walter Roberson on 7 Jan 2020
then is not a valid MATLAB keyword in your if/then/else construct.
MATLAB comparison uses the == operator, not the = operator.
To test that three conditions all hold, use the && operator between them.
symbolic variables can never be used to index arrays. symsum(Fx(i),i,1,4) will not work. Try sum(Fx(1:4)) instead.
It is not obvious that your nonlinear constraint function has access to the variable Fx

3 Comments

Thank you very much.
I changed everything but its still not working.
You have another idea what else could be wrong?
What is your revised code?
So I rewrote some parts, now it looks like this
My problem looks like
objective = @z;
load parameter();
load Inputs;
x(1:4) = [FxW(1), FxW(2), FxW(3), FxW(4)];
x(5:8) = [alpha(1), alpha(2), alpha(3), alpha(4)];
%initial guess
x0=[0, 0, 0, 0, 0, 0, 0, 0];
%variable bounds
%case decision braking or driving backwards (decision input: flag=0 oder 1)
%braking mode
if flag == 1 %if it is possible the regenerative braking torque is the limit
if %if it is not possible to follow the target trajectory the maximum of the manual braking is used
Fx_v-sum((FxW(i)/cos(d(i)))-c_w(i)*sin(d(i))*alpha(i),(1:4)) == 0;
&& Fy_v-sum(Fy_v-sum(sin(d(i))*FxA(i)-cos(d(i))*c_w(i)*alpha(i),(1:4)) == 0;
&& Mz_v-sum(Mz(i),(1:4)) == 0;
% max. regenerative braking torque
lb(1:4) = [M_reg(1)/r_dyn, M_reg(2)/r_dyn, M_reg(3)/r_dyn, M_reg(4)/r_dyn]; %regenerative braking torque must be negative
% mechanical braking
else lb(1:4) = [FxB_max, FxB_max, FxB_max, FxB_max]; %FxB_max is negative
end % close conditions for braking loop
%driving mode
else lb(1:4) = [-P_max(:,1), -P_max(:,1), -P_max(:,1), -P_max(:,1)];
end
lb(5:8)= [d_min_vl-beta(:,1), d_min_vr-beta(:,2), d_min_hl-beta(:,3), d_min_hr-beta(:,4)]; %d and betato the left side are positive, to the right side are negative
ub = [P_max(:,1)/(r_dyn*w), P_max(:,2)/(r_dyn*w), P_max(:,3)/(r_dyn*w), P_max(:,4)/(r_dyn*w), d_max_vorne-beta(:,1), d_max_vorne-beta(;,2), d_max_hinten-beta(:,3), d_max_hinten-beta(:,4)];
%linear constraints
%Definition alpha
Aeq(1:5) = [0 0 0 0 1 0 0 0; 0 0 0 0 0 1 0 0; 0 0 0 0 0 0 1 0; 0 0 0 0 0 0 0 1; 0 0 0 0 0 0 0 0];
beq(1:5) = [d(1)-beta(1); d(2)-beta(2); d(3)-beta(3); d(4)-beta(4); 0];
% active stimulation for beta
%if k = 1 there is a stimulation
% k, j and n are Inputs, j and n are the actors stimulated
for k = 1
Aeq(5,j) = [1]; % target moduls
&& A(5,n) = [-1];
&& beq(5) = [delta_TaA]; % delta torque
end
%steering priority
f = 1:4;
if SOC(f) <= SOC_krit; % state of charge of one actor is really low, actor is only used for steering
A(1,f) = 1;
&& b(1) = 0;
else
A = [];
b = [];
%nonlinear constraints
nonlincon=@nlcon;
%optimize with fmincon
[x,fval,exitflag,output]=fmincon(objective,x0,A,b,Aeq,beq,lb,ub,@nonlcon);
then the objective function is
%the two objective functions
%case decision
i = 1:4;
if flag = 1; % decision braking or driving mode
S_1(i) = (A_i/SOC(i))*abs(FxW(i)); %driving mode
else
S_1(i) = ((B_i*M_reg(i))/SOC(i))*(1/FxW(i)); %braking mode
end
%delta steering angle
delta_d(i) = d_delay(i)-d(i); %d_delay in a seperate file or in simulink, equals d(t-1)
%minimizing the steering movement
S_2(i) = C_i*delta_d(i);
% sum of the three objectives
syms i
z_1 = sum(S_1,(1:4));
z_2 = sum(S_2,(1:4));
%objektive function
create function z = z_1 + z_2 ;
and my nonln constraints are
function[C,Ceq] = nlcon(FxW,alpha)
i = 1:4;
Fx_v(i) = u(1);
Fy_v(i) = u(2);
Mz_v(i) = u(3);
%Following the target trajectory ( equal for braking or driving mode)
Mz(i) = -w_vl*(cos(d(1))*FxW(1)-sin(d(1))*alpha(1)*c_w)+w_vr*(cos(d(2))*FxW(2)-sin(d(2))*alpha(2)*c_w)-w_hl*(cos(d(3))*FxW(3)-sin(d(3))*alpha(3)*c_w)+w_hr*(cos(d(4))*FxW(4)-sin(d(4))*alpha(4)*c_w)+l_v*(sin(d(1))*FxW(1)-cos(d(1))*alpha(1)*c_w+sin(d(2))*FxW(2)-cos(d(2))*alpha(2)*c_w)-l_h*(sin(d(3))*FxW(3)-cos(d(3))*alpha(3)*c_w+sin(d(4))*FxW(4)*c_w-cos(d(4))*alpha(4)*c_w);
Ceq(:,1) = Fy_v-sum(sin(d(i))*FxW(i)-cos(d(i))*c_w(i)*alpha(i),(1:4));
Ceq(:,2) = Mz_v-sum(Mz(i),(1:4));
Ceq(:,3) = Fx_v-sum((FxW(i)/cos(d(i)))-c_w(i)*sin(d(i))*alpha(i),(1:4));
the file integrated in simulink with all the inputs is
function y = fcn(u)
%definition of vehicle forces and torque
u(1) = Fx_v;
u(2) = Fy_v;
u(3) = Mz_v;
%definition of current torque limits
i = 1:4;
u(4) = P_max(i);
u(5) = M_reg(i);
%active stimulation Input
u(6) = k;
u(7) = delta_T_aA;
u(8) = beta(i);
u(9) = w(i);
u(10) = d_delay(i);
u(11) = r_dyn;
u(12) = SOC(i);
y = @myfun;
Does ist look correct and can you tell me how I will get the Inputs u into my other files ? Its not working with @
If you can give me some advise thx a lot.

Sign in to comment.

Categories

Asked:

on 7 Jan 2020

Commented:

on 9 Jan 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!