필터 지우기
필터 지우기

Extracting values from optimization variable and setting constraints

조회 수: 7 (최근 30일)
J B
J B 2024년 4월 19일
댓글: J B 2024년 4월 20일
I have a basket of 20 products with baseline quantities E.g., 40 Tomatoes, 15 Onions, 35 Apples etc. Standard linear optimization with the objective being maximizing the value of the basket subject to constraints on the total weight of the basket, total number of items etc.
The optimization variable QTY is used by the optimizer to adjust the baseline quantity for each product. QTY can take integer values of -15 to +15, i.e., it can increase or reduce baseline amounts of each product by 5 units.
For reasons outside the scope of this question, I need to limit the total number of adjustments it makes across all products, i.e., the sum of the absolute value of QTY <= 30. So it can't reduce Tomatoes and Onions by 10 each but increase Apples by 15.
How do I place this constraint?
The ABS function can't access the values in QTY. Adding the constraint on the squared value of QTY gives me a "nonlinear problem" error. Assigning values in QTY to another variable doesn't work since it just aliases. Various other attempts have yielded the same "nonlinear" error. I've been able to make it work in the past by setting 2 optimization variables - QTYUP and QTYDOWN but that becomes clunky.
Current thought is to find a way to extract the values in QTY into a temporary variable, calculate the sum of absolute values, and go. But I haven't found a straightforward way to this.
Thanks.

채택된 답변

John D'Errico
John D'Errico 2024년 4월 19일
편집: John D'Errico 2024년 4월 19일
Are you saying you want to constraint the sum of absolute values? That is, in context of a LINEAR programming problem?
This is done via the creation of slack variables. (That is, you must split the problem into two variables for each of your variables.) That is essentially what you said seems to be a clunky solution. If you want to use an LP formulation, that is your option, and the only way to transform the problem into one an LP can handle.
Such is life. Things are sometimes clunky looking.
  댓글 수: 1
J B
J B 2024년 4월 19일
Yeah, I was hoping there was a clean way to access the values in the optimization variable itself and extract them to create a constraint. But sounds like there isn't and I need to back to the old solution. Thanks for confirming.

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

추가 답변 (1개)

Torsten
Torsten 2024년 4월 19일
Defining vectors QTYup and QTYdown as integer vectors with 0<=QTYup(i)<=5 and -5<=QTYdown(i)<=0 (i=1,...,3 for tomatoes, onions and apples in the above case) and using "intlinprog" to solve is the way to go in your case.
  댓글 수: 3
Torsten
Torsten 2024년 4월 19일
To me, this solution looks very elegant, not clunky - and you remain in the realm of linear programming (ok, integer linear programming).
J B
J B 2024년 4월 20일
Yes, somewhere along the line I internalized fewer lines of code is better. Forever chasing that mythic goal even when it may not be relevant. I just reimplemented and it works fine, with the added bonus of allowing slightly more fine tuned controls in a couple of areas. Thanks again.

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

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by