How to formulate constraints in linear programming so that a set of consecutive variables are forced to be equal? - linear-programming

Let's say we are optimizing over 2 variables, each a vector of 6. That is, Y=[y0,y1,...y5], and X=[x0, x1, ..., x5]. How do I formulate a constraint in linear programming so that it forces the following solutions: x0=x1=x2=x3 & x4=x5. Or is it better to penalize the differences (e.g. |x0-x1|) in the objective function? Is so, how?

x0=x1 can be expressed as x0-x1 <= 0 and x0-x1 >= 0. The other equalities in the same way.
Edit: As pointed out in the comments, directly stating x0-x1 = 0 is the better way.

Related

Implementing a constraint based on previous variable's value in GNU Mathprog/AMPL

I have a binary program and one of my variables, x_it is defined on two sets, being I: Set of objects and T: Set of the weeks of the year, thus x_it is a binary variable standing for whether object i is assigned to something on week t. The constraint I failed to implement in AMPL/GNU Mathprog is that if x_it equals to 1 then x_i(t+1) and x_i(t+2) also should take value of 1. Is there a way to implement this constraint in a simple mathematical programming language?
The implication you want to implement is:
x(i,t) = 1 ==> x(i,t+1) = 1, x(i,t+2) = 1
AMPL supports implications (with the ==> operator), so we can write this directly. MathProg does not.
A simple way to implement the implication as straightforward linear inequalities is:
x(i,t+1) >= x(i,t)
x(i,t+2) >= x(i,t)
This can easily be expressed in AMPL, MathProg, or any modeling tool.
This is the pure, naive translation of the question. This means however that once a single x(i,t)=1 all following x(i,t+1),x(i,t+2),x(i,t+3)..=1. That could have been accomplished by just the constraint x(i,t+1) >= x(i,t).
A better interpretation would be: we don't want very short run lengths. I.e. patterns: 010 and 0110 are not allowed. This is sometimes called a minimum up-time in machine scheduling and can be modeled in different ways.
Forbid the patterns 010 and 0110:
(1-x(i,t-1))+x(i,t)+(1-x(i,t+1)) <= 2
(1-x(i,t-1))+x(i,t)+x(i,t+1)+(1-x(i,t+2)) <= 3
The pattern 01 implies 0111:
x(i,t+1)+x(i,t+2) >= 2*(x(i,t)-x(i,t-1))
Both these approaches will prevent patterns 010 and 0110 to occur.

linear programming line removal optimal solution

How would I prove the constraints in line 35.19 are redundant in this linear program in that if we remove them in lines (35.17) – (35.20), any optimal solution to the linear program must satisfy x(v)≤1 for each v∈V.
LP
I think I need to use the relaxation version of linear program but am not sure.
Beyond that I am not sure how I would prove this.
You're already looking at a relaxed version of the program, otherwise your variables would be constrained to being members of a set.
Likely you need to consider the dual of this program, which will convert >= constraints into <= constraints.
Your claim is (almost) true assuming that the weights w are non-negative (otherwise, if some w_v is negative then removing x_v <= 1 will allow x_v go to infinity, and the problem will be unbounded).
Now, assume that in an optimal solution there exists one x_v = 1 + ε, with ε > 0. If we change this solution to one in which x_v = 1, then the problem is still feasible, and the objective value is no worse than what it used to be before, so it is also optimal.
This proves that there exist optimal solutions in which x_v <= 1 (if bounded optimal solutions exist at all).
Thus, while it is not true that every optimal solution has x_v <= 1, it is true that every finite optimal solution can be converted to a solution with x_v <= 1 without loss of generality, and in this sense the constraints are redundant.
For reasons I do not consider here, regular solvers will most likely return a solution in which x_v <= 1 anyway, because of the way they work (they return basic solutions).
I hope this helps.

do this binary numbers have a specific name or property to get them?

i want binary number that only have 0's at the beginning or end, for instance,
1111111
01111110
001111111
000111000
but no:
01001
0011101
do they have an specific name or property to get them?
I'm looking for something like linear integer optimization conditions, my solutions must have this form, but i can't think of any condition i can add to ensure that
Regards,
This is something which is not nice to formulate within mixed-integer programs. Most problems involving this are more suitable for alternative methods (SAT-solving, SMT-solving, Constraint-programming).
It can be done of course, but the solver will have some work as the formulation is non-trivial and introduces a lot of binary-variables (and the basic approach of MIP-solvers won't work amazingly here; bad integrality-gap).
I won't give you a complete solution, but some basic idea on how to formulate this and i also indicate how hard and cumbersome it is (there are alternative formulations; actually infinite many; but nothing much more simple).
The basic idea here is the following:
your binary number is constructed from N binary-variables
let's call them x
you introduce N auxiliary binary-variables l (left)
l[i] == 1 implicates: every l[j] with i<j is 0
you introduce N auxiliary binary-variables r (right)
r[i] == 1 implicates: every r[j] with i>j is 0
you add the following constraint for each position k:
x[k] == 0 implicates: l[i] == 1 for i < k OR r[i] == 1 for i>k
idea::
if there is a zero somewhere, either all on the left-side or all on the right-side are zeroes (or means: at least one side; but can be both)
To formulate this, you need two more ideas:
A: How to formulate the equality-check?
B: How to formulate the implication?
Remark: a -> b == not a or b (propositional calculus)
(this was wrongly stated earlier and corrected by OP)
These are common in MIP and you will find the solution in many integer-programming books, tutorial and papers. Here is an example (start with indicator-variables).
Another small common formulation:
if a is binary, b is binary:
a OR b is equivalent to: a+b >= 1 (the latter is a linear-expression ready to use for MIP)
Remark: The formulas in my idea-setting above might be wrong in regards to indices (i vs. i-1, vs. i+1) and binary-relations (<vs. <=). You will need to do the actual math yourself and just learn from the idea itself!
Remark 2: This kind of constraint is cumbersome in MIP, but more easily formulated within SAT and CP.

Enumerate some extreme points near optimum solution

I am looking for a simple way to obtain a lot of "good" solutions in a LP problem (not MIP) with CPLEX, and not only (one of the) optimal basic solution(s). By "good" solutions I mean that the corresponding objective values are not so far from the real optimal value. Such pool of solutions could help the decision-maker...
More precisely, given a certain polyedron Ax<=b with x>=0 and an objective function z=cx I want to maximize, after running the LP, I can obtain the optimal value z*. Then I want to enumerate all the extreme points of the polyhedron given by the set of constraints
Ax <= b
cx >= z* - epsilon
x >= 0
when epsilon is a given tolerance.
I know that CPLEX offers way to generate solution pool (see here), but it will not function because this method is for MIP : it enumerates all the solutions of an IP (or one solution for every given set of fixed integer variables if the problem is a MIP).
An interesting efficient way is to visit the adjacent solutions of the optimal basic solution, i.e. all the adjacent extreme points : if I suppose the polyhedron is not degenerative, for each pair of basic variable x_B and non-basic variable x_N, I compute the basic solution obtained when x_B leaves the basis and x_N enters in the basis. Then I throw the solutions with cx < z*-epsilon, and for the others I repeat the procedure. [I know that I could improve this algorithm, but this is the general idea].
The routine CPPXpivot of the Callable Library could help to do this pivoting operation, but I did not find an equivalent in the C++ API (concert technology). Does someone know if such an equivalent exist, or could propose me an other way to answer my original problem ?
Thanks a lot :) !
Rémi L.
There is one interesting way to make this suitable for use with the Cplex solution pool. Use binary variables to encode the current basis, e.g. basis[k] = 0 meaning nonbasic and basis[k] = 1 indicating variable (or row) k is basic. Of course we have sum(k, basis[k]) = m (number of rows). Finally we have x[k] <= basis[k] * upperbound[k] (i.e. if nonbasic then zero -- assuming positive variables). When we add this to the LP model we end up with a MIP and can enumerate (all or some, optimal or near optimal) bases using the Cplex solution pool. See here and here.

Solving equation. Counting (x,y)

I'm having a trouble with my math:
Assume that we have a function: F(x,y) = P; And my question is: what would be the most efficient way of counting up suitable (x,y) plots for this function ? It means that I don't need the coordinates themself, but I need a number of them. P is in a range: [0 ; 10^14]. "x" and "y" are integers. Is it solved using bruteforce or there are some advanced tricks(math / programming language(C,C++)) to solve this fast enough ?
To be more concrete, the function is: x*y - ((x+y)/2) + 1.
x*y - ((x+y)/2) + 1 == P is equivalent to (2x-1)(2y-1) == (4P-3).
So, you're basically looking for the number of factorizations of 4P-3. How to factor a number in C or C++ is probably a different question, but each factorization yields a solution to the original equation. [Edit: in fact two solutions, since if A*B == C then of course (-A)*(-B) == C also].
As far as the programming languages C and C++ are concerned, just make sure you use a type that's big enough to contain 4 * 10^14. int won't do, so try long long.
You have a two-parameter function and want to solve it for a given constant.
This is a pretty big field in mathematics, and there are probably dozens of algorithms of solving your equation. One key idea that many use is the fact that if you find a point where F<P and then a point F>P, then somewhere between these two points, F must equal P.
One of the most basic algorithms for finding a root (or zero, which you of course can convert to by taking F'=F-P) is Newton's method. I suggest you start with that and read your way up to more advanced algorithms. This is a farily large field of study, so happy reading!
Wikipedia has a list of root-finding algorithms that you can use as a starting place.