u/Petrov_Classifier

Where the f do I buy razor blades??

So, I bought a safety razor a few months ago and I love it, I can't go back to shaving with disposable ones. Unfortunately (well, fortunately in every other aspect except this one), I have to travel a lot for work, and I thought "Fine, if I ever have to travel by plane I'll just take out the blade and buy some for dirt cheap when I get there, they should be easy to get as everyone says on the internet". WRONG, omg so wrong. Idk if everyone saying that is american and it's actually a thing there, but at least in Paris, Lyon, Barcelona, Copenhagen and London I went to every single supermarket, pharmacy, "ethnic" store, whatever, not a single one sold razor blades, they even looked at me like I was the crazy one for expecting them to sell it. Am I going crazy or was I just extremely unlucky?

reddit.com
u/Petrov_Classifier — 2 days ago
▲ 726 r/CivVI

So I wanted to know if it was better to attack with melee or ranged first, so I did a bunch of simulations and got a pretty plot!

WARNING: A bit of math ahead.

So, you probably have a vague intuition that, if you have a ranged unit and a melee unit that can attack the same target, it's probably better to attack with the ranged unit first, since it doesn't cost HP to do so, you reduce the enemy's HP, and so your melee unit loses less HP than it would have if you attacked with it first.

This is because the formula for calculating damage in the game is:

Damage(HP) = 30*e^(0.04*Strength_Difference)*RandomBetween(80%,120%)

And if a unit is damaged, there is a penalty to its combat strength that follows:

Combat_Strength_Damaged = Combat_Strength - round(10 - HP/10)

So you attack with the ranged unit first, the enemy unit loses combat strength, and deals less damage to your melee unit when you attack it.

But if you recall your high school math, you'll notice that the damage formula is an exponential in the strength difference, with a very small exponent because of the 0.04 multiplying, but exponential nonetheless. So I wondered if there couldn't be a situation, like if your ranged unit is much stronger than your melee unit, where it's actually better to attack with the melee unit first to make the exponent in the damage formula larger when you attack with the ranged unit.

To put some numbers to it, let's say you have a melee unit with 30 combat strength, attacking a unit with also 30 combat strength, and a ranged unit with 40 ranged combat strength.
Then, if you attack with your ranged unit first, you'd expect to deal on average

30*e^(0.04*10) = 44.75 damage.

So now the enemy unit has 55.25 HP, and an effective combat strength of

30 - round(10 - 55.25/10) = 26

So then when you attack with the melee unit, you'd expect to deal

30*e^(0.04*4) = 35.21 damage, and you'd receive
30*e^(0.04*(-4)) = 25.56 damage.

In the end, we dealt a total of 79.96 damage, and received 25.56.
If we decide to attack with the melee unit first, you'd expect to deal and receive

30*e^(0.04*0) = 30 damage.

And now the enemy unit has 70 HP, and an effective combat strength of 27. Then, attacking with the ranged unit deals on average

30*e^(0.04*13) = 50.46.

As such, we dealt in total 80.46 damage (a bit more!) but received 30 damage (a lot more). Clearly in this example it's better to stick to the intuitive strategy. However, the question I asked myself was if there were combinations of these three values (enemy CS, my melee unit CS and my ranged unit ranged CS) where the other strategy was better.

Since we live in the future, instead of me doing the calculations, I can have my computer do them. But we can go a bit further. I decided to simulate the scenario, taking into account the randomness that comes with the damage calculation (a Monte Carlo simulation, if that means anything to you). This is the setup:

All three units start with 100 health. For every combination of Enemy CS, Melee Cs, and Ranged CS we simulate taking turns attacking the enemy unit with each strategy, we assume that the enemy unit doesn't attack us back for simplicity, it doesn't change the outcome really. We repeat until either we kill the enemy unit, or our melee unit dies. To judge which strategy wins we consider:

  • If we kill the enemy unit, whichever strategy kills it first wins.
  • If both strategies kill the enemy unit in the same turn, whichever leaves the melee unit with more HP wins.
  • If our melee unit dies, whichever strategy dies last wins.
  • If for both strategies the melee unit dies in the same turn, whichever leaves the enemy unit with less HP wins.
  • If somehow the melee unit ended up with the same HP in both, or the enemy unit ended up with the same HP in both, then it's a tie.

We simulate this 200 times to get a good estimate of the winrate of each strategy.

And that's the gif you see! The x axis is the ranged CS, the y axis is the melee CS, each frame of the gif is a different CS for the enemy unit. The colors represent the difference in winrate between the strategies, so close to 1 (red) means ranged first wins most of the time, close to -1 (blue) means melee first wins most of the time, close to 0 (grayish?) means either they are 50-50, or they just tie all the time (which are not the same thing, I'll let you think about why).

Don't worry, I'm already trying to understand what's causing the "ripples." It's a very odd result indeed. My best guess is that they have something to do with the rounding in the damaged CS formula. But I'll update you when I find out what's going on.

The result is very clear that most of the time it's best to attack with the ranged unit first, so we recover the intuition that we had. But interestingly as well, it looks roughly like there is a region inside the circle with center 0, 0 and radius equal to the enemy's CS, where it doesn't really matter, or it's even very slightly better to attack with the melee unit first. That is sqrt(Melee_CS^2 + Ranged_CS^2) < Enemy_CS. I guess this could be useful if you are playing on Deity against a very advanced AI? Or if you are very behind in MP? But honestly, it isn't very helpful, since your units probably die in one hit anyway in this case haha.

Next interesting question would be to do the same kind of experiment, but with two melee (or ranged) units of different strengths, is it better to attack with the strongest unit? I'd guess so, but is it always the case? Will I get another interesting gif out of it? I'll let you know.

Of course this analysis is completely agnostic of the type of units, terrain conditions, if the enemy is fortified, etc. You just calculate the final combat strength of each unit after taking into account all relevant modifiers, and that's it. This also applies for city or district range attacks as well, you should always attack with them first (unless it's somehow much weaker than your melee unit? Which is extremely unlikely).

That's all I have, I hope you adopt this strategy if you weren't using it already!

TLDR: Yeah, attacking with the ranged unit first is better. Who knew?

u/Petrov_Classifier — 3 months ago