你能用simpy做一个递归模拟吗

问题描述 投票:0回答:1

我试图进行递归实时模拟,看看它在简单的框架内是否可行。 该函数被用来跟踪日志字典...... 当我运行这段代码时,jupyter 内核停止工作并关闭,为什么?

def simStudy(env,AVERAGE_PROGRAMME_LENGTH):
    i = 0
    while i < int(3000/TYPING_RATE):
        ans = input("Target achieved?")
        log[env.now] = int(bool(ans))
        print(log)
        AVERAGE_PROGRAMME_LENGTH -= TYPING_RATE
        yield env.timeout(1)

env = sim.rt.RealtimeEnvironment(factor = 10,strict = True)

def startSim():
    try:
        env.process(simStudy(env,AVERAGE_PROGRAMME_LENGTH))
        env.run()
    except RuntimeError as e:
        startSim()
        print(str(e)[-8:-3])
        
        
startSim()
python simpy code-complexity
1个回答
0
投票

我在模拟中不经常使用递归。我的大多数进程都会有一个队列,如果一个实体需要相同的进程,我只需将其放回队列中并让队列处理器多次处理它。

这个例子是一次剥一层洋葱

这里是递归

"""
demonstrates how recursion can be used in simpy
by simulating the pealing of a onion

programmer: Michael R. Gibbs
"""

import simpy
import random

class Onion():
    """
    Simple onion object
    id:     unique oning id
    layers: number of layers to peal
    """

    # class var for generating ids
    id = 0

    def __init__(self):
        """
        initialize onion with unique id and random number of layers
        """

        Onion.id += 1
        self.id = Onion.id
        self.layers = random.randint(5,9)

 
def get_onions(env, res):
    """
    sim startup
    generate a bunch of onions to be pealed and 
    start pealing them
    """

    for i in range(5):
        onion = Onion()
        env.process(peal_onion_layer(env,res,onion,0))
        # note the lack of of a yeild so all onions get processed in parallel

def peal_onion_layer(env,res,onion,pealed):
    """
    gets a pealer resource and peals one onion layer
    and release pealer.
    will need to get the pealer resource again if another layer
    needs to be pealed
    """

    with res.request() as req:  # Generate a request event
        yield req                    # Wait for access
        yield env.timeout(1)         # peal
        # release resource


    pealed += 1
    print(f"{env.now}  onion: {onion.id} pealed layer {pealed}, {onion.layers - pealed} layers to go")
    
    if onion.layers <= pealed:
        #finish, stop recursion
        print(f"{env.now}   onion: {onion.id} is pealed, {pealed} layers")
    else:
        # still have layers to peal, recurse
        yield env.process(peal_onion_layer(env,res,onion,pealed))

    # do any post recurse acions here
    print(f"{env.now}   onion: {onion.id} exiting layer {pealed}")

    
# start up recursion
env = simpy.Environment()
res = simpy.Resource(env, capacity=2)
get_onions(env,res)
env.run()

这是没有递归的相同 sim

"""
demonstrates how queue can be used instead of recursion
by simulating the pealing of a onion

programmer: Michael R. Gibbs
"""

import simpy
import random

class Onion():
    """
    Simple onion object
    id:     unique oning id
    layers: number of layers to peal
    """

    # class var for generating ids
    id = 0

    def __init__(self):
        """
        initialize onion with unique id and random number of layers
        """

        Onion.id += 1
        self.id = Onion.id
        self.layers = random.randint(5,9)

 
def get_onions(env, res):
    """
    sim startup
    generate a bunch of onions to be pealed and 
    start pealing them
    """

    for i in range(5):
        onion = Onion()
        env.process(peal_onion_layer(env,res,onion,0))
        # note the lack of of a yeild so all onions get processed in parallel

def peal_onion_layer(env,res,onion,pealed):
    """
    gets a pealer resource and peals one onion layer
    and release pealer.
    will need to get the pealer resource again if another layer
    needs to be pealed
    """

    with res.request() as req:  # Generate a request event
        yield req                    # Wait for access
        yield env.timeout(1)         # peal
        # release resource


    pealed += 1
    print(f"{env.now}  onion: {onion.id} pealed layer {pealed}, {onion.layers - pealed} layers to go")
    
    if onion.layers <= pealed:
        #finish, stop recursion
        print(f"{env.now}   onion: {onion.id} is pealed, {pealed} layers")
    else:
        # still have layers to peal, recurse
        env.process(peal_onion_layer(env,res,onion,pealed))
    
# start up recursion
env = simpy.Environment()
res = simpy.Resource(env, capacity=2)
get_onions(env,res)
env.run()
© www.soinside.com 2019 - 2024. All rights reserved.