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!
--
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.