You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SimPy带闭店逻辑的资源调度模型实现咨询

SimPy带闭店逻辑的资源调度模型实现咨询

看起来你正在构建一个带有严格营业时间规则的SimPy资源调度模型,先帮你把核心需求再明确下:

  • 商店每日8:00开门,20:00闭店,18:00起停止接受新的资源请求
  • 闭店前1分钟会强制清退所有正在使用资源的实体,随后用优先级-1的请求占用全部资源12小时(直到次日8:00),模拟闭店期间无法使用资源的状态
  • 实体的等待逻辑分两种情况:
    • 18:00-20:00到达的实体,会先等待到20:01再尝试请求资源(此时资源已被闭店请求占用,只能等次日开门)
    • 若实体在18:00前到达但没抢到资源,会等待约2小时到闭店时间,同样需等到次日开门后再尝试获取资源

另外注意你代码里的HTML实体&gt;/&lt;我换成了Python原生的比较运算符>=/<,避免运行报错。以下是格式化后的完整代码(最后一行看起来你没粘贴完整,我按原样保留):

import simpy
import random
import math
import numpy as np

class default_params():
    random.seed(10)
    run_time = 10080
    iterations = 1
    no_resources = 4
    inter_arr = 120
    resource_time = 240
    #shop Opening Hours
    shop_open_time = 8
    shop_stop_accept = 18
    shop_close_time = 20

class spawn_entity:
    def __init__(self, p_id):
        self.id = p_id
        self.arrival_time = np.nan
        self.resource_gained_time = np.nan
        self.leave_time = np.nan

class shop_model:
    def __init__(self, run_number, input_params):
        #start environment, set entity counter to 0 and set run number
        self.env = simpy.Environment()
        self.input_params = input_params
        self.entity_counter = 0
        self.run_number = run_number
        #establish resources
        self.resources = simpy.PriorityResource(self.env, capacity=input_params.no_resources)

    ##############################MODEL TIME###############################
    def model_time(self, time):
        #Function to work out day and hour in the model
        day = math.floor(time / (24*60))
        day_of_week = day % 7
        #If day 0, hour is time / 60, otherwise it is the remainder time once
        #divided by number of days
        hour = math.floor((time % (day*(24*60)) if day != 0 else time) / 60)
        return day, day_of_week, hour

    ###########################ARRIVALS##################################
    def arrivals(self):
        yield self.env.timeout(1)
        while True:
            #up entity counter and spawn a new entity
            self.entity_counter += 1
            p = spawn_entity(self.entity_counter)
            #begin entity to shop process
            self.env.process(self.entity_journey(p))
            #randomly sample the time until the next arrival
            sampled_interarrival = round(random.expovariate(1.0 / self.input_params.inter_arr))
            yield self.env.timeout(sampled_interarrival)

    ######################### CLOSE SHOP ################################
    def close_shop(self):
        while True:
            #if first day, close shop until open time, then wait until close
            if self.env.now == 0:
                time_closed = self.input_params.shop_open_time
                time_out = self.input_params.shop_close_time
            else:
                #close for 12 hour overnight and time out process until next day
                time_closed = 12
                time_out = 24

            #Take away all the resources for the close time to simulate the shop
            # being closed.
            print(f'--!!!!!CLOSING SHOP AT {self.env.now} FOR {time_closed * 60} MINS!!!!!--')
            i=0
            for _ in range(self.input_params.no_resources):
                i += 1
                print(f'--claiming resource {i}')
                self.env.process(self.fill_shop(time_closed * 60))
            print(f'--!!!!!SHOP CLOSED!!!!!--')

            #Timout for timeout period until next closure.
            yield self.env.timeout(time_out * 60)

    def fill_shop(self, time_closed):
        #If close time, take away all the resources
        with self.resources.request(priority=-1) as req:
            yield req
            print(f'--resource claimed for close at {self.env.now} for {time_closed} mins')
            yield self.env.timeout(time_closed)

    ######################## ENTITY JOURNEY #############################
    def entity_journey(self, entity):
        #Arrival
        entity.arrival_time = self.env.now
        day, day_of_week, hour = self.model_time(entity.arrival_time)
        print(f'entity {entity.id} starting process at {entity.arrival_time}')

        #If arrival hour between stop accept and close, add an extra wait
        #to ensure they don't sneak in between these times
        if ((hour >= self.input_params.shop_stop_accept) and (hour < self.input_params.shop_close_time)):
            #Time out until shop close, where resources will all be claimed
            #then entity can wait until next open.
            next_close = ((day * 60 * 24) + (self.input_params.shop_close_time * 60))
            time_out = (next_close - entity.arrival_time) + 1
            print(f'entity {entity.id} arrived in queue after stop accepting. Time out {time_out} mins until close')
            yield self.env.timeout(time_out)
            print(f'entity {entity.id} has waited until close at {self.env.now}')

        #request resource
        i=1
        with self.resources.request(priority=1) as req:
            #Find out current model time
            time = self.env.now
            print(f'entity {entity.id} requesting resource at time {time} - attempt {i}')
            day, day_of_week, hour = self.model_time(time)
            #Work out the minutes until the next shop stop accept time (next day
            #if hour after stop accept hour)
            next_stop_accept_day = (day + 1 if hour >= self.input_params.shop_stop_accept else day)
            next_stop_accept = ((next_stop_accept_day * 60 * 24) + (self.input_params.shop_stop_accept * 60))
            time_to_stop_accept = (next_stop_accept - time) + 1
            print(f'entity {entity.

(注:代码最后一行存在截断,我按你提供的内容原样保留)

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 11:18:03