GAMS Support Wiki

Site Tools

gams:model_an_absolute_value_in_a_linear_model

Differences

This shows you the differences between two versions of the page.

 gams:model_an_absolute_value_in_a_linear_model [2008/10/29 12:34]support gams:model_an_absolute_value_in_a_linear_model [2020/05/19 06:58]Frederik Fiand Both sides previous revision Previous revision 2020/05/19 07:00 Frederik Fiand 2020/05/19 06:58 Frederik Fiand 2020/05/18 15:29 Frederik Fiand 2008/10/29 12:34 support 2007/11/06 21:14 Franz Nelissen 2007/11/06 21:12 Franz Nelissen 2007/11/06 21:06 Franz Nelissen 2007/11/06 21:03 Franz Nelissen 2007/11/06 21:03 Franz Nelissen created Next revision Previous revision 2020/05/19 07:00 Frederik Fiand 2020/05/19 06:58 Frederik Fiand 2020/05/18 15:29 Frederik Fiand 2008/10/29 12:34 support 2007/11/06 21:14 Franz Nelissen 2007/11/06 21:12 Franz Nelissen 2007/11/06 21:06 Franz Nelissen 2007/11/06 21:03 Franz Nelissen 2007/11/06 21:03 Franz Nelissen created Last revision Both sides next revision Line 1: Line 1: ===== How do I 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 ​ + You cannot ​put an absolute term for a variable directly ​ into a linear model. The model fragment below will **not** work: into a linear model. The model fragment below will **not** work: <​code>​ <​code>​ - obj..   ​z=e=sum(j, abs(x(j))); + [...] + obj..       ​z=e=sum(j, abs(x(j))); cons(i).. ​  ​sum(j,​ a(i,​j)*x(j)) =l= b(i); cons(i).. ​  ​sum(j,​ a(i,​j)*x(j)) =l= b(i); Line 11: Line 12: ​ - You have to introduce two positive variables xpos(j) and xneg(j), + Various error messages will be given: - and replace: + - * ''​abs(x(j)) =  xplus(j) + xneg(j)''​ + - * ''​x(j) = xplus(j) - xneg(j)''​ + - Thus the correct model fragment ​ is: + <​code>​ <​code>​ - positive variable ​xplus(j), xneg(j); + 14  solve foo minimizing z using lp; - obj..   ​z=e=sum(j, ​xplus(j) + xneg(j)); + ****                                 ​\$51,​256 - cons(i).. ​  ​sum(j, a(i,j)*(xplus(j) - xneg(j))) =l= b(i); + ****  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 + ​ + + 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>​ + [...] + 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/; model foo /all/;