Python Forum
cannot import scipy.optimize.Bounds - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: cannot import scipy.optimize.Bounds (/thread-17910.html)



cannot import scipy.optimize.Bounds - larkypython - Apr-29-2019

I used this:
from scipy import optimize
then it is ok to use optimize.minimize
but cannot use optimize.Bounds, cannot use optimize.LinearConstraint, saying no this module
then I tried
from scipy.optimize import Bounds
from scipy.optimize import LinearConstraint

it will report error

so the question is: I don't know how to import the Bounds and Linear Constraint module....

Please help out, thanks!


RE: cannot import scipy.optimize.Bounds - larkypython - May-05-2019

I wrote a very simple program to test the optimize of scipy, it all works for unconstrained optimizations, but when it comes to constrained optimization, the Bounds, LinearConstraint module will be needed, but I cannot import any of them. the program is as shown below:
from scipy import optimize
# from scipy.optimize import minimize
# from scipy.optimize import Bounds
# from scipy.optimize import LinearConstraint
import numpy

def f(x):
	return 0.5*(1-x[0])**2+(x[1]-x[0]**2)**2

bounds = Bounds([0, -0.5], [1.0, 2.0])

result=optimize.minimize(f, [0.5, -0.5], method='BFGS', bounds=bounds)
print(result.x)
the error says:

File "testopt.py", line 13, in <module>
    bounds = Bounds([0, -0.5], [1.0, 2.0])
NameError: name 'Bounds' is not defined
I also tried to import them one by one:
from scipy.optimize import minimize
from scipy.optimize import Bounds
from scipy.optimize import LinearConstraint
but, the error becomes:
Traceback (most recent call last):
  File "testopt.py", line 3, in <module>
    from scipy.optimize import Bounds
ImportError: cannot import name 'Bounds'
so no matter what, Bounds and LinearConstraint will never be found. I am wondering what kind of problem will this be, if the installation got problems, the minimize function should not work either.

Please help out, this already hindered me for weeks.


RE: cannot import scipy.optimize.Bounds - larkypython - May-05-2019

Problem solved, looks like I just need to upgrade scipy from 0.19 to 1.1 or above.