0

这是一个银行模拟,考虑到 20 条不同的服务线路和一个队列,客户以指数速率到达,并且他们在遵循平均 40 和标准偏差 20 的正态概率分布的时间内得到服务。

在我决定使用这种方法排除正态分布给出的负值之前,一切都很好:

def getNormal(self):

    normal = normalvariate(40,20)

    if (normal>=1):
        return normal
    else:
        getNormal(self)

我搞砸了递归调用吗?我不明白为什么它不起作用。我已将 getNormal() 方法更改为:

def getNormal(self):

    normal = normalvariate(40,20)

    while (normal <=1):
        normal = normalvariate (40,20)

    return normal

但我很好奇为什么前面的递归语句被破坏了。

这是完整的源代码,如果您有兴趣。

""" bank21: One counter with impatient customers """
from SimPy.SimulationTrace import *
from random import *

## Model components ------------------------



class Source(Process):
    """ Source generates customers randomly """

    def generate(self,number):       
        for i in range(number):

            c = Customer(name = "Customer%02d"%(i,))

            activate(c,c.visit(tiempoDeUso=15.0))


            validateTime=now()
            if validateTime<=600:
                interval = getLambda(self)
                t = expovariate(interval)

                yield hold,self,t #esta es la rata de generación
            else:
                detenerGeneracion=999
                yield hold,self,detenerGeneracion



class Customer(Process):
    """ Customer arrives, is served and  leaves """

    def visit(self,tiempoDeUso=0):       
        arrive = now()       # arrival time                     
        print "%8.3f %s: Here I am     "%(now(),self.name)

        yield (request,self,counter),(hold,self,maxWaitTime)    
        wait = now()-arrive  # waiting time                     
        if self.acquired(counter):                              
            print "%8.3f %s: Waited %6.3f"%(now(),self.name,wait)

            tiempoDeUso=getNormal(self)
            yield hold,self,tiempoDeUso
            yield release,self,counter                          
            print "%8.3f %s: Completed"%(now(),self.name)
        else:
            print "%8.3f %s: Waited %6.3f. I am off"%(now(),self.name,wait)

## Experiment data -------------------------

maxTime = 60*10.5  # minutes                                 
maxWaitTime = 12.0 # minutes. maximum time to wait              

## Model  ----------------------------------

def model():                                                    
    global counter                                              
    #seed(98989)
    counter = Resource(name="Las maquinas",capacity=20)                            
    initialize()
    source = Source('Source')
    firstArrival= expovariate(20.0/60.0) #chequear el expovariate
    activate(source,
             source.generate(number=99999),at=firstArrival)   
    simulate(until=maxTime)                                     


def getNormal(self):

    normal = normalvariate(40,20)

    if (normal>=1):
        return normal
    else:
        getNormal(self)

def getLambda (self):

        actualTime=now()
        if (actualTime <=60):
            return 20.0/60.0
        if (actualTime>60)and (actualTime<=120):
            return 25.0/60.0
        if (actualTime>120)and (actualTime<=180):
            return 40.0/60.0
        if (actualTime>180)and (actualTime<=240):
            return 30.0/60.0
        if (actualTime>240)and (actualTime<=300):
            return 35.0/60.0
        if (actualTime>300)and (actualTime<=360):
            return 42.0/60.0
        if (actualTime>360)and (actualTime<=420):
            return 50.0/60.0
        if (actualTime>420)and (actualTime<=480):
            return 55.0/60.0
        if (actualTime>480)and (actualTime<=540):
            return 45.0/60.0
        if (actualTime>540)and (actualTime<=600):
            return 10.0/60.0
## Experiment  ----------------------------------
model()
4

3 回答 3

8

我想你想要

return getnormal(self)

代替

getnormal(self)

如果函数在没有遇到 return 语句的情况下退出,那么它会返回特殊值 None,它是一个 NoneType 对象——这就是 Python 抱怨“NoneType”的原因。abs() 函数需要一个数字,但它不知道如何处理 None。

此外,您可以通过使用避免递归(以及创建新堆栈帧的成本)

def getNormal(self):
    normal = 0
    while normal < 1:
        normal = normalvariate(40,20)
    return normal
于 2009-05-05T03:12:39.460 回答
1

你需要有:

return getNormal(self)

代替

getNormal(self)

但实际上,不需要递归:

def getNormal(self):
    normal = 0
    while normal < 1:
        normal = normalvariate(40,20)

    return normal
于 2009-05-05T03:13:04.263 回答
1

我不完全确定,但我认为您需要将方法更改为以下内容:

def getNormal(self):

normal = normalvariate(40,20)

if (normal>=1):
    return normal
else:
    return getNormal(self)
于 2009-05-05T03:17:38.403 回答