我的脚本没有在循环中创建类的实例[python]

问题描述 投票:0回答:1

我正在为业务流程编写模拟。客户到达公司并经过多个站点。每个站都有处理时间,容量等。每个客户都有到达时间,空闲时间等。我创建了一个工作站类和一个客户类,以将这些值保存在每个客户/工作站的类的实例中。

工作站模块:

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)

客户模块

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)

然后在主模块中,我首先设置了工作站:

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)

到目前为止,效果很好。我可以循环浏览电台,打印属性。

然后我设置一个代码来确定模拟的周期,并希望在每个到达时间创建一个客户实例:

#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

这里出现问题。当我打印客户时,它将客户标识为类对象。但是,当下一个客户“到达”并且if函数为true时,我得到

TypeError'客户'对象是不可调用的。

而且我也无法打印任何属性。对我来说,好像我以相同的方式初始化车站和客户,但是车站以某种方式工作而客户却无法使用,我不知道为什么。

在python 3.5.4 64位上运行

python python-3.x class
1个回答
0
投票

[当您要创建客户实例时,您正在做一些奇怪的事情:

    # 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)

尝试一下,但您应该阅读帖子下的评论。

玩得开心:)

© www.soinside.com 2019 - 2024. All rights reserved.