Linear Programming - variable that equals the sign of an expression

血红的双手。 提交于 2019-12-05 18:28:10

You can't model z = sign(x-c) exactly with a linear program (because the constraints in an LP are restricted to linear combinations of variables).

However, you can model sign if you are willing to convert your linear program into a mixed integer program, you can model this with the following two constraints:

 L*b <= x - c <= U*(1-b)
 z = 1 - 2*b

Where b is a binary variable, and L and U are lower and upper bounds on the quantity x-c. If b = 0, we have 0 <= x - c <= U and z = 1. If b = 1, we have L <= x - c <= 0 and z = 1 - 2*1 = -1.

You can use a solver like Gurobi to solve mixed integer programs.

For k » 1 this is a smooth approximation of the sign function:

Also

when ε → 0

These two approximations haven't the division by 0 issue but now you must tune a parameter.


In some languages (e.g. C++ / C) you can simply write something like this:

double sgn(double x)
{
  return (x > 0.0) - (x < 0.0);
}

Anyway consider that many environments / languages already have a sign function, e.g.

Pay close attention to what happens when x is equal to 0 (e.g. the Fortran function will return 1, with other languages you'll get 0).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!