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
- 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.