## 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.

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)

1. which approximation is better, this one or the one suggested by conopt manual for max approx?

this is the one I'm talking about:

for max(f(x),g(y)),
(f(x)+g(y)+SQRT(SQR(f(x)-g(y)) + SQR(delta)))/2

thanks.

2. The behavior near the kink is slightly different. E.g. F(x) = abs(x) = max(-x,x). Then for x=0, y=-x we have (f(x)+g(y)+SQRT(SQR(f(x)-g(y)) + SQR(delta)))/2 = sqr(delta)/2. If you want to require F(0) = 0 then the version y + (x-y)*0.5*(1+tanh(k*(x-y))) may be more appropriate as this is 0 for x=0, y=-x.

See http://yetanothermathprogrammingconsultant.blogspot.com/2008/09/smooth-approximations.html