How do I model an absolute value in a linear model?
Moderator: aileen
Forum rules
Please ask questions in the other sub-forums
Please ask questions in the other sub-forums
How do I model an absolute value in a linear model?
How can I put an absolute term for a variable into a linear model?
Re: How do I model an absolute value in a linear model?
You cannot put an absolute term for a variable directly into a linear model such as LP or MIP. The model fragment below will not work:
Various error messages will be given:
Instead of using the abs() function, you could introduce two positive variables xpos(j) and xneg(j) and substitute:
abs(x(j)) = xpos(j) + xneg(j)
x(j) = xpos(j) - xneg(j)
This reformulation splits the x(j) into a positive part xpos(j) and a negative part xneg(j). Note that this only works if abs(x(j)) is minimized, because in that case, either xpos(j) or xneg(j) is forced to zero in an optimal solution.
The reformulated model fragment is:
Code: Select all
[...]
obj.. z=e=sum(j, abs(x(j)));
cons(i).. sum(j, a(i,j)*x(j)) =l= b(i);
model foo /all/;
solve foo minimizing z using lp;
Code: Select all
14 solve foo minimizing z using lp;
**** $51,256
**** 51 Endogenous function argument(s) not allowed in linear models
**** 256 Error(s) in analyzing solve statement. More detail appears
**** Below the solve statement above
**** The following LP errors were detected in model foo:
**** 51 equation obj.. the function ABS is called with non-constant arguments
abs(x(j)) = xpos(j) + xneg(j)
x(j) = xpos(j) - xneg(j)
This reformulation splits the x(j) into a positive part xpos(j) and a negative part xneg(j). Note that this only works if abs(x(j)) is minimized, because in that case, either xpos(j) or xneg(j) is forced to zero in an optimal solution.
The reformulated model fragment is:
Code: Select all
[...]
positive variable xpos(j), xneg(j);
[...]
obj.. z=e=sum(j, xpos(j) + xneg(j));
cons(i).. sum(j, a(i,j)*(xpos(j) - xneg(j))) =l= b(i);
model foo /all/;
solve foo minimizing z using lp;