Sirp's Combat Advisor

Sirp

Emperor
Joined
Nov 19, 2001
Messages
1,746
Location
Texas
A veteran archer and a veteran warrior meet a regular spearman guarding a settler headed for the only source of iron on this rock. You're desperate to kill the settler, but who do you strike with first? The archer or the warrior? Which gives you the best chance of winning? Does it matter?

You have three veteran horsemen and two elite horsemen, ready to attack an enemy city. Who do you send in first? The vets or the elites? Which gives you the highest chance of winning? Which is the most cost effective? Which gives you the best chance of getting a Great Leader?

I was pondering over questions such as these, when I figured, why not make a program that works it out for you? Why not make a special combat advisor that calculates every single possibility in a battle and given a certain goal, tells you which unit to strike with first in order to get the highest chance of achieving that goal?

So, here it is, a combat advisor that given a battle involving multiple units on either side, will tell you your chance of victory, along with which unit you should strike with first: Sirp's Combat Advisor

Just download the program, unzip, and run.

At the moment there are five goals you can choose for your advisor to try to get:

- To win the battle; that is, to make all the defending units die or retreat.
- To generate one or more great leaders (the program currently assumes that if you get a great leader, you will be able to use him on the same turn and so it'll try to get you as many as possible)
- To kill as many enemy units as possible (weighted by the 'value' of the units, which you can set).
- To balance your losses against there's as well as possible
- To generate and maintain as many elite units as possible

Note that the advisor will go through every possible outcome of a battle. This increases exponentially as the number of units increases. I haven't optimized the program much as of yet, and so anything past a 4 on 4 battle or so will take a LONG time to calculate.

Any feedback on this program would be great. What it does might not be exactly clear, so if there are any questions, I'm willing to explain :)

-Sirp.
 
What language is it in? Could we get source code? I would probably modify it to do something like 10,000 tests with each attacking order and see the results, then give the "optimal" path.... It would take less time that way, I would imagine....

Arathorn
 
The source code is in C++, written using VC++6. Here it is: EDIT: Source Code available now.

The calculation logic is in the files 'unit.cpp' and 'unit.h'. If you wanted to use it with another C++ compiler you'd probably have to write a new gui interface, keeping only those two files.

However, do note that the number of sequences of attack are many, and really, you don't know what to attack with second until you've seen the results of the first combat.

There are other ways it could be speeded up by orders of magnitude keeping the current system. The most noticeable being that when you attack, you generally have many units of the same type: 10 veteran jags and 5 veteran horses for instance. At the moment, it treats each unit as being completely seperate, and doesn't realize that attacking with one veteran jag is as good as attacking with any other.

Also, how injured an attacking unit gets doesn't really matter, as long as it wins the battle, for the results currently reported. Yet, a horseman winning and losing one hitpoint is treated completely differently to a horseman winning and losing zero or two hitpoints at the moment. This could likewise be optimized.

This is essentially an early version of the program (I'm not even sure if all its calculations are exactly correct), and especially if there is interest in it, I will likely add improvements: give more possible goals, more detailed reports, and optimize it for speed.

-Sirp.
 
Well, for me, this is pretty important. I'm to the point where I can eyeball a defense and my offense and know about what I'm gonna need to take and about how good my odds are. Charis's calculator, for example, almost always matches my gut feel, so it's not adding much anymore, for me.

But I always wonder what the best order to attack is. I have so many questions that I think/hope an efficient calculator like yours could answer. Questions like:
- With a mix of high attack/low attack, which do you use first? (e.g. horsemen and knights or swords and MDI or jags and horsies or tanks and cavs or...)
- Does that change depending on numbers/goals?
- With a mix of fast and slow, which do you attack with first?
- What about with different offensive numbers between the fast and slow?
- How does it change once the defender is hurt?

I have this intuition that says I could be saving a unit or two per major engagement by optimizing an attack strategy....

It might also help determine best ratio of attacking units (odds are best with 3 swords and 2 horsies used in *this* combination -- better than 5 swords or 5 horsies). And how that ratio might change with different goals....

Anyway, I'll probably look at the source code a little over lunch today and see how much sense it makes. Thanks for posting it.

Arathorn
 
Yeah those are the sorts of questions I had in mind too when writing it, Arathorn. Actually, initial analysis from running it has shown me that in most cases, it *doesn't* make that much difference who you attack with first. Often less than a 1% difference in terms of who wins the battle. In some cases that might crawl up to a couple of percent.

In terms of other goals though, like minimizing losses, it might make more difference.

-Sirp.
 
Filipino_Kid: You just download the program, given in my first post in this thread, unzip it and run it. It will pop up with a dialog box that asks you to enter in the attackers and defenders in a battle, you enter them, set your goal and ask you to calculate a report of what unit you should attack with and your chances of winning.

It doesn't directly integrate into PTW or anything though...

-Sirp.
 
I have done some of the obvious optimizations on the code, and now it's about 10 times faster. Which means it can handle battles involving about 10 units instead of battles involving about 6 (I just hate exponential based algorithms :( ). Here it is: Sirp's Combat Advisor

-Sirp.
 
This thing is really useful and it flys along now! Thanks! Just a point, but it's hard to tell if the defending unit is fortified. Not only should this be set to fortified as default (we're usually going to be using this to attack towns) and remain ticked if we go to the edit option (at the moment it removes the tick) but it would be great if fortified units were in bold or something. Just a small point, but it should be easy to do. :)

A second very useful feature, although more work, would be to make two tabs at the top. One would lead to the original 'mutiple combat' and one would lead to 'skirmish' - a version of the html "combat calculator" (updated to including the PTW UU's). This would mean that you don't need both apps open and also give the tool more flexibility. Yes I know I could set it up for just one event, but it takes longer than merely using a drop down menu.


Now if you could also modify it so that it includes a list of all the individual probabilities, so that you could evaluate them yourself to an extent:

Three vet knights (2/4, 3/4, 4/4) attack two fortified spearmen (3/3, 3/3) in a grassland town (no river).

Analysis
<Original output>

Against Spearman #1 (3/3)
Knight 1 (2/4) - 47% (25.7, 21.3)
Knight 2 (3/4) - 67.7% (20.7, 25.7, 21.3)
Knight 3 (4/4) - 81.6% (13.9, 20.7, 25.7, 21.3)

Against Spearman #1 (3/3)
Knight 1 (2/4) - 47% (25.7, 21.3)
Knight 2 (3/4) - 67.7% (20.7, 25.7, 21.3)
Knight 3 (4/4) - 81.6% (13.9, 20.7, 25.7, 21.3)

This would mean that you could have a more detailed look at the situation where, for instance you want to take the position formost, but would like the increased possibility of leaders.

You could also make a seperate button that would analyse all the possible objectives and present the information:

Analysis
To Generate a leader: Knight 1 (2/4)
To Kill More: Knight 2 (3/4)
To Kill More/Lose Less: Knight 2 (3/4)
Maximise elite units: Knight 2 (3/4)
Take position: Knight 2 (3/4)
+ any stats that might be useful.

Not sure how useful this would be though.
 
Hi Bouchehog, and welcome to CFC. All your ideas sound pretty good. I will look at implementing them next time I get to play with the code.

-Sirp.
 
Thank you - that would be truly superb. With a little time this could become the tool for civ players, although you've got some stiff competition from CivLackey!
 
MusicLaunch, sometimes the CFC upload server has...'issues'. Try again later. It might make it to my site at some stage...so much to do so little time...

-Sirp.
 
Looking good so far Sirp :goodjob:

Just a few quick (constructive) comments for future versions.

(1) An analysis status dialog with a cancel option would be useful, giving the user some feedback on what's happening and the chance to abort.

(2) A warning popup when the number of units involved is likely to take a long time (more than a couple of minutes, say).

(2) Copying the results to a "log" window or control so that multiple scenarios can be easily compared in one session. The option to save the log would be nice too :)

Keep up the good work. I, for one, appreciate the effort.


regards

Ted
 
Ted, Thanks for the comments!

The main reason I haven't done (1) is that it'd likely require multithreading, which is kinda icky. I might do it at some stage though.

(2) is a good idea, although the major problem with it is it's hard to predict how long things will take.

I'm thinking I might try a solution where after a certain number of calculations, it stops and asks the user if they really want to keep going.

(3) is a good idea, I'll look into it.

-Sirp.
 
Wow, just tried it out. Very impressive. I really liked the in- plain-English status pop-up. I was worried that the analysis was going to be presented in same manner as Bouchehog"s earlier post, (which I'm much too simple minded to understand).

But, actually I'm even too simple to really understand the analysis.

Knowing what to attack with 1st is great, but what does "...and then all other attackers were chosen as advised" mean?
Does this tell me who to attack with 2nd ?

Not sure what the average losses inflicted means? If I get a 79.7661, does this mean I would expect only do 80% of damage necessary to win battle?

Loss differential : I have no clue what this means.
as well as Positions Analyzed.

Looks like a great program, just asking for a dummied-down explanation for me.

thanks
 
jbprivate,

When it says "and then all other attackers were chosen as advised", it means that after attacking with the first unit, you should adjust the units list by deleting units that have been killed or who can no longer attack, and recording damage on defending units by reducing their hitpoints, and then rerun the advisor.

Think of it like this:

If you have 2 swordsmen and 2 horses attacking 3 spearmen, and you tell the advisor you want to aim for a certain goal, it might tell you to attack first with one of the horses. But it doesn't know the outcome of the battle yet, so it can't possibly tell you who to attack second with, since it doesn't know which units will still be defending. If the spearman survives and promotes the best unit to attack with second will be completely different than if the spearman dies.

So, if your horseman has to retreat from the spearman, you then have one horseman and two swords vs three spears. You can now run the advisor again to see what it advises you attack with next.

Every unit has a 'value'. The value will default to the shield cost of the unit - spearmen are worth 20, swords are worth 30, and so forth - but you can customize it to whatever value you want. If you inflict 79.7661, it means that on average, you will kill 79.7661 in 'value' of units.

This is useful in a war of attrition, if you want to try to attack with a sequence that has you losing the least value of units, and your enemy losing the most.

-Sirp.
 
Thanks, sorry for slow reply.

Ok, I better understand now. I tried this program using some of the values from TET's Mod. Some had huge hitpoints etc.
The program either crashed or was it that with that high of values it never finished calculating possibilities?
 
If you place lots of units in there, it'll take a long time to calculate, and won't stop running until you kill it off or it's done. It doesn't support any way to abort calculating nicely at the moment.

-Sirp.
 
I only put in 3 different units attacking 2 , but the hitpoints were around 11 each. I just couldn't tell if program was still running or not
 
Top Bottom