## Extracting values from optimization variable and setting constraints

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.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. 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. optimization, constraints MATLAB Answers — New Questions