开发者

Infinite loop in simulation

I'm starting out in python.. The details I have written in the below.. It goes to an infinite loop and give me an error when I try to call the function inside itself.. Is this kind of recursion not allowed ?

Posting code below.. Thanks for all your help :)

The program assumes that we have 100 passengers boarding a plane. Assuming if the first one has lost his boarding pass, he finds a random seat and sits there. Then the other incoming passengers sit in their places if unoccupied or some other random seat if occupied. The final aim is to find the probability with which the last passenger will not sit in his/her own seat. I haven't added the loop part yet which would make it a proper simulation. The question above is actually a puzzle in probability. I am trying to verify the answer as I don't really follow the reasoning.

import random
from numpy import zeros

rand = zeros((100,3))
# The rows are : Passenger number , The seat he is occupying and if his designated     seat is occupied. I am assuming that the passengers have seats which are same as the order in which they enter. so the 1st passenger enter has a designated seat number 1, 2nd to enter has no. 2 etc.

def cio(r):  # Says if the seat is occupied ( 1 if occupied, 0 if not)
    if rand[r][2]==1:
        return 1
    if rand[r][2]==0:
        return 0

def assign(ini,mov):    # The first is passenger no. and the second is the final seat he gets. So I keep on chaning the mov variable if the seat that he randomly picked was occupied too. 
    if cio(rand[mov][2])== 0 :
        rand[mov][2] = 1
        rand[mov][1] = ini
    elif cio(rand[mov][2])== 1 :
        mov2 = random.randint(0,99)
 #       print(mov2)            Was used to debug.. didn't really help
        assign(ini,mov2)        # I get the error pointing to this line :(

# Defining the first passenger's stats.
rand[0][0] = 1
rand[0][1] = random.randint(1,100)
m = rand[0][1]
rand[m][2]= 1

for x in range(99):
    rand[x+1][0] = x + 2

for x in range(99):
    assign(x+1,x+1)

if rand[99][0]==rand[99][1] :
    print(1);
else :
    print(0);

Please tell me if y'all get the same error.. ALso tell me if I am breaking any rules coz thisi sthe first question I'm posting.. Sorry if it seems too long.

This is how it should've been... The code does work fine in this case with the following mods :

def assign(ini,mov):
if cio(mov)== 0 :     """Changed here"""
    rand[mov][2] = 1
    rand[mov][1] 开发者_如何学JAVA= ini
elif cio(mov)== 1 :    """And here"""
    mov2 = random.randint(0,99)
    assign(ini,mov2)  

I am using Python 2.6.6 on Windows 7, using a software from Enthought Academic Version of Python. http://www.enthought.com/products/getepd.php

Also the answer to this puzzle is 0.5 which is actually what I am getting(almost) by running it 10000 times.

I didn't see it here but it had to be available online.. http://www.brightbubble.net/2010/07/10/100-passengers-and-plane-seats/


Recursion, while allowed, isn't your best first choice for this.

Python enforces an upper bound on recursive functions. It appears that your loop exceeds the upper bound.

You really want some kind of while loop in assign.

def assign(ini,mov):    
   """The first is passenger no. and the second is the final seat he gets. So I keep on chaning the mov variable if the seat that he randomly picked was occupied too. 
   """
   while cio(rand[mov][2])== 1:
      mov = random.randint(0,99)

   assert cio(rand[mov][2])== 0
   rand[mov][2] = 1
   rand[mov][1] = ini

This may be more what you're trying to do.

Note the change to your comments. Triple-quoted string just after the def.


you may be able to find the exact solution using dynamic programming http://en.wikipedia.org/wiki/Dynamic_programming For this you will need to add memoization to your recursive function: What is memoization and how can I use it in Python?

If you just want to estimate the probability using simulation with random numbers then I suggest you break out of your recursive function after a certain depth when the probability is getting really small because this will only change some of the smaller decimal places (most likely.. you may want to plot the change in result as you change the depth).

to measure the depth you could add an integer to your parameters: f(depth): if depth>10: return something else: f(depth+1)

the maximum recursion depth allowed by default is 1000 although you can change this you will just run out of memory before you get your answer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜