




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





See discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/
Conference Paper · November 2016
DOI: 10.13140/RG.2.2.33593.
CITATIONS
READS
4 authors , including:
Some of the authors of this publication are also working on these related projects:
Parameter control of differential evolution View project
Jia-Fong Yeh National Taiwan Normal University
3 PUBLICATIONS 0 CITATIONS
SEE PROFILE
Tsung-Che Chiang National Taiwan Normal University
62 PUBLICATIONS 617 CITATIONS
SEE PROFILE
All content following this page was uploaded by Tsung-Che Chiang on 24 October 2016.
The user has requested enhancement of the downloaded file.
Department of Computer Science and Information Engineering, National Taiwan Normal University, Taiwan {40247006S, 40247031S, 40247032S}@ntnu.edu.tw, [email protected]
<< This paper is accepted to be presented in the 2016 Conference on Technologies and Applications of Artificial Intelligence (TAAI 2016) and will be included in IEEEXplore. >>
Abstract —Snake game is a computer action game, whose goal is to control a snake to move and collect food in a map. In this paper we develop a controller based on movement rating functions considering smoothness, space, and food. Scores given by these functions are aggregated by linear weighted sum, and the snake takes the action that leads to the highest score. To find a set of good weight values, we apply an evolutionary algorithm. We examine several algorithm variants of different crossover and environmental selection operators. Experimental results show that our design method is able to generate smart controllers.
Keywords—game; artificial intelligence (AI); snake; evolutionary algorithm
Snake game is a classic computer action game, in which we control a snake to move and collect food in a map. In this paper we are interested in developing a controller with artificial intelligence (AI) using movement rating functions and evolutionary algorithms (EA). Before we elaborate how we design our controller, we first describe the snake game we implemented and define the objective.
The game environment is a 3030 map. The snake starts with length one. At each time step the snake moves one step, and it can go straight, turn left, or turn right. There are two kinds of food in our game, apples and bananas. Eating an apple increases the length of the snake by one, whereas eating a banana decreases the length by two. There is always one apple and at most one banana in the map. One banana appears after every 30 apples are eaten. We can play the original snake game with a simple strategy: we move the snake up and down from the right to the left without touching the last row (the row at the bottom); when the snake is close to the leftmost boundary, go back to the rightmost boundary along the last row. In this way, the snake keeps alive until it occupies the whole environment.
To make our snake game more challenging and interesting, we set the initial score of an apple by 50 and decrease it to 25 as time passes. Besides, eating a banana decreases the score by 75. The game ends when any of the following four conditions is satisfied.
(1) The snake hits its own body or the wall. (2) The length of the snake gets zero.
(3) The game score gets negative.
(4) The game score does not change after 900 time units.
The objective of our game is to maximize the score. The above-mentioned simple strategy may keep the snake alive, but without moving toward the apples efficiently it cannot get a high score. Thus, we need to design a more intelligent controller, which is the topic of this paper. The rest of this paper is organized as follows. Section II reviews related work about computer game design using EAs. The main component of our AI controller is a set of rating functions, which will be described in Section III. We aggregate these functions through weighted sum, and Section IV presents how we set weights through an EA. Section V gives the experiments and results. Conclusions are made in Section VI.
Fig. 1. A screenshot of the Snake game
0 0 1 2 3 4 5
(a) A length-5 snake is moving from right to left. (1: head)
0 2 2 1 1 2 3 4 5 2
(b) Rating the go-straight choice: step 1 and 2
4 3 2 3 4 3 2 1 1 2 3 4 5 4 3 2 3 4 4 3 4 (c) Rating the go-straight choice: step 3 and 4
4 3 2 3 4 5 3 2 1 1 5 3 4 5 4 3 2 3 4 5 5 4 3 4 5 (d) Rating the go-straight choice: step 5
4 3 2 3 4 5 6 7 (^3 2 1) 1 5 6 7 8 4 3 2 3 4 5 6 7 5 4 3 4 5 6 7 8 (e) Rating the go-straight choice: completed
Fig. 1. Illustration of the smoothness rating function (assuming map size 48)
Fig. 2 illustrates the rating process. Assume that a lengh-5 snake is moving horizontally from left to right in Fig. 2(a). It has three choices, and now we rate the go-straight choice in Fig. 2(b). The numbers in Fig. 2(b) mark the reachable positions and represent the distance within two move steps. Keeping the above process (doing a breadth-first search, BFS, on the map), we have the state in Fig. 2(c). Note that we need a check condition to know if we can move to a position where the snake originally occupied. Whenever the snake moves one step, each of the indices of the snake (including its head and body) increases by one. Thus, the condition that the snake can move to where it occupied implies that the body disappears:
BodyIndex + MoveSteps > SnakeLength. (1)
In Fig. 2(c), the snake head cannot move to the position where the snake head (index 1) originally occupied in Fig. 2(a) since the condition is not satisfied (1 + 4 is not greater than 5). In Fig. 2(d), the snake head can move to the position where snake body with index 2 originally occupied since the condition is satisfied (2 + 5 > 5), leading to a distance of five. Finally, Fig. 2(e) shows that the snake can move 8 steps with only one turn if the decision is to go straight in Fig. 2(a).
B. Space
The smoothness rating function helps to identify the way to live easily. However, sometimes we do need to choose a way to live even though the life is tough. Take Fig. 3 as an example. The smoothness of turning right is seven and leads to a dead end. Although the smoothness of turning left is six (smaller than seven), but there is a chance for the snake to keep alive by making many turns. The space rating function returns the number of positions reachable. The smoothness and space rating functions are complementary and can find the safe way for the snake together.
1 2 3 4 5 6 7 11 10 9 8 12 21 13 20 19 18 14 15 16 17 Fig. 2. Illustration of the space rating function (assuming map size 67)
C. Food: Apple and Banana
The apple and banana rating functions are identical except that the target is different – one is the apple and the other is the banana. To rate a choice (go straight, turn left, or turn right), we check whether the target is still reachable after taking that choice. If it is not reachable, the distance is infinite and the score is zero; otherwise, we calculate the score by
f ( target ) = space / distance ( target ). (2)
Take Fig. 4 as an example. The star symbol represents an apple. Although turning left gets closer to the apple, the space is small (the snake will die), leading to a low food score 3/1 = 3. By contrast, turning right gets farther but makes it possible to eat the apple and survive. The food score of this choice is 16/3 = 5.33, higher than 3. Thus, the snake will choose to turn right.
1 5 4 3 2 6 7 8 9 Fig. 3. Illustration of the food rating function (assuming map size 47)
D. Direction Control
For each direction choice, we rate it by four functions described in previous three sub-sections. Let fi ( c ) denote the rating functions, where i {1, 2, 3, 4} and c is a choice. Since different rating functions may have different importance, we use weighted sum to aggregate these scores:
4
1
i
i i i
The choice that maximizes the final score is made.
Note that in (3) we have two weights for each function. One weight w 2 i 1 determines the base effect of function fi ( c ). The symbol l denotes the length of the snake. The other weight w 2 i represents the effect as the length of the snake grows. This helps us to express some complicated principles. For example, we are able to express that one rating function is important when the snake is short (eating apples is good at the beginning) but becomes relatively less important as the snake gets long (keeping alive is more important) by a large w 2 i 1 and a small w 2 i. Here we divide l by 900, which is the size of the map (3030) and also the maximal length of the snake. In other words, as the snake grows its length to the maximal value, w 2 i increases its effect to be equal to w 2 i 1.
In Section III we explain that our controller chooses the best direction that maximizes F ( c ) in (3). The next question is how to determine the weight values (i.e. the importance of each rating function). It could be a very tiring and time-consuming job for a human game designer to tune these weight values manually. In this paper, we propose an EA to do this job automatically and intelligently.
A. Chromosome Representation and Initialization
The goal of our EA is to find good values of eight weight parameters in (3). Therefore, the chromosome representation in our EA is a real vector of eight variables. The range of each variable is [-15, 15]. The value of each gene is set by zero, r , or – r in equal probability, where r is a random value in [0, 1].
smoothness space apple banana w 1 w 2 w 3 w 4 w 5 w 6 w 7 w 8
Fig. 4. Chromosome representation
B. Evaluation
Each individual is evaluated by playing the snake game with the proposed direction control procedure (Section III-D) using weight values encoded on the individual. Fig. 6 illustrates the relationship between direction control, EA, and the game. At first, the EA sends a set of weight values to the direction control module. Then, the game interacts with the direction control module by sending game states and receiving actions. Finally, as the game ends, the game sends a score to the EA as the fitness of the individual to be evaluated.
average, and minimum scores over ten runs are reported in Table II. Succ denotes the number of runs in which the snake grows to the maximum length 900. AvgLen denotes the average length when the game ends. The best performance is marked in bold.
TABLE II. SCORES OF CONTROLLERS USING DIFFERNET CROSSOVER AND ENVIRONMENTAL SELECTION OPERATORS (LENGTH-RELATED WEIGHTS DISABLED)
RC _N+N RC_GM SP_N+N SP_GM Max Avg Min
44721 28080 5991
42657 20795 0
43935 22190 5959
36522 19387 5952 Succ 1 0 0 0 AvgLen 567 420 448 393
We have some observations from the results. First, the best-case performance of the tested variants is close except that SP_GM is much worse than the other three. On average the RC crossover is better than the SP crossover, and the N+N method is better than the GM method. The RC crossover can exchange consecutive or non-consecutive genes between parents, but the SP crossover can exchange only consecutive genes. The higher variety of produced offspring might be the reason why RC is better than SP. The N+N method realizes the elitism and keeps good individuals in the population. Its better performance than the GM method implies that in this problem good individuals are not easy to find and they should be kept once they are found. Comparing with their counterparts, RC and GM tend to search in a diversified manner. This feature makes the combination of them (RC_GM) lose search direction sometimes, resulting in the zero score. The worst-case performance of the other three variants is almost the same. Actually, the evolved controllers in these cases were the controllers that only considered eating apples, i.e., only w 5 was positive but the remaining three weights were zeros. These solutions form the local optima in the search space.
D. Experiment 3: Need of Length-related Weights
The goal of this experiment is to verify the need of length-related weights, i.e., w 2 , w 4 , w 6 , and w 8 in (3) and Fig. 5. Here, they joined the evolution process, and thus the length of chromosome increased from 4 to 8. Scores of the four EA variants are listed in Table III.
TABLE III. SCORES OF CONTROLLERS USING DIFFERNET CROSSOVER AND ENVIRONMENTAL SELECTION OPERATORS (LENGTH-RELATED WEIGHTS ENABLED)
RC _N+N RC_GM SP_N+N SP_GM Max Avg Min
44803 44055 42744
44781 40004 19876
44805 41016 23381
44790 33934 13100 Succ 3 3 2 2 AvgLen 886 805 827 683
From Tables II and III, we can clearly see that the introduction of length-related weights significantly improves the performance of the controller. The average score increases by at least 14,547 points, and the worst score increases by 7,148 points. The life time of the snake also gets much longer, and the snake can grow to the maximum length in at least two of the ten runs for all EA variants. The addition of length-related weights doubles the chromosome length and enlarges the search space. However, the experimental results show that many solutions in the enlarged space are good and easy to find.
Until now we evaluated individuals with the same deterministic snake game based on two reasons: (1) we want to evaluate individuals in a fair manner; (2) the time to play a game is long. Next, we took the best controllers obtained in experiment 2 (length
TABLE IV. AVERAGE SCORES OVER 100 RANDOM GAMES OF CONTROLLERS WITH LENGTH-RELATED WEIGHTS DISABLED AND ENABLED
DISABLED ENABLED RC_N+N-Best 18158 20539 RC+GM-Best 15768 21491 SP_N+N-Best 12152 21557 SP_GM-Best 20768 20670 Average 16712 21064
E. Experiment 4: Evolved Controller vs. Heuristic Controller
In the last experiment we want to compare the evolved controller with a heuristic controller 錯誤! 找不到參照來源。. The heuristic controller was designed based on two ideas. First, the snake is only allowed to move to the positions that there is a path to
reach its own tail; if there are more than one position, move to the position closer to the apple. Second, the snake keeps the unoccupied positions connected. To achieve this, the snake moves away from its tail. If turning and going straight lead to the same distance from the tail, the snake turns. This kind of movement prevents the snake from circling itself to separate the space into two regions with the apple unreachable in the inner region.
We let the heuristic controller play the deterministic game we used to evolve controllers. It grew the snake to the maximum length and got 44609 points. Checking the results in Table III, all the four EA variants can evolve controllers that outperform the heuristic controller. We also let the heuristic controller play 100 random games. It still grew the snake to the maximum length. The results show that our EA can generate good controllers for a given game but they are not general enough to deal with unknown games. This is an important topic in our next study.
In this paper we proposed to generate AI controllers for the snake game through four rating functions and EA-based optimization of their weights. The rating functions consider smoothness, space, and food. We also proposed the idea about dynamically adjusting the weights according to the length of the snake. In the experiments, we first examined the need of combining multiple rating functions. Then, we compared four EA variants and identified good crossover operator and environmental method. The benefit of length-related weights in the controller was also confirmed by results of EA optimization and playing random games. Finally, we compared our evolved controller and a heuristic controller. Although our controllers can play better in a given game, they cannot play well in unknown games. In our future work, we will address this weakness by using multiple games in the evaluation of individuals in EA. This could be time consuming, and thus we will rely on parallel EAs. The idea in the heuristic controller such as the connectedness will be included in our future design. We will also investigate better reproduction operators such as those in differential evolution (DE) and particle swarm optimization (PSO).
[1] M. Gallager and A. Ryan, “Learning to play Pac-Man: an evolutionary, rule-based approach,” Proceedings of IEEE Congress on Evolutionary Computation , pp. 24622469, 2003. [2] N. Cole, S. J. Louis, and C. Miles, “Using a genetic algorithm to tune first-person shooter bots,” Proceedings of IEEE Congress on Evolutionary Computation , pp. 139145, 2004. [3] N. Böhm, G. Kókai, and S. Mandl, “An evolution approach to Tetris,” Proceedings of The Sixth Metaheuristics International Conference , 2005. [4] Y. Shichel, E. Ziserman, and M. Sipper, “GP-Robocode: using genetic programming to evolve Robocode players,” Lecture Notes in Computer Science , vol. 3447, pp. 143154, 2005. [5] J. Muñoz, G. Gutierrez, and A. Sanchis, “Multi-objective evolution for car setup optimization,” UK Workshop on Computational Intelligence (UKCI) , 2010. [6] C.-Y. Cheng, Y.-H. Chen, and T.-C. Chiang, “Intelligent level generation for Super Mario using interactive evolutionnary computation,” The 27th Annual Conference of the Japanese Society for Artificial Intelligence , 2013. [7] T. Ehlis, “Application of genetic programming to the snake game,” Gamedev.Net , 2000. [8] A heuristic snake AI controller (written in Chinese), http://blog.csdn.net/fox64194167/article/details/
View publication statsView publication stats