Reusing/Resetting SimPy Models with Different Environments

31 views
Skip to first unread message

henrikw

unread,
May 22, 2025, 7:35:54 AMMay 22
to python-simpy

Hi everyone,

I'm working on a simulation of a production process using SimPy, and I'm currently trying to optimize certain parameters through multiple simulation runs. For each run, I use different parameter values, which requires me to reset the simulation environment (simpy.Environment) each time.

Right now, I'm rebuilding the entire model from scratch for every simulation using a function like build_model(env)where I pass in a fresh environment. However, since my model is quite complex, rebuilding it repeatedly becomes a performance bottleneck. I would like to reuse the model structure and just reset the environment, or at least avoid having to fully reconstruct all components each time.

Has anyone found a good way to "recycle" or reuse an existing model while resetting the environment? Is there a way to replace or rebind environment-dependent methods (like process generators) to a new environment without reinitializing everything?

Here’s a simplified version of what I’m doing:

import simpy
import random

class Machine:
    def __init__(self, env, processing_time):
        self.env = env
        self.processing_time = processing_time
        self.process = env.process(self.run())

    def run(self):
        while True:
            yield self.env.timeout(self.processing_time)
            print(f"Processed at {self.env.now}")

def build_model(env, processing_time):
    return Machine(env, processing_time)

# Run simulations with different parameters
for processing_time in [5, 10, 15]:
    env = simpy.Environment()
    machine = build_model(env, processing_time)  # Instead of rebuilding I'd like to recycle the once build model with a new environment to start a new simulation at env.now = 0
    env.run(until=50)

This works, but in the real model, build_model() initializes many interconnected components, which makes frequent rebuilding expensive.

What I’d like to do instead:

  • Reuse the structure (e.g., machine instances)

  • Just reset the env or "rebind" the model components to a new environment

  • Avoid reinitializing the full model tree

Is there a way to decouple the environment more cleanly or restructure my model for this kind of reuse? Any design patterns or best practices for this kind of use case?

Thanks in advance for any tips or ideas!

Adrian Mc

unread,
May 22, 2025, 9:45:26 AMMay 22
to python-simpy
Hi, 

Why don’t you run multiple simulations concurrently with different parameters?

Regards,
Adrian. 

Cornelius Sheahan

unread,
May 22, 2025, 10:55:09 AMMay 22
to Adrian Mc, python-simpy
Hi,

Couldn't you just make an initialization method that does all the setup/ resetup for each simulation and call that at the start of each experiment?

Regards,
Neil

--
You received this message because you are subscribed to the Google Groups "python-simpy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-simpy...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/python-simpy/19b8463d-c363-4c3c-abf7-d030dc7c4a05n%40googlegroups.com.

henrikw

unread,
Jun 20, 2025, 6:19:33 AMJun 20
to python-simpy
Hi,

thank you for your input.
I solved the issue by overwriting each env-dependent attribute of my simulation objects with a new env. However when overwriting environments, it seems to be important to keep track of the memory allocations because old environments and potentially additional objects will be kept inside memory and therefore slowing down the simulation while overloading memory.

Regards,
Henrik

Reply all
Reply to author
Forward
0 new messages