New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: scipy 'minimize' with method='trust-constr' with equality constraints raises ValueError('expected square matrix') #20618
Comments
The trigonometric constraints (as specified in the latex equation) are impossible to satisfy.
To be feasible the roots of |
Actually, in my example the second constraint is cos(...) instead of sin(...), and the problem has solutions. See also this discussion on StackOverflow and this smaller example by @nickodell raising the same exception. It seems that the problem is due to the presence of two equality constraints on one variable. |
@andyfaff do you think this is indeed a bug? Do you think there is an easy way to fix it? |
I'm not familiar with trust-constr, so I don't know. It'll need someone who knows. |
In the meantime you could try setting breakpoints/use print statements to see how the linear operator is setup, or what the R array is on the last call |
This is a peculiar edge case. If @antonior92 could you help out here? |
<bear in mind that I'm not an expert in Linear Algebra> Ok, a little bit of progress. This error arises because the number of equality constraints is greater than the number of independent variables. The problem doesn't appear if the number of equality constraints is less than or equal to the number of independent variables. I checked this by modifying a test example based on your first post - if one has two independent variables and two equality constraints there are no issues, but if there are three equality constraints there is a problem. I then tried the example using SLSQP (1 independent variable, two equality constraints) and the minimize fails with:
Thus, it would not be surprising that the same behaviour is required for the trust-constr solver. I therefore don't think there's a bug. The example can 'work' by using a different
It's worth looking at the docstring there for guidance. With respect to improving the error message that will probably require a greater knowledge of the codebase. It should be possible to catch this specific error when the |
How about the following. It's a little bit messy, but one can't use try:
Z, LS, Y = projections(A, factorization_method)
except ValueError as e:
if str(e) == "expected square matrix":
# can be the case if there are more equality
# constraints than independent variables
raise ValueError(
"The 'expected square matrix' error can occur if there are"
" more equality constraints than independent variables."
" Consider how your constraints are setup, or use"
" factorization_method='SVDFactorization'."
) from e
else:
raise e This would give the following error trace:
|
Describe your issue.
I am trying to solve constrained optimization problems using scipy.optimize.minimize with method='trust-constr'. My problems typically involve both equality and inequality nonlinear constraints. For instance, let's say I want to solve the following problem
$\text{minimize} \quad x^4 - \frac{7}{4}x^3 - \frac{1}{2}x^2 + \frac{27}{16}x + \frac{23}{16}$
$\text{subject to} -x \leq -\frac{1}{2}, \sin(x) = 0, \cos(2x + \frac{\pi}{2}) = 0$ .
I am getting errors when
scipy.optimize.minimize
is called, and in particolar when the QR decomposition of the Jacobian is computed withinequality_constrained_sqp
, since the R matrix is not squared for some reason.This error is not raised when I only use inequality constraints (since the
equality_constrained_sqp
is not invoked), or when I only use one equality constraint.I could not find much documentation apart from this link and this link. I wonder whether I am calling the function with bad parameters, in which case I think a better error should be raised, or whether this is a bug.
Reproducing Code Example
Error message
SciPy/NumPy/Python version and system information
The text was updated successfully, but these errors were encountered: