Sie sind auf Seite 1von 9

Investigation into the Average Hits Problem

K. B.

ABSTRACT

In this paper, I derive an expression for average number of hits required to kill a monster in a game called Maplestory.
The equation has no known analytical solution, so I have also developed a program that calculates the exact solution to
the equation. Due to the computational load involved in most typical instances of the average hits problem, I present two
separate approximation techniques that serve to obtain an approximated solution to the equation in a more timely manner;
these techniques are named "the mean expansion method" and "the density reduction method". Merits and limitations to
these methods are discussed, but the density reduction method is deemed as the most effective means of determining the
approximate solution to an instance of the average hits problem.

1 Background
The average hits problem finds its origins in a computer game called Maplestory. The objective of the game is to achieve the
maximum level by obtaining experience points (EXP). There are several ways to obtain such points, but one of the main ways
is to kill monsters. Monsters have a property called hit points (HP). The monster dies once it is left with no hit points, and the
player is subsequently awarded with the experience points associated with the particular monster.
The means by which the player kills the monster, or the combat mechanic, is key here. Essentially, the player deals damage
to monsters by hitting them. The damage dealt is typically a function of several things such as the monster’s weapon defense
and the player’s equipment and "skills", but after taking these precalculations into consideration, the player’s damage is always
an integer chosen at random between a minimum attack, hereto abbreviated as (atk)min , and a maximum attack (atk)max . The
attack range is then defined as R ≡ (atk)max − (atk)min + 1, so that the attack range expresses the total number of possible
integers for each hit.
Since the objective of the game is to reach reach the maximum level, it is natural to inquire into the quickest way of
achieving this goal. Given that killing monsters is arguably the primary method by which to level up, it follows that there exists
a kind of monster that will yield the quickest EXP gain rate. This rate is best measured with respect to hits required to kill the
EXP
monster. In other words the EXP gain rate, µ, shall be defined as the amount of EXP gained per hit: µ = Hit(HP,(atk) min ,(atk)max )
.
The number of hits required to kill a monster is actually a function of HP, (atk)min , and (atk)max , and this is actually where the
problem is born.
There are many different ways of killing a monster as determined by the parameters listed above, and each distinct
combination of integers within the attack range that end up killing the monster can be of different sizes. This implies that, for
any given monster, the number of hits it takes to kill it can range depending on the value of each element in the sequence of hits.
Because of this, it is necessary to quantify that average number of hits required to kill the monster. Hence, the average hits
problem presents itself.
In fact, the average hits problem is similar to the infamous change-making problem, which asks for the minimum number of
coins that add up to some amount of money. However, the average hits problem is different in several ways. Most importantly,
the total damage dealt to the monster may be more than the monster’s HP. Moreover, the average hits problem does not concern
itself with the minimum number of hits required; it would like to know all possible sequences of hits. Let each full sequence of
hits that leads to a kill be defined as a kill route. Once all kill route sizes are known, the average number of hits required can be
determined.

2 Introduction and Initial Results


I first encountered this problem via my attempts to construct a spreadsheet that indicates which monster yields the highest
experience-per-hit ratio. A key parameter required in the calculation for experience-per-hit is the average number of hits it takes
to kill the monster, but this parameter is less trivial to compute than I had initially imagined.
My first approach was simply to use the average attack as determined from the maximum and minimum attack values.
Intuitively, it seemed to me that for high HP values relative to the maximum attack value, the average hits required per kill
would converge to the value determined by the average attack, and while the truth to this is undetermined, this certainly is not
the case in the low HP limit. In fact, most monster interactions occur in this regime, where the attack range is fairly near to the
monster’s total HP. This makes the resolution to this problem crucial in order to accurately predict which monster lends itself
for most efficient leveling up.
To see the shortcomings of my first-order approximation, consider the following example. Let us begin with the scenario in
which the monster’s HP is 4 and our attack range is 1-3. In this instance and for the rest of the paper, I will neglect monster
defense effects on the attack range, as this can be easily incorporated. The average attack is given by the arithmetic mean,

(atk)min + (atk)max
(atk)avg =
2
which in this case evaluates to 2. With this attack value, it would take 2 hits to kill the monster. This is the lowest integer value
that satisfies the kill requirement: (atk)avg × n ≥ HP. What is the actual average number of hits required to kill the monster,
though? A 2-valued attack will always take 2 hits to kill a monster with 4 HP, but there are many more ways to reach this HP
given the full attack range of 1-3. To see all possible combinations of elements within the attack range that satisfy the kill
requirement, it is convenient to use tree diagrams. All possible ways to kill a monster with 4 HP using an attack range of 1-3
are shown in the hit tree diagram below.

Figure 1. 4 HP Hit Tree with Attack Range 1-3

The hit tree diagram begins at the top with the indicated HP value. From there, the tree branches into the next possible hit
values. After each hit, if the monster is not yet dead, then any value within the attack range is equally probable for the next
hit. This is an important point; it is assumed that all values within the attack range are equally likely to occur at any given
hit. When reading a hit tree diagram, one must maintain a running tally of the current HP after each hit. In figure 1, the final
hit in the kill route that requires 2 total hits is circled in blue, 3 total hits in red, and 4 total hits in green. From the hit tree
diagram, then, it is easy to see that there are 5 possible ways to get the kill in 2 hits, 8 possible ways to get the kill in 3 hits, and
3 possible ways to get the kill in 4 hits. I will call the degeneracy in the number of ways to kill in n hits the multiplicity, and
the multiplicity function will be defined as Ω(n). With this definition, we can summarize the results of this hit tree as follows.

Ω(1) = 0, Ω(2) = 6, Ω(3) = 8, Ω(4) = 3

That is, the multiplicity of 1-hit-kills is 0 in this case, the multiplicity of 2-hit-kills is 6, and so forth. To obtain the average
number of hits required from the set of all possible kill routes, we could be tempted to use an ensemble average. Let the average
number of hits required be denoted by γ. It might then be natural to suppose that

∑∞
n=1 nΩ(n)
γ=
∑∞n=1 Ω(n)

However, this is not true. This expression is only valid if all possible kill routes are equally likely, but that is not the case.
To reiterate, only the hit values within the attack range are equally likely; certain kill routes are more likely than others. To
accurately compute the average number of hits required, we must determine the probability of each kill route occurring. Let us
evaluate these probabilities for the tree in figure 1.

2/9
There are 6 possible ways of killing the monster in 2 hits, but we must consider the probability of each of these ways. There
is a 1/3 chance of hitting a 3 in the first hit, and any attack value thereafter will yield a kill. The probability associated with this
is 13 × 33 . If the value of the first hit were a 2, there would be 2 possible, subsequent outcomes that yield the kill. In this case, we
have the probability 13 × 23 . Finally, if a 1 were hit first, the probability of achieving a 2-hit kill would be 13 × 13 . The sum of
these probabilities gives the overall probability of getting a 2-hit kill:

1 3 1 2 1 1 2
P(2) = × + × + × =⇒ P(2) =
3 3 3 3 3 3 3
Here, P(2) represents the probability of a 2-hit kill. We may rearrange the expression above to reveal a pattern.

1 3 1 2 1 1 13 2 1 1 Ω(2)
P(2) = × + × + × = + + = (3 + 2 + 1) = 2
3 3 3 3 3 3 3 3 3 3 9 3
In general, we can say that the probability of an n-hit kill is

Ω(n)
P(n) =  n
(atk)max − (atk)min + 1

so that the average number of hits required can be succinctly expressed as

∞ ∞
nΩ(n)
γ= ∑ nP(n) = ∑ n
n=1 n=1 R

where R ≡ (atk)max − (atk)min + 1. This is simply the total attack range. We can use the expression above to find the actual
average number of hits required.

2 Ω(3) 8 Ω(4) 3
P(1) = 0, P(2) = , P(3) = 3 = , P(4) = 4 =
3 3 27 3 81

∴ γ = 1 × 0 + 2 × 32 + 3 × 278 3
+ 4 × 81 =⇒ γ ≈ 2.37
This result is not too far removed from my initial prediction of 2 hits-to-kill, but it shows that the first-order approximation
is inappropriate to accurately determine the correct result. There are countless conceivable cases where the first-order
approximation fails. For example, if the monster’s HP were located at the average of the attack range, then the first-order
approximation would claim that it would take an average of 1 hit to kill the monster. Yet, we know that half the time the monster
should die in a single hit, and the other half of the time, it should take more than 1 hit. As a result, we should not rely on the
first-order approximation to return acceptable values.

3 The Exact Solution Algorithm


For the sake of completion, I shall present the fundamental concept behind the algorithm I developed; it computes the average
hits required via the boxed equation above (see appendix for complete code). I used this algorithm in the form of a function
in order to facilitate numerical analysis. At any rate, the non-trivially obtained parameter in this equation is the multiplicity
function. Although not explicitly stated, the multiplicity function is a function of the minimum and maximum attacks as well as
the target monster’s HP. These would be the function’s inputs.
Here is how the algorithm constructs the multiplicity function. Firstly, a vector of all possible hit values in the attack range
is created. Let the set of live damage values be known as the population. The population at this point consists of all possible
first-hit outcomes. Next, the program sifts through the population and removes all values which are greater than or equal to the
monster’s HP. The current hit number is taken into account, and the number of removed elements are recorded. The remaining
population consists of damage values which did not kill the monster in 1 hit. Now, every value within the attack range is added
to each element in the population to form an array. Every entry in this array is an element of the new population; the entries in
the array represent all possible values of damage accrued after 2 hits. Once the array is in place, it is transformed back into a
vector so that elements of the vector can be removed dynamically. As was done right after the first step, the elements of this

3/9
new array are filtered out based on their value relative to the monster’s HP, and the number of entries removed is recorded. This
process continues until the vector is empty and the complete multiplicity function is consequently constructed.
To illustrate this process, suppose that the attack range is 1-3 and the monster’s HP is 3. The evolution of the population
would then develop like so:


2 3 4
1 2 3 → 1
 2 →

3 4 5
→ 2 3
 4 3 4 5 → 2 → 3 4 5 → | |

In the first step, the population is initialized from the attack range. The population is then filtered for damage values that are
greater than or equal to the HP threshold. In this case, the 3 gets removed, leaving a vector containing the elements 1 and 2.
Here, the algorithm establishes that Ω(1) = 1. The third step shows the resulting array from adding the attack range to both
elements of the reduced vector. The top row of the array displays the result of adding all values between and including 1 and 3
to the first vector entry of 1, and the second row of the array shows the result of the performed addition on the second vector
entry of 2. The array is then rearranged in step 4 to form a single vector containing the entire population created in the previous
step. Again, for the following step, this vector is reduced by filtering the damage values. In this case, 5 entries were removed
so that Ω(2) = 5. The process repeats, and the final nonzero output for the multiplicity function is Ω(3) = 3. With this, the
multiplicity function has been fully defined. Finally, the algorithm performs the remaining calculation as shown in the boxed
equation above to determine the precise number of average hits required.

4 Approximation Methods
Unfortunately, the method used above works for very small hit trees relative to the size of those found in practice. For hit trees
with large HP and large attack range, it is very difficult to construct and/or compute the multiplicity functions numerically. In
fact, if the HP is n times greater than the maximum attack value, then there will be at least Rn kill routes. For large ranges and
large HP values, this statement suggests that obtaining an average hit number becomes computationally taxing. In light of this,
I initially attempted to arrive at an analytical expression for the multiplicity function, but I could not. Therefore, I will focus on
a numerical approach for estimating the number of average hits required via extrapolation in a 3-dimensional space comprised
of minimum and maximum attack and monster HP.
There are countless ways to approximate the solution to an instance of the average hits problem. More pertinently, for any
combination of minimum attack, maximum attack, and monster hp, there will be an exact solution which may be approximated
using suitable techniques. Below, I have outlined two such techniques that aim to maximize solution accuracy and minimize
computational load. As an important disclaimer, these techniques are demonstrated via case studies on a single instance of the
average hits problem. The intention is to use this case as a canonical example that illustrates a typical solution space for the
problem. There are undoubtedly plenty of cases where the solution space may take on different and varying forms, but most
instances of the average hits problem should exhibit similar characteristics to that of the considered case.
Before presenting these methods, though, I would like to briefly touch on some other approaches I took. Initially, I tried
fixing the attack range and varying HP to find some sort of pattern that would allow me to extrapolate into higher HP values– the
motivation being that higher HP values lead to heavier computational load. I did find that, for most cases, beyond a certain point,
the relationship between average hits required and monster HP was linear for a fixed attack range. This was initially promising.
However, even if this were always the case, obtaining the constant of proportionality between monster HP and hits required
proved to be intractable because the problem is fundamentally numerical anyway. Nonetheless, the relationship is not always
linear. For certain attack ranges and HP values, I observed nonlinear behavior of varying nature. My best assessment of these
observations is that the complexity of the multiplicity function, which hitherto has no known analytical solution, complicates
the relationship between the average number of hits required and monster HP. While I had originally imagined single-variable
extrapolation would suffice, these shortcomings eventually led me to explore the space over two variable dimensions.

4.1 Mean Expansion


To begin exploring the solution space in hopes of finding suitable means of approximation, I plot average hits versus both
minimum and maximum attack values for a fixed HP value. Visualizing the interaction between the attack range and the average
number of hits required while keeping HP constant may provide valuable insight into a viable means of approximating the
exact solution.
Figure 2 shows the resulting plot of average hits required versus a series of attack ranges. Sub-figure 2(a) is included to
provide some intuition for the solution space. The shape of the surface is as expected; low minimum and maximum attack
values yield a high number of hits required on average, and high minimum and maximum attack values result in a low number
of hits required. Despite the visual appeal of the graph, it is does not necessarily lend any useful information as it stands,
but it reveals a hint of homogeneity across certain lines. More specifically, distinct points in the attack range domain map to

4/9
(a) Full Surface (b) Mesh Grid with Contours

Figure 2. Average Hits Required Vs. Attack Range for 10 HP

similar-valued average hits required. It would be interesting, then, to see how the locus of points that define the surface contours
are related.
Figure 2(b) shows the top-down view of figure 2(a) but as a mesh with contour lines. The mesh is exact and only connects
each available data point, but the contour lines are not entirely accurate. Given the discrete nature of the data, Matlab must
interpolate between values to determine where the function is constant. Since discrete-valued functions are typically sparse
(discontinuous) when dealing with small domains, the contour line serves mostly as an approximation, but the contour lines do
reveal an interesting result.
The output associated with the attack range domain defined by a selected average attack tends to remain about the same,
or it may approach some value as the attack range increases about said average attack. To see this, consider figure 2(b). I
have labeled two sets of four points that correspond to the same average attack. The bottom set of points, corresponding to an
average attack of 4, align quite nicely with the contour line as the attack range increases about this average value. The top
set of points, corresponding to an average attack of 5, does not have a reference contour line, but the points’ corresponding
hits required values tend toward some amount as attack range is increased about the average attack. Despite the difference
in behavior between these two set of points, it suggests a powerful approach for approximating the average number of hits
required for large attack ranges.
Explicitly, the average number of hits required can be approximated by expanding an artificial attack range about the
average attack value of the original attack range– hence the name "mean expansion". For example, suppose I would like to
know the average number of hits required to kill a monster with 10 HP if my attack range were 2-8. Then, given figure 2(b), I
could run the computation using the average attack of 5 and an expansion factor (EF) of 1. Using an expansion factor of 1
simply means that the attack range will span 1 unit to the right and 1 unit to the left of the average attack. In this case, An
expansion factor of 1 would yield an artificial attack range of 4-6. Running the computation with this artificial range results in
an average of about 2.333 hits required to kill the monster. This is about a 6% error when compared to the actual value of 2.489.
The benefit of using this approximation technique is that it reduces the computation time significantly for larger attack ranges.
With the concept of mean expansion established, let us apply it to a typical instance of the average hits problem. Figure
3(a) shows how increasing expansion factor affects the prediction for average hits required over a range of HP values. As
the expansion factor is increased, the curves deviate away from a step-function-like shape; when the expansion factor is zero,
the associated curve would be a step function. Plateau behavior is observed for expansion factors up to about 15, and this is
because of the clumped nature of the artificial attack range. As the expansion factor approaches 20, the plateau length decreases
because of the span of the artificial attack range. With increasing expansion factor, the curves decrease in slope and pivot about
a certain point. The 40.5 HP mark shows a striking result.
In average-attack-valued increments along the HP axis, all curves appear to pass through the same point. However, as
monster HP increases to several times the average attack value, this coalescent behavior weakens. Immediately, in figure 3, it
can be seen that the second instance of coalescence is a bit more loose than the sharpness of the first one. Nonetheless, this
observation is useful because it makes clear that regardless of the expansion factor, the EF curve will always pass through some
multiple of 0.5 at an HP value corresponding to some multiple of the average attack. It may be tempting to conclude that a

5/9
linear model may yield an adequate approximation, but this is exactly what I have been trying to avoid due to its inability to
capture nonlinear behavior like the one present at around 60 HP. To reiterate, the curves do not pass exactly through the same
point, but this behavior shows that the curve will pass very near this point of coalescence.

(a) Increasing Expansion about 40 Attack (b) Error as compared to EF = 20

Figure 3. Mean Expansion about 40 Attack over an HP Range

4.1.1 Error Analysis


For large attack ranges, relatively small expansion factors result in inaccurate predictions for average hits required. To see
this, consider figure 3(a). For low expansion factor values, the mean expansion model predicts step-like behavior. Such a
characteristic is unattractive when attempting to approximate large attack ranges because the error in certain regions of the plot
can reach values of up to 0.5 as shown in 3(b). In this case, such a region is the 39-41 HP region, where at 39 and 41 HP, there
is nearly an error of 0.5 when compared with the 20 expansion factor curve. The error drops off in an interesting pattern, and
it approaches zero near multiples of the average attack. The error begins to behave unpredictably past 80 HP, where lower
expansion factors may yield higher error.
Overall, the mean expansion method yields reasonable results, but its convergence is somewhat slow. That is, suppose that
the minimum attack is 20 and the maximum attack is 60. This is equivalent to an average attack of 40 with an expansion factor
of 20. With reference to figure 3, it is visually apparent that low expansion factors simply do not capture the behavior of the
desired attack range– in this case, 20-60. Only for expansion factors near the target expansion factor do we observe reasonable
agreement. Notwithstanding, the method can be useful as a first-pass, where we may establish upper and lower bounds on
the average hits required using small expansion factors. For the most part, EF curves do not cross each other apart from the
points of coalescence, so for any given HP, an increase in EF monotonically increases or decreases the predicted number of hits
required depending on where this HP value finds itself relative to the average attack value.

4.2 Density Reduction


The density reduction technique works by increasing the spacing between successive integers. To obtain the exact solution to
the average hits problem, a dense attack range is used; all integers between and including the minimum and maximum attack
are possible damage values. The attack range density can then be reduced by increasing the spacing between accessible damage
values so that less integers span the attack range. The default spacing between integers is 1, so spacing values of 2 and higher
make the range less dense and thus less computationally strenuous.
As before, consider the case where the attack range is 20-60. With a spacing of 1, all integers in this attack range are
viable damage values. The result of using this spacing is shown in blue in figure 4; this curve delineates the exact solution.
Immediately, by inspection, it is evident that this approximation method is, qualitatively, very different than the mean expansion
method. Step-like behavior is still observed but at much smaller amplitudes, and as spacing is decreased toward 1, convergence
occurs a lot more quickly; all spacing curves are packed fairly closely. To elaborate on convergence, consider this example.
When the spacing is 5, the available attack values are {20, 25, 30, ... , 60}. In total, there are 9 elements in this set. For the
expansion method to manipulate 9 elements, the expansion factor would have to be 4, and as shown in figure 3(b), such an
expansion factor yields large errors. Because of this, it is clear that the density reduction technique is more successful in

6/9
obtaining accurate approximations; it converges much more quickly as spacing is reduced, and it achieves more accurate results
for the same number of attack range elements.

(a) Varying Spacing for Attack Range 20-60 (b) Effect of Spacing on Error

Figure 4. Average Hits Required Vs. Attack Range for 10 HP

4.2.1 Error Analysis


Just from figure 4, it can be noted that the error associated with the density reduction method is much less pronounced than
the error resulting from mean expansion. Figure 5 shows the error present in figure 4, and it indicates that the error for this
particular case can slightly exceed 0.2. Remarkably, though, small spacing, such as 3 and 5 shown in figure 5, yields adequate
agreement with the target curve. Indeed, the maximum error pertaining to these spacings are about 0.05 and 0.1 respectively.
This is a huge success for this approximation method, as a spacing of 5 reduces the range by a factor of about 5 for large ranges.
Though the resulting error associated with attack-range density reduction is more favorable, the approximation method
itself (and its resulting error) displays some exotic behavior. After a certain HP value, the error associated with 9 spacing
actually drops below the 7-spacing error. While I cannot present an explanation or a means to predict the occurrence of this
phenomenon as of yet, I can confirm that this does occur for other cases. This is a notable difference when compared to the
mean expansion method’s error, which monotonically decreases with expansion factor increase. With that being said, unknown
sources of error reduction are redeeming qualities for the density reduction method.
Overall, the density reduction technique proves to be suitably accurate and robust. Although its capabilities have only been
demonstrated on a single case, it is likely to yield similarly pleasing results for larger attack ranges and higher HP values.

5 Conclusion
The average hits equation is well-posed, but there does not exist a closed-form solution for it. Accordingly, an algorithm has
been presented that numerically solves for the multiplicity function and ultimately the average hits equation. Because of the
inherent, numerical complexity of the problem, the algorithm that solves for the exact solution quickly becomes intractable at
higher attack ranges. To remedy this issue, two approximation methods have been presented. Though the mean expansion
method reveals an interesting fact about the mathematical space and is useful for setting upper and lower bounds to the
target solution, the density reduction technique is ultimately more effective at actually attaining an acceptably accurate result.
Therefore, for most instances of the average hits problem, I finally conclude that the density reduction method should be used.
With high-end resources or for lower attack ranges and HP values, the exact solution algorithm may be employed.

7/9
Appendix
Exact Solution Algorithm
function [avg_hits]=getavghits(atkmin,atkmax,hp)

range=[atkmin:atkmax];
rangesize=size(range);

%First hit
n=1;
multiplicity(n)=0;
population=range;
populationsize=size(population);
k=1;

while k<=populationsize(2)
if population(k)>=hp
population(k)=[];
multiplicity(n)=multiplicity(n)+1;
k=k-1;
end
k=k+1;
populationsize=size(population);
end

%Subsequent Hits
while isempty(population)==0
clear newpopulation;
n=n+1;

multiplicity(n)=0;

for k=1:populationsize(2)
newpopulation(k,:)=population(k)+range;
end
newpopulationsize=size(newpopulation);
row=1;

for p=1:rangesize(2):(newpopulationsize(1)*newpopulationsize(2)-newpopulationsize(2)+1)
population(1,p:p+rangesize(2)-1)=newpopulation(row,:);
row=row+1;
end
populationsize=size(population);

k=1;
while k<=populationsize(2)
if population(k)>=hp
population(k)=[];
multiplicity(n)=multiplicity(n)+1;
k=k-1;
end
k=k+1;
populationsize=size(population);
end
populationsize=size(population);
end

8/9
multiplicitysize=size(multiplicity);

for s=1:multiplicitysize(2)
probability(s)=multiplicity(s)/(rangesize(2))^s;
end

avg_hits=0;
for k=1:multiplicitysize(2)
avg_hits=avg_hits+k*probability(k);
end
end

Mean Expansion Method


function [avg_hits]=getapproxavghits(atkmin,atkmax,hp,expansion)
avgatk=(atkmin+atkmax)/2;
avg_hits=getavghits(avgatk-expansion,avgatk+expansion,hp);
end

Density Reduction Method


function [avg_hits]=getapproxavghits2(atkmin,atkmax,hp,spacing)
range=[atkmin:spacing:atkmax];
rangesize=size(range);
.
.
.
The rest follows as in the exact solution algorithm.

9/9

Das könnte Ihnen auch gefallen