sounds like a variant of the "traveling salesman problem".
Maybe you'll find an algorithm under that name.
An introduction video about TSP https://www.youtube.com/watch?v=GiDsjIBOVoA (but you'd need a more advanced variant of it)
fun fact: Some researcher also used some "slime mold" organism to solve this kind of problem: https://youtu.be/3V3CMD_9xXs?si=BEmqG4mRfJsR_9kW&t=840
There is also a variant of the TSP with multiple agents, that seems to be called "mTSP", maybe you find some algorithms or code under that name.