My approach would be:
- Designate one soldier of the formation "the leader". (this does not necessarily need to be the actual commanding officer of the formation. You can pick any soldier. You will probably get the best results when you pick the soldier closest to the center of the formation, but that might be worth experimenting with)
- When the formation moves, calculate a path only for "the leader"
- WhenWhile "the leader" moves along the calculated path, have all other soldiers in the formation maintain their relative position to "the leader". This should not require additional route finding, only blindly moving into the relative direction.
- Should that be impossible, for example because an obstacle in the way requires to break formation, then the individual soldier should calculate an individual path to its designated position within the formation. In order to regain unit cohesion before arriving at the destination, those separated soldiers should move faster than usual. Or alternatively the leader should notice when they lose a soldier and then slow down until the lost soldier has caught up.
When your game features combat with individual soldiers being able to die at any time, then you also need to handle the case that "the leader" dies, potentially even while the formation is on the move. In that case you would have to select a new solder as the new leader and reevaluate the relative positions in the formation of all remaining soldiers.