You are on page 1of 5

Clash of Clans Particle Swarm Optimizer

Avery Chiu Brandon Goh Jessica Wormald


Department of Mechanical & Department of Mechanical & Department of Systems Design
Mechatronics Engineering Mechatronics Engineering Engineering
University of Waterloo University of Waterloo University of Waterloo
Waterloo, Canada Waterloo, Canada Waterloo, Canada
a27chiu@uwaterloo.ca bgoh@uwaterloo.ca jwormald@uwaterloo.ca

Mathurah Ravigulan Mansheel Chahal Joshua Kurien


Department of Systems Design Department of Mechanical & Department of Mechanical &
Engineering Mechatronics Engineering Mechatronics Engineering
University of Waterloo University of Waterloo University of Waterloo
Waterloo, Canada Waterloo, Canada Waterloo, Canada
mravigul@uwaterloo.ca m3chahal@uwaterloo.ca j2kurien@uwaterloo.ca

Abstract— This research introduces a novel metaheuristic of each particle in the swarm is affected both by the local best
optimization method inspired by Particle Swarm Optimization solution during its search (individual) and the position of the
(PSO) and tailored to the dynamics of the popular video game global best particle in its surrounding (trajectory) [2].
Clash of Clans. The proposed Clash of Clans Optimizer utilizes A widely used foundation for other optimizers, PSO
diverse troop types with unique characteristics, presenting a
dynamic balance between exploration and exploitation in multi-
is an attractive solution due to its ease of initialization and
dimensional search applications. Despite a slower runtime particle parameter tuning, yet one of the inherent limitations
compared to traditional PSO, our algorithm demonstrates the with PSO is a premature convergence to local optimum points
ability to identify better global optimums, showcasing its of the objective landscape. To mitigate these shortcomings,
effectiveness. The algorithm's tunability is highlighted through we propose a metaheuristic optimization method based on the
the adjustment of elixir amounts, allowing users to control the popular video game, Clash of Clans [3]. Particles are
number of troops generated and customize the optimization represented through diverse troops and characters, each of
process. Moreover, the concept of hard-coding troop which possess unique capabilities and behaviour patterns.
compositions is introduced as a potential enhancement, Swarm initialization consists of allocating troops to specific
providing a strategic approach to team generation. The results
suggest that the Clash of Clans Optimizer presents a promising
regions of the optimization landscape based on ability and
alternative for optimization scenarios, addressing the strategic considerations. Through the assignment of fitness
limitations of premature convergence often associated with scores each iteration (referred to as a ‘raid’ in the algorithm)
PSO. This study opens avenues for further exploration and on a one-to-three-star scale, a feedback loop is introduced by
refinement of this innovative optimization approach. which future raid decisions are derived. Each raid iteration
consists of a troop attack on its neighbours, following a
Keywords—PSO, optimization, clash of clans, metaheuristic Variable Neighborhood Search approach.
search

I. INTRODUCTION II. METHODS & IMPLEMENTATION


Particle Swarm Optimization (PSO), a family of The setup for the Clash of Clans Optimizer starts from a
metaheuristic optimization methods inspired by natural base implementation of the PSO algorithm. In the particle
search dynamics observed in flock-based animal groups, are initialization phase, several key design changes have been
widely used in multi-dimensional search applications made, as follows. The implementation of the Clash of Clans
wherein the objective functions are complex and require a Optimizer is available on GitHub at
cooperative approach [1]. PSO operates based on the concept
of particles as candidate solutions to an optimization https://github.com/joshuakurien/clash-of-clans-raid-
problem. Each particle contains the ability to spread over an search
optimization landscape, intercommunicate local findings, and
adapt their characteristics as well as search direction based on
community findings. As such, the iterative change in position

©2023 IEEE
A. Troop Design neighborhoods containing the top three lowest found costs are
To create a dynamic balance between the amount of assigned a star ranking whereby the global best solution of
exploration across the optimization landscape and the iteration is awarded three stars, the second best
exploitation of local results, six particle objects, or troops, are performing receives two stars, and the third neighborhood
defined and initialized with characteristic member attributes one star- to then inform the next raid iteration. These top-
from a base particle class. Characteristics have been selected scoring neighborhoods are then passed as input arguments to
and expressed through weights, or hyperparameters to either the next iterations of the algorithm.
augment or decrease the velocity of each particle. As such, The neighborhood generation involves two integral
𝛼1 denotes the weight on the momentum component, or components. Firstly, a function is introduced to partition a
overall particle speed, 𝛼2 weighs the local best solution, and given numerical range into distinct sets. Given a start and end
determines how much to exploit the landscape, and 𝛼3 point of a range, as well as the desired number of sets, the
weighs the global best solution velocity component, and thus function calculates the size of each subinterval and iteratively
how much to explore the landscape [1]. computes the start and end points for each set. The resulting
Additionally, for the control of the proportion of each sets are represented as tuples and collectively form a division
troop type to initialize in an iteration of raids, an “elixir” of the original range. This function provides a foundational
hyperparameter is introduced. Elixir determines the mechanism for discretizing continuous numerical domains.
program’s total capacity for how many troops, or particles The neighborhood generation function leverages this to
will be implemented [3]. Each troop type is assigned an elixir construct neighborhoods within a two-dimensional region.
value in the range of 1-10 to control how many of each type Through nested loops, it combines the width and height
can be at play. This parameter can be adjusted depending on subintervals to generate a grid of neighborhoods, where each
a user’s preference for more exploration versus exploitation. neighborhood is represented by a list of width and height
subintervals. This configuration allows the partitioning of the
1. Giant: More inclined for exploitation techniques whole neighborhood into discrete rows and columns, yielding
during a raid iteration, giants are designed to move a structured grid-based neighborhood.
in a slower fashion through their local C. Troop Tuning
neighborhood. Thus, the particles are assigned small
alpha values in initialization. For our The 𝛼1,2,3 weights were then fine-tuned to mimic each
implementation purposes, they have been assigned troops assigned behaviour in the game, to thus iteratively
an average elixir value of five. update their velocity, as seen in Equation (1).
2. Archer: Archers offer a middle ground approach for
exploration versus exploitation. With a large weight 𝑇𝑜𝑡𝑎𝑙 𝑣𝑒𝑙𝑜𝑐𝑖𝑡𝑦 = 𝛼1 ∗ 𝑣𝑒𝑙𝑜𝑐𝑖𝑡𝑦 + 𝛼2 ∗ (𝑙𝑜𝑐𝑎𝑙𝑏𝑒𝑠𝑡 −
𝑝𝑜𝑠𝑖𝑡𝑖𝑜𝑛) + 𝛼3 ∗ (𝑔𝑙𝑜𝑏𝑎𝑙𝑏𝑒𝑠𝑡 − 𝑝𝑜𝑠𝑖𝑡𝑖𝑜𝑛 ()
on the local best velocity component, Archers have
the capacity to cover large distances towards self-
For visualization purposes of the tuning stage, the algorithm
identified targets. Relatively cheap at an elixir of
was run using one troop of the corresponding type, of which
three, many archers can be deployed at a time.
the results are shown below.
3. Goblin: With a tendency to quickly identify local
best solutions due to a large momentum weight,
1. Giant: The low alpha values represent the slow
goblin particle types face the challenge of becoming
behaviour of this troop as seen in Figure 1.
stuck in local bests, similar to a local treasure trove
in the video game. In this case, goblins are the
lowest cost character at an elixir value of two.
4. Wizard: Wizard troop types offer a balance through
mid-range weights on all velocity components. The
prescribed elixir value for this troop type is average
at a value of 4.
5. Dragon: Designed to mimic the character with a
long-range aerial view, the dragon is designed for
high exploration across the optimization landscape
through large weights on momentum, as well as Fig. 1. Optimization results for test with Giant hyperparameters.
local and global best solutions. A rare character,
dragons are deemed worth 10 elixir units. 2. Archer: A larger gain of alpha values were used
6. Hog Rider: Enabled with agility, the hog rider with emphasis on 𝛼2 to mimic the middle ground
particles can jump using large steps from iteration to approach of this troop as seen in Figure 2.
iteration yet employ a moderate speed through a
mid-range momentum weight within local search.
The given elixir for these particles is 6, slightly
above the middle ground.

B. Neighborhood Implementation
Another distinction from PSO concerns the best
neighborhood assignment in each iteration. In similar fashion
to the score assigned a raid in Clash of Clans, the
Fig. 2. Optimization results for test with Archer hyperparameters. Fig. 6. Optimization results with Hog Rider hyperparameters.

3. Goblin: A medium sized gain was used for the alpha From this setup, several well-known benchmark
values with emphasis on 𝛼1 due to the fast mobility cost functions have been defined for algorithm execution and
of this troop, which is represented in Figure 3. testing. For comparative analysis to similar metaheuristic
optimization methods, additional algorithms were selected to
run on the same cost function, convergence criteria, and
iterations, of which the final global best and runtime are
recorded. A plotting method to trace the path of the particles
has been included for ease of visualization.

III. ANALYSIS & RESULTS


For the simulation, analysis, and comparison of the
proposed algorithm against similar accepted optimization
techniques, the Clash of Clans Optimizer was run in
Fig. 3. Optimization results for test with Goblin hyperparameters. conjunction with PSO, Local Search (Hill Climbing), and GA
(Genetic Algorithm)- all with a common objective function
4. Wizard: This well-rounded troop was mimicked and iteration parameters set as: maximum iterations = 500,
with average alpha values compared to other troops. elixir = 20, local best defined at the best solution across all
The medium sized range can be seen below in iterations, and global best solution defined as the global best
Figure 4. among all algorithm iterations. For the local search/raid by a
troop particle within a neighborhood, the convergence
threshold has been set to 0.01.
Simulation results for the top performing
neighborhood and global best solution is shown in Figures
7,8, and 9 for a revenue cost function [4]. Fig. 7 visualizes the
three-dimensional cost function, in which the optimization
path of the algorithm is traced in red, and the final optimum
in blue. Fig. 8 is a two-dimensional view of Figure 9 provides
Fig. 4. Optimization results with Wizard hyperparameters. additional context though the plotting of the historical path of
the argument.
5. Dragon: The long-range aerial movement has been
modelled using extremely high alpha values, as
shown in Figure 5.

Fig. 5. Optimization results with Dragon hyperparameters.


Fig. 7. Best neighbourhood optimization results (3D).
6. Hog Rider: This troop has slightly higher mobility
than an archer due to its ability to jump over
obstacles in the game, therefore the alpha values are
slightly higher.
Noting the quantitative results of the algorithm
against its comparators in Table 1, while the algorithm
runtime is significantly higher than its peers, the resultant
optimal solution of the Clash of Clans Optimizer is notably
better- having found the lowest cost amongst all implemented
methods.
IV. DISCUSSION
The results of our Clash of Clans Optimizer showcase an
intriguing alternative to the widely used Particle Swarm
Fig. 8. Best neighbourhood optimization results (2D). Optimization (PSO) algorithm. Despite exhibiting a slower
runtime compared to PSO, our algorithm demonstrates the
capability to identify better global minimums in multi-
dimensional search applications. This suggests that the unique
characteristics of troop-based particles, inspired by the
popular video game Clash of Clans, contribute to a distinct
optimization approach.
One key advantage of our algorithm lies in its tunability.
By adjusting the elixir amount, users can control the number
of troops generated during each iteration. This feature
introduces a valuable parameter for optimization
customization, allowing a balance between exploration and
exploitation strategies. The elixir parameter acts as a lever,
Fig. 9. Best neighbourhood historical results (2D). enabling users to fine-tune the algorithm's behavior based on
specific preferences and problem characteristics.
With the inclusion of Fig. 9 to visualize the total Furthermore, our Clash of Clans Optimizer introduces the
historical path of particles within the highest-ranked concept of hard-coding troop compositions as a potential
neighborhood, the misleading nature of Fig. 8’s short path enhancement. Unlike the current random generation of troop
and conclusive early convergence can be understood. The teams, hard coding allows for the creation of predetermined,
varied path trajectories and iterative jump lengths highlight strategically designed teams. This modification could
the nature of diverse troop attacks, in which a cohesive search potentially lead to more efficient and targeted exploration of
approach is applied to the landscape. Results for the the optimization landscape. The ability to specify ideal troop
algorithm’s performance in other common objective combinations aligns with the notion of leveraging domain
functions [4] are detailed in Table 1 below. knowledge to guide the search process, potentially
overcoming some of the limitations associated with purely
TABLE I
random troop generation.
Comparison of Methods The utilization of diverse troop types, each with unique
Algorithm Cost Runtime
Function Final cost characteristics and behaviors, presents an innovative
(s)
Revenue departure from traditional particle representations in
27.8 -181499999.99999952
optimization algorithms. The dynamic balance achieved
Clash of Griewank 18.7 0 through troop design, incorporating characteristics such as
Clans momentum, local best solution weighting, and global best
Schwefel 16.48 6.689
Optimizer
solution weighting, provides a nuanced approach to balancing
Schaffer 22.98 0 exploration and exploitation. This nuanced balance addresses
Revenue 1.01 -181377617.65602878 one of the inherent limitations of PSO—the premature
Griewank convergence to local optima—by leveraging the strategic
0.0478 0.0073960944278980145
PSO diversity inherent in Clash of Clans troop dynamics.
Schwefel 2.83 123.70750017327083 The introduction of the Variable Neighborhood Search
Schaffer approach, modeled after raids in Clash of Clans, adds an
1.828 0.0159
additional layer of complexity and adaptability to our
Revenue 0.027 -181457732.8459237 optimization algorithm. This approach allows for dynamic
Griewank 0.0478 0.017668157859585754 adjustments in troop attack strategies, responding to the
Genetic
Algorithm Schwefel
evolving landscape of the optimization problem. Such
0.0497 5.167 adaptability is crucial for handling complex and dynamic
Schaffer 0.0396 2.5148 objective functions, offering a potential advantage over more
Revenue static optimization methods.
0.011 274225900.64544237
In conclusion, our Clash of Clans Optimizer presents a
Griewank 0.008 0.7173885164256175 novel and tunable metaheuristic optimization method inspired
Local Search
Schwefel 0.00899 510.6 by the dynamics of troop-based raids in the popular game.
While acknowledging the slower runtime compared to PSO,
Schaffer 0.008998 10.01 the algorithm's ability to identify global minimums, along
with its customizable nature, opens avenues for further tune and customise based on the nature of the objective
exploration and application in diverse optimization scenarios. function.
The proposed hard coding of troop compositions and the
strategic balance achieved through troop design provide
directions for future research and refinement of this innovative E. Benchmarking
optimization approach. Extending the evaluation of the Clash of Clans optimizer
to a wider breadth of benchmark problems and applications
V. FUTURE WORK is necessary in order to properly assess the algorithm’s global
A. Hyperparameter Tuning problem-solving capabilities. Future work should involve
testing the algorithm on a larger set of functions and scenarios
A promising area for future improvement involves a more
to evaluate its performance. Additionally, benchmarking
in-depth investigation into hyperparameter tuning to increase
against state-of-the-art optimization algorithms, not just
the optimal solution performance. While demonstrating
comparable base implementations of common algorithms
promising results with the current troop parameter tuning,
will lead to a better understanding of Clash of Clans
there exists potential for greater efficiency. Future
Optimizer's inherent strengths and weaknesses, guiding
improvements could explore advanced optimization
further improvements and adaptations that cannot be seen
simulation techniques, such as Bayesian optimization or
solely within the context of the proposed algorithm.
Genetic Algorithm to systematically search the possibilities
for hyperparameters. Leveraging automated hyperparameter
tuning would streamline the process, enabling the algorithm The future research proposed aims to enhance Clash
to adapt dynamically to diverse problem applications. of Clans Optimizer’s efficiency, adaptability, and usability,
for a range of metaheuristic optimization algorithm needs.
Within these noted areas, future contributions can add to the
B. Advancements in the Raid Penalty/Reward System ongoing work of swarm intelligence algorithms, as well as
The Clash of Clans Optimizer can be further refined their efficacy in solving challenging optimization problems.
through enhancing the current three-star raid reward system. ACKNOWLEDGMENT
Future work could focus on designing more sophisticated
penalty functions with the use of reinforcement learning We would like to express our sincere appreciation to
techniques. Adaptive adjustments based on raid performance Professor Benyamin Ghojogh for his exceptional guidance
and convergence time will lead to a historically informed and and inspirational teaching in the realm of metaheuristic
quickly adaptive optimization process. optimization methods. Professor Ghojogh's passion for the
subject, coupled with his clarity in conveying complex
mathematical concepts, has not only deepened our
C. Elixir Choice for Runtime Optimization understanding but has also ignited a genuine enthusiasm for
The current implementation of the Clash of Clans exploring the frontiers of this fascinating field. His
Optimizer utilizes the elixir hyperparameter to influence the unwavering commitment to academic excellence and his
particle distribution. An informed analysis of elixir tuning ability to inspire curiosity have left an indelible mark on our
would be beneficial to understand its direct impact on runtime learning journey. I am grateful for the invaluable mentorship
and performance. Future work could hone in on the and inspiration provided by Professor Ghojogh, which will
development of heuristic approaches to dynamically adjust undoubtedly shape our future pursuits in this area. We
elixir values during the algorithm’s runtime, thus optimizing acknowledge Professor Benyamin Ghojogh for being an
the trade-off between exploration and exploitation through inspiring educator and mentor.
dynamic troop allocation. This would produce a more
REFERENCES
adaptive algorithm that tailors its elixir choices to the given
optimization landscape. [1] S. Darvishpoor, A. Darvishpour, M. Escarcega, and M. Hassanalian,
“Nature-inspired algorithms from oceans to space: A comprehensive
review of heuristic and meta-heuristic optimization algorithms and
their potential applications in drones,” Drones, vol. 7, no. 7, p. 427,
D. Troop Type Analysis for Algorithmic Trends 2023.
To gain insight into algorithm strengths, an analysis of the [2] J. Kennedy and R. Eberhart, “Particle swarm optimization,” in
distribution of troop types contributing to global best Proceedings of ICNN’95-international conference on neural networks,
vol. 4, pp. 1942–1948, IEEE, 1995.
solutions is of great interest. Future work would involve a
[3] Clash of Clans Wiki. (n.d.). [Online]. Available:
thorough examination of the relationship between troop types https://clashofclans.fandom.com/wiki.
and solution quality. Such analysis will point to where the [4] Test functions for optimization. In Wikipedia. Retrieved June 2013,
Clash of Clans Optimizer excels, reveal patterns in solution from https://en.wikipedia.org/wiki/Test_functions_for_optimization.
types and troops, and provide a clear path forward on how to

You might also like