Simpy - setting resource requests at specific times

Viewed 108

I am trying to model a system where after a process is defined, there are two sets of resource requests. Following an initial intake of the original process, a set of new resource requests are immediately requested. This is straightforward and is easily completed with the with resource.request() as req: yield req type of instruction.

Where I am completely stuck is on the second set of requests, which need to be scheduled on a regular basis, for example at each of the following 10 time units after the instantiation of the process.

So for example, if the original process starts at t = 0.5, there would be a bunch of stuff that gets queued up right away but then other stuff that gets queued at t = 1.5, 2.5, 3.5, etc. Does anyone have any suggestions for how to implement this second set of requests?

1 Answers

I found a solution after scouring all the examples I could find. Ultimately it is based on ideas found in the Event Latency example of the documentation. I think my understanding of simpy syntax just needed to catch up to what I was trying to do. Here's an example that runs returns to do something every 7 time steps while also stepping through another random sequence:

import simpy
import numpy as np
import random
random.seed(123)

env = simpy.Environment()

def roundup(x, b):
  """Round up x in base b""" 
  return b * np.ceil(x/b)

def stalling(env, base):
    """Something needs to get tasked repeatedly."""
    while True:
      yield env.timeout(max(0.01, roundup(env.now, base)-env.now))
      if env.now >= base:
        print("%.1f: New process called here" % env.now)
      yield(env.timeout(0.01))

def parent(env, base):
  env.process(stalling(env, base))
  while True:
    x = random.randint(1,10)
    print("%.1f: Wait for %i" % (env.now, x))
    yield(env.timeout(x))

env.process(parent(env, 7))
env.run(until = 50)
"""
0.0: Wait for 1
1.0: Wait for 5
6.0: Wait for 2
7.0: New process called here
8.0: Wait for 7
14.0: New process called here
15.0: Wait for 5
20.0: Wait for 2
21.0: New process called here
22.0: Wait for 1
23.0: Wait for 7
28.0: New process called here
30.0: Wait for 9
35.0: New process called here
39.0: Wait for 9
42.0: New process called here
48.0: Wait for 6
49.0: New process called here
"""
Related