This is an experiment to introduce a simple form of kiting to the ai.
Since the ai currently has no per turn fine control over what the units do (or at least doesn't do it), this just uses the minimal distance of ranged units to simulate a kiting-like behavior.
So the problem is being outsourced from Petra to UnitAi, but it should lead to a better fighting position for the ai, since the ranged units will more likely walk behind the melee units and therefore be more protected and provide more value.
Also, attacker will be more spread out and spend more time chasing.
Disclaimer: There are now a number of ai mods out there, which all try to improve the ai. I haven't look at any of them, but since this is a minor change, chances are that someone else had the same idea before me, so if someone has done/ used the same thing it is coincidental and I wouldn't mind giving them credit.