Sign Function in AMPL

548 views
Skip to first unread message

Isha

unread,
Mar 5, 2011, 11:49:13 PM3/5/11
to AMPL Modeling Language
Hi,

I want to add sign function in a constraint where

sign(x)
returns 1 if x>0
-1 if x<0
0 if x=0

How can I do this in AMPL..Any idea???

Thanks,
Isha

Paul

unread,
Mar 6, 2011, 6:28:46 PM3/6/11
to am...@googlegroups.com
The signum function is discontinuous, so you cannot use it in a mathematical programming model (unless you are using a solver that can handle discontinuous functions). The usual workaround is to introduce binary variables, but that will not get you exactly what you want.

Let's assume that you know a priori lower and upper bounds L, U for x (with L < 0 < U). The following does almost what you want:

param L < 0;
param U > 0;
var x >= L <= U;
var pos binary;  # 1 if x is positive
var neg binary;  # 1 if x is negative
s.t. IsXPositive: x <= M*pos;
s.t. IsXNegative: x >= L*neg;
s.t. PickSign: pos + neg <= 1;

So if x is positive, pos will be 1 and neg will be 0; if x is negative, pos will be 0 and neg will be 1.  In those cases, the sign of x is pos - neg.  The bad news is that if x is zero, anything is possible: pos = 1, neg = 1 or both zero.

A partial workaround is:
param epsilon > 0;  # pick epsilon small, but remember that rounding error will make a really small epsilon ineffective
# change two of the constraints above
s.t. IsXPositive: x <= M*pos - epsilon*neg;
s.t. IsXNegative: x >= L*neg + epsilon*pos;

Now if x has an absolute value < epsilon, both pos and neg must be 0 (and so pos - neg is zero).  You're still stuck with "anything goes" if |x| < epsilon.

Paul

Harvey Greenberg

unread,
Mar 7, 2011, 2:33:11 PM3/7/11
to AMPL Modeling Language
Here's a thought. Add constraint x*v = x^2. Then, x>0 --> v=1, x<0 --
> v=-1, and x=0--> no constraint. To deal with
"forcing" v=0 in the last case, add nuisance cost to objective: c*z,
where z >= v an z >= -v (so z=abs(v) for c > 0).

Paul

unread,
Mar 7, 2011, 8:06:58 PM3/7/11
to am...@googlegroups.com
Harvey,

I assume you intend v to be a general integer variable between -1 and +1?  The constraint might not work with some (most?) solvers, because for fixed v it is not a convex <= inequality in x (and if the solver relaxes integrality on v, the constraint is even less tractable).  It might work with some global solvers, though.

Paul

Harvey Greenberg

unread,
Mar 7, 2011, 10:43:20 PM3/7/11
to AMPL Modeling Language
Sorry, I meant to write x*v = sqrt(x^2), and v is an otherwise
unconstrained variable. This yields the result: v = sqrt(x^2)/x for x!
=0, which = sgn(x). The stability issue is still there when x is near
0. I'm not sure what solvers do when the derivative is near
infinity. I guess instability can be disguised but not removed.

isha sharma

unread,
Mar 7, 2011, 10:48:14 PM3/7/11
to am...@googlegroups.com
Thank you everyone for your suggesions
-Isha

--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To post to this group, send email to am...@googlegroups.com.
To unsubscribe from this group, send email to ampl+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ampl?hl=en.




--
Isha Sharma
Reply all
Reply to author
Forward
0 new messages