How to use the decision variables of the optimization problem in if else statement
4 views (last 30 days)
Show older comments
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
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 Comments
Answers (1)
Alan Weiss
on 28 Feb 2019
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
See Also
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!