User Tools

Site Tools


gams:model_an_absolute_value_in_a_linear_model

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;
gams/model_an_absolute_value_in_a_linear_model.txt · Last modified: 2008/10/29 11:34 by support