0

I'm trying to print a function which uses several parameters from numpy array's and lists, but I keep getting the error "numpy.float 64 object is not iterable". I've looked at several questions on the forum about this topic and tried different answers but none seem to work (or I might be doing something wrong I'm still a beginner at python) but it all comes down to the same thing, I'm stuck and I hope you guys can help. I'm using python 2.7, this is the code:

EDIT: Included the error message and changed the print to "print(T, (obj(T),))"

   from __future__ import division
   import numpy as np
   import random

   K = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1,])
   x = len(K)
   #Production rates and demand rates of products setup costs and holding costs (p, d, c, h)
   p = np.array([193, 247, 231, 189, 159])
   d = np.array([16, 16, 21, 19, 23])
   #c1 = np.array([random.random() for _ in range(x)]) use these values as test values for c
   c = [0.752, 0.768, 0.263, 0.152, 0.994, 0.449, 0.431, 0.154, 0.772]
   h = [0.10*c[i]/240 for i in range(x)]
   n = len(p)
   t = [10.76, 74.61, 47.54, 29.40, 45.00, 90.48, 17.09, 85.19, 35.33]


   def obj(T):
      for i in range(n):
         for q in range(x):
             for k in range(x):
                 return ((1. / T) * c[q] + sum((.5*h[k]*(p[i]-d[i])* (p[i]/d[i])*(t[k])**2)))

   for T in range(200, 900):
       print(T, (obj(T),))

 runfile('C:/Users/Jasper/Anaconda2/Shirodkar.py',     wdir='C:/Users/Jasper/Anaconda2')
 Traceback (most recent call last):

File "<ipython-input-1-0cfdc6b9fe69>", line 1, in <module>
runfile('C:/Users/Jasper/Anaconda2/Shirodkar.py',  wdir='C:/Users/Jasper/Anaconda2')

  File "C:\Users\Jasper\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
execfile(filename, namespace)

  File "C:\Users\Jasper\Anaconda2\lib\site-  packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/Jasper/Anaconda2/Shirodkar.py", line 24, in <module>
print(T, (obj(T),))

   File "C:/Users/Jasper/Anaconda2/Shirodkar.py", line 21, in obj
return ((1. / T) * c[q] + sum((.5*h[k]*(p[i]-d[i])*(p[i]/d[i])*(t[k])**2)))

 TypeError: 'numpy.float64' object is not iterable
2
  • 3
    Please post the full stack trace, including the line where the error occurs Commented Oct 12, 2016 at 14:33
  • What do you think an unconditional return in a loop does? Commented Oct 12, 2016 at 15:32

1 Answer 1

2

I suspect the problem is here:

sum((.5*h[k]*(p[i]-d[i])* (p[i]/d[i])*(t[k])**2))

The end result of that expression is a float, isn't it? What is the sum() for?

Sign up to request clarification or add additional context in comments.

4 Comments

It is an equation to sum up the holding costs of different products, this is the math:
Have a look at the documentation of the Python builtin sum: docs.python.org/2/library/functions.html#sum. It requires an iterable (e.g. a list) its argument. However, you are trying to sum up a single value (a numpy.float64).
1/T [ c(q)+∑_(k=1)^k▒〖1/(2 )*h^k (p^k-d^k )(p^k/d^k 〗)(t^k )**2
Thanks your combined answers made me realise the sum() wasn't needed, it works now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.