cristiklein / simpy

MIT License
107 stars 16 forks source link

accessing resource identifier #3

Closed acseckin closed 5 years ago

acseckin commented 5 years ago

I want to access resource identifier.

The codeIt is designed for 2 carwash machine and multiple car order. ıt is from this link: https://simpy.readthedocs.io/en/3.0.3/examples/carwash.html gives.

Car 0 arrives at the carwash at 0.00.
Car 1 arrives at the carwash at 0.00.
Car 2 arrives at the carwash at 0.00.
Car 3 arrives at the carwash at 0.00.
...

here is my project

But I want to log match results like below (multiple carwash machine and multiple car order.)

Car 0 arrives at the carwash 1 at 0.00.
Car 1 arrives at the carwash 3 at 0.00.
Car 2 arrives at the carwash 5 at 0.00.
Car 3 arrives at the carwash 8 at 0.00.
...

How can I do this? is there a way access to resource requests identifiers

Note: below link is a similar but I want to make it in resource type

SimPy Resource of 3 where each has different characteristics

here is my project with 3 different resource

# -*- coding: utf-8 -*-
import numpy as np
import simpy

o_id=0
supplier_fault={}

def order(env,suppliers,workers,technicans,machines,material_depot,production_depot,m_production):
    global o_id
    while True:
        t_order=np.random.uniform(0.1,24)
        n_order=np.random.randint(1,5)#(m_production-200,m_production+200)
        t_manufacture=np.random.uniform(1,6)
        for i in range(n_order):
            o_id+=1
            order_report=[]
            order_report.append(env.now)
            order_report.append("O%d"% o_id)
            env.process(supply(env,suppliers,workers,technicans,machines,material_depot,production_depot,o_id,t_manufacture,order_report))
            env.process(prepare(env,suppliers,workers,technicans,machines,material_depot,production_depot,o_id,t_manufacture,order_report))
            env.process(manufacture(env,suppliers,workers,technicans,machines,material_depot,production_depot,o_id,t_manufacture,order_report))
        yield env.timeout(t_order)

def supply(env,suppliers,workers,technicans,machines,material_depot,production_depot,o_id,t_manufacture,order_report):
    global supplier_fault
    print("supply",o_id,env.now)
    with suppliers.request() as req:
        s=yield req
        print("yierld s",s)
        supplier=str(req).split(" ")[-1][:-1].replace("0x258c8","s")

        yield env.timeout(0.1)
        order_report.append(str(supplier))
        print("supply", o_id, "supplier: ",supplier)
    print("End supply",env.now)

def prepare(env,suppliers,workers,technicans,machines,material_depot,production_depot,o_id,t_manufacture,order_report):
    global worker_fault
    print("prepare",o_id,env.now)
    with workers.request() as req:
        worker=str(req).split(" ")[-1][:-1].replace("0x258c8","w")
        yield req
        yield env.timeout(time_prepare(worker))
        order_report.append(str(worker))
        print("prepare", o_id, "worker: ",worker)
    print("End prepare",env.now)

def time_prepare(worker):
    global worker_dict
    return np.random.uniform(1,10)

def manufacture(env,suppliers,workers,technicans,machines,material_depot,production_depot,o_id,t_manufacture,order_report):
    global machine_fault
    print("Manufacture",o_id,env.now)
    print("select machine")
    with machines.request() as req:
        machine=str(req).split(" ")[-1][:-1].replace("0x258c8","m")
        yield req
        yield env.timeout(t_manufacture)
        order_report.append(str(machine))
        print("Manufacture", o_id, "with ",machine)

    print("End Manufacture",env.now)
    print (order_report)

num_suppliers=5
num_workers=7
num_technicans=2
num_machines=40

num_days=604
m_production=500

material_capacity=5000
production_capacity=5000

env = simpy.Environment()
suppliers=simpy.Resource(env,capacity=num_suppliers)
workers=simpy.Resource(env,capacity=num_workers)
technicans=simpy.Resource(env,capacity=num_technicans)
machines=simpy.Resource(env,capacity=num_machines)

material_depot=simpy.Container(env, init=material_capacity,capacity=material_capacity)
production_depot=simpy.Container(env, init=production_capacity,capacity=production_capacity)

env.process(order(env,suppliers,workers,technicans,machines,material_depot,production_depot,m_production))

env.run(until=80)
cristiklein commented 5 years ago

Hi,

AFAIK, simpy does not directly support your use-case. However, you could keep a list of free carwashes and push / pop into that. Something along the lines:

carwashes = range(5)

with ... as req:
  yield req
  my_carwash = carwashes.pop() # get a free carwash
  print(f'Driving into carwash {my_carwash}')
  ...
  print(f'Driving out of carwash {my_carwash}')
  carwashes.append(my_carwash) # return the carwash to the list of unused ones

Does that help?

acseckin commented 5 years ago

Thanks. I've already done it in other ways. I wish it could be done with this library.