Tower defense method combining Hungary algorithm and deep neural network
By combining the Hungarian algorithm and deep neural networks to extract tower defense system status information and train strategies, it solves the problem of low accuracy of existing tower defense methods in complex environments and achieves efficient decision-making and strategy optimization in a variety of scenarios.
Patent Information
- Application Number
- CN202311859377.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-30
- Publication Date
- 2024-09-06
Smart Images

Figure CN118607337A
Abstract
Description
Technical Field
[0001] The present invention relates to a tower defense method, in particular to a tower defense method combining a Hungarian algorithm and a deep neural network. Background Art
[0002] The classic tower defense method is a strategic planning method in which the user needs to block the enemy's attack by placing tower defense devices. These tower defense devices are usually set on a specific map, and the user needs to face increasingly powerful enemies in different levels. The core of the classic tower defense method lies in the user's strategic ability. The user needs to accurately select and place different types of tower defense devices and prepare countermeasures based on the type and behavior of the enemy. In addition, the player needs to consider factors such as resource management, the ability to upgrade tower defense devices, and adjusting tactics. As the environment of the tower defense system becomes more complex, the user will face more and more challenges and need to continuously optimize his strategy to win.
[0003] The user's strategy plays a key role in the classic tower defense method. The user needs to develop a comprehensive strategic plan that takes into account various factors. First, the user needs to study the characteristics and attack methods of the enemy to choose a suitable defense strategy. Secondly, the user needs to make decisions under limited resources, such as where to build tower defense devices and which tower defense devices should be upgraded. In addition, the user also needs to consider the coordination and collaboration between tower defense devices to maximize their effectiveness. In the process of method planning, players also need to respond flexibly to emergencies and make timely adjustments and adjust strategies. Through continuous learning and practice, users can improve their strategy level and achieve better results.
[0004] Maria Manolaki proposed in her master's thesis "Development of a Tower Defense Game with Reinforcement Learning Agents" to comprehensively utilize convolutional networks, LSTM neural networks, etc. for deep reinforcement learning training to achieve a better tower defense strategy. Regarding the adaptive training of intelligent agents, PA Rummel proposed in 2011 that the use of adaptive algorithms can enable AI to not need any information about the type and path layout of tower defense devices, and discussed a simplified algorithm to solve this problem (using only one type of tower defense device) in the paper, as well as possible expansion algorithms. In addition, Per-Arne Andersen proposed an intelligent algorithm for tower defense methods in 2017.
[0005] However, existing tower defense methods have low accuracy and are difficult to apply to tower defense systems in complex environments. In addition, existing tower defense methods are designed for specific scenarios and are difficult to directly apply to other different scenarios. Summary of the invention
[0006] The purpose of the present invention is to solve the technical problem that the existing tower defense method has low accuracy and is difficult to apply to tower defense systems in complex environments, and to provide a tower defense method that combines the Hungarian algorithm and a deep neural network.
[0007] To achieve the above purpose, the technical solution adopted by the present invention is:
[0008] A tower defense method combining the Hungarian algorithm and the deep neural network, which is special in that it includes the following steps:
[0009] Step 1, presetting a tower defense system; the tower defense system is provided with a plurality of tower defense devices, a plurality of enemy aircraft and a target tower body for tower defense confrontation; the tower defense devices and the enemy aircraft are respectively of one or more types; taking the target tower body as the center, the periphery thereof is divided into a plurality of space areas along the circumferential direction; each space area includes a defense area and an attack and defense area, a plurality of tower defense devices are located in the defense area and are arranged along the periphery of the target tower body, the defense area is inside the attack and defense area, and a plurality of enemy aircraft are preset in the attack and defense area;
[0010] The state space of the tower defense system includes the current position of the tower defense device, the aiming direction and attack capability, and the position and speed of the enemy aircraft;
[0011] Step 2: Based on the state space of the tower defense device, the action space of the tower defense device is obtained using the Hungarian algorithm;
[0012] Step 3: Based on the action space of the tower defense device, the tower defense device is trained through the PPO algorithm to obtain the trained tower defense strategy so that each tower defense device can make the best decision according to the actual situation.
[0013] Furthermore, step 2 is specifically as follows:
[0014] Step 2.1, based on the position of the current tower defense device in the state space of the tower defense system, the aiming direction and attack capability, and the position and speed of the enemy aircraft, a multi-layer perceptron is used to obtain an enemy aircraft vector, whose shape is [n, 32], and a tower defense device vector, whose shape is [m, 32]; wherein n represents the number of enemy aircraft, and m represents the number of tower defense devices;
[0015] Step 2.2: Based on the two-dimensional density distribution of tower defense devices and enemy aircraft in the state space of the tower defense system, a convolutional neural network is used to obtain two density vectors, both of which have the shape
[16] ;
[0016] Step 2.3, concatenate the two density vectors on the first dimension of the enemy aircraft vector and the tower defense device vector, respectively, to obtain the enemy aircraft density vector U1 with a shape of [n, 64] and the tower defense device density vector U2 with a shape of [m, 64];
[0017] Step 2.4: Construct the distance matrix M based on the enemy aircraft density vector U1 and the tower defense device density vector U2 dis :
[0018] Step 2.5: Distance matrix M dis Based on this, the Hungarian algorithm is used to obtain the best matching result between tower defense devices, that is, to obtain the action space of the tower defense devices.
[0019] Furthermore, in step 2.3, the enemy aircraft density vector U1=[U1 0 ,U1 1 ,...U1 n ], the tower defense device density vector U2 = [U2 0 ,U2 1 ,...U2 m ];
[0020] In step 2.4, the distance matrix M dis for:
[0021]
[0022] Further, define: enemy aircraft as a vertex set X, tower defense devices as another vertex set Y, distance matrix M dis Each element in represents the distance between a vertex in the vertex set X and a vertex in the vertex set Y;
[0023] Step 2.5 is as follows:
[0024] Step 2.5.1, initialize a matching set M to be empty;
[0025] Step 2.5.2: Distance matrix M dis Based on this, for each vertex in X, find the reachable vertices in Y associated with it and add them to the starting node set of the augmenting path;
[0026] Step 2.5.3: If there is an augmenting path, select an edge from the augmenting path and add it to the matching set M, and return to step 2.5.1 until no augmenting path can be found; if there is no augmenting path, the current matching set M is the best matching result, that is, the action space of the tower defense device is obtained.
[0027] Furthermore, step 3 is specifically as follows:
[0028] Step 3.1, based on the action space of the tower defense device, use the initial tower defense strategy to select and execute the actions of each tower defense device; and after each tower defense device acts, save the corresponding reward signal and output probability ratio after each tower defense device acts;
[0029] Step 3.2, calculate the advantage function after each tower defense device moves, and use the PPO algorithm to update and optimize the tower defense strategy;
[0030] Step 3.3, in the process of updating and optimizing the tower defense strategy, the loss function of the PPO algorithm is calculated according to the corresponding reward signal, output probability ratio and calculated advantage function after each tower defense device action is saved, and the update and optimization range of the tower defense strategy is limited by the loss function;
[0031] Step 3.4, determine whether the tower defense strategy converges or reaches the preset number of training rounds. If so, obtain the trained tower defense strategy so that each tower defense device can make the best decision based on the actual situation; if not, return to step 3.1 until the tower defense strategy converges or reaches the preset number of training rounds.
[0032] The beneficial effects of the present invention are:
[0033] Advantages of multi-layer perceptron: It can extract rich information and features of aircraft point cloud data and tower defense device status. It can learn to represent features at different levels through different hidden layers and activation functions, and realize hierarchical abstraction and representation of input data. It has good generalization ability and adaptability, and can adjust network parameters through training to adapt to the statistical characteristics and distribution of input data. It can flexibly parse and process data according to actual conditions, and improve the performance and effect of the system.
[0034] Advantages of combining distance matrix with Hungarian algorithm: The distance matrix provides distance information between aircraft and tower defense devices, providing an important basis for Hungarian algorithm matching. The Hungarian algorithm is a classic algorithm for solving the maximum weight matching problem of bipartite graphs. The best matching result can be found by finding augmenting paths. The introduction of the Hungarian algorithm provides interpretable support for understanding the results of decision-making. The distance matrix obtained through training combined with the Hungarian algorithm can analyze the decision-making mechanism of the neural network, which is conducive to further optimization and upgrading of the algorithm. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] Figure 1 This is a schematic diagram of the existing tower defense confrontation;
[0036] Figure 2 is a network structure diagram of a multi-layer perceptron in an embodiment of the present invention;
[0037] Figure 3 It is a convolutional network structure diagram in an embodiment of the present invention. DETAILED DESCRIPTION
[0038] In order to make the purpose, advantages and features of the present invention clearer, the tower defense method combining the Hungarian algorithm and the deep neural network proposed by the present invention is further described in detail below in conjunction with the accompanying drawings and specific embodiments. According to the following specific implementation methods, the advantages and features of the present invention will be clearer. It should be noted that: the drawings are all in a very simplified form and use non-precise proportions, which are only used to conveniently and clearly assist in explaining the purpose of the embodiments of the present invention; secondly, the structure shown in the drawings is often part of the actual structure.
[0039] like Figure 1 As shown, in the tower defense system of this embodiment, the enemy aircraft approaches the tower along a certain trajectory (from the periphery to the center in the figure), and the tower defense device rotates at a certain speed to aim at and attack the aircraft. The tower defense device and the aircraft can have various types and performances.
[0040] In a tower defense system, deciding which aircraft should be tracked and destroyed is a very important task. This study obtains a tower defense strategy by training tower defense devices, so that each tower defense device can make the best decision based on the surrounding environment.
[0041] Deep reinforcement learning combines the ideas of deep neural networks and reinforcement learning, and can directly obtain the best strategy from the raw data in the tower defense system. PPO is a common, efficient and easy-to-use algorithm in deep reinforcement learning. By using the PPO algorithm, the strategy in the tower defense method can be optimized.
[0042] In step 1 of the present embodiment, a tower defense system is preset; a plurality of tower defense devices, a plurality of enemy aircraft and a target tower body are arranged in the tower defense system; the tower defense devices and the enemy aircraft are of one or more types respectively; the target tower body is taken as the center, and its periphery is divided into a plurality of space areas along the circumferential direction; each space area includes a defense area and an attack and defense area, a plurality of tower defense devices are located in the defense area and arranged along the periphery of the target tower body, the defense area is inside the attack and defense area, and a plurality of enemy aircraft are preset in the attack and defense area;
[0043] The state space of the tower defense system includes the current position of the tower defense device, the direction and attack capability of the aiming device, and the position and speed of the enemy aircraft; the action space is determined by the target number that the tower defense device needs to lock. For example, when there are four tower defense devices, action = [23, 24, 12, 6] means that device No. 1 aims at aircraft 23, device No. 2 aims at aircraft 24, and so on.
[0044] Neural Network Architecture
[0045] The data of the enemy aircraft is the sensor information collected by the aircraft in the air, including position coordinates and velocity vector. The data of the enemy aircraft can be abbreviated as the following list:
[0046] Enemy aircraft data (Info_UAV) = [[Uid1 position x, Uid1 position y, Uid1 position z, Uid1 speed x, Uid1 speed y, Uid1 speed z], [Uid2 position x, Uid2 position y, Uid2 position z, Uid2 speed x, Uid2 speed y, Uid2 speed z], ...];
[0047] The data of the tower defense device includes the location coordinates, the aiming angle of the aiming device and the attack capability. The data of the tower defense device can be abbreviated as the following list:
[0048] Tower defense device data (Info_defender) = [[Uid1 position x, Uid1 position y, Uid1 position z, Uid1 aiming at phi, Uid1 aiming at theta, Uid1 attack capability], [Uid2 position x, Uid2 position y, Uid2 position z, Uid2 aiming at phi, Uid2 aiming at theta, Uid2 attack capability omega], ...];
[0049] In this patent, a multilayer perceptron (i.e., a feedforward neural network) is used to parse the two tensors Info_UAV and Info_defender respectively. Since Info_UAV and Info_defender contain a large amount of information and features, this information can be extracted and effectively parsed by using a multilayer perceptron. The multilayer perceptron can learn to represent features at different levels through different hidden layers and activation functions, thereby achieving hierarchical abstraction and representation of input data. In addition, the multilayer perceptron has good generalization ability and adaptability, and the network parameters can be adjusted through training to adapt to the statistical characteristics and distribution of the input data. This enables the multilayer perceptron to flexibly parse and process Info_UAV and Info_defender according to actual conditions, thereby improving the performance and effectiveness of the system. The specific network structure is as follows: Figure 2 .
[0050] Step 2.1. After the multi-layer perceptron, we can get the enemy aircraft vector Vector_UAV and the tower defense device vector Vector_defender, whose shapes are [n,32] and [m,32] respectively, where n represents the number of aircraft and m represents the number of tower defense devices.
[0051] Step 2.2: Convolutional Neural Network for Parsing Density Information
[0052] The environment also provides the density distribution of aircraft and tower defense devices, so it is necessary to parse these density maps. This patent uses a 5-layer 2D convolutional neural network CNN to parse the two density maps respectively. After this part is completed, a vector of shape
[16] will be output. The structure diagram of the convolutional network is as follows Figure 3 shown.
[0053] Combination of distance matrix and Hungarian algorithm
[0054] Implementation of distance matrix
[0055] The above steps give the vector of the aircraft Vector_UAV, the vector of the tower defense device Vector_defender, and two vectors Vector_map1 and Vector_map2 after parsing the density map.
[0056] Step 2.3, concatenate the two density vectors on the first dimension of Vector_UAV and Vector_defender, and you will get:
[0057] Enemy aircraft density vector U1 = [[Vector_UAV[0], Vector_map1, Vector_map2], [Vector_UAV[1], Vector_map1, Vector_map2], ...] = [U1 0 ,U1 1 ,...], with a shape of [n,64].
[0058] Tower defense device density vector U2 = [[Vector_defender[0], Vector_map1, Vector_map2], [Vector_defender[1], Vector_map1, Vector_map2], ...] = [U2 0 ,U2 1 ,...], with a shape of [m,64].
[0059] Step 2.4: Based on U1 and U2, further construct the distance matrix M dis :
[0060]
[0061] U1 i U2 j =[Vector_UAV[i],Vector_map1,Vector_map2]
[0062] ·[Vector_defender[j],Vector_map1,Vector_map2] T
[0063] Distance matrix M dis It is an important basis for the Hungarian algorithm to perform matching, that is, each tower defense device will look for a matching aircraft that is closest to it.
[0064] Step 2.5: Combination of Hungarian algorithm and distance matrix; using distance matrix M dis Based on this, the Hungarian algorithm is used to obtain the best matching result between tower defense devices, that is, to obtain the action space of the tower defense devices;
[0065] The Hungarian Algorithm, also known as the Kuhn-Munkres algorithm or the bipartite graph optimal matching algorithm, is used to solve the maximum weight matching problem of bipartite graphs. The time complexity of the Hungarian Algorithm is O(n^3).
[0066] Consider the enemy aircraft as a vertex set X, the tower defense device as another vertex set Y, and the distance matrix M dis Each element in represents the distance between a vertex in X and a vertex in Y.
[0067] Here are the brief steps for matching using the Hungarian algorithm:
[0068] Step 2.5.1. Initialize a match M to be empty, indicating that no edge is selected.
[0069] Step 2.5.2: Distance matrix M dis Based on this, for each vertex in X, find the reachable vertices in Y associated with it and add them to the starting node set of the augmenting path.
[0070] Step 2.5.3: If there is an augmenting path, select an edge from the augmenting path and add it to the matching set M, and return to step 2.5.1 until no augmenting path can be found; if there is no augmenting path, the current matching set M is the best matching result, that is, the action space of the tower defense device is obtained.
[0071] Here, augmenting path is a concept in the Hungarian algorithm, which is used to find a method to increase the matching. In the second step of the Hungarian algorithm, a set of reachable vertices Y associated with the vertices in X is found by searching, and they are added to the starting node set of the augmenting path.
[0072] In the process of searching for augmented paths, we will try to start from the unmatched X vertex, continuously select the Y vertices adjacent to it, and consider the X vertices associated with these Y vertices to form an alternating path, that is, alternately select the matched edges and the unmatched edges. The ultimate goal is to find a path that starts from the unmatched X vertex and ends at the unmatched Y vertex. The edges on this path can increase the current matching by exchanging matching states.
[0073] When an augmenting path is successfully found, the matching status is exchanged on this path, that is, the unmatched edges become matched edges, and the matched edges become unmatched edges. In this way, the original matching can be expanded.
[0074] Repeat steps 2 and 3 until no augmenting path can be found, that is, there is no path that can further expand the match. At this time, the current match M is the best match result.
[0075] Next is the training process of the PPO algorithm.
[0076] Step 3 is as follows:
[0077] Step 3.1, based on the action space of the tower defense device, use the initial tower defense strategy to select and execute the actions of each tower defense device; and after each tower defense device acts, save the corresponding reward signal and output probability ratio after each tower defense device acts;
[0078] Step 3.2, calculate the advantage function after each tower defense device moves, and use the PPO algorithm to update and optimize the tower defense strategy;
[0079] Step 3.3, in the process of updating and optimizing the tower defense strategy, the loss function of the PPO algorithm is calculated according to the corresponding reward signal, output probability ratio and calculated advantage function after each tower defense device action is saved, and the update and optimization range of the tower defense strategy is limited by the loss function;
[0080] Step 3.4, determine whether the tower defense strategy converges or reaches the preset number of training rounds. If so, obtain the trained tower defense strategy so that each tower defense device can make the best decision based on the actual situation; if not, return to step 3.1 until the tower defense strategy converges or reaches the preset number of training rounds.
[0081] In other embodiments of the present invention, the present invention can also be applied to other fields that require real-time strategy planning and decision-making. For example, in multi-target aircraft recognition and tracking, the algorithm can analyze and interpret the characteristics of flying objects; in intelligent transportation, the algorithm can be used to analyze and interpret the characteristics and routes of vehicle travel and provide intelligent traffic control strategies.
Claims
1. A tower defense method combining Hungarian algorithm and deep neural network, characterized in that: The following steps are involved: Step 1, presetting a tower defense system; the tower defense system is provided with a plurality of tower defense devices, a plurality of enemy aircraft and a target tower body for tower defense confrontation; the tower defense devices and the enemy aircraft are respectively of one or more types; taking the target tower body as the center, the periphery thereof is divided into a plurality of space areas along the circumferential direction; each of the space areas includes a defense area and an attack and defense area, a plurality of the tower defense devices are located in the defense area and are arranged along the periphery of the target tower body, the defense area is inside the attack and defense area, and a plurality of the enemy aircraft are preset in the attack and defense area; The state space of the tower defense system includes the current position of the tower defense device, the aiming direction and attack capability, and the position and speed of the enemy aircraft; Step 2: Based on the state space of the tower defense system, the action space of the tower defense device is obtained using the Hungarian algorithm; Step 3: Based on the action space of the tower defense device, the tower defense device is trained through the PPO algorithm to obtain the trained tower defense strategy so that each tower defense device can make the best decision according to the actual situation.
2. A tower defense method combining Hungarian algorithm and deep neural network according to claim 1, characterized in that: Step 2 is as follows: Step 2.1, based on the position of the current tower defense device in the state space of the tower defense system, the aiming direction and attack capability, and the position and speed of the enemy aircraft, a multi-layer perceptron is used to obtain an enemy aircraft vector, whose shape is [n, 32], and a tower defense device vector, whose shape is [m, 32]; wherein n represents the number of enemy aircraft, and m represents the number of tower defense devices; Step 2.2: Based on the two-dimensional density distribution of tower defense devices and enemy aircraft in the state space of the tower defense system, a convolutional neural network is used to obtain two density vectors, both of which have the shape [16]; Step 2.3, concatenate the two density vectors on the first dimension of the enemy aircraft vector and the tower defense device vector, respectively, to obtain the enemy aircraft density vector U1 with a shape of [n, 64] and the tower defense device density vector U2 with a shape of [m, 64]; Step 2.4: Construct the distance matrix M based on the enemy aircraft density vector U1 and the tower defense device density vector U2 dis : Step 2.5: Distance matrix M dis Based on this, the Hungarian algorithm is used to obtain the best matching result between tower defense devices, that is, to obtain the action space of the tower defense devices.
3. A tower defense method combining Hungarian algorithm and deep neural network according to claim 2, characterized in that: In step 2.3, the enemy aircraft density vector U1 = [U10, U11, ... U1 n ], the tower defense device density vector U2 = [U20, U21, ... U2 m ]; In step 2.4, the distance matrix M dis for:
4. A tower defense method combining the Hungarian algorithm and a deep neural network according to claim 2 or 3, characterized in that: Definition: Enemy aircraft is a vertex set X, tower defense device is another vertex set Y, distance matrix M dis Each element in represents the distance between a vertex in the vertex set X and a vertex in the vertex set Y; Step 2.5 is as follows: Step 2.5.1, initialize a matching set M to be empty; Step 2.5.2: Distance matrix M dis Based on this, for each vertex in X, find the reachable vertices in Y associated with it and add them to the starting node set of the augmenting path; Step 2.5.3: If there is an augmenting path, select an edge from the augmenting path and add it to the matching set M, and return to step 2.5.1 until no augmenting path can be found; if no augmenting path can be found, the current matching set M is the best matching result, that is, the action space of the tower defense device is obtained.
5. A tower defense method based on point cloud neural network and statistical difference according to claim 1 or 2, characterized in that: Step 3 is as follows: Step 3.1, based on the action space of the tower defense device, use the initial tower defense strategy to select and execute the actions of each tower defense device; and after each tower defense device acts, save the corresponding reward signal and output probability ratio after each tower defense device acts; Step 3.2, calculate the advantage function after each tower defense device moves, and use the PPO algorithm to update and optimize the tower defense strategy; Step 3.3, in the process of updating and optimizing the tower defense strategy, the loss function of the PPO algorithm is calculated according to the corresponding reward signal, output probability ratio and calculated advantage function after each tower defense device action is saved, and the update and optimization range of the tower defense strategy is limited by the loss function; Step 3.4, determine whether the tower defense strategy converges or reaches the preset number of training rounds. If so, obtain the trained tower defense strategy so that each tower defense device can make the best decision based on the actual situation; if not, return to step 3.1 until the tower defense strategy converges or reaches the preset number of training rounds.
