How to solve the problem about "Conversion to logical from optim.prob​lemdef.Opt​imizationC​onstraint is not possible!"

batteryNominalEnergy=optimvar('batteryNominalEnergy',1,'LowerBound',0,'UpperBound',20);
powerElectronicsRatedPower=optimvar('powerElectronicsRatedPower',1,'LowerBound',0,'UpperBound',10);
Ppv_load=optimvar('Ppv_load',simTime,1,'LowerBound',0,'UpperBound',9.9824e+03);
Ppv_batt=optimvar('Ppv_batt',simTime,1,'LowerBound',0);
Ppv_grid=optimvar('Ppv_grid',simTime,1,'LowerBound',0,'UpperBound',9.9824e+03);
Pcurtail=optimvar('Pcurtail',simTime,1,'LowerBound',0,'UpperBound',5000);
Pbatt_load=optimvar('Pbatt_load',simTime,1,'LowerBound',0);
Pbatt=Ppv_batt*Eta_inv-Pbatt_load/Eta_inv;
Esoh=optimexpr(simTime,1);
Esoh(1)=1;
if Pbatt(i-1)>=0 %% this line reports the arror
Esoh(i)=Esoh(i-1)-0.2*(sampletime/gvarYEARS2SECONDS/20+0.5*Pbatt(i-1)*sampletime/(batteryNominalEnergy*gvarKWH2WS*Esoh(i-1))/4500);
else
Esoh(i)=Esoh(i-1)-0.2*(sampletime/gvarYEARS2SECONDS/20-0.5*Pbatt(i-1)*sampletime/(batteryNominalEnergy*gvarKWH2WS*Esoh(i-1))/4500);
end
Hi, I have meet the problem in MATLAB using linprog. "Pbatt" can be positive or negative. when it is posive, the battery will be charging. And when it is negetive, the battery will be discharging. The equation above caculates the battery SOH whenever the battery is charging or discharging.
"Pbatt" is calculated from the optimization variables. But if I use "abs(Pbatt)", there will be an error that I can't use abs function.
Appreciate for your help!

18 Comments

I have a similar problem as yours, please let me know if you find a way to use the abs function, please share. I will do likewise when I find a way around this.
Hi. Recently I'm using another MATLAB tool box which is Yalmip. Maybe you can try it. Or you can try Matt's answer. I will try it, too. Then I will leave my conclusion right here.
how can i use the same Pbatt which is calculated from optimisation variables depending on its sign in objective function?
Like if Pbatt is positive i have to multiply with cost and if its negative i have to multiply it with another cost .How do i specify Pbatt in objective function accordingly ?
I am also getting the error
Conversion to logical from optim.problemdef.OptimizationConstraint is not possible.
i have used below code.
for i = 1:N
if PbattV == 200
Cost1 = Costa;
elseif PbattV == -200
Cost2 = Costb;
end
end
prob.Objective = dt*Cost1'*PbattV +Cost2'*PbattV;
Kindly help
Provided that the variable is not 0, then
sqrt(VARIABLE^2)/VARIABLE
is the sign of VARIABLE.
You can then add 1 and divide the result by 2 to get a value that is 0 when VARIABLE is negative, and 1 when the VARIABLE is positive. Multiply that by the difference between the two costs, and add the first cost.
(sqrt(VARIABLE^2)/VARIABLE + 1)/2 * (cost2-cost1) + cost1
Warning this will fail if abs(VARIABLE) < sqrt(realmin) (which is roughly 1.5E-154). It will definitely fail if VARIABLE is 0 exactly.
thank you so much , but can you please explain this in detail, i didnt understand the expression.
I have tried this below code also, but getting error :
r=optimvar('absPbattV',N,'LowerBound',0);
Cost = (PbattV<=r).*Cost 1+ (~PbattV>=r).*Cost2;
P1= dt*Cost'*PbattV;
P2=dt*Cost'*PbattV;
P=P1-P2;
An error occurred while running the simulation and the simulation was terminated
Caused by:
  • times
Please help
Cost = (PbattV<=r).*Cost 1+ (~PbattV>=r).*Cost2;
That kind of computation is simply not supported for Problem Based Optimization.
can you please explain this in detail
Mathematically, if X is real-valued, then X^2 is non-negative. sqrt() of a value is defined as the "principle" (non-negative) square root. So sqrt(X^2) is abs(X) provided that X is real.
Then the sign() function https://en.wikipedia.org/wiki/Sign_function can be defined as either X/abs(X) or abs(X)/X -- they are equivalent for real-valued X that is not 0. So sqrt(X^2)/X is sign(X) provided that X is real and non-zero.
So now you have an expression which is sign(X) and you want that when sign(X) < 0 you want cost1 and if sign(X) > 0 you want cost2. So take sign(X) which is going to be -1 or NaN or +1, and add 1 to it, which will give you 0 or NaN or +2. Divide the result by 2, which will give you 0 or Nan/2 (= NaN) or +1 . So (sign(X)+1)/2 takes us from sign(X) to 0 or 1 (or NaN). Put those together with the formula for sign(X) to get (sqrt(X^2)/X + 1)/2 is either 0, or NaN, or +1 depending on whether sign(X) < 0 or sign(X) > 0.
Now take that 0 or 1 and multiply it by (cost2-cost1) . The result will be either 0 or else (cost2-cost1) .
Take the result and add cost1 . 0 + cost1 -> cost1 . (cost2-cost1) + cost1 -> cost2.
So (sqrt(X^2)/X + 1)/2 * (cost2-cost1) + cost1 now takes us to cost1 if X < 0, and takes us to cost2 if X > 0 (and takes us to NaN if X is 0 exactly.)
I have used the expression Cost = (PbattV<=r).*Cost 1+ (~PbattV>=r).*Cost2; in the same context as used below:
if A > B
C = A;
else
C = B;
end
C = (A>B).*A + (~A>B).*B;
Can you please advice how i can remove the error for the below code .
r=optimvar('absPbattV',N,'LowerBound',0);
Cost = (PbattV<=r).*Cost 1+ (~PbattV>=r).*Cost2;
P1= dt*Cost'*PbattV;
P2=dt*Cost'*PbattV;
P=P1-P2;
An error occurred while running the simulation and the simulation was terminated
Caused by:
  • times
When i tried the code, i am getting the below error
An error occurred while running the simulation and the simulation was terminated
Caused by:
sqrt
Cost = (PbattV<=r).*Cost 1+ (~PbattV>=r).*Cost2;
There is no possibility of using a logical comparison multiplied by a value when you are using optimization variables.
I already explained that in detail.
When i used this code , (sqrt(VARIABLE^2)/VARIABLE + 1)/2 * (cost2-cost1) + cost1
i am getting the error
An error occurred while running the simulation and the simulation was terminated
Caused by:
sqrt
basically i need to check if Pbatt is positive or negative and accordingly i have to chose Cost and use it for objective function;how can i edit the code?It would be great help for me.
(sqrt(VARIABLE.^2)./VARIABLE + 1)/2 * (cost2-cost1) + cost1
I will need the rest of your code to test further than that.
N is the vector dimension 288
dt opt time set as 300
Ppv , load, Cost are 1*1 strut arrays with subfields (time(288*1) and signals(288*241) )
Please find the attached Matlab example
Unfortunately the repo you provided has an energyOptimizationScript that does not pass two Cost matrices to battSolarOptimize -- this is within EMS_Optimization_MATLAB_Only
The slx model you provided calls battSolarOptimize that is inside the Resources folder, and the MATLAB Function block that calls battSolarOptimize does not pass in two cost matrices.
Also, you do not define r anywhere.
Please also double-check: you were trying to do
Cost = (PbattV<=r).*Costa + (~PbattV>=r).*Costb;
but what do you want to have happen when PbattV is exactly equal to r ? Do you want it to use Costa or Costb ?
Be careful, also, ~A>=B does not mean ~(A>=B), it means (~A)>=B which means (A==0)>=B . If you were going to use logical masking like that (which you cannot do with optimzation variables) then you would have been better off with
Cost = (PbattV<=r).*Costa + ~(PbattV<=r).*Costb;
or
Cost = (PbattV<=r).*Costa + (PbattV>r).*Costb;
Anyhow, I tested with
Costa = Cost(:,1);
Costb = Cost(:,end) + randn(size(Costa))/20; %because I do not have a real Costb
r = 200;
%Cost = (PbattV<=r).*Costa+ (~PbattV>=r).*Costb;
Cost = (sqrt(PbattV.^2)./PbattV + 1)/2 .* (Costb-Costa) + Costa;
P1= dt*Cost'*PbattV;
P2=dt*Cost'*PbattV;
P=P1-P2;
and did not have any problems with the sqrt(): the code seemed to execute with no difficulty.
Sorry i repllaced r with 200 and checked..
When PbattV is exactly equal to r ? Do you want it to use Costa or Costb ? Cost a
Basically when Pbatt is positive Cost a and when negative Costb.
I put exactly the same code with sqrt , but i get error with sqrt :-(
Dont know why :-( Do i miss something
An error occurred while running the simulation and the simulation was terminated
Caused by:
  • sqrt
And when i tried with
Cost = (PbattV<=r).*Costa + ~(PbattV<=r).*Costb;
or
Cost = (PbattV<=r).*Costa + (PbattV>r).*Costb;
An error occurred while running the simulation and the simulation was terminated
Caused by:
  • times
Do i need to add r as constraint or variable ?
(sqrt(X^2)/X + 1)/2 * (cost2-cost1) + cost1 now takes us to cost1 if X < 0, and takes us to cost2 if X > 0 (and takes us to NaN if X is 0 exactly.)
The expression is giving Nan when X is 0, i am not sure if that gives the error for sqrt , can you please advice how can i avoid that Nan and it gives the answer 0 whenever it is Nan

Sign in to comment.

 Accepted Answer

Introduce variables r to represent bounds on Pbatt
r=optimvar('absPbatt',simTime1,'LowerBound',0);
prob.Constraints.r_upper=Pbatt<=r;
prob.Constraints.r_lower=Pbatt>=-r;
This is equivalent to abs(Pbatt)<=r. Now, add sum(r) to your objective
prob.Objective=prob.Objective+sum(r);
and finally
prob.Constraints.SOH = Esoh(2:end)==Esoh(1:end-1)-0.2*(sampletime/gvarYEARS2SECONDS/20+0.5*r(1:end-1)*sampletime/(batteryNominalEnergy*gvarKWH2WS*Esoh(1:end-1))/4500);

2 Comments

Thank you very much, Matt. I think your answer will help me. I will try it. Then I will leave futher solution about your answer.
Akinkunmi Adegbenro's comment relocated here:
Hi Matt J, please have a look at my question too. I am facing a similar challenge as Nicholas
I am trying copy the idea you shared earlier in your answer, but the last piece of code (i.e. prob.Constraints.SOH) would be different in my case. I am struggling to understand what you did here so I am unable to adapt your answer to my application.

Sign in to comment.

More Answers (0)

Asked:

on 15 Jul 2019

Commented:

NN
on 5 May 2021

Community Treasure Hunt

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

Start Hunting!