Skip to content

6.2.0 no longer handles values that are numpy.float64 in constraint comparisons being negated #1218

@jnh277

Description

@jnh277

Describe the bug

When using pyscipopt 6.2.0 with numpy 2.x, passing a numpy.float64 value to a constraint comparison raises an AttributeError. This occurs when a numpy scalar is negated and used in a comparison with pyscipopt expression variables.

The error is:
AttributeError: 'pyscipopt.scip.ExprCons' object has no attribute 'view'

This worked correctly in pyscipopt 6.1.0.

To Reproduce

  import numpy as np
  from pyscipopt import Model

  model = Model()
  x = model.addVar(vtype="C", lb=None, ub=None, name="x")
  y = model.addVar(vtype="C", lb=None, ub=None, name="y")
  z = model.addVar(vtype="C", lb=0, name="z")

  # This value is a numpy.float64
  value = np.float64(5.0)

  # This raises AttributeError on pyscipopt 6.2.0
  model.addCons(-value <= x - y + z, name="test")

  Full traceback:
  Traceback (most recent call last):
    File "test.py", line 12, in <module>
      model.addCons(-value <= x - y + z, name="test")
    File "src/pyscipopt/expr.pxi", line 211, in pyscipopt.scip.ExprLike.__array_ufunc__
    File "src/pyscipopt/matrix.pxi", line 63, in pyscipopt.scip.MatrixExpr.__array_ufunc__
  AttributeError: 'pyscipopt.scip.ExprCons' object has no attribute 'view'

Expected behavior

The constraint should be added to the model without error, as it does in pyscipopt 6.1.0.

Workaround

Converting numpy floats to native Python floats before using in constraints works:

model.addCons(-float(value) <= x - y + z, name="test")  # Works

System

  • OS: macOS (Darwin 25.3.0)
  • Python: 3.12.12
  • pyscipopt: 6.2.0
  • SCIP version: 10.0
  • numpy: 2.4.4
  • Installed via: uv pip install pyscipopt

Additional context

This issue appears to be related to the array_ufunc implementation in expr.pxi and matrix.pxi. When numpy negates a numpy.float64 scalar and then compares it with a pyscipopt expression, numpy's array protocol calls array_ufunc on the pyscipopt ExprCons object, which attempts to call .view() on it, but ExprCons doesn't have that attribute.

The issue does not occur with native Python floats, only with numpy scalar types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions