# "Conversion to logical from optim.prob​lemdef.Opt​imizationE​quality is not possible" for below Code

조회 수: 10 (최근 30일)
Laxmi Akshaya Thela 2021년 7월 12일
편집: Laxmi Akshaya Thela 2021년 7월 14일
I need to optimize my battery SOC .My control variable is flywheel speed( w_FW) which should have the initial and final speed equal to 78.539.
State Variable are SOC,flywheel speed(w_FW).
1) How should I set constraints to optimization variable and some intermediate variables like Motor rating,w_FW and others.
2) Error : Conversion to logical from optim.problemdef.OptimizationEquality is not possible @if w_FW(i,1)==w(i,1)
3) How do I set value to Final Fywheel Speed(w_FW) as I am needed to maintain my initail anfd final flywheel speed values to be same i.e:78.539
Flywheel Speed(w_FW) is a vector of size (1436x1). 1436 is the number of secs in my driving cycle.
I will need to implement dynammic programing using back Propagation
Any help would be appreciated.
m=1200;
A_f=2;
rho=1.205;
g=9.8;
r=0.3;
f=0.01;
C=0.3;
delta_1 =0.04;
delta_2=0.017;
i_fd=3.29;
i_1=1;
eff_tr=0.95;
delta=1+(delta_1)+(delta_2)*(i_fd)^2*(i_1)^2;
T_whl(1:1436,1)=((delta.*m).*(a(1:1436,1))+0.5.*C.*A_f.*rho.*v(1:1436,1).^2+m.*g.*f).*r
w(1:1436,1)=v(1:1436,1)./r
k_1=2;
k_2=4;
J_Fw=0.5;
P_rated=170000;
Q_new=(P_rated*108)/0.94;
Pm_rated=zeros(length(v),1);
w_m=zeros(length(v),1);
prob=optimproblem("ObjectiveSense",'min')
soc_min=0.2
soc_max=0.8
w_FW_min=0
w_FW_max=2303
w_m_min=0
w_m_max=628
Pm_rated_min=0
Pm_rated_max=170000
%I am not sure whether my flywheel could be considered optimization
%variable
soc=optimvar('soc',1436,1,"LowerBound",soc_min,"UpperBound",soc_max)
w_FW=optimvar('w_FW',1436,1,"LowerBound",w_FW_min,"UpperBound",w_FW_max)
% w_m=optimvar('w_m',1436,1,"LowerBound",w_m_min,"UpperBound",w_m_max)
% Pm_rated=optimvar('Pm_rated',1436,1,"LowerBound",Pm_rated_min,"UpperBound",Pm_rated_max)
prob.Constraints.cons1=w_FW>=w_FW_min
prob.Constraints.cons2=w_FW<=w_FW_max
prob.Constraints.w_FW_last=w_FW(1436,1)==78.539%I am not able to set the final flywheel speed value=78.593 as I can see that it is not available in workspace
for i=1:1:1436
if v(i+1,1)>=v(i,1)
T_m(i,1)=T_whl(i,1)./(k_1.*3.1255)
elseif v(i+1,1)<v(i,1)
T_m(i,1)=(T_whl(i,1).*0.95)./(k_1.*3.29)
end
% w_ir(i,1)=k_1.*w_v(i,1)
% w_or(i,1)=w_FW(i,1)./k_2
if v(i+1,1)>=v(i,1)
if w_FW(i,1)==w(i,1) % ERROR:Conversion to logical from optim.problemdef.OptimizationEquality is not possible.
%soc(i-1,1)=soc(i,1)
diff_soc(i,1)=0
elseif w_FW(i,1)<=w(i,1)
w_FW(i+1,1)=(w_FW(i,1).*2)-T_m(i,1)
elseif w_FW(i,1)>=w(i,1)
w_FW(i+1,1)=(w_FW(i,1).*2)-T_m(i,1)
end
end
if v(i+1,1)<v(i,1)
if w_FW(i,1)==w(i,1)
%soc(i-1,1)=soc(i,1)
diff_soc(i,1)=0
elseif w_FW(i,1)<=w(i,1)
if soc(i,1)<0.8
%soc(i-1,1)=soc(i,1)+diff_soc(i-1,1)
w_FW(i+1,1)=w_FW(i,1)
elseif soc>=0.8
diff_soc=0
end
elseif w_FW(i,1)>=w(i,1)
w_FW(i+1,1)=J_FW.*w_FW(i,1)-T_m(i,1)
end
end
if v(i+1,1)>=v(i,1)
w_FW(i+1,1)=w_FW(i,1)
else v(i+1,1)<v(i,1)
w_FW(i+1,1)=w_FW(i,1)
end
soc(i+1,1)=soc(i,1)
diff_soc=0
w_FW(i+1,1)=w_FW(i,1)
w_m=(w_FW(i,1)./k_2)-(k_1.*w(i,1))
prob.Constraints.cons3=w_m>=w_m_min
prob.Constraints.cons4=w_m<=w_m_max
Pm_rated=T_m(i,1).*w_m(i,1)
prob.Constraints.cons5=Pm_rated>=Pm_rated_min
prob.Constraints.cons6=Pm_rated<=Pm_rated_max
% if v(i,1)>=v(i-1,1)
% diff_soc(i-1,1)=Pm_rated(i,1)./(Q_new.*0.85.*0.94)
% elseif v(i,1)<v(i-1,1)
% diff_soc(i-1,1)=Pm_rated(i,1).*0.85.*0.94/(Q_new)
% end
% soc(1,1)=0.6
end
prob.Objective=sum(diff_soc,'all')
%write(prob.Objective,'objectivedescription.txt')
x0.w_FW=78.539;
x0.SOC=0.6;
[sol,fval]=solve(prob,x0)
for i=1:1:1436
soc(i+1,1)=soc(i,1)+change_soc(i,1)
end

댓글을 달려면 로그인하십시오.

### 채택된 답변

Walter Roberson 2021년 7월 12일
prob.Constraints.w_FW_last=w_FW(1436,1)==78.539 % I don't see where the value is being set anywhere in the workspace
Remember that == is bit-for-bit equality, but when you give the literal 78.539 you are approximating as the closest representable number. If you want exactly 78.539 then you would be safer testing
prob.Constraints.w_FW_last=w_FW(1436,1)*1000==78539
Anyhow, you have
w_FW=optimvar('w_FW',1436,1,"LowerBound",w_FW_min,"UpperBound",w_FW_max)
so w_FW is a vector of optimization variables.
if w_FW(i,1)==w(i,1) % Error (Conversion to logical from optim.problemdef.OptimizationEquality is not possible.)
You cannot test optimization variables in if statements. The only tests permitted for them are in setting the constraints. optimization variables are similar to symbolic variables, in that inside the workspace, they do not have specific values and so cannot be tested.
##### 댓글 수: 2없음 표시없음 숨기기
Laxmi Akshaya Thela 2021년 7월 12일
The mentioned Flywheel Speed(w_FW) is not the optimization variable I dont know how to set the constraints.Coud you please suggest the exact way how to mention the constraints.
The code written for each if block defines the mode of operation of the vehicle.
I actually need to implement recursion for this but I dont know exactly how to do that.
Any suggetsion related to this would be very helpful.Thank you
Walter Roberson 2021년 7월 12일
w_FW=optimvar('w_FW',1436,1,"LowerBound",w_FW_min,"UpperBound",w_FW_max)
Looks like an optimization variable to me.
prob.Constraints.cons1=soc>=soc_min
That is a valid examine of setting constraints.
while(true)
Two of your while loops are while(true) but neither of them have a break() statement. They will continue indefinitely.
while(T_m==0 & w_m(i-1,1)==w_m(i,1))
soc(i-1,1)=soc(i,1)
diff_soc=0
w_FW(i-1,1)=w_FW(i,1)
end
You have a condition on the while loop, so the body might not be executed at all. If the condition is true the first time, then in order for the loop to stop, then either the test needs to be non-deterministic (random numbers), or else at least one of the variables involved in the test needs to be modified. You do not modify T_m or w_m(-i1,1) or w_m(i,1) anywhere inside the loop, so if the body of the loop gets executed at all, it is going to keep going indefinitely.
MATLAB offers two different methods of optimization: Problem Based, and Solver Based.
For Problem Based Optimization, you set up systems of variables and their relationships, and constraints, and then you tell MATLAB to go ahead and solve it using whatever method it feels is appropriate. MATLAB will examine the nature of the calculations and the constraints, and will choose the most-specific solver algorithm that applies. It will automatically translate the system of variables and constraints into inputs usable by the particular solver algorithm it chooses.
When you use Problem Based Optimization, much of the time you do not invoke any user-written functions. It is sometimes possible to import user-written functions for use with Problem Based Optimization, but not when they have conditions that are based upon the "current" values of the variables. So looping a fixed number of iterations might be okay, but not looping until error is less than a threshold, for example.
For Solver Based Optimization, you choose a particular solver and you work out all necessary parameter matrices (such as building up linear inequality matrices). You program an objective function, and possibly a nonlinear constraints function as well. The objective function and nonlinear constraint function can use any MATLAB libraries they need, including iteration until a tolerance, or even including calling out to an external program to do calculations. Solver Based is more flexible as to what you can do, but it is more difficult to set up.
I suspect that you are indicating that you need to use the Solver-Based approach.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Multiobjective Optimization에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by