MILP BigM : variable must remains in defined boundaries or set to 0 - linear-programming

I'm modeling some energy systems via MILP / Pyomo.
In that context I'm modeling a bi-directional elec. converter.
Power/energy can flow in both ways:
However, In one way, the power P can only be within boundaries [LB;UB], otherwise it must be 0.
I use this formulation to ensure that:
LB - P <= LB * x
P <= UB * (1 - x)
x being a binary variable
and it seems to be working...
in the other way, the power P can only be within [-UB;-LB], otherwise 0.
but I'm struggling hard to ensure that, I just can't get the logic behind to build that kind of constraint...
Any help and explanation would be appreciated.
Thanks a lot,
Max

I would think you have three states (or feasible regions):
p = 0
p ∈ [L,U]
p ∈ [-U,-L]
I don't think that can be done with one binary variable.
You can do something like :
δ1*L-δ2*U ≤ p ≤ δ1*U-δ2*L
δ1+δ2 ≤ 1
δ1,δ2 ∈ {0,1}
This is essentially:
δ1=δ2=0 => p = 0
δ1=1,δ2=0 => p ∈ [L,U]
δ1=0,δ2=1 => p ∈ [-U,-L]
δ1=δ2=1: not allowed

Related

Given two segments in 3D, compute closest point between them using CGAL

Given two segments in 3D in CGAL, I would like to compute the closest points between one segment and another. These two segment may be anywhere in space.
I have looked in CGAL and there is a function which computes distance between both segments (https://doc.cgal.org/latest/Kernel_23/group__squared__distance__grp.html), and with that I guess I could create two spheres and compute the intersection between them, but this seems slow and cumbersome.
Is there something out of the box?
Sorry to say, there are no out-of-the-box way in the CGAL library to see two points, defining the shortest segment between two line segments in 3D. I'm saying "to see" because these two points are actually computed deeply inside the CGAL::squared_distance function, then the distance between them is calculated, and then these two points are discarded.
If you really need these two points you can modify the Distance_3/Segment_3_Segment_3.h file and add a function with an additional argument:
template <class K>
inline
typename K::FT
squared_distance(const Segment_3<K>& seg1,
const Segment_3<K>& seg2,
Segment_3<K>& res)
{
// ... modifications
}
Of course you'll need to modify other code in this file as well.
Analytically:
You want to minimize the function
(A + p AB - C - q CD)²
under inequality constraints 0 ≤ p ≤ 1 and 0 ≤ q ≤ 1.
By differentiation, you get a linear system of 2 equations in the 2 unknowns p and q. After resolution, if these values fall in the allowed range, you are done.
p AB² - q AB.CD = AB.AC
p CD.AB - q CD² = CD.AC
Otherwise, you need to saturate one constraint (p=0, p=1, q=0 or q=1) and check if the other parameter is in range. (Endpoint to segment distances.)
Otherwise, you saturate both constraints (4 combinations). (Endpoint to endpoint distances.)
Finally, you keep the feasible solution that yields the smallest distance.
If the two segments are parallel or collinear, there can be an infinity of solution points.
Addendum:
WLOG, AB.CD ≤ 0 (otherwise swap two endpoints). Then then from the inequalities we draw
- AB.CD ≤ p AB² - q AB.CD ≤ AB²
- AB.CD ≤ p AB.CD - q CD² ≤ CD²
which directly gives us the condition for existence of a segment-segment solution:
- AB.CD ≤ AB.AC ≤ AB²
- AB.CD ≤ CD.AC ≤ CD²
We get the conditions for endpoint-segment solutions by substituting 0 or 1 for p or q.

Either or constraint WITH ONE POINT IN COMMON in linear programming operations-research

suppose we have a quantity variable x (which is upperbounded by n), and a logic variable y which is equal to
y = 1 if x >= s; where s is a generic number
y = 0 otherwise => if x is strictly lesser than s => if x < s
Surfing around on the internet I found out this clear explanation https://youtu.be/iQ3PlKKorXA?t=35 which turned out to be the common pattern about either-or constraint. Therefore following the video, the solution would be:
s - x <= (1 - y)*n
x - s <= y*n
And yet x could be equal to s in both the cases.
How can we fix this?
You can consider the following two constraints:
x-s ≤ My - ɛ(1-y)
s-x ≤ M(1-y)
where M is a sufficiently large upper bound and ɛ is a small positive constant.
The first enforce the logical constraint if x >= s then y = 1 and the second the constraint if x < s then y = 0.
Note that these are often referred to as indicator constraints and are supported by several solvers (e.g., cplex) with advantages in terms of a more numerically stable model.
With many solvers you can use logical constraints.
For example with CPLEX OPL you can write
int s=3;
dvar int x;
dvar boolean y;
subject to
{
y==(x>=s);
}

What does Big M method do in constraints when converting nonlinear programming into linear programming?

I got a question regarding this constraints in the paper. This paper says it used big M method in order to make non-linear programming model into LP. I get that big number M1is a huge number, but I don't get what big number M1 really does on the constraints. Would you guys give me some insight on the use of the big M in this constraints?
Below is constraints with big number M1.
The paper says these constraints are
when K[m][i] = p[i]*x[m][i],
maximize sum(m in M, i in I) (K[m][i]-c[i]*x[m][i]
K[m][i]-M[1]*(1-x[m][i]) <= p[i]
K[m][i]+M[1]*(1-x[m][i]) >= p[i]
K[m][i]-M[1]*x[m][i] <= 0
it originally looked like this in non linear programming
maximize sum(m in M, i in I)(p[i]-c[i])*x[m][i]
So, basically, converting nonlinear programming into linear programming led to a little change in some decision variables and 3 additional constraints with big number M.
Here is another constraint that includes big number M.
sum (j in J) b[i][j]*p[j]-p[i]<= M[1]*y[i]
which originally looked like
p[i]<sum (j in J) b[i][j]*p[j], if y[i]==1
Here is the last constraint with big number M
(r[m][j]=p[j])*b[i][j]*x[m][i] >= -y[i]*m[1]
which was
(r[m][j]-p[j])*b[i][j]*x[m][i](1-y[i])>=0
in nonlinear program.
I really want to know what does big M do in the model.
It would be really appreciated if anyone gives me some insight.
Thank you.
As you said, the big-M is used to model the non-linear constraint
K[m][i] = p[i] * x[m][i]
in case x is a binary variable. The assumption is that M is an upper bound on K[m][i] and that K[m][i] is a non-negative variable, i.e. 0 <= K[m][i] <= M. Also p is assumed to be non-negative.
Since x[m][i] is binary, we can have two cases in a feasible solution:
x[m][i] = 0. In that case the product p[i] * x[m][i] is 0 and thus K[m][i] should be zero as well. This is enforced by constraint K[m][i] - M * x[m][i] <= 0 which in this case becomes just K[m][i] <= 0. The two other constraints involving M become redundant in this case. For example, the first constraint reduces to K[m][i] <= p[i] + M which is always true since M is an upper bound on K[m][i] and p is non-negative.
x[m][i] = 1. In that case the product p[i] * x[m][i] is just p[i] and the first two constraints involving M become K[m][i] <= p[i] and K[m][i] >= p[i] (which is equivalent to K[m][i] = p[i]). The last constraint involving M becomes K[m][i] <= M which is redundant since M is an upper bound on K[m][i].
So the role of M here is to "enable/disable" certain constraints depending on the value of x.
to model logical constraints you may either use logical constraints or rely on big M
https://www.ibm.com/support/pages/difference-between-using-indicator-constraints-and-big-m-formulation
I tend to suggest logical constraint as the default choice.
In https://www.linkedin.com/pulse/how-opl-alex-fleischer/
let me share the example
How to multiply a decision variable by a boolean decision variable in CPLEX ?
// suppose we want b * x <= 7
dvar int x in 2..10;
dvar boolean b;
dvar int bx;
maximize x;
subject to
{
// Linearization
bx<=7;
2*b<=bx;
bx<=10*b;
bx<=x-2*(1-b);
bx>=x-10*(1-b);
// if we use CP we could write directly
// b*x<=7
// or rely on logical constraints within CPLEX
// (b==1) => (bx==x);
// (b==0) => (bx==0);
}

Can I check whether the given number can be the sum of any arithmetic progression having n terms in it?

Is it possible for a given number s to just check that Is there any possible arithmetic progression having n terms and sum of these n terms results in s.
where starting element and difference of AP must not be zero.
for eg:
s = 24 & n = 4
yes, it is possible where AP is 3 5 7 9.
Note: I just want to check whether it is possible or not . No need to find the actual array. 0 < n < 10^9 & 0 < s < 10^18.
My Attempt:
we know that sum of an AP is equal to s = n(first+last)/2;
therefore first+last = 2*s/n;
2*s/n should be an integer.
we also know that last = first+(n-1)diff;
so my expression becomes 2*first + (n-1)diff = 2*s/n;
first = (2*s/n - (n-1)diff)/2; and it should be an integer for a particular value of diff.
this is my approach to doing this but its time complexity is too large to cover 10^18.
Please help. :)
Case 1: a and d are real numbers
Using s for the sum, n for the number of terms, a for the first term and d for the difference between terms, you get the result
2 * s / n = 2 * a + (n - 1) * d
This gives you one degree of freedom. So you can see that it's always possible to pick an infinite set of a and d values that satisfies this result.
Case 2: a and d are integer numbers
You can see from my result that if a and d are constrained to be integers, then the decomposition is only possible if the left hand side of this equation is also an integer; that is 2 * s is a multiple of n. (In your case, 2 * s is 48 which is a multiple of 4. So yes, there exists an integral a and d in that case).
Let a be the initial term of the progression and d its common difference. You want to solve the linear diophantine equation
n * a + (n*(n-1)/2) * d = s
The solution will exist if and only if s is a multiple ofgcd(n, n*(n-1)/2).
If n is odd, gcd(n, n*(n-1)/2) = n * gcd(1, (n-1)/2) = n.
If n is even, gcd(n, n*(n-1)/2) = (n/2) * gcd(2, n-1) = n/2.
In any case, the solution exists if and only if 2 * s is a multiple of n.
I think this is not possible in every case but if you can provide some more data then it can.
because there is multiple possibilities of same AP sum.
so in case you will give some hint it is possible

Hash and Modular Arthmetic

Let h(y) be the function defined as (a*y+b)mod m. So h(y) can take values from 0 to m-1.
Now we are given 7 integers- a,b,x,n,c,d,m. Our task is to find the total count of h(x),h(x+1),h(x+2)...h(x+n) such that the value of h(x+i) falls in the range of [c,d].where 0<=i<=n
Integer limits are:
1 ≤ m ≤ 10^15, c ≤ d < m, a,b < m, x+n ≤ 10^15, and a*(x+n) + b ≤ 10^15
For Example.
for input set {1,0,0,8,0,8,9} the output should be 9. Please suggest an efficient algorithm. Thanks!!!
This isn't a particularly strong hash. The only hard part about this problem is the obtuse notation with single-letter variables and specifying the problem as a 7-tuple.
Each increment of x increases h(x) by a. Therefore the total distance along x to get from c to d is simply (d-c)/a. Add one for the fencepost problem, or specify the problem with a half-open range for the sake of sanity.