이 재귀 진술이 잘못된 이유는 무엇입니까?
문제
이것은 단일 대기열이있는 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()
해결책
나는 당신이 원한다고 생각합니다
return getnormal(self)
대신에
getnormal(self)
함수가 return 문에 도달하지 않고 종료되면, 특수 값이 없음을 반환합니다. 이는 비에 타이프 객체 인 Python이 '비에 타이프'에 대해 불평하는 이유입니다. ABS () 함수는 숫자를 원하며 아무것도하지 않고 무엇을 해야할지 모릅니다.
또한 사용하여 재귀 (및 새 스택 프레임 생성 비용)를 피할 수 있습니다.
def getNormal(self):
normal = 0
while normal < 1:
normal = normalvariate(40,20)
return normal
다른 팁
전적으로 확실하지는 않지만 방법을 다음과 같이 변경해야한다고 생각합니다.
def getNormal(self):
normal = normalvariate(40,20)
if (normal>=1):
return normal
else:
return getNormal(self)
당신은 다음과 같습니다.
return getNormal(self)
대신에
getNormal(self)
그러나 실제로 재귀가 필요하지 않습니다.
def getNormal(self):
normal = 0
while normal < 1:
normal = normalvariate(40,20)
return normal
제휴하지 않습니다 StackOverflow