How to solve "Conversion to logical from optim.problemdef.OptimizationInequality is not possible." for logical statement for variable.
22 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Alan Weiss
on 9 Mar 2022
You need to take care of logical constraints using a roundabout sort of modeling construct. The documentation is here: Integer and Logical Modeling
An example here: Optimal Dispatch of Power Generators: Problem-Based
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
2 Comments
Alan Weiss
on 13 Mar 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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!