Inequality Indexing using Optimization Toolbox

4 Ansichten (letzte 30 Tage)
Samuel L. Polk
Samuel L. Polk am 8 Okt. 2020
Bearbeitet: Matt J am 8 Okt. 2020
I would like to optimize an objective function with a constraint that is based on logical indexing. However, it seems like I can't just treat the optimization variable as I would a regular double. I was hoping for some advice about how to structure the constraint.
The code I have is written below. The variables A and B are n x 1 doubles and c is a scalar. So, I want to sum the values in A corresponding to indices for which the optimization variable x is greater than B.
x = optimvar('x',n); % Creates optimization variable
objfun = sum(x); % Creates objective function
constraint = sum(A(x>=B))>=c; % Constraint based on logical indexing
Thank you!
  1 Kommentar
Abdolkarim Mohammadi
Abdolkarim Mohammadi am 8 Okt. 2020
The error says all about it:
Unable to use a value of type optim.problemdef.OptimizationInequality as an index.
You cannot use OptimizationVariable objects for indexing. Such constraints should be defined using extra binary integer variables, so that when x>=B then the constraint is effective, and otherwise the constraint becomes redundant.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 8 Okt. 2020
Bearbeitet: Matt J am 8 Okt. 2020
What Abdolkarim is getting it is the following, I believe. It requires known upper bounds U (vector or scalar) and lower bounds L on x.
x = optimvar('x',n,'Lower',L,'Upper',U);
e = optimvar('e',n,'Lower',0,'Upper',1,'type','integer');
Con.constr1 = e>=(x-B)./(U-B)/2; % x>B ==> e=1
Con.constr2 = e<=1-(B-x)./(B-L)/2; % x<B ==> e=0
Con.constr3 = A(:).'*e>=c;
prob=optimproblem('Objective',sum(x),'Constraints', Con)

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with Optimization Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by