Skip to content

API: Model.getSolVal should receive MatrixExpr #1136

@Zeroto521

Description

@Zeroto521

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

getSolVal only requires Expr. So, for a MatrixExpr, it has to use a loop to query the result
But getVal could receive MatrixExpr.

def getSolVal(self, Solution sol, Expr expr):

def getVal(self, expr: Union[Expr, MatrixExpr] ):

Describe the solution you'd like
A clear and concise description of what you want to happen.

Change the getSolVal argument.

Additional context
Add any other context or screenshots about the feature request here.

A demo is as follows. And np.vectorize could also simplify getSol codes.

from functools import wraps

import numpy as np
from pyscipopt import Model


def to_ndarray(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs).view(np.ndarray)

    return wrapper


@to_ndarray
@np.vectorize
def getSolVal(expr, model, sol):
    return model.getSolVal(sol, expr)


if __name__ == "__main__":
    model = Model()
    model.hideOutput()
    x = model.addMatrixVar((2, 3), ub=10)
    model.setObjective(x.sum(), "maximize")
    model.optimize()

    res1 = model.getVal(x)
    print(type(res1))
    # <class 'numpy.ndarray'>
    print(res1)
    # [[10. 10. 10.]
    #  [10. 10. 10.]]
    sol = model.getBestSol()
    res2 = getSolVal(x, model, sol)
    print(type(res2))
    # <class 'numpy.ndarray'>
    print(res2)
    # [[10. 10. 10.]
    #  [10. 10. 10.]]

    assert np.array_equal(res1, res2)

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