May-05-2019, 02:48 AM
(This post was last modified: May-05-2019, 02:48 AM by larkypython.)
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:
Please help out, this already hindered me for weeks.
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 definedI also tried to import them one by one:
from scipy.optimize import minimize from scipy.optimize import Bounds from scipy.optimize import LinearConstraintbut, 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.