Optimization with many nonlinear Constraints
Show older comments
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
on 7 Jan 2020
Edited: Walter Roberson
on 7 Jan 2020
0 votes
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
Sarah Kern
on 7 Jan 2020
Walter Roberson
on 7 Jan 2020
What is your revised code?
Sarah Kern
on 9 Jan 2020
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!