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

2024-10-04 01:29:51 发布

您现在位置:Python中文网/ 问答频道 /正文

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

车站模块:

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)

到目前为止,这一切都很好。我可以循环浏览站点,打印属性

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

#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“customer”对象不可调用

我也不能打印任何属性。在我看来,我用同样的方式初始化电台和客户,但不知怎么的,电台工作,客户不工作,我不知道为什么

在Python3.5.4 64位上运行


Tags: thefromimportself客户defcustomerstation
1条回答
网友
1楼 · 发布于 2024-10-04 01:29:51

当您想要创建customer实例时,您正在执行一些奇怪的操作:

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

尝试一下,但是你应该阅读你帖子下面的评论

玩得开心:)

相关问题 更多 >