cvxpy icon indicating copy to clipboard operation
cvxpy copied to clipboard

Clearer and custom error messages for better traceability

Open Transurgeon opened this issue 1 year ago • 0 comments

This issue will serve as a tracking issue since there are many cases and examples that could be improved. We will add examples/suggestions in the form of comments to this issue. The example below is taken from the cvxpy discord.

import cvxpy as cp


def test_plan():

    # A variable for the preexisting holdings of each share
    preexisting_holdings_variables = cp.Constant([0, 0])

    print(preexisting_holdings_variables)
    print(preexisting_holdings_variables[True])

    # Define constraints
    purchase_constraints = []

    # of the number of those shares we charge to the basket
    # divided by the weight of the share in the basket
    opt = cp.sum(
            cp.hstack([preexisting_holdings_variables[True]])
        )

    objective = cp.Maximize(opt)
    problem = cp.Problem(objective, purchase_constraints)
    problem.solve(solver=cp.SCS) 

results in the following error message, even though the reshape atom was never explicitly called.

ValueError: cannot reshape array of size 2 into shape (1,)

In this case, the error should be raised earlier (when defining the expression preexisting_holdings_variables[True]) to avoid confusion. On a related note, #2613 suggests improvements that will also need custom error messages.

Transurgeon avatar Nov 09 '24 17:11 Transurgeon