MATLAB Answers

0

How to use the decision variables of the optimization problem in if else statement

kelly Tang さんによって質問されました 2019 年 2 月 27 日
最新アクティビティ kelly Tang さんによって 編集されました 2019 年 2 月 28 日
I got the error that 'Conversion to logical from optim.problemdef.OptimizationConstraint is not possible.' when I am trying to use the mixed integer optimization. Though I have seen some answers of the similar questions, I still have problems with my code. I have variables ll and ul which are single integers, eg. ll=12, ul=17. And I have a function v(:,j) that is related to the two variables. The function is
Picture1.png
The original code is as follows:
ll=optimvar('ll','Type','integer','LowerBound',0,'UpperBound',20);
ul=optimvar('ul','Type','integer','LowerBound',0,'UpperBound',20);
v=1:20;
for j=1:20
if j>=ll && j<=ul
v_ba(1,j)=10000
else
v_ba(1,j)=v(1,j)
end
end
I somehow know that the big-M method should be applied here but I couldn't transfer it to the code.
For example I changed the code as follows and it still doesn't work. Can anyone explain me how to encode it specifically? Thanks very much.
ll=optimvar('ll','Type','integer','LowerBound',0,'UpperBound',20);
ul=optimvar('ul','Type','integer','LowerBound',0,'UpperBound',20);
v=1:20;
for j=1:20
j-ll>=0;
(j-ll)-M1*y1<=0;
(ul-j)-M2*y2<=0;
v_ba(:,j)=10000*(y1*y2)+v(:,j)*(1-y1*y2);
end

  0 件のコメント

サインイン to comment.

製品


リリース

R2018a

1 件の回答

回答者: Alan Weiss
2019 年 2 月 28 日

I do not understand your formulation. You have ll and ul as optimization variables, which is why you are getting the complaints that conversion of an optimization constraint to logical is not possible.
For an example that uses big-M formulation, see Mixed-Integer Quadratic Programming Portfolio Optimization: Problem-Based.
Alan Weiss
MATLAB mathematical toolbox documentation

  1 件のコメント

kelly Tang 2019 年 2 月 28 日
Thx for the reply. I have made some changes of the illustration in the question to make it more clear.
Let's say v is an 1x20 row vector that is given as [1 2 3 4.. 20]. While v_ba is 1x20 row vector and its element values depending on the variables ll and ul.
For example:
v_ba(1,ll:ul)=10000
v_ba(1,1:ll-1)=v
v_ba(1,ul+1:20)=v
hence
v_ba=[10000 10000 ... 11 12 13 ...10000 10000]
which is what the first formulation should be. Previously I am trying to use the loop to assign the values to the v_ba but apparently it doesn't work because of the logical conversion. So I convert the formulation to the Big-M constraints that
for j=1:20
j-ll>=0;
(j-ll)-M1*y1<=0 %where M1 is the upper bound of j-ll
(ul-j)-M2*y2<=0 %where M2 is the upper bound of j-ul
v_ba(:,j)=10000*(y1*y2)+v(:,j)*(1-y1*y2);
end
From my understanding, the above constraints implies that when y1=1, then j>=ll, and when y2=1, thenj<=ul, hence y1*y2 implies ll=<j<=ul which is the first condition in the first formulation. And (1-y1*y2) implies the second condition. So the v_ba is expressed as:
v_ba(i,j)=10000*(y1*y2)+v(:,j)*(1-y1*y2);
it still doesn't work. Did I use the big-M method correctly?

サインイン to comment.



Translated by