User Tools

Site Tools


This is an old revision of the document!

How do I model an absolute value in a linear model?

You can not put an absolute term for a variable directly into a linear model. The model fragment below will not work:

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;

You have to introduce two positive variables xpos(j) and xneg(j), and replace:

  • abs(x(j)) = xplus(j) + xneg(j)
  • x(j) = xplus(j) - xneg(j)

Thus the correct model fragment is:

positive variable xplus(j), xneg(j);
obj..   z=e=sum(j, xplus(j) + xneg(j));
cons(i)..   sum(j, a(i,j)*(xplus(j) - xneg(j))) =l= b(i);

model foo /all/;
solve foo minimizing z using lp;
IMPRESSUM / LEGAL NOTICEPRIVACY POLICY gams/model_an_absolute_value_in_a_linear_model.1225280079.txt.gz · Last modified: 2008/10/29 12:34 by support