Binary variables non exactly binary in Python

743 views
Skip to first unread message

Nicola Rosada

unread,
Jul 13, 2016, 4:33:31 AM7/13/16
to Gurobi Optimization
Hi,

I'm using some binary variables in Gurobi for Python:

self.y = {}

for m in range(1,self.NUM_OF_TM+1):

    for n in range(1,self.N+1):

        self.y[m,n] = self.mod.addVar(name='y_%s_%s' % (m,n), vtype=GRB.BINARY)


after have solved the model I get the variables with getAttr:

self.y_solution = self.mod.getAttr('x', self.y)

print 'self.y_solution:'

print self.y_solution

I obtain a dictionary with float variables and in most of the cases the values are 0.0 or 1.0 as expected. But I found very few instances where the values are not exactly 0 or 1, but they are something like 1e-8 or 0.99999999.
It seems strange, is there something wrong in what I'm doing?

Thank you all!
Bye
image.png

Tobias Achterberg

unread,
Jul 13, 2016, 4:55:31 AM7/13/16
to gur...@googlegroups.com
This can happen due to feasibility and integrality tolerances. You will also
find that solution that Gurobi (as all floating-point based MIP solvers)
provides may slightly violate your constraints.

The reason is that floating-point numeric as implemented in the CPU hardware is
not exact. Rounding errors can (and usually will) happen. As a consequence, MIP
solvers use tolerances within which a solution is still considered to be
correct. The default tolerance for integrality in Gurobi is 1e-5, the default
feasibility tolerance is 1e-6. This means that Gurobi is allowed to consider a
value that is at most 1e-5 away from an integer to still be integral, and it is
allowed to consider a constraint that is violated by at most 1e-6 to still be
satisfied.

You can tighten the tolerances up to 1e-9, but this does not get rid of the
fundamental issue that the CPU makes errors when calculating with floating point
numbers.


Best regards,

Tobias

Nicola Rosada

unread,
Jul 13, 2016, 7:54:35 AM7/13/16
to Gurobi Optimization
I knew about the tolerance for the floating point numbers, but I thought that this problem didn't happen for binary and integer values that can be correctly stored in memory.
Thank you very much for the answer.

Best regards,
Nicola
Reply all
Reply to author
Forward
0 new messages