Skip to content

Constraint creation with 0 coefficient, taken from NumPy array, leads to boolean instead. #162

@rschwarz

Description

@rschwarz

Constraints are built with coefficients that are in a NumPy array (float64).
A coefficient of 0.0 multiplied with with the left-hand side leads to the value True of type numpy.bool_, rather than a mip.LinExpr.

Consider this example:

import mip
import numpy

m = mip.Model()

x = m.add_var(name="x")
y = m.add_var(name="y")

coeffs = numpy.array([0.0])
lhs = x * coeffs[0]
rhs = y + 1.0
print("lhs: ", lhs, type(lhs))
print("rhs: ", rhs, type(rhs))

cons1 = lhs == rhs
cons2 = rhs == lhs
print("cons1: ", cons1, type(cons1))
print("cons2: ", cons2, type(cons2))

This results in

lhs:  0.0 <class 'numpy.float64'>
rhs:  + y + 1.0 <class 'mip.entities.LinExpr'>
cons1:  True <class 'numpy.bool_'>
cons2:  + y  = - 1.0 <class 'mip.entities.LinExpr'>

I would have expected that both cons1 and cons2 yield a LinExpr.

Note that the problem does not occur if:

  • one uses a float of 0.0 instead of the coeffs[0] reference to a NumPy array.
  • the coefficient is multiplied from the left: coeffs[0] * x
  • the left- and right-hand sides are swapped (as in cons2).

The behavior is strange to me, since it's the case that isinstance(coeffs[0], float) gives True, so it should be handled the same way when the mip library compares agains numbers.Real.

So, is this a problem with __add__ in some numpy type?

  • Linux t440p 4.19.0-12-amd64 Error removing constraints #1 SMP Debian 4.19.152-1 (2020-10-18) x86_64 GNU/Linux
  • Python version: 3.7.3
  • Python-MIP version: 1.12.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    mipIssues related to the core mip package

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions