"简单模拟 - 超市结账指南"

2024-04-29 12:52:36 发布

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

我想用simpy模拟一个超市结账,包括

  • 把产品送到收银台。你知道吗
  • 结账员每次扫描一个产品
  • 一旦产品被扫描,一个新的产品被送入,而被扫描的产品被移动到产品缓冲区的末端
  • 客户等待至少2个产品被扫描,然后将两个产品放入购物车

Simpy提供了许多不同的概念,如流程、资源、存储等。 我尝试了以下方法:

import simpy
FEEDIN_TIME = 3
SCAN_TIME = 1

def myProcess(env, jobId):
    # describe the process
    feed2clerk = env.process(productInfeed(env, jobId, belt))
    yield feed2clerk
    bufferInfeed1_ = env.process(bufferInfeed1(env, jobId, buffer1))
    yield bufferInfeed1_
    bufferInfeed2_ = env.process(bufferInfeed2(env, jobId, buffer2))
    yield bufferInfeed2_

def productInfeed(env, jobId, res):
    with res.request() as req:
        yield req
        print('Product %d is moving to clerk 1 at %d' % (jobId,env.now))
        feedin_duration = FEEDIN_TIME
        yield env.timeout(feedin_duration)
        print('Product %d arrived in clerk at %d' % (jobId,env.now))
        prodScan = env.process(productScan(env, jobId, clerk))
        yield prodScan

def productScan(env, jobId, res):
    with res.request() as req:
        yield req
        print('Product %d is scanning at %d' % (jobId,env.now))
        scanin_duration = SCAN_TIME
        yield env.timeout(scanin_duration)
        print('Product %d is scanned at %d' % (jobId,env.now))


def bufferInfeed1(env, jobId, res):
    with res.request() as req:
        yield req
        print('Product %d is moving to buffer zone 1 at %d' % (jobId,env.now))
        feedin_duration = FEEDIN_TIME
        yield env.timeout(feedin_duration)
        print('Product %d arrived in buffer zone 1 at %d' % (jobId,env.now))

def bufferInfeed2(env, jobId, res):
    with res.request() as req:
        yield req
        print('Product %d is moving to buffer zone 2 at %d' % (jobId,env.now))
        feedin_duration = FEEDIN_TIME
        yield env.timeout(feedin_duration)
        print('Product %d arrived in buffer zone 2 at %d' % (jobId,env.now))

# Creating environment
env = simpy.Environment()

# Creating resources
belt = simpy.Resource(env, capacity=1)
clerk = simpy.Resource(env, capacity=1)
buffer1 =  simpy.Resource(env, capacity=1)
buffer2 =  simpy.Resource(env, capacity=1)

# Describe the process
for i in range(4):
    env.process(myProcess(env, i))

# run the process
env.run()

它产生以下结果

Product 0 is moving to clerk 1 at 0
Product 0 arrived in clerk at 3
Product 0 is scanning at 3
Product 0 is scanned at 4
Product 1 is moving to clerk 1 at 4
Product 0 is moving to buffer zone 1 at 4
Product 1 arrived in clerk at 7
Product 0 arrived in buffer zone 1 at 7
Product 1 is scanning at 7
Product 0 is moving to buffer zone 2 at 7
Product 1 is scanned at 8
Product 2 is moving to clerk 1 at 8
Product 1 is moving to buffer zone 1 at 8
Product 0 arrived in buffer zone 2 at 10
Product 2 arrived in clerk at 11
...

我至少做到了

  • 当一个扫描的产品被移动到缓冲区时,另一个被送至店员。你知道吗

其他要求我都没有成功。我的问题有什么样的样板吗?你知道吗


Tags: toinenvzone产品isbufferres