First: your code is of the form
You need to move the "global x" to before you assign the value to x.
Second: either you make a mistake in constructing the values, or else the function goes to +/- infinity within the bounds to be explored, or else the function is discontinuous and the estimation of the derivative is not possible because of that.
You need to re-check how you construct your values, and you need to recheck whether the function is discontinuous or has singularities.
Third: your function either suffers badly from numeric round-off, or else your function is discontinuous. The optimizer has found a point where it cannot step finely enough to find a point that might possibly solve the problem because it has detected that if a solution exists it must be within an interval smaller than the smallest step size you allow, but at both the upper and lower bound of the interval with the potential solution, the constraints cannot be satisfied.
You need to check that your function is continuous and you need to check that your constraints can be satisfied taking into account the limits of double precision calculations.
In summary: you probably asked it to solve a discontinuous optimization, and it cannot find the solution because of the discontinuity.