Abstract
In this paper, the following work is done: The application of genetic algorithms in games is discussed, and based on reading relevant references, the design of the algorithm is explained in detail; the current situation of combining algorithms with games is analyzed; the novelty of combining algorithms with games is explained; and the paper concludes with a summary of the considerations and the scope of application of using genetic algorithms in games. The aim of the study is to reduce the cost of consultation and understanding for game makers and researchers, allowing them to focus on the specific application of genetic algorithms in games to make the AI of the game evolvable and intelligent to the level of the players.
Introduction
Many interesting things and phenomena are presented in the form of games, such as military and life. Therefore, the game is a great place to simulate and solve problems. Therefore, using genetic algorithm to simulate solving problems is often presented in the form of game.
Compared with the forward position research, the current stage of the game intelligence is still not enough. The NPC is still mainly on the traditional logic mechanism for example behavioral tree, state machine, etc. The genetic algorithm has not been adopted for the intelligent design of NPCs on a large scale.
In order to give readers a deeper understanding of what genetic algorithm is, hence, we describe some basic biological knowledge. Then popularizing the standard genetic algorithm.
As known to all, one of life phenomenon is inheritance which can make them hold some of trait from progenitor. That is called genetic. Cell is the basic structure and remove a unit to constitute life. Also, it has genes, which control biological traits. In order for trait to be inherited by future generation, cell passes genes to offspring through dividing and replicating, thus retaining this trait. After the systematic studies, it was found that the substance of determining the bio-genetic shape is a substance called a deoxycodide which is arranged in an orderly manner in the chromosome. The substantially constituent unit of deoxycodide is nucleotide. The nucleotide forms a long chain structure by combining a phosphate diester bond. Long chain is curving through hydrogen bond, finally retouching the double helix structure. A gene is given a point or segment in the chromosome, which is called a locus. The sum of all genetic substance is called the genome. Homologous staining produces new chromosomes by crossover. In germ cells, mutation is an important concept that can not be ignored. It is usually caused by error repairing after DNA replication error or DNA damage. It also can introduce a new alleles into biological population, thus increasing the genetic variation of population.
Attempting to make their own multiply, in order to adapt to the living environment, organisms will continue to change the genetic traits; this phenomenon is called evolution. According to Darwin’s theory of natural selection, when genes for survival are increasing, the pupulation will develop in the direction of adaptation to its environment, resulting in superior species.
On this basis, it leads to the key point of this paper-genetic algorithm. Genetic algorithms were designed and put forward by John Holland [1] in 1970s according to the evolution law of nature. The algorithm is simulated by computer in mathematical way, and it is an adaptive global optimization probability search algorithm. The main behaviors are as follows.
Initialization: Generate random populations with a sufficient number of individuals. Calculate fitness: Calculate the merit of individuals by means of a designed fitness function. Selection: Select the best individuals from the population. Crossover: Swap two chromosomes. Mutation: Mutation of a gene in a chromosome.
In the 1980s, it was summarized by Goldberg, and finally formed the basic framework of genetic algorithm.
The basic framework of genetic algorithm.
It is important to know that, compared with other algorithms, the genetic algorithm is a robust search algorithm that can be used in complex systems and optimize computation. Using the value of the objective function as the search information, the search direction and scope are determined, which avoids the problems of function derivation, function optimization, combinatorial optimization and so on. At the same time, it has implicit parallelism and searches for the optimal solution of multiple individuals. It avoids stagnation by local optimal solution causing.
All kinds of new algorithms and improved algorithms emerge in an endless stream with the progress of hardware and other technical conditions in today’s society. Also according to the evolutionary characteristics of genetic algorithms, the following narrative is made.
For assessment purposes, time limits and achievement scores are given within the framework. The robot tries to move. The score is increased by 1 when the robot reaches the target point. Then it returns to the newly formed starting point and repeats the process until the end of time. Achievement scores will be included in the replication process as a fitness function.
In the experiment, a total of three different maps were used. The first map A is used for the machine to learn, and the other two maps B with mirrored structures are compared with C. It should be noted that map B is similar to map A. In the process, the robot scored higher when the maze map was similar. The results showed that the robot could understand the intent of the maze.
According to the situation that will occur, the researchers designed a 3-digit code length for the chromosome code to represent the last three games (each with 3 integers, that is, 0 represent the scissors, 1 represent the paper, and 2 represent the stone), a total of 27 strategies. Therefore, the code length of 27 bits is set (at this time, each bit represents the position, but also indicates the situation of the last three games at that time), and the number in each bit indicates the decision made.
In the experiment, compared with the system strategy, GA obviously has the advantage of predicting the behavior of opponents. But it is not accurate when faced with human players, because the behavior of human players is extremely difficult to predict.
The encoding is in the form of 12 groups with 33 bits as a group. Each group can represent a task group or a ship group. Bits 1 to 3 in a group are used to specify the type of ship, bits 4 to 5 are used to specify the number of ships, and the remaining 28 bits are used to specify movement at a given time.
And the fitness function is given as the following equation:
Two methods of estimating benefit items that have successfully achieved the goal of victory are used in terms of the overall design, which boil down to the combination of elite individual and measurement method.
Specifically, the sequence of line segment points connected from the starting point to the end point will be chosen as the initial sequence of path points of the first path. Secondly, the path point sequence randomly distributed in the obstacle avoidance area is used as the initial path point sequence of other paths. Then the path planning algorithm of the traditional artificial potential field method is used to make the initial sequence of path points form different obstacle avoidance paths, which forms the initial population. The details are as follows:
Individuals of each generation are seeking the shortest path, so the fitness function is designed as the difference between the longest path in a generation group and the path length of the current individual.
If the individual path is longer, the value of the adaptation function is smaller. On the contrary, the shorter the individual path is, the larger the value of the adaptation function is, and the more it meets the demand. The selection probability of each individual was calculated by the ratio of the fitness value of each individual to the total fitness value. The sequence of points with a large distance difference has a higher probability of recombination.
Finally, the shortest path is fitted by the least square method to make it a smooth and continuous curve. It is convenient for soccer robot to control. The experimental results show that this algorithm can effectively generate obstacle avoidance paths.
When the algorithm is applied to checkers games, it is required as a whole to minimize the number of pieces lost by the computer, maximize the number of pieces lost by users, and avoid actions that lead to bad results. Two kinds of tests are carried out in this paper. In the first kind of human players against artificial intelligence, the AI win rate reached 52.5% in 40 games, and the second compared the performance of two different genetic algorithms in millisecond average time. Finally, the author also said that although the results are satisfactory, the random factors of the algorithm make the application less stable. And expect to add the mobile tree in the next version; the performance of the algorithm will be significantly improved through the depth tree search.
A scene from Warcraft 3.
Jason et al. [7] used genetic algorithm to evolve the weights of artificial neural networks (ANNs), and applied it to Warcraft 3. In the game, it plays a role in deciding which units to produce to defeat the opponent. The researchers took the number of enemies of each unit type as input and the number of each unit type produced by the agent as output. At the same time, there is a limit on the number of units. The generation of units will consume food.
The residual food of the agent unit is subtracted from the remaining food of the enemy as the fitness function in the design.
The concept of elite is introduced to avoid losing good individuals in the process of optimization. Experiments show that this combination algorithm can be used as an effective adjustment technology for unit generation, and the resulting troops can also effectively defeat opponents. In the end, the researchers also pointed out a more specific research direction, that is, incremental learning and multi-objective concepts may lead to better solutions to the problem.
A scene from Geometry Friends.
Then for the game, the authors et al. performed the following fitness function design.
Indicates the number of diamonds collected and indicates the value of diamonds. It is a reward that will be given only after the player has collected all diamonds. Then denotes the longest game time, which indicates how long the player has been playing the game.
Based on the above adaptive functions, elite population land reservation is performed, which leads to iterations. The final set of quality parameters under rule-based is obtained.
When an action makes itself about to reach a certain mode, it corresponds to its own attack value. Or when the opponent is prevented from reaching a certain mode, it corresponds to its own defense value, resulting in the fitness function is defined.
At the same time, in order to prevent AI who attempt to obtain a higher score from choosing to not end the game, the sooner AI wins more in the game, the greater the score is given. In the experiment, the initial population was 2000 and the peak was 3500. Each time 200 individuals with the highest fitness were retained to produce the next generation. When the genotypes of the best individuals are stable for five consecutive generations, the genetic algorithm converges. The experimental results show that in the Gobang game, the genetic algorithm has a deeper search than the traditional algorithm based on a game tree, resulting in a better and more efficient solution.
A scene of Angry Birds.
Misaki [10] of Limengguan University and others use the GA algorithm to automatically create levels for the game AngryBirds. According to the player’s current game results, the level suitable for the player’s skill level is generated by improving the fitness function and adjusting the fitness function parameters. That is, the function designed by the researchers at this time focuses on balancing the number of blocks, pigs and birds to prevent any variable from being too large or too small, thus ensuring the difficulty of the next level. The fitness function is given as the following equation.
In the experiment, the parameters are adjusted in real time, and 12 players are invited to test. The results show that the method proposed by the researchers can well generate game levels suitable for players, especially for players who can not play Angry Birds well.
Unlike most other AI using the three abstract levels of strategy, tactics, and reactive control, the researchers proposed in the experiment that for the actions taken by a unit, the learning model needs to be selected from a pre-set set of actions. Actions include kites that are hit and run, distance priority that is attacking the closest enemy, and blood volume priority that is attacking the enemy unit that has the lowest Hp.
And design three scenes to correspond to the above three actions. Scenario 1: Allied tanks have a longer range of attack than enemy marines. Taking action A. Scenario B: The enemy tank in the lower left corner is within range of the ally tank. Taking action B. Scenario C: It is better for all units to focus on one enemy than for each unit to engage in one-on-one combat. Action C will be taken.
A scene from StarCraft.
When a chromosome is encoded, 15 gene sites are assigned to it. The first 12 bits are interpreted as whether action will be taken, and the last three indicate the priority of the operation.
Three experiments were designed for testing. They are the operation selection of unit type, the cooperation between units, and the cooperation of ground and air forces. It is noted that the length of chromosome will be changed when the last emperiment is tested.
The final experiments show that the learning model of the current framework enables troops to take appropriate actions and collaborate with other allies. At the same time, the potential research directions in the future are pointed out, such as the generation of automatic action, the competition of individuals generated by genetic algorithms, and so on.
In addition, there are some limitations in order to be able to fully implement the GP algorithm in games. First, the characters must follow the same rules as the human player, such as moves or tactics that can’t physically be reproduced by the human player, and the AI also can’t appear. Second, the AI’s reaction time should be on par with the human, not faster than the human player.
When the initial population is created, the actions of the game characters are manipulated by genetic operators. When defining the fitness function, the differences in individual performance should be taken into account. The selected system should be robust, not just based on simple wins and losses.
That is to say, the opponent’s technology and whether the result is achieved should be considered. Therefore, the expected score of the player is calculated using the following formula: P1 and P2 correspond to player one and player two, respectively. The rating R is adjusted whenever the game is over. K is used to control the intensity factor adjustment. S denotes the bool value regarding victory and defeat.
The selection method is defined by preserving the top individuals and using crossover operators to produce offspring with the chance of mutation.
They added elite individuals, descendants of elite individuals, and random individuals to the new population. Building AI characters in an evolutionary way can save a lot of costs and automatically generate a lot of different characters.
Finally, the procedure is able to create a wide diversity of participants with different strategic skills, which could be potentially used as a starting point for further adaptive process.
Specifically, implement a function that creates a virus and propagates the virus to individual population based on the biological virus behavior. In the design of chromosome coding, the number of genes is equal to the number of points to be passed. That is, the length of genes is 8 bits.
At the same time, the fitness function is given as the following equation.
The individual
After the comparison of experiments, it is shown that mutation is the key point to solve the problem of pathfinding. Among the four algorithms compared, the standard GA
Specific design-neural network part: each group has a random and exclusive neural network. What needs to be evaluated are the neural networks that lead the agents to victory. The vertical coordinate of the agent, the horizontal distance of the nearest obstacle in front of the agent and the height of the nearest obstacle in front of the agent are designed as the input layer, and the forward movement and jump are designed as the output layer.
Specific design-genetic algorithm part: the initial population has 20 individuals, each agent has its own neural network, and the weight in the neural network is randomly generated; The fitness function design is relatively simple to achieve the target score; In the selection function, each agent has the opportunity to be selected to participate in the generation of offspring, but the agent with the highest adaptability is more likely to be chosen; In the crossover function, only the replication process is performed. In the catastrophe function, the weight will be changed randomly.
Other genetic algorithms are not much different from the standard GA.
At the same time, in order to improve user engagement and provide user-friendly educational materials, it is necessary to continuously optimize the offspring. Therefore, iterative operations are performed by means of two adaptation functions. The following equations are used.
WFS denotes fitness function of winning. LFS denotes fitness function of failure. Once any of
A scene from Hearthstone.
H.C. Chia et al. [17] applied genetic algorithms with the expectation of finding a good chessboard evaluation criterion in order to choose the best action based on it. Thus, they designed their own fitness function after referring to others’ fitness functions. The fitness function is given as the following equation.
An elitist structure was used for both selection and reproduction, with the top 5% of individuals retained and crossover and mutation based on a 9:1 ratio. The genetic algorithm was used to establish the board evaluation criteria for the Hearthstone game.
A scene from Legends of Code and Magic.
Y. Yang et al. [18] applied genetic algorithms to the evaluation of cards by scoring functions, and then selected the corresponding cards into the deck to increase the player’s win rate. They designed three coding schemes to correspond to three adaptation functions, respectively.
In the first one, the chromosome length is 160 (corresponding to 160 cards in the game) and the score of the corresponding card is at the gene. The agent can select the card with the highest score value into the deck; the second one differentiates attributes and special abilities, and then calculates the score of the card by weighting. The third option is an extension of the second one. The genome is added to option two, representing the desired number of cards with different mana costs. While the first two adaptation functions are simpler, we show the third adaptation function formula.
Most of the AI systems in commercial games rely on the designs of game designers. And most of the game designers are not academic researchers. They love games, but their lack of knowledge about mathematics and science makes them rely more on experience. So, for AI design, state machines, behavior trees, and other approaches are usually used. Therefore, genetic algorithms are actually used less in commercial games.
The novelty of combining the genetic algorithm with the game
We need to know that genetic algorithms are usually used in image processing and robotics. In image processing, it is mainly used for image enhancement, image recovery, etc., while in robotics, it is mainly used for behavior planning, path planning, etc.
In games, state machines and behavior trees are usually used to structure AI systems. When the state machine structure is simple, it is clear at a glance and gives the AI a certain degree of intelligence. However, once the states and conditions are increased, the state machine becomes complex and the intelligence of the AI is affected to some extent, while the behavior tree uses an object-oriented design concept, which strips the behavior logic from the state data. The use of tree structure improves visualization and facilitates problem troubleshooting. It allows NPCs to perform different actions in parallel, randomly, selectively or sequentially. At the same time, the degree of AI intelligence merit at this point relies more on the designer’s experience.
The NPCs with genetic algorithm, on the other hand, are able to correspond to the player’s level. When the preliminary work is well prepared, it is equipped with evolutionary nature in the later training process. The level of intelligence of the corresponding NPC can be adjusted according to the player level.
In a small way, we need to know that the advantage of combining genetic algorithms with games is that the AI designed using it is instantaneous and diverse. The combination of the two will make the whole game rich and hierarchical. This combination is full of uncertainty compared to traditional approaches, such as using state machines. This is what players love.
On the larger side, the combination of games and genetic algorithms that we have seen in the paper has a part that requires the researcher to re-architect the corresponding game, which is an extremely time-consuming act. The other part is that the developer opens up the game framework, allowing the researcher to better apply the genetic algorithm to it. It is true that, as stated above, it is extremely rare to apply genetic algorithms to commercial games. The reason is that this combined capability is not available to smaller game companies. The companies that have both capabilities are mostly large companies. This increases the inequality of the market.
Therefore, we want to give templates that can reduce the cost of understanding for game makers about the combination of genetic algorithms and games. Make the game AI more intelligent, correspond to the level of players and keep up with the market demand. In turn, better drive the whole game market.
Conclusion
The following considerations should be noted
Keeping the best problem-solving game AI samples; In order to ensure that there is still space for development in the anaphase, it is necessary to retain the diversity of the game AI. According to the difference of the game, it is necessary to design the corresponding strategies that the agent will adopt to solve the problem. It is necessary to design the corresponding chromosome coding according to the type of game and the experimental test object, which focuses on the design of the corresponding meaning of code length and code. When designing the fitness function, it is necessary to design the function in line with the current situation of the game, which can be compared after multiple experiments. The difference of fitness function will have an important effect on sample generation and convergence. The probability of beneficial evolution needs to be increased appropriately based on the principles of mutation and crossover. The probability value can not be set at will. It needs to be determined after verification and comparison.
When GA is applied in games, sometimes the obtained NPCs are not intelligent enough due to premature convergence. This requires that the population size is large enough when training is performed. When GA is used in games, it increases the amount of algorithm and time of the system. These are the things that need to be noted.
After combining other algorithms, the main points for attention are
The focus will not be on the most basic chromosome coding, but on the use of genetic algorithms to embed other algorithms as a whole. For example, when genetic algorithm is combined with neural networks, we can not only cross-reorganize the neural network structure, but also focus on choosing the neural network that scores better in the game or performs better in other ways. It is necessary to make behavior design correspond to solving game problems in the context of combination. The focus can be placed on other algorithms at this point, such as the design of the input layer and output layer of the neural network. The distance solution of the point sequence under the artificial potential field method, and so on.
In general, the most important point of genetic algorithm is its evolution. Choosing the best samples at the moment by constantly simulating natural selection. Thus, when the genetic algorithm is applied to a game, it is natural to select the AI population that is best suited to the game environment. At the same time, the AI can be updated to make the game smarter by constantly changing and optimizing the fitness function.
For game companies, AI with varying degrees of intelligence under the genetic algorithm can be retained to correspond to players of different game levels. In the guarantee of competitive at the same time, there is no lack of entertainment (The level of the player and the AI level are equal).
The scope of application of genetic algorithms combined with games
At the same time, genetic algorithm has a biased choice for game types. Competition NPCs in games such as real-time strategy, FPS, fighting and adventure are the primary embedded object of genetic algorithm. Uncertainty and immediacy are the most desired and significant features of real-time strategy, FPS, combat and adventure games. Genetic algorithms are used to frame the chromosomes and design the corresponding adaptive functions according to the game situation, which eventually allows the NPCs in the system to react differently and instantaneously. Therefore, genetic algorithms are mainly used for the design of NPCs in real-time strategy, FPS, combat and adventure games.
For drama, romance and other games, they mainly interact with the player through drama, unlocking and collecting cards. The immediate feedback of the game is fixed by the staff and there is no possibility of changing it. Genetic algorithms, on the other hand, are characterized by the need to frame the chromosomes and design the corresponding adaptive functions. This property is what allows the NPCs in the system to react differently instantly. So they are not in the range of games where genetic algorithms need to be applied. Therefore, genetic algorithms are not applicable to games in drama, romance, etc.
Footnotes
Acknowledgments
This research work is funded by project 0054/2021/A granted by the Science and Technology Development Fund of Macau (FDCT). The authors would like to express their appreciation for the support provided by the fund.
