Forum Archive

Scene module and simulation module Simpy

upwart

I am running SimPy (not SymPy) om my iPad with Pythonista. That works very well. But now I would like to add animation to my model. Well, Scene is the most obvious way to do that. However, the simulation runs as one program an there's not a way break out former a Scene event. Would it be possible to disable the time driven event loop and trigger that from my own program, something like doupdate(t)?
Otherwise, I have to go for a post mortem animation, but that's not what I'm after.

JonB

From scene, you can call another function using an Action.call. So if you can structure your simpy to be updated by scene, thats the best option.

It might be possible to have another thread update the state if a SceneView object, and your scene code would then just draw th current state.

Another option is ui.View with custom draw method, or ui.ImageView with an updated ui.Image.

upwart

It's still not clear to me how to do this. Think of a very calculation intensive program that has a concept of time. At the same time, I would like to shows some objects, like the time and some shapes on a refresh basis (let's say 60 times per second).
In PyGame you just can control the rendering loop. But in Scene the render loop is time based and, as far as I know, you can't trigger that from a program.
Has somebody an idea how to handle this? If so, please privide some example program(s).

JonB

If you need things to update only when simpy comletes its time step, I would suggest using a custom draw method on a ui.View.

JonB

Actually, maybe you can elaborate: do you call a simpy update function ror each time step? Or you call one function, and provide it a callback?

Skimming through the simpy manual, I think your best bet is to have Scene call simpy's env.step(), rather than calling env.run(). Depending on the frame interval you use in scene, you would interpret time in simpy to be N/60 sec steps.

abcabc

One possible solution would be to put the generated events in a list (instead of printing) and simulate the list using scene with nice graphics. I have done a quick and dirty implementation of this idea for the "car wash"' example. May be I will refine this implementation later with nice graphics. I hope this helps.

https://gist.github.com/be4aec09389ce2e75d27085c9c49cfff

abcabc

@upwart you have mentioned that you do not want go for a post mortem animation, but let me try to see with threads whether my solution could be made online.

abcabc

ok. Threads are not needed and we could use env.peek and env.step. Since the simulation is very fast, we could generate the events list at the interval of every hundred or thousand steps in the update function. I will update the program soon with this change.

#env.run(until=SIM_TIME) equivalent to 
until = SIM_TIME
while env.peek() < until:
      env.step()
abcabc

Here is the updated gist. There is no emv.run and event simulation is integrated with scene.
https://gist.github.com/balachandrana/a0d9a42e1b66e054fe8b5e9a437c71b8

I have not tested this well and I also need to clean up the code and improve the graphics. Anyway I hope it illustrates how simpy could be run with scene.

upwart

@abcabc
Thank you. I've restructured my SimPy model and although I haven't done the animation really, I can just see it's going to work.