Search code examples
pythonsimpy

Periodically receiving tasks from one Dataframe in SimPy


I started working with SimPy for simulating a manufacturing environment. I have a generated DataFrame containing a certain amout of tasks (attributes of each task: ID, start_location, end_location, time, distance).

I want to implement a process in SimPy to pass the first task (first row) of the DataFrame described above to an other DataFrame within the simulation. Each Taks should be passed periodically after a random generated time random.normalvariat(45s, 15s). Afterwards it should be executed. Does anybody have an idea how to implement this in a SimPy environment? Does it make sense to make use of the env.timeout(random.normalvariate(45s, 15s) function? When yes, how exactly would be an approach for implementation?

I would be thankful to any kind of help.


Solution

  • Sounds like the need a master controller sim process that you pass all your data inputs to. This controller then uses the passed in data to build and manage the sim. This is a very simple example, but I have done transportation sims where my inputs would be locations, travel times, and truck schedule with depart loc, depart time, and destination loc. The controller would create the locations, and used to schedule to create and move trucks as needed.

    """
        quick sim demo with a controler process that
        works through a dataframe generating processes
        that model what is being simulated
    
        Programmer: Michael R. Gibbs
    """
    
    from pkg_resources import Environment
    import simpy
    import random
    import pandas as pd
    
    def sim_process(env, id, dur):
        """
            This would be one of several processes
            that would make up the simulation
        """
    
        print(f'{env.now} starting task {id}')
    
        yield env.timeout(dur)
    
        print(f'{env.now} finished task {id}')
    
    def controlProcess(env, df):
        """
            controls the simulation by reading
            a dataframe and generating sim processes as needed.
            This controler is building the sim.
            This example just has one sim porcess, but can be
            much more complicated
        """
    
        # loop the dataframe and gen sim processes
        for _, row in df.iterrows():
    
            # wait
            yield env.timeout(random.normalvariate(45, 15))
    
            # not no yield here, create and move on
            env.process(sim_process(env,row['id'], row['dur']))
    
    # start up
    
    # get dataframe
    df = pd.DataFrame(
        [
            ['task 1', 40],
            ['task 2', 50],
            ['task 3', 60],
            ['task 4', 40],
            ['task 5', 50],
            ['task 6', 60],
            ['task 7', 40],
            ['task 8', 50],
            ['task 9', 60],
            ['task 10', 40]
    
        ],
        columns=['id','dur']
    )
    
    # boot sime
    env = simpy.Environment()
    env.process(controlProcess(env, df))
    env.run(1000)