How to solve absolute problem in optimization
이전 댓글 표시
Hello, i want to solve a quadratic optimization problem (prob.Objective =sum((PgridV).^2);), in constrains i have a one variable where i need to find the absolute value of (Pb1dc). so i introduce a variable called 'K' . where -K<=Pb1dc<=K;
- FYI: i have used absoulte value in the following constrains (prob.Constraints.loadBalanceAC=Pb1==Pb1dc-(0.05*K);)
MATLAB solution showing exactely whati want . its working well.
However, when i want to chage the objective function with little modifcation (prob.Objective =sum((PgridV-M).^2);) where M is a reference signal (mean of Pload), when i run the simulation
absolute vaules (K) are not getting exactley what mean for. its showing random values ( K = absolute ((Pb1dc))
clc
clear all
Pload=[0;1;3;4;2;6;9;10;2;4]; % load
N=10;
M=mean(Pload)+zeros(N,1);
Einit1=0.5; % initial energy
E=zeros(N,1);
Emin1 = 0; % mini energy
Emax1 = 3;
dt=1;
prob = optimproblem;
PgridV = optimvar('PgridV',N,'LowerBound',0,'UpperBound',20); % grid power
Pb1= optimvar('Pb1',N,'LowerBound',-1,'UpperBound',1); % ac power
Pb1dc= optimvar('Pb1dc',N,'LowerBound',-1,'UpperBound',1); % dc power
K=optimvar('K',N,'LowerBound',0);% absolute of (Pb1dc)
EbattV1 = optimvar('EbattV1',N,'LowerBound',Emin1,'UpperBound',Emax1); % energy
prob.ObjectiveSense = 'minimize';
% prob.Objective =sum(K.^2);
%prob.Objective =sum((PgridV).^2);
prob.Objective =sum((PgridV-M).^2);
% 1 constrains
prob.Constraints.energyBalance = optimconstr(N);
prob.Constraints.energyBalance(1) = EbattV1(1) == Einit1-Pb1dc(1)*dt; %Its Ploss is constanat
prob.Constraints.energyBalance(2:N-1) = EbattV1(2:N-1) == EbattV1(1:N-2)-Pb1dc(2:N-1)*dt;
prob.Constraints.energyBalance(N) = EbattV1(N) ==Einit1;
% K constrain for Pb1dc modulous
prob.Constraints.kbalance1=optimconstr(N);
prob.Constraints.kbalance1(1:N)=-K(1:N)<=Pb1dc(1:N);
prob.Constraints.kbalance2=optimconstr(N);
prob.Constraints.kbalance2(1:N)=Pb1dc(1:N)<=K(1:N);
% load Balance
prob.Constraints.loadBalance=PgridV+Pb1==Pload;
% loss term
prob.Constraints.loadBalanceAC=Pb1==Pb1dc-(0.05*K);
options = optimoptions(prob.optimoptions,'Display','final');
% options = optimoptions(prob.optimoptions,'Algorithm','interior-point');
[values,fval,exitflag] = solve(prob,'Options',options)
% Parse optmization results
if exitflag <= 0
PgridV = zeros(N,1);
Pb1 = zeros(N,1);
Pb1dc = zeros(N,1);
EbattV1 = zeros(N,1);
K = zeros(N,1);
else
PgridV = values.PgridV ;
Pb1 = values.Pb1;
Pb1dc = values.Pb1dc
EbattV1 = values.EbattV1;
K = values.K
end
.
댓글 수: 4
Torsten
2023년 6월 8일
Can you show in your code above exactly what you mean ?
I ran the code with
prob.Objective =sum((PgridV-M).^2);
instead of
prob.Objective =sum((PgridV).^2);
(see above). What is not satisfactory with the results ?
Jayachandra Malavatu
2023년 6월 8일
Jayachandra Malavatu
2023년 6월 8일
답변 (1개)
Jayachandra Malavatu
2023년 6월 8일
0 개 추천
댓글 수: 15
Jayachandra Malavatu
2023년 6월 8일
Torsten
2023년 6월 8일
Yes, it's easily seen from the output of your program (see above) that the constraints are satisfied:
abs(Pb1dc) <= K
Torsten
2023년 6월 8일
fun = @(x,y) x-abs(y);
prob.Constraints.kbalance1=optimconstr(N);
prob.Constraints.kbalance1(1:N)=fcn2optimexpr(fun,K,Pb1dc)==0;
Jayachandra Malavatu
2023년 6월 8일
Yes, add an initial guess for your solution variables in the call to "solve".
See the second example under
on how to do this.
Jayachandra Malavatu
2023년 6월 8일
Torsten
2023년 6월 8일
Without using fcn2optimexpr, you could add the constraints
K.^2 - Pb1dc.^2 == 0
with K constrained to be >=0.
Jayachandra Malavatu
2023년 6월 8일
Torsten
2023년 6월 8일
is this something correct ?
Yes. Is "lsqnonlin" chosen as solver ?
Jayachandra Malavatu
2023년 6월 8일
Jayachandra Malavatu
2023년 6월 8일
clc
clear all
Pload=[0;1;3;4;2;6;9;10;2;4]; % load
N=10;
M=mean(Pload)+zeros(N,1);
Einit1=0.5; % initial energy
E=zeros(N,1);
Emin1 = 0; % mini energy
Emax1 = 3;
dt=1;
prob = optimproblem;
PgridV = optimvar('PgridV',N,'LowerBound',0,'UpperBound',20); % grid power
Pb1= optimvar('Pb1',N,'LowerBound',-1,'UpperBound',1); % ac power
Pb1dc= optimvar('Pb1dc',N,'LowerBound',-1,'UpperBound',1); % dc power
K=optimvar('K',N,'LowerBound',0);% absolute of (Pb1dc)
EbattV1 = optimvar('EbattV1',N,'LowerBound',Emin1,'UpperBound',Emax1); % energy
prob.ObjectiveSense = 'minimize';
% prob.Objective =sum(K.^2);
%prob.Objective =sum((PgridV).^2);
prob.Objective =sum((PgridV-M).^2);
% 1 constrains
prob.Constraints.energyBalance = optimconstr(N);
prob.Constraints.energyBalance(1) = EbattV1(1) == Einit1-Pb1dc(1)*dt; %Its Ploss is constanat
prob.Constraints.energyBalance(2:N-1) = EbattV1(2:N-1) == EbattV1(1:N-2)-Pb1dc(2:N-1)*dt;
prob.Constraints.energyBalance(N) = EbattV1(N) ==Einit1;
% K constrain for Pb1dc modulous
prob.Constraints.kbalance1=optimconstr(N);
prob.Constraints.kbalance1=K.^2-Pb1dc.^2==0;
% load Balance
prob.Constraints.loadBalance=PgridV+Pb1==Pload;
% loss term
prob.Constraints.loadBalanceAC=Pb1==Pb1dc-(0.05*K);
x0.PgridV=3+zeros(N,1);
x0.Pb1=1+zeros(N,1);
x0.Pb1dc=1+zeros(N,1);
x0.K=1+zeros(N,1);
x0.EbattV1=1.5+zeros(N,1);
options = optimoptions(prob.optimoptions,'Display','final');
% options = optimoptions(prob.optimoptions,'Algorithm','interior-point');
[values,fval,exitflag] = solve(prob,x0,'Options',options)
% Parse optmization results
if exitflag <= 0
PgridV = zeros(N,1);
Pb1 = zeros(N,1);
Pb1dc = zeros(N,1);
EbattV1 = zeros(N,1);
K = zeros(N,1);
else
PgridV = values.PgridV ;
Pb1 = values.Pb1;
Pb1dc = values.Pb1dc
EbattV1 = values.EbattV1;
K = values.K
end
Jayachandra Malavatu
2023년 6월 8일
Torsten
2023년 6월 8일
Yes. Nonlinear constraints in lsqnonlin were introduced recently.
카테고리
도움말 센터 및 File Exchange에서 Linear Least Squares에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

