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

2024-09-30 10:38:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图做一个递归实时模拟,看看它在simpy框架中是否可行。 该函数用于跟踪日志字典。。。 当我运行此代码时,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()

Tags: env框架logtypingratedeflengthint
1条回答
网友
1楼 · 发布于 2024-09-30 10:38:38

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

例如,一次一层剥一层洋葱

这里是递归

"""
demonstrates how recurssion 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()

相关问题 更多 >

    热门问题