More Features

Strict Priorities

PARAMS = {
    …other parameters…
    "p_priority": (0.2, 0.8),
}

class Simulation:
    …other methods…
    def rand_priority(self):
        pri = self.params["p_priority"]
        return random.choices(list(range(len(pri))), pri, k=1)[0]
class Simulation:
    def __init__(self, params):
        …as before…
        self.queue = PriorityStore(self.env)

class Job:
    …as before…
    def __lt__(self, other):
        if self.priority == other.priority:
            return self.t_create < other.t_create
        return self.priority < other.priority
    def monitor(self):
        while True:
            lengths = dict((i, 0) for i in range(len(self.params["p_priority"])))
            for job in self.queue.items:
                lengths[job.priority] += 1
            for pri, num in lengths.items():
                self.queue_lengths.append(
                    {"time": rv(self.env.now), "priority": pri, "length": num}
                )
            yield self.env.timeout(self.params["t_monitor"])
queue length with strict priorities

Weighted Priorities

class Job:
    def __init__(self, sim):
        …as before…
        self.weight = len(sim.params["p_priority"]) - self.priority

class WeightedStore(Store):
    def __init__(self, env):
        super().__init__(env)

    def _do_get(self, event):
        # Block if nothing available.
        if not self.items:
            return

        # Choose and return.
        item = random.choices(self.items, weights=[job.weight for job in self.items], k=1)[0]
        self.items.remove(item)
        event.succeed(item)
queue length with weighted priorities
queue length with weighted priorities and long time

Periodic Triage

PARAMS = {
    …as before…
    "n_triage": 100,
    "t_triage": 50,
}

class Triage:
    def run(self):
        while True:
            yield self.sim.env.timeout(self.sim.params["t_triage"])
            p_triage = self.calculate_prob()
            if p_triage is not None:
                self.discard_items(p_triage)

    …other methods to calculate probability and discard jobs…
queue length with weighted priorities and regular triage
completion rates with weighted priorites and regular triage