这个递归语句为什么错了?

0 投票
3 回答
746 浏览
提问于 2025-04-15 11:24

这是一个银行模拟程序,它考虑了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()

3 个回答

1

你需要拥有:

return getNormal(self)

而不是

getNormal(self)

其实,真的不需要用递归:

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

    return normal
1

我不是很确定,但我觉得你需要把你的方法改成下面这样:

def getNormal(self):

normal = normalvariate(40,20)

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

我觉得你想要的是

return getnormal(self)

而不是

getnormal(self)

如果一个函数在没有遇到返回语句的情况下结束,那么它会返回一个特殊的值None,这个值是NoneType类型的对象——这就是为什么Python会对'NoneType'发出警告。abs()函数需要一个数字,但它不知道如何处理None。

另外,你可以通过使用

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

来避免递归(以及创建新堆栈帧的开销)。

撰写回答