the-anylogic-company / AnyLogic-Pypeline

A custom AnyLogic library for running Python inside an AnyLogic model (Java)
https://github.com/the-anylogic-company/AnyLogic-Pypeline/wiki
MIT License
106 stars 26 forks source link

Python and Anylogic Connection #71

Closed selinozen closed 6 months ago

selinozen commented 7 months ago

Hello dear t-wolfeadam,

I am trying to connect Anylogic and Python. I know there is a Pipeline tool in Anylogic but I intend to get the non-stop generated output to Anylogic block in real time. I have searched for it and I encountered Python's socket library and Anylogic's REST API. I am confused about which one to use. By the way, I want to get the data from an IoT device as an input to Python.

t-wolfeadam commented 7 months ago

Hi, so the Pypeline tool is this one :)

I'm not sure what you mean by "real-time" - simulation tools like AnyLogic are intended to run faster than real time, so typically any external endpoints are queried at the start of run to initialize the model and then it's executed under its own rules.

Though, it's still possible to have the model query something as often as you want - just keep in mind it will likely either slow down the model or, if your model's playback speed is set to faster than real-time (e.g., > x1 with model units sec to seconds), will make excessive calls to the endpoint.

Also, Pypeline is probably exactly what you want. It uses Python's socket library for communication. For reference, AnyLogic's REST API is specifically for running models uploaded to the AnyLogic Cloud, so it's not what you want.

I made a very simple example model showing how it works, similar to the problem you described (though the provided examples are typically more useful). Please read the documentation if you need help installing it or for more information.

Model344.zip