How to solve "Conversion to logical from optim.prob​lemdef.Opt​imizationI​nequality is not possible." for logical statement for variable.

29 visualizaciones (últimos 30 días)
I have tried to find the optimal location (D) to install the renewables on the contact wire of the railway in which the lowest line losses are produced and lowest power from the grid is required (Pgrid). The objective function is to minimize sum(Pgrid). The error occured while calculated the contact wire loss (function: CW_calculation_1) due to logical operations of variable D.
Contact wire losses are calculated according to train location (left or right of D, train travels from left to right in this example).
xa(i) is train location at time i (Nx1vector), N is the number of time step. Ptr and Pres are parameters (Nx1 vector).
I have read all questions and answers in this community but still could not solve this problem. Is there any way to solve this issue? Please kindly advise. Thanks.
% Main optimization
% Decision variables
Pgrid = optimvar('Pgrid',N);
D = optimvar('D','LowerBound',0,'UpperBound',Dts);
% Minimize Contact wire loss
prob.ObjectiveSense = 'minimize';
prob.Objective = sum(Pgrid*DeltaT)
% Satisfy power load with power from PV, grid and battery
prob.Constraints.loadBalance = Pres + Pgrid == Ptr+CW(Ptr,Pres,Itr,Ires,xa,Dts,D,N,r);
Function CW below:
for i=1:1:N
if A>0 %case2.1
if D-xa(i)>=0 % this creates error.
%calculate losses while train is on the left-hand side of D
B=optimexpr(N);
B(i)=10*xa(i);
elseif xa(i)-D>=0
%%calculate losses while train is on the right-hand side of D
C=optimexpr(N);
C(i)=10*(xa(i)-D);
end
elseif A<0
do something
end
CW=B+C;
end
Below is the error I encountered:
Conversion to logical from optim.problemdef.OptimizationInequality is not possible.
Error in CW_calculation_1 (line 30)
if D-xa(i)>=0

Respuestas (1)

Alan Weiss
Alan Weiss el 9 de Mzo. de 2022
You need to take care of logical constraints using a roundabout sort of modeling construct. The documentation is here: Integer and Logical Modeling
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
  2 comentarios
Nakaret Kano
Nakaret Kano el 13 de Mzo. de 2022
Hi Alan, Thank you for your answer. I have tried to model based on the Big-M formulation but I still get the same error. I used the following Express logical constraints:
My main code is:
prob = optimproblem;
M=27;
% Decision variables
Pgrid = optimvar('Pgrid',N);
Dres = optimvar('Dres','LowerBound',0,'UpperBound',Dts);
%Binary variables
z = optimvar('z',N,'Type','integer','LowerBound',0,'UpperBound',1);
z1= optimvar('z1',N,'Type','integer','LowerBound',0,'UpperBound',1);
% Minimize Contact wire loss
prob.ObjectiveSense = 'minimize';
prob.Objective = sum(Pgrid*DeltaT)+sum(z+z1);
% Constraints for logical statements big-M
prob.Constraints.cons1 = Dres-xa <= M*z;
prob.Constraints.cons2 = Dres-xa >= -M*z1;
prob.Constraints.cons3 = z+z1 ==1 ;
% Satisfy power load + losses with power from RESs and grid
prob.Constraints.loadBalance = Pres + Pgrid == Ptr+CW_calculation_2_short(Ptr,Pres,Itr,Ires,xa,Dts,Dres,N,r,z,z1);
% Solve the linear program
options = optimoptions(prob.optimoptions,'Display','none');
[value,sol,fval] = solve(prob,'Options',options);
And code for function CW_calculation_2_short is:
function [Pcw_total]=CW_calculation_2_short(Ptr,Pres,Itr,Ires,xa,Dts,Dres,N,r,z,z1)
%% Create Zeros vector for Contact wire for each scenario
Pcw1=zeros(N,1); Pcw5=zeros(N,1);Pcw9=zeros(N,1);Pcw13=zeros(N,1);
Pcw2=zeros(N,1);Pcw6=zeros(N,1);Pcw10=zeros(N,1);Pcw14=zeros(N,1);
Pcw3=zeros(N,1);Pcw7=zeros(N,1);Pcw11=zeros(N,1);Pcw15=zeros(N,1);
Pcw4=zeros(N,1);Pcw8=zeros(N,1);Pcw12=zeros(N,1);
Pcw_total=zeros(N,1);
%% Calculate Contact wire losses
for i=1:1:N
if Pres(i)>0 && Ptr(i)>0 && Pres(i)-Ptr(i)>0 %case2.1
%calculate losses while train is on the left-hand side of Dres
if Dres-xa(i)>=0
Pcw3=optimexpr(N);
Pcw3(i)=Itr(i)^2*r*(Dres-xa(i));
Pcw4=optimexpr(N);
Pcw4(i)=(Ires(i)-Itr(i))^2*r*(Dts-Dres);
%%calculate losses while train is on the right-hand side of Dres
elseif Dres-xa(i)<=0
Pcw5=optimexpr(N);
Pcw5(i)=Ires(i)^2*r*(xa(i)-Dres);
Pcw6=optimexpr(N);
Pcw6(i)=(Ires(i)-Itr(i))^2*r*(Dts-xa(i));
end
end
Pcw_total=optimexpr(N);
Pcw_total=Pcw3+Pcw4+Pcw5+Pcw6;
end
Please kindly advise on this issue. Thanks.
Alan Weiss
Alan Weiss el 13 de Mzo. de 2022
I have not gone through all your code in detail, but one line struck me as being inadmissible:
if Pres(i)>0 && Ptr(i)>0 && Pres(i)-Ptr(i)>0 %case2.1
As I tried to explain briefly before, you cannot have this kind of line in your code. The strightforward test: if you are using an if statement with optimization variables, then you did it wrong.
It is difficult to give straightforward recipes for converting code from "if" statement form to "logical model" form. I tried to give the building blocks in Integer and Logical Modeling. But it is easy to get confused and not be sure how to proceed.
You need to create binary variables tied to your real optimization variables, and use the binary variables to encapsulate the "if" constraints.
You have a few other lines of code that are not sensible. For example, in a loop on i you have
Pcw4=optimexpr(N);
Pcw4(i)=(Ires(i)-Itr(i))^2*r*(Dts-Dres);
The line Pcw4=optimexpr(N) zeros out the Pcw4 variable, and so every time through the loop this command erases whatever came before. This leads to another rule of thumb: if you have an optimexpr line in a loop, you did it wrong. Use optimexpr the same way you would use zeros: to initialize a variable to zeros of the right size before running a loop, not to zero out variables in the middle of a loop.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

Iniciar sesión para comentar.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by