简体   繁体   中英

My script doesn't create an instance of a class in a loop [python]

I am writing a simulation for a business process. Customers arrive at the business and go through a number of stations. Each station has processing times, capacity etc. Each customer has arrival times, idle times, etc. I created a station class and a customer class to save these values in instances of the classes for each customer / station.

Station module:

from scipy.stats import norm
import random

#define station class
class Station:

#Initialise instance
def __init__(self, StaID):
    self.StaID = StaID

#assign instance values
def set_StaIdle(self, StaIdle):
    self.StaIdle = StaIdle

def set_StaMean(self, StaMean):
    self.StaMean = StaMean 

def set_StaSD(self, StaSD):
    self.StaSD = StaSD 
(... the rest of the attributes)

Customer module

from scipy.stats import poisson
import random
import math
#define the customer class
class Customer:

#constructor method to initialise variables
def __init__(self, CustID):
    self.CustID = CustID

#functions to assign values to instances
def set_IsIdle(self, IsIdle):
    self.IsIdle = IsIdle

def set_Station(self, Station):
    self.Station = Station

def set_IdelTime(self, IdleTime):
    self.IdleTime = IdleTime

def set_StartTime(self, StartTime):
    self.StartTime = StartTime

def pois_Arrival(ticker):
    m = 15
    p = random.randint(1,99) / 100
    x = poisson.ppf(p, mu = m)
    if x >= 1:
        x = math.trunc(x)
    else:
        x = 1
    arrival = x + ticker
    return arrival
(... the rest of the attributes)

Then in the main module, I first set up the stations:

from customers import Customer
from customers import pois_Arrival 
from stations import Station

#Set up stations
stationMeans = [18, 10, 15]
stationSDs = [4, 3, 5]
stationNext = [1, 2, -1]
stationCapacity = [2, 1, 2]

for i in range(0,len(stationMeans)):
    stations.append(Station(i))
    stations[i].set_StaMean(stationMeans[i])
    stations[i].set_StaSD(stationSDs[i])
    stations[i].set_NextSta(stationNext[i])
    stations[i].set_Capacity(stationCapacity[i])
    stations[i].set_Serving(0)

This works fine so far. I can loop through stations, print attributes.

Then I set a ticker to determine the periods of the simulation and want to create a customer instance at each of the arrival times:

#set timer
ticksTotal = 2880
#set first customer 
nextCustomerArrival = 1
custCount = 1
tick = 1
#start ticker loop
for tick in range (1,ticksTotal):
    #check if customer has arrived
    if tick == nextCustomerArrival:
        #create new customer
        i = custCount - 1

        customers.append(Customer(custCount))
        customers[i].set_StartTime =1
        customers[i].set_NextSta = 0
        customers[i].set_IsIdle = 1
        customers[i].set_IdleTime = 0
        customers[i].set_Entered = tick


        #determine next arrival
        nextCustomerArrival = pois_Arrival(tick)
        custCount = custCount + 1

Here something goes wrong. When I print customers it identifies customer as a class object. However when the next customer "arrives" and the if-function is true, I get

TypeError 'customer' object is not callable.

Also I can't print any of the attributes. To me it looks like I initialise the stations and the customers in the same way, but somehow the stations work and the customers don't and I can't figure out why.

Running on python 3.5.4 64-bit

When you want to create an instance of customer, you are doing something strange:

    # Here you did this
    customers[i].set_StartTime =1
    customers[i].set_NextSta = 0
    customers[i].set_IsIdle = 1
    customers[i].set_IdleTime = 0
    customers[i].set_Entered = tick

    # Maybe you wanna do this ?
    customers[i].set_StartTime(1)
    customers[i].set_NextSta(0)
    customers[i].set_IsIdle(1)
    customers[i].set_IdleTime(0)
    customers[i].set_Entered(tick)

Try that but you should read the comments under your post.

Have fun:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM