Skip to content

BUG: can't call getVal from a matrix expression #1062

Closed

Description

@Zeroto521

Describe the bug

It can't call getVal from a matrix expression. If the expression has a division operation.

To Reproduce

from pyscipopt import Model

model = Model()
model.hideOutput()
x = model.addMatrixVar(3, lb=1, ub=3)

model.setObjective(x.sum(), "maximize")
model.optimize()

print(model.getVal(x))
# [3. 3. 3.]

print(model.getVal(x / x))
# Traceback (most recent call last):
#   line 12, in <module>
#     print(model.getVal(x / x))
#           ^^^^^^^^^^^^^^^^^^^
#   File "src/pyscipopt/scip.pxi", line 10399, in pyscipopt.scip.Model.getVal
# TypeError: Argument 'expr' has incorrect type (expected pyscipopt.scip.Expr, got pyscipopt.scip.ProdExpr)

print(model.getVal((x / x).sum()))
# Traceback (most recent call last):
#   line 20, in <module>
#     print(model.getVal((x / x).sum()))
#           ^^^^^^^^^^^^^^^^^^^^^^^^^^
#   File "src/pyscipopt/scip.pxi", line 10401, in pyscipopt.scip.Model.getVal
# TypeError: Argument 'expr' has incorrect type (expected pyscipopt.scip.Expr, got pyscipopt.scip.SumExpr)

Expected behavior

Get the value without any error.

Screenshots

Image

System

  • OS: Windows 11
  • Version: 24H2 (26100.4061)
  • SCIP version: 9.2.3
  • How did you install pyscipopt?: build from c681f94, base on 5.5.0

Activity

  1. Joao-Dionisio commented on Aug 25, 2025

    @Joao-Dionisio
    Member

    Same with normal variables, this isn't a matrix variable bug.

  2. self-assigned this
    on Oct 8, 2025
  3. Joao-Dionisio commented on Oct 13, 2025

    @Joao-Dionisio
    Member

    Seems like I wasn't very nice in this end of August period, sorry about that @Zeroto521 😓

    What I meant to say is that this seems to be a problem with using getVal in fractions, not really matrix variables.

    >>> from pyscipopt import Model
    >>> m = Model()
    >>> x = m.addVar(lb=1,ub=1)
    >>> m.hideOutput()
    >>> m.optimize()
    >>> m.getVal(x)
    1.0
    >>> m.getVal(x+x)
    2.0
    >>> m.getVal(x*x)
    1.0
    >>> m.getVal(x/x)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "src/pyscipopt/scip.pxi", line 10401, in pyscipopt.scip.Model.getVal
        result = self.getSolVal(self._bestSol, expr)
    TypeError: Argument 'expr' has incorrect type (expected pyscipopt.scip.Expr, got pyscipopt.scip.ProdExpr)
  4. Joao-Dionisio commented on Jan 6, 2026

    @Joao-Dionisio
    Member

    @Zeroto521 do you mind if I assign this to you, since you've been looking at expressions?

  5. Zeroto521 commented on Jan 7, 2026

    @Zeroto521
    ContributorAuthor

    @Zeroto521 do you mind if I assign this to you, since you've been looking at expressions?

    Move _evaluate into Term/Expr/GenExpr/MatrixExpr as a private method, like def _evaluate(self, Solution sol) -> float:.
    The current _evaluate only works for Expr/MatrixExpr. _evaluate doesn't have any logic to handle GenExpr.
    It may fix this problem, based on my experience.

    def __getitem__(self, expr: Union[Expr, MatrixExpr]):
    if isinstance(expr, MatrixExpr):
    result = np.zeros(expr.shape, dtype=np.float64)
    for idx in np.ndindex(expr.shape):
    result[idx] = self.__getitem__(expr[idx])
    return result
    # fast track for Variable
    cdef SCIP_Real coeff
    cdef _VarArray wrapper
    if isinstance(expr, Variable):
    wrapper = _VarArray(expr)
    self._checkStage("SCIPgetSolVal")
    return SCIPgetSolVal(self.scip, self.sol, wrapper.ptr[0])
    return sum(self._evaluate(term)*coeff for term, coeff in expr.terms.items() if coeff != 0)
    def _evaluate(self, term):
    self._checkStage("SCIPgetSolVal")
    result = 1
    cdef _VarArray wrapper
    wrapper = _VarArray(term.vartuple)
    for i in range(len(term.vartuple)):
    result *= SCIPgetSolVal(self.scip, self.sol, wrapper.ptr[i])
    return result

  6. Joao-Dionisio commented on Jan 20, 2026

    @Joao-Dionisio
    Member

    Closing this to clear the issue page, as it's being actively developed in #1148

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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