Wednesday, August 27, 2008

Smooth approximation for ifthen()

The construct ifthen( x1 > x2, y, z) can be approximated by the smooth function:

z + (y-z)*0.5*(1+tanh(k*(x1-x2)))

where k is a large constant (e.g. k=1000). This will allow you to use NLP and CNS model types instead of DNLP.

See also: http://en.wikipedia.org/wiki/Heaviside_step_function




The logistic function (http://en.wikipedia.org/wiki/Logistic_function) can be expressed in two ways:
  1. f(x,k) = (1+tanh(k·x))/2
  2. f(x,k) = 1/(1+exp(-2k·x))

The exp() version can overflow more easily. Newer versions of GAMS implement a version of exp() that does not overflow (by truncation) so either choice should work.

This approximation can be used also for max(x,y) and min(x,y) as we have:

  1. max(x,y) = ifthen(x>y, x, y)
  2. min(x,y) = ifthen( y>x, x, y)