Python Forum
Python : NameError: name 'total_wait' is not defined
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python : NameError: name 'total_wait' is not defined
#1
I'm trying to plot the results, but I get this error:
Error:
NameError: name 'total_wait' is not defined.
I am able to print the result but unable to get the value in my graph.
May I know where I did it wrong?




import numpy as np

class Simulation:

   def generate_interarrival(self):
       return np.random.poisson(1./3)

   def generate_service(self):
       return np.random.poisson(1./4)  


   def __init__(self):
       self.queue = []
       self.averageWaitingTime=[]
       self.total_wait=[]

       self.num_in_system = 0

       self.clock = 0.0
       self.t_arrival = self.generate_interarrival()
       self.t_depart = float('inf')

       self.num_arrivals = 0
       self.num_departs = 0
       self.total_wait = 0.0
       self.averageWaitingTime= 0.0       


   def advance_time(self):
       t_event =min(self.t_arrival, self.t_depart)
       self.total_wait += self.num_in_system*(t_event - self.clock)  

       print self.total_wait, 'customer total wait' 



       self.clock = t_event


       if self.t_arrival <=self.t_depart:
           self.handle_arrival_event()
       else:
          self.handle_depart_event()
          self.averageWaitingTime =self.total_wait/self.t_depart
          print  self.averageWaitingTime, ' average waiting time per customer'    


   def handle_arrival_event(self ):
       self.num_in_system +=1
       self.num_arrivals += 1

       num_arrivals = self.clock+ self.generate_interarrival() 
       print num_arrivals, ' customer {} interarrival'.format(i)



       if self.num_in_system <=1:
               self.t_depart = self.clock + self.generate_service()        
       self.t_arrival = self.clock + self.generate_interarrival()


   def handle_depart_event(self):
       self.num_in_system -=1
       self.num_departs += 1        
       num_departs = self.clock 
       print num_departs, ' customer {} departed'.format(i)


       if self.num_in_system>0:
           self.t_depart = self.clock + self.generate_service()
       else:
          self.t_depart = float(' inf')


np.random.seed(0)
s = Simulation()
for i in range (1000):
   s.advance_time()    

import matplotlib.pyplot as plt
plt.plot(total_wait)
plt.ylabel('Waiting time(min)')
plt.xlabel('Number of Customers')
Reply
#2
Quote:plt.plot(total_wait)
total_plot doesnt exist, but there is a class attribute for Simulation
s is your Simulation object in which has an attribute total_wait. Within a class definition you use self.total_wait, outside of the class you need to refer to it using the object, in this case 's'
Quote:s = Simulation()
try this....
plt.plot(s.total_wait)
Recommended Tutorials:
Reply
#3
I have try that but it's not working. I don't why
Reply
#4
(Jun-04-2017, 05:43 PM)carla Wrote: but it's not working
in what way? Do you get another error? Post all errors in full
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError: name 'predicted_labels' is not defined hobbyist 2 710 Dec-08-2023, 02:18 PM
Last Post: hobbyist
  NameError: name 'rand' is not defined Truman 7 6,734 Jun-17-2020, 07:14 PM
Last Post: Truman
  NameError mlab not defined PythonAndArduino 5 6,946 Nov-08-2017, 06:47 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020