I know this is a very old question, but I was intrigued.
When does this happen?
This problem occurs when the optimization function is not reliably differentiable. If you use a nice smooth function like:
opt = numpy.array([2, 2, 2, 2, 2]) def func(x): return sum((x - opt)**2)
The problem goes away.
How to impose strict restrictions?
Please note that none of the limited algorithms in scipy.minimize guarantees that a function will never be evaluated outside of the restrictions. If you need it, you should use conversions. So, for example, to ensure that no negative values ββfor x [3] are ever used, you can use the transformation x3_real = 10^x[3] . Thus, x [3] can be any value, but the variable you use will never be negative.
Deeper analysis
Examining the Fortran code for slsqp provides the following information about when this error occurs. The procedure returns the MODE variable, which can take these values:
C* MODE = -1: GRADIENT EVALUATION, (G&A) * C* 0: ON ENTRY: INITIALIZATION, (F,G,C&A) * C* ON EXIT : REQUIRED ACCURACY FOR SOLUTION OBTAINED * C* 1: FUNCTION EVALUATION, (F&C) * C* * C* FAILURE MODES: * C* 2: NUMBER OF EQUALITY CONTRAINTS LARGER THAN N * C* 3: MORE THAN 3*N ITERATIONS IN LSQ SUBPROBLEM * C* 4: INEQUALITY CONSTRAINTS INCOMPATIBLE * C* 5: SINGULAR MATRIX E IN LSQ SUBPROBLEM * C* 6: SINGULAR MATRIX C IN LSQ SUBPROBLEM *
The part that assigns mode 4 (which is the error you get) is as follows:
C SEARCH DIRECTION AS SOLUTION OF QP - SUBPROBLEM CALL dcopy_(n, xl, 1, u, 1) CALL dcopy_(n, xu, 1, v, 1) CALL daxpy_sl(n, -one, x, 1, u, 1) CALL daxpy_sl(n, -one, x, 1, v, 1) h4 = one CALL lsq (m, meq, n , n3, la, l, g, a, c, u, v, s, r, w, iw, mode) C AUGMENTED PROBLEM FOR INCONSISTENT LINEARIZATION IF (mode.EQ.6) THEN IF (n.EQ.meq) THEN mode = 4 ENDIF ENDIF
So basically you can see that he is trying to find the direction of the descent, if the restrictions are active, he is trying to get a derivative estimate for the restriction and fails with the singular matrix in the subproblem lsq ( mode = 6 ), then this explains that if all the equations the restrictions were evaluated, and none of them gave successful descent directions, this should be a contradictory set of restrictions ( mode = 4 ).