Wednesday, September 3, 2008

Bug in ifthen (GAMS)

Consider the models:

   1  variable x;
2 equation e1,e2;
3
4 scalar s /1/;
5
6 e1.. x$(s>0) =e= 0;
7 e2.. ifthen(s>0,x,0) =e= 0;
8
9 model m1 /e1/;
10 solve m1 using cns;
11
12 model m2 /e2/;
13 solve m2 using cns;


The models are the same. The ifthen is exogenous but GAMS does not recognize this. As a result the fist model solves fine and the second model leads to numeric problems:

** Error in Square System: Pivot too small.

Turns out the gradients are not correct for ifthen:

---- e2  =E= 
e2.. (0)*x =E= 0 ; (LHS = 0)


Advice: don't use ifthen in equations as the derivatives of this function are wrong. If possible use $ conditions (exogenous if) or a smooth approximation (endogenous if). Smooth approximations for ifthen are discussed in this blog.