0

I don't know why this probelm , all the time the varaiable of parameters out of range , and now also the problem is for code and give me this eroor object of type 'Bounds' has no len():

(I hope i can find the error in my code or find other good code for using bounds of variabels)

Traceback (most recent call last):

File "./trigger_SLQ.py", line 160, in <module>
main()

File "./trigger_SLQ.py", line 156, in main
opt(x0)
File "./trigger_SLQ.py", line 148, in opt

res = basinhopping(rmse, x0, minimizer_kwargs=minimizer_kwargs, T=10000.0, 
stepsize=1.0, niter=100, niter_success=200, accept_test=bounds)

File "/usr/lib/python3/dist-packages/scipy/optimize/_basinhopping.py", line 
611, in basinhopping
accept_tests, disp=disp)

File "/usr/lib/python3/dist-packages/scipy/optimize/_basinhopping.py", line 
72, in __init__
minres = minimizer(self.x)

File "/usr/lib/python3/dist-packages/scipy/optimize/_basinhopping.py", line 
280, in __call__
return self.minimizer(self.func, x0, **self.kwargs)

File "/usr/lib/python3/dist-packages/scipy/optimize/_minimize.py", line 455, 
in minimize
constraints, callback=callback, **options)

File "/usr/lib/python3/dist-packages/scipy/optimize/slsqp.py", line 329, in 

_minimize_slsqp

if bounds is None or len(bounds) == 0:

TypeError: object of type 'Bounds' has no len()

I use this code

  b0 = (5, 95)

  b1 = (0.10, 0.95)

  b2= (2, 8)

  bounds=((5, 95), (0.10, 0.95), (2, 8))

class Bounds(object):

def __init__(self, xmax=[0,0,2], xmin=[100,0.100,8], stepsize=0.01):
    self.xmax = np.array(xmax)
    self.xmin = np.array(xmin)
    self.stepsize = stepsize

def __call__(self, x):

    min_step = np.maximum(self.xmin - x, -self.stepsize)
    max_step = np.minimum(self.xmax - x, self.stepsize)

    random_step = np.random.uniform(low=min_step, high=max_step, size=x.shape)

    xnew = x + random_step

    return xnew

    bounded_step = Bounds(np.array([b[0] for b in bounds]), np.array([b[1] 
    for b in bounds]), np.array([b[2] for b in bounds]))


def opt(x0):

bounds = Bounds()

minimizer_kwargs = {'method':'SLSQP', 'bounds':bounds,'options':{'maxiter':1000, 'ftol':1e-2, 'eps':1e-2},}
res = basinhopping(rmse, x0, minimizer_kwargs=minimizer_kwargs, T=10000.0, stepsize=1.0, niter=100, niter_success=200, accept_test=mybounds)
print(res.x)

return res.x


def main():

   x0 = np.array((50.0, 0.50, 5.0)) # initial guess
   opt(x0)


if __name__ == "__main__":
   main()
Azeezof
  • 3
  • 1
  • 5
  • Show the complete error stack-trace. It probably exactly tells you the line where it's happening. – sascha Nov 08 '17 at 12:20
  • File "/usr/lib/python3/dist-packages/scipy/optimize/slsqp.py", line 329, in _minimize_slsqp if bounds is None or len(bounds) == 0: TypeError: object of type 'Bounds' has no len() – Azeezof Nov 08 '17 at 12:33
  • That's not the complete stack-trace. And add it formatted to your question as edit....... – sascha Nov 08 '17 at 12:33
  • So scipy's bounds argument need to be a list of pairs (or maybe a single tuple) while you are giving a custom class-object (which has no len as python says). Why would you expect it to work? Stick to the API. Your bounds class has nothing to do with scipy's need for bounds. It looks more like something for basinhopping's take_step. – sascha Nov 08 '17 at 12:53
  • yes sure, i did it now – Azeezof Nov 08 '17 at 12:54
  • The problem, i couldn't find method for bounds of variables, because of all methods don't working? – Azeezof Nov 08 '17 at 13:04
  • I already answered to some question of yours a few days ago and it's working. – sascha Nov 08 '17 at 13:05
  • if you notice i use your method with littel changes because i have three elements for x0 but also this method guess negative values for parameters and if you notics all my bounds is positive – Azeezof Nov 08 '17 at 13:12
  • Little changes? You broke it completely! There is no mybounds in your code and 'bounds'=bounds is wrong as explained. The above code is not running at all so there is nothing to analyze. – sascha Nov 08 '17 at 13:13
  • If you used some code from somewhere to base your program, you may want to reference it if it's relevant. Regarding your error, your `Bounds` class has no `__len__()` method, but even if you accounted for that, you need to understand what structure the keyword `bounds` expects as input. – Reti43 Nov 08 '17 at 13:17
  • sascha what you think if i delete ( bounds = Bounds()) , it will be same ycode , right? – Azeezof Nov 08 '17 at 13:44
  • The code of yours has no ```mybounds```. It won't run. And even when repairing that, removing bounds is different from my approach. I really don't get the confusion. My [answer](https://stackoverflow.com/a/47058263/2320035) did explain every component and even has a **reproducible example!** – sascha Nov 08 '17 at 13:45
  • @ sacha , I am very grateful to you , i hope you can help me in my problem as you did already, i use this code to get results for my Phd thesis , i use python for just one year so i am so sorry if i don't understand some comments about your code – Azeezof Nov 08 '17 at 13:52
  • The code you provided does not output anything. Without showing code, which runs and shows the problem you are observing with a clear description of the expected output, there can't be no help. I'm sorry for your trouble in regards to your academic work, but your questions are all of very low-quality (impossible to help; conceptually and often wrong formatting) and i think it's getting worse every time. Remember when i asked you to prepare a question and only post it when you used 1 hour or more to prepare it? You posted something new within 10 mins and it was even worse than the original one. – sascha Nov 08 '17 at 13:53

0 Answers0