Quantitative analysis: A combat simulator in R
from
GalCiv2 Forums
I’ve put together a little GalCiv2 combat simulator using RLink, an open source statistical programming language.
Basically, there’s a function simulateCombat(iterations, AttackingFleet, DefendingFleet). For each iteration, it simulates combat between the two fleets and stores the result in a vector (one element for each iteration). The result is calculated as the sum of the remaining hitpoints of the attacking fleet minus the sum of the remaining hitpoints of the defending fleet. Note that since one of these is at zero (by definition) when combat ends, this number will be positive if and only if the attacker wins and negative if and only if the defender wins; also, the absolute value of the result is the remaining hitpoints of the victor.
The user can then plot the results to visualize the probability distribution of the outcome of combat between any two fleets. Here’s what the graphs look like so far (they’re not very pretty yet).
Plot: Link
iterations = 1000
attacking fleet: 5 ships x 20 hitpoints, 8 beam attack, 3 beam defense
defending fleet: same as attacking fleet
Probability that attacking fleet wins: 72.1%
Probability that defending fleet wins: 27.9%
Median result: 29 hitpoints remaining for the attacker
And this is with identical fleets; it’s clear that attacking first is a huge advantage!
Anyway, I just got this up and running, and I thought it was neat.
It can also be used to answer some higher-level questions. For example, imagine the set of fleets with the following characteristics:
3 identical ships
40 hitpoints per ship
beam attack + beam defense = 20
beam attack > 5 (because 2 attack, 18 defense versus 2 attack, 18 defense simulations take a long time)
Using the combat simulator, we can construct a 15x15 matrix of the probability that any given configuration (there are 15, from attack=6, defense=14 to attack=20, defense=0) will win when attacking any other configuration. At the same time, we can construct a similar matrix of the median result.
Setting the iterations per simulation to 200 (for a total of 15 x 15 x 200 = 45000 iterations, a little under 8 minutes at 1.8GHz), we get tables composed of the victory probability and median result for each attack fleet / defense fleet pair of the 15 possible models of the ship. I won’t reproduce the tables here, but after a bit of analysis, the computed best ship (Nash equilibrium) to choose if you’re attacking is the attack = 20, defense = 0 model, and the best ship to choose if you’re defending is the attack = 15, defense = 5 model (there’s a 69.5% chance of the attacker winning, and the median result is 39 hitpoints remaining for the attacker… the data say that the attacker cannot choose better given the choice of the defender, and the defender cannot do better given the choice of the attacker, which is the definition of a Nash equilibrium). This is according to the median result data; there is no Nash equilibrium if victory probability is the payoff, although this is just a quick analysis and there’s some noise in the data. It’s hard to visualize what’s going on in this example… maybe in a later post.
Anyway, that’s just to demonstrate the sort of thing one can do with this combat simulator.
If anyone has requests for simulations I’d be happy to run them, if possible. I’m planning on making a follow-up post once I’ve had more time to play with this, let me know if there’s something you want to see.
If there’s interest I’ll post the source once I polish it some more.
Basically, there’s a function simulateCombat(iterations, AttackingFleet, DefendingFleet). For each iteration, it simulates combat between the two fleets and stores the result in a vector (one element for each iteration). The result is calculated as the sum of the remaining hitpoints of the attacking fleet minus the sum of the remaining hitpoints of the defending fleet. Note that since one of these is at zero (by definition) when combat ends, this number will be positive if and only if the attacker wins and negative if and only if the defender wins; also, the absolute value of the result is the remaining hitpoints of the victor.
The user can then plot the results to visualize the probability distribution of the outcome of combat between any two fleets. Here’s what the graphs look like so far (they’re not very pretty yet).
Plot: Link
iterations = 1000
attacking fleet: 5 ships x 20 hitpoints, 8 beam attack, 3 beam defense
defending fleet: same as attacking fleet
Probability that attacking fleet wins: 72.1%
Probability that defending fleet wins: 27.9%
Median result: 29 hitpoints remaining for the attacker
And this is with identical fleets; it’s clear that attacking first is a huge advantage!
Anyway, I just got this up and running, and I thought it was neat.
It can also be used to answer some higher-level questions. For example, imagine the set of fleets with the following characteristics:
3 identical ships
40 hitpoints per ship
beam attack + beam defense = 20
beam attack > 5 (because 2 attack, 18 defense versus 2 attack, 18 defense simulations take a long time)
Using the combat simulator, we can construct a 15x15 matrix of the probability that any given configuration (there are 15, from attack=6, defense=14 to attack=20, defense=0) will win when attacking any other configuration. At the same time, we can construct a similar matrix of the median result.
Setting the iterations per simulation to 200 (for a total of 15 x 15 x 200 = 45000 iterations, a little under 8 minutes at 1.8GHz), we get tables composed of the victory probability and median result for each attack fleet / defense fleet pair of the 15 possible models of the ship. I won’t reproduce the tables here, but after a bit of analysis, the computed best ship (Nash equilibrium) to choose if you’re attacking is the attack = 20, defense = 0 model, and the best ship to choose if you’re defending is the attack = 15, defense = 5 model (there’s a 69.5% chance of the attacker winning, and the median result is 39 hitpoints remaining for the attacker… the data say that the attacker cannot choose better given the choice of the defender, and the defender cannot do better given the choice of the attacker, which is the definition of a Nash equilibrium). This is according to the median result data; there is no Nash equilibrium if victory probability is the payoff, although this is just a quick analysis and there’s some noise in the data. It’s hard to visualize what’s going on in this example… maybe in a later post.
Anyway, that’s just to demonstrate the sort of thing one can do with this combat simulator.
If anyone has requests for simulations I’d be happy to run them, if possible. I’m planning on making a follow-up post once I’ve had more time to play with this, let me know if there’s something you want to see.
If there’s interest I’ll post the source once I polish it some more.
Someone beat you to it.
attacker.ships+fleet.composition[i, 1]),] = ship.medium.1