0

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,

kurokirasama
  • 737
  • 8
  • 31
  • In integer programming this is called "fixing". We fix `x[0]=0` and `x[N]=0`. – Erwin Kalvelagen Apr 20 '17 at 17:37
  • I'm not familiar with linear programming, i think one possible way could be to `mask` the number in order to achieves `0`. – Adam Apr 21 '17 at 16:00
  • Well, in MIP we are limited to linear constraints. Just encode your binary number as an array of binary (i.e. 0-1) variables. Otherwise the first bit and last bit of an integer variable `K` can also be set to zero by requiring `K >= 2` and `K <= MAXK/2`; – Erwin Kalvelagen Apr 21 '17 at 19:25
  • Sorry, that `K>=2` should be `K is multiple of 2`, or `K=2*J` where `J` is another integer variable. – Erwin Kalvelagen Apr 21 '17 at 19:54

1 Answers1

1

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.

sascha
  • 32,238
  • 6
  • 68
  • 110