Clear Filters
Clear Filters

Extracting values from optimization variable and setting constraints

9 views (last 30 days)
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.

Accepted Answer

John D'Errico
John D'Errico on 19 Apr 2024
Edited: John D'Errico on 19 Apr 2024
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 Comment
J B on 19 Apr 2024
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.

Sign in to comment.

More Answers (1)

Torsten on 19 Apr 2024
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.
Torsten on 19 Apr 2024
To me, this solution looks very elegant, not clunky - and you remain in the realm of linear programming (ok, integer linear programming).
J B on 20 Apr 2024
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.

Sign in to comment.


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!