Skip to content
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

scipy.optimize.minimise - ValueError: x0 violates bound constraints #14084

Closed
Kieran-Bacon opened this issue May 18, 2021 · 8 comments
Closed

Comments

@Kieran-Bacon
Copy link

Kieran-Bacon commented May 18, 2021

I'm getting an x0 violates bound constraints while running the minimize function - it seems that this issue doesn't appear for all targets but appears almost randomly.

Investigating where the issue is raised and printing out the values - you can see that an equality fails rasing an error, despite the values on first look seeming fine.

Looking precisely at those values the x0 margins turns out to be 0.0499999999999

It shouldn't be the case that the minimize functions updates x0 to an invalid value. (also the documentation (https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html) doesn't even mention that this error can be raised.)

python3.6.9
scipy==1.5.4

Reproducing code example:

def example(self, forecastAJVTarget):
        def objective(margins):
            x = -self._calculateForecastRevenue(margins)
            print('objective {} {}'.format(margins, x))
            return x


        # Constrain the margins to not result in an AJV greater than the target AJV for reactive works
        # constraint = lambda margins: forecastAJVTarget - self._calculateForecastAJV(margins)
        def constraint(margins):
            x = forecastAJVTarget - self._calculateForecastAJV(margins)
            print('constraint {} {}'.format(margins, x))
            return x

        # Find the margins the tends to have maxmise profits and bring the AJV value as class to the agreed amount as possible
        result = minimize(
            objective,
            x0 = self.initialMargins,  # [.2,.2,.2,.2,.2]
            bounds = self.marginBoundaries,  # [(0.05, 1.), (0.05, 1.), (0.05, 1.), (0.05, 0.7), (0.05, .5)]
            method = 'SLSQP',
            constraints = {
                'type': 'eq', # Adjust the margins so that the forecast AJV and the optimal AJV are `equal` (value should be zero)
                'fun': constraint # Get the difference between forecast AJV and optimal AJV
            }
        )

Error message:

constraint [0.2 0.2 0.2 0.2 0.2] 140.46597017171308
objective [0.2 0.2 0.2 0.2 0.2] -4498431.099914347
objective [0.20000001 0.2        0.2        0.2        0.2       ] -4498431.100549523
objective [0.2        0.20000001 0.2        0.2        0.2       ] -4498431.102364394
objective [0.2        0.2        0.20000001 0.2        0.2       ] -4498431.10764781
objective [0.2        0.2        0.2        0.20000001 0.2       ] -4498431.11932903
objective [0.2        0.2        0.2        0.2        0.20000001] -4498431.125540859
constraint [0.2 0.2 0.2 0.2 0.2] 140.46597017171308
constraint [0.2 0.2 0.2 0.2 0.2] 140.46597017171308
constraint [0.20000001 0.2        0.2        0.2        0.2       ] 140.4659700887923
constraint [0.2        0.20000001 0.2        0.2        0.2       ] 140.46596985220742
constraint [0.2        0.2        0.20000001 0.2        0.2       ] 140.46596940376378
constraint [0.2        0.2        0.2        0.20000001 0.2       ] 140.4659688291963
constraint [0.2        0.2        0.2        0.2        0.20000001] 140.46596897257996
objective [1.   1.   1.   0.7  0.05] -5472738.3886545645
constraint [1.   1.   1.   0.7  0.05] 44.655374734706015
<class 'numpy.ndarray'> <class 'numpy.ndarray'> float64 float64
constraint [0.2 0.2 0.2 0.2 0.2] 140.46597017171308
objective [0.2 0.2 0.2 0.2 0.2] -4498431.099914347
objective [0.20000001 0.2        0.2        0.2        0.2       ] -4498431.100549523
objective [0.2        0.20000001 0.2        0.2        0.2       ] -4498431.102364394
objective [0.2        0.2        0.20000001 0.2        0.2       ] -4498431.10764781
objective [0.2        0.2        0.2        0.20000001 0.2       ] -4498431.11932903
objective [0.2        0.2        0.2        0.2        0.20000001] -4498431.125540859
constraint [0.2 0.2 0.2 0.2 0.2] 140.46597017171308
constraint [0.2 0.2 0.2 0.2 0.2] 140.46597017171308
constraint [0.20000001 0.2        0.2        0.2        0.2       ] 140.4659700887923
constraint [0.2        0.20000001 0.2        0.2        0.2       ] 140.46596985220742
objective [1.   1.   1.   0.7  0.05] -5472738.3886545645
constraint [1.   1.   1.   0.7  0.05] 44.655374734706015
<class 'numpy.ndarray'> <class 'numpy.ndarray'> float64 float64
Traceback (most recent call last):
  File "<string>", line 8, in <module>
  File "/home/vscode/.local/lib/python3.6/site-packages/scipy/optimize/_minimize.py", line 626, in minimize
    constraints, callback=callback, **options)
  File "/home/vscode/.local/lib/python3.6/site-packages/scipy/optimize/slsqp.py", line 427, in _minimize_slsqp
    g = append(sf.grad(x), 0.0)
  File "/home/vscode/.local/lib/python3.6/site-packages/scipy/optimize/_differentiable_functions.py", line 188, in grad
    self._update_grad()
  File "/home/vscode/.local/lib/python3.6/site-packages/scipy/optimize/_differentiable_functions.py", line 171, in _update_grad
    self._update_grad_impl()
  File "/home/vscode/.local/lib/python3.6/site-packages/scipy/optimize/_differentiable_functions.py", line 92, in update_grad
    **finite_diff_options)
  File "/home/vscode/.local/lib/python3.6/site-packages/scipy/optimize/_numdiff.py", line 392, in approx_derivative
    raise ValueError("`x0` violates bound constraints. {} {} {} {} {}".format(x0, lb, ub, (x0 < lb), (x0 > ub)))
ValueError: `x0` violates bound constraints. [1.   1.   1.   0.7  0.05] [0.05 0.05 0.05 0.05 0.05] [1.  1.  1.  0.7 0.5] [False False False False  True] [False False False False False]

Scipy/Numpy/Python version information:

@andyfaff
Copy link
Contributor

probably #11403, fixed in #13009.
It should be in scipy 1.6, what version of scipy are you using.

@Kieran-Bacon
Copy link
Author

Ah I am using scipy==1.5.4 - I will try this soon (I've made a fix for now and I need to get something done with this fix in place) and get back to you.

I did try to upgrade scipy but it suggested I had the most recent version - I will install it explicitly.

@Kieran-Bacon
Copy link
Author

Yeah...

ERROR: Could not find a version that satisfies the requirement scipy==1.6.* (from versions: 0.8.0, 0.9.0, 0.10.0, 0.10.1, 0.11.0, 0.12.0, 0.12.1, 0.13.0, 0.13.1, 0.13.2, 0.13.3, 0.14.0, 0.14.1, 0.15.0, 0.15.1, 0.16.0, 0.16.1, 0.17.0, 0.17.1, 0.18.0, 0.18.1, 0.19.0, 0.19.1, 1.0.0b1, 1.0.0rc1, 1.0.0rc2, 1.0.0, 1.0.1, 1.1.0rc1, 1.1.0, 1.2.0rc1, 1.2.0rc2, 1.2.0, 1.2.1, 1.2.2, 1.2.3, 1.3.0rc1, 1.3.0rc2, 1.3.0, 1.3.1, 1.3.2, 1.3.3, 1.4.0rc1, 1.4.0rc2, 1.4.0, 1.4.1, 1.5.0rc1, 1.5.0rc2, 1.5.0, 1.5.1, 1.5.2, 1.5.3, 1.5.4)

That version isn't released yet

@andyfaff
Copy link
Contributor

It's definitely released, https://pypi.org/project/scipy/1.6.3/

@Kieran-Bacon
Copy link
Author

Requires: Python >=3.7, <3.10

^^ that is my problem my b - so this issue is still apparent in the 3.6 version of python

@tupui
Copy link
Member

tupui commented May 19, 2021

Hi @Kieran-Bacon, as you pointed out we are not supporting anymore python 3.6. That being said, you could try to install from source on Python 3.6–but again we would not provide support as we have limited ressources. In case you are not aware of it, Python 3.6 is out of support.

The issue being fixed I propose to close this then. @andyfaff, agreed?

@Kieran-Bacon
Copy link
Author

Yes - happy with that. Thanks for your help!

Where should I have seen that python3.6 was no longer supported for future reference?

@tupui
Copy link
Member

tupui commented May 19, 2021

For SciPy supported versions it is here: https://scipy.github.io/devdocs/dev/toolchain.html#python-versions
For Python itself my bad, there is security support only until the end of the year. Here is the relevant link: https://devguide.python.org/#status-of-python-branches

@tupui tupui closed this as completed May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants