【问题标题】:Creating as many events as needed in Simpy simulation在 Simpy 模拟中根据需要创建尽可能多的事件
【发布时间】:2021-04-15 19:02:36
【问题描述】:

我正在使用 Simpy 进行 Python 模拟,但我遇到了一些问题。模拟有到货流程和发货流程。目标是在订单到达时运送尽可能多的订单,而不是让订单等待。此代码为我提供了一个流程,该流程发出的订单多于到达的订单。

class LC:
    def __init__(self,env):
        self.dispatch=simpy.Container(env,capacity=100000, init=0)
        self.costs=simpy.Container(env, capacity=100000, init=0)
        self.shipment_count=simpy.Container(env,capacity=10000, init=0)
        self.orders=simpy.Container(env,capacity=10000,init=0)
def shipping(env, ship):
    while True:
        #yield env.timeout(i)
        print('Shipment was sent at %d' % (env.now))
        time,weight=get_weight_n_time() ####other function that assigns weight and time of shipment
        if weight <=35:
            cost=get_cost(weight,0)###other function that calculates costs 
        else:
            cost=get_cost(weight,1)
        yield env.timeout(time)
        print ('Shipment of %d kgs where delivered after %d days with cost of $ %d' %(weight,time,cost))
        yield ship.dispatch.put(weight)
        yield ship.orders.get(1)
        yield ship.costs.put(cost)
        yield ship.shipment_count.put(1)

def arrival (env,ship):
    while True:
        print('Order arrived at %d'%(env.now))
        yield ship.orders.put(1)
        yield env.timeout(1)
        

def shipment_gen(env,ship):
    
        env.process(arrival(env,ship))
        num_trucks=5
        for i in range(num_trucks):
           env.process(shipping(env,ship))
           yield env.timeout(0)
        
env = simpy.Environment()
ship=LC(env)
env.process(shipment_gen(env,ship))    
env.run(until=100)

当我添加一个变量来确定天气或不根据订单数量进行发货时,发货过程不会发生

class LC:
    def __init__(self,env):
        self.dispatch=simpy.Container(env,capacity=100000, init=0)
        self.costs=simpy.Container(env, capacity=100000, init=0)
        self.shipment_count=simpy.Container(env,capacity=10000, init=0)
        self.orders=simpy.Container(env,capacity=10000,init=0)
        self.order=0
def shipping(env, ship):
    while True:
        #yield env.timeout(i)
        print('Shipment was sent at %d' % (env.now))
        time,weight=get_weight_n_time()
        if weight <=35:
            cost=get_cost(weight,0)
        else:
            cost=get_cost(weight,1)
        yield env.timeout(time)
        print ('Shipment of %d kgs where delivered after %d days with cost of $ %d' %(weight,time,cost))
        yield ship.dispatch.put(weight)
        yield ship.orders.get(1)
        yield ship.costs.put(cost)
        yield ship.shipment_count.put(1)

def arrival (env,ship):
    while True:
        print('Order arrived at %d'%(env.now))
        yield ship.orders.put(1)
        yield env.timeout(1)
        ship.order+=1
        

def shipment_gen(env,ship):
    
        env.process(arrival(env,ship))
        # if ship.orders.level >0:
        #     num_trucks=get_number_trucks(ship)
        # else:
        #     num_trucks=get_number_trucks(ship)
        num_trucks=5
        for i in range(num_trucks):
            if ship.order>0:
                env.process(shipping(env,ship))
                yield env.timeout(0)
                ship.order-=1
            else:
                print('-')
env = simpy.Environment()
ship=LC(env)
env.process(shipment_gen(env,ship))    
env.run(until=100)

【问题讨论】:

    标签: python variables simpy


    【解决方案1】:

    你不需要在类中添加额外的 ship.order。容器中有订单数量的信息(ship.orders.level)。所以删除ship.order并删除shipping_gen中的循环,您的代码将正常执行。 Simpy利用多线程实现多进程。如果在 ship 类中添加额外的变量,可能会导致错误。

    【讨论】:

      【解决方案2】:

      你想移动你的

      yield ship.orders.get(1)
      

      在函数顶部设置一行,以便您的卡车在执行其他任何操作之前等待订单到达?

      还有 ship_gen 在时间 0 执行其 if 语句,但您的订单计数器直到时间 1 才 =+1 所以在时间 0 你的订单计数器仍然是 0 所以你的 if 总是会做 else

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-08
        • 1970-01-01
        • 1970-01-01
        • 2021-06-13
        • 1970-01-01
        • 1970-01-01
        • 2017-12-30
        相关资源
        最近更新 更多