Is it possible to model a logical OR without boolean variables in linear programming? - linear-programming

I want to state a linear model where I can't use boolean variables due to efficiency reasons. I can only use a solver that can handle boolean variables not that efficiently. And in a productive model I would need hundreds of those variables.
I use a boolean variable to decide if I can satisfy demand either from one source (continuous variable A) or another source (continuous variable B) but not both.
The constraint is:
A + B >= demand
But either A OR B can be non-zero.
This can be ensured by using a boolean variable (Bool_A) and the following constraints:
A <= 1000 * Bool_A
B <= 1000 * (1- Bool_A)
If Bool_A = 1, then the variable A can take non-zero values and B is forced to 0, and if Bool_A = 0 then vice versa.
My question is now: does anyone know, if it is possible to model this using only linear variables (no booleans and integer variables) or has a proof that it is not possible.

In Brown, G. and Dell, R., "Formulating linear and integer linear programs: A rogues’ gallery" the following linear programming formulation for the XOR (exclusive or) can be found :
X = A xor B
resolves to
X ≤ A + B
X ≥ A - B
X ≥ - A + B
X ≤ 2 - A - B
Using an auxiliary variable:
X = A + B - 2*H
H ≤ A
H ≤ B
H ≥ A + B - 1
H ≥ 0

Related

Converting a simple conditional statement constraint into linear equation

I am using PuLP method to optimize a problem. I have a simple conditional statement as:
If A = 0, x = 0
Else if A>0, x = 1
How can I change it into mathematical equation (or a list of equations) if I want to use it as a constraint in optimization using PuLP method?
0.001⋅x ≤ A ≤ 640⋅x
x ∈ {0,1}
For PuLP split this into two inequality constraints.

if condition in ampl

I am new in ampl and I want to use if condition in ampl with the following information:
I have a binary variable X[p,r], where {p in P, r in R}.
Now I want to make a new constraint such that the variable R[p,r] is used where X[p,r]=0.
I do not know how I can write it or even if the ampl can handle it or not, I tried the following constraint but they did not work:
s.t. a1{r in R, p in P and X[p,r]=0}:
s.t. a2{r in R p in P and X[p,r]=0};
s.t. a2{r in R ,p in P, and X[p,r]=0};
s.t. a2{r in R, p in P: and X[p,r]=0};
You cannot include a decision variable in the "for all" part of the constraint (in AMPL, the part inside the {...}). Instead, you need build into the constraint itself the logic that says the constraint is only active if X[p,r] = 0. The way to do that depends on the type of constraint: >=, =, or <=. I'll write each case separately, and I'll do it in a generic way instead of specific to your problem.
In the explanation below, I assume that the constraint is written as
a[1]y[1] + ... + a[n]y[n] >=/=/<= b,
where a[i] and b are constants and y[i] are decision variables. I also assume we want the constraint to hold if x = 0, where x is a binary decision variable, and we don't care whether the constraint holds if x = 1.
Let M be a new parameter (constant) that equals a large number.
Greater-than-or-equal-to constraints:
The constraint is a[1]y[1] + ... + a[n]y[n] >= b. Rewrite it as
a[1]y[1] + ... + a[n]y[n] >= b - Mx.
Then, if x = 0, the constraint holds, and if x = 1, it has no effect since the right-hand side is very negative.
(If all of the a[i] are nonnegative, you can instead use
a[1]y[1] + ... + a[n]y[n] >= bx,
which is tighter.)
Less-than-or-equal-to constraints:
The constraint is a[1]y[1] + ... + a[n]y[n] <= b. Rewrite it as
a[1]y[1] + ... + a[n]y[n] <= b + Mx.
Then, if x = 0, the constraint holds, and if x = 1, it has no effect since the RHS is very large.
Equality constraints:
The constraint is a[1]y[1] + ... + a[n]y[n] = b. Rewrite it as
a[1]y[1] + ... + a[n]y[n] <= b + Mx
a[1]y[1] + ... + a[n]y[n] >= b - Mx.
Then, if x = 0, the equality constraint holds, and if x = 1, the constraints have no effect.
Note: If your model is relatively large, i.e., it takes a non-negligible amount of time to solve, then you need to be careful with big-M-type formulations. In particular, you want M to be as small as possible while still enforcing the logic of the constraints above.

Range Update - Range Query using Fenwick Tree

http://ayazdzulfikar.blogspot.in/2014/12/penggunaan-fenwick-tree-bit.html?showComment=1434865697025#c5391178275473818224
For example being told that the value of the function or f (i) of the index-i is an i ^ k, for k> = 0 and always stay on this matter. Given query like the following:
Add value array [i], for all a <= i <= b as v Determine the total
array [i] f (i), for each a <= i <= b (remember the previous function
values ​​clarification)
To work on this matter, can be formed into Query (x) = m * g (x) - c,
where g (x) is f (1) + f (2) + ... + f (x).
To accomplish this, we
need to know the values ​​of m and c. For that, we need 2 separate
BIT. Observations below for each update in the form of ab v. To
calculate the value of m, virtually identical to the Range Update -
Point Query. We can get the following observations for each value of
i, which may be:
i <a, m = 0
a <= i <= b, m = v
b <i, m = 0
By using the following observation, it is clear that the Range Update - Point Query can be used on any of the BIT. To calculate the value of c, we need to observe the possibility for each value of i, which may be:
i <a, then c = 0
a <= i <= b, then c = v * g (a - 1)
b <i, c = v * (g (b) - g (a - 1))
Again, we need Range Update - Point Query, but in a different BIT.
Oiya, for a little help, I wrote the value of g (x) for k <= 3 yes: p:
k = 0 -> x
k = 1 -> x * (x + 1) / 2
k = 2 -> x * (x + 1) * (2x + 1) / 6
k = 3 -> (x * (x + 1) / 2) ^ 2
Now, example problem SPOJ - Horrible Queries . This problem is
similar issues that have described, with k = 0. Note also that
sometimes there is a matter that is quite extreme, where the function
is not for one type of k, but it could be some that polynomial shape!
Eg LA - Alien Abduction Again . To work on this problem, the solution
is, for each rank we make its BIT counter m respectively. BIT combined
to clear the counters c it was fine.
How can we used this concept if:
Given an array of integers A1,A2,…AN.
Given x,y: Add 1×2 to Ax, add 2×3 to Ax+1, add 3×4 to Ax+2, add 4×5 to
Ax+3, and so on until Ay.
Then return Sum of the range [Ax,Ay].

How to convert this into a set of linear constraints?

Given a 1-dimensional array of binary variables, for example
x = [0,1,0,0,1]
I would like to create a new variable y such that y <= max(x). In other words
y = 0 only if sum(x) = 0.
y = 1 only if sum(x) > 0.
How do I convert this into a set of linear constraints?
I know this must be possible because IBM CP Optimizer Suite can handle this automatically, but I don't have access to it.
Try something simple like y <= sum(x) which will force y to zero if all the x are zero.
Then for forcing y to 1 you have several choices. You could simply add a constraint that y >= x for every variable in x, or use a big M constraint like My >= sum(x) where M is some constant which is the maximum number of variables in x that can be simultaneously equal to 1. Adding the separate constraints might give a tighter linear relaxation, especially if there are many x variables.

Linear Programming - variable that equals the sign of an expression

I am trying to write a linear program and need a variable z that equals the sign of x-c, where x is another variable, and c is a constant.
I considered z = (x-c)/|x-c|. Unfortunately, if x=c, then this creates division by 0.
I cannot use z=x-c, because I don't want to weight it by the magnitude of the difference between x and c.
Does anyone know of a good way to express z so that it is the sign of x-c?
Thank you for any help and suggestions!
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.
Sign[x] in Mathematica
sign(x) in Matlab
Math.signum(x) in Java
sign(1, x) in Fortran
sign(x) in R
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).