matrx-software / matrx

Human-Agent Teaming Rapid Experimentation Software
https://www.matrx-software.com
MIT License
10 stars 3 forks source link

Is it possible to use async in agent actions? #324

Closed zhanghuishun closed 2 years ago

zhanghuishun commented 2 years ago

What is your question? Is it possible to use async? I want to make the agent sleep a few seconds and then continue the action. Or is there some alternative way to do that?

To what is your question related?

thaije commented 2 years ago

Hi Huishun Zhang, thanks for the question. Do you want your agent to 'sleep' for a couple seconds and do nothing in the mean time, or do you want to do some stuff in your agent that takes longer than 1 tick?

If it is the first, you can pass the action_duration action keyword argument for a specific action. If you e.g. set the action_duration to 5, MATRX assumes your agent is "busy" with that action for 5 ticks, and will only call the decide_on_action after 5 ticks again. Also see the tutorial on tick_duration here

Alternatively, if you want the agent to do some complex calculations that might take a while, the most easy solution I can think of is to use multithreading for your complex calculation in the agent, and in the mean time just return the None action every time MATRX calls the decide_on_action of your function. So something like:

Tick number action returned by agent notes
1 None Agent starts new thread to do some complex calculation
2 None Agent working on complex calculation in thread
3 None Agent working on complex calculation in thread
4 None Agent working on complex calculation in thread
5 Action returned that uses output of complex calculation Agent done with complex calculation

The most important thing to note is that the current version of MATRX is a stochastic, sequential, framework which runs at a specified tick speed.

Every tick it will check each agent, and see if they are still busy with some action, but if not, the decide_on_action function is called as to request the action they want to do. So why async may not work is that MATRX is waiting for a response on the action the agent is going to do. If that takes a long time, MATRX will wait and the entire simulation will stop until the agent responded. But I'm not too familair with async Python, so I'm not too sure.

Also the sequential stochastic nature of MATRX v2 is on purpose, as you can then rerun the simulation and reproduce the exact same results. But running everything sequentially is also quite a performance bottleneck, so for a MATRX v3 we would also want to use something like async or multithreading to speed it up.

zhanghuishun commented 2 years ago

Thanks! the action_duration fits my work.