I doesn't really matter how much invading pathfinding code would be, because i think it should be done before the attack, when siegers appears.
Think of it as how the battleplan was made : if it doesn't work, you can't find a new one there, you have to retreat. Depending upon the siegers cleverness (their leader's one), several battleplans could be made.
If you want to enhance siegers AI by adpatability and learning from previous sieges, you can't just rely on how to have less losses. You have to think towards how to be more effective, meaning how much losses you inflincted to the enemy, take into account whether they were civilians, melee or ranged fighters, and how you killed them. More, high-skilled warriors killed should have a special treatment.
There would also need to keep how many buildings were destroyed, and how much value they represented.
Keeping track of how much of the battleplan worked and what screwed it up would help too.
Now with this said, organisation needs to be improved. Goblins fit their duty currently, but it isn't of any fun.
Siegers could be organised as squads, each having goals sorted by priority.
So there would be a 4-5 unit squad around a thief that would have for first mission to protect the latter. They would fight every melee fighter that comes close and use their shields to enhance the thief's protection from arrows.
Battleplans would have random points related to the leader's intelligence and military skills, acting as bonus. Then these points could be spent on different strategies, formations etc, each costing more points the more complex and organisation needing.
There also needs a more complex behaviour when winning the battle. If siegers haven't had losses for a certain time, while your losses keep on growing, they should consider themselves winners and start rampage, siezing food, drinks and everything valuable. Battleplan would be considered successful then.
Also, note that you fortress hasn't necesarily crumbled to its end then. Some have already built panic rooms. With enough luck, one could lose the battle, let siegers in, lock every civilian in the panic room, hold for a few years (now this would make an awesome story), train new militarians while living on kitten meat and undergound water and finally repel the enemy.