Mahjong AI playing method based on multi-dimensional rule feature and dynamic graph local interaction fusion
Patent Information
- Application Number
- CN202610835673.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-06-10
- Publication Date
- 2026-10-30
AI Technical Summary
[0004]本发明的目的在于:提出一种基于多维规则特征与动态图局部交互融合的麻将AI对弈方法,解决现有麻将对弈复杂规则下难以同时具有较好训练稳定性、决策准确性的问题
[0008]本发明提供的有益效果是:本发明通过引入多维规则特征编码、基于全局卷积主干、动态图注意力局部牌张交互分支与动态门控融合模块的联合决策模型、基于打后向听和多因素排序的教师目标构建机制、两阶段训练策略以及合法动作掩码推理机制,使模型不仅能够学习历史对局中的状态—动作映射关系,还能够在复杂规则、强约束、多路线并存的真实麻将博弈场景下输出更稳定、更精细的动作决策结果。与常规单标签监督训练相比,本发明在训练目标构建方式和决策结构建模方式上均具有明显差异。
Smart Images

Figure CN122998715A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of mahjong artificial intelligence and intelligent decision-making technology, and in particular to a mahjong AI game method based on the fusion of multi-dimensional rule features and dynamic graph local interaction. Background Technology
[0002] With the continuous development of artificial intelligence technology, game decision-making methods, and intelligent game interaction systems, research on machine learning-based intelligent agents for card and board games has gradually become one of the important directions in the field of intelligent decision-making. Mahjong, as a typical game task characterized by incomplete information, strong rule constraints, multi-stage decision-making, and high strategy complexity, places high demands on the agent's ability to understand states, select actions, and weigh long-term gains. Especially in regional Mahjong scenarios, the rules vary significantly across different regions, often including multiple special hand types and constraints such as Thirteen Orphans and sequences of honor tiles, further increasing the difficulty of modeling Mahjong AI.
[0003] Most existing intelligent decision-making solutions for Mahjong are based on traditional rule search, heuristic evaluation, or single-supervised learning methods. While traditional rule search methods offer some interpretability, they typically rely on manually designed rules, making them ill-suited for the complex and ever-changing real-world game environment. Furthermore, they suffer from high computational costs and insufficient policy generalization when the action space is large and rule combinations are numerous. Single-supervised learning methods, although capable of learning the mapping between states and actions from historical game data, often suffer from problems due to the prevalence of multiple reasonable actions corresponding to the same state in Mahjong games. Simply relying on single-label classification for training often leads to an overemphasis on label consistency, neglecting the relative merits of different candidate actions. This results in models that, while achieving some accuracy on offline metrics, perform inconsistently in real-world games, failing to balance basic card principles with decision-making capabilities for special strategies. Summary of the Invention
[0004] The purpose of this invention is to propose a Mahjong AI game method based on the fusion of multi-dimensional rule features and dynamic graph local interaction, which solves the problem that existing Mahjong game methods with complex rules are difficult to achieve both good training stability and decision accuracy at the same time.
[0005] Specifically, this invention provides a Mahjong AI game-playing method based on the fusion of multi-dimensional rule features and dynamic graph local interaction, the method comprising the following steps: S1. Obtain historical mahjong game data, and perform cleaning and preprocessing on the historical mahjong game data to obtain training samples; S2. Based on the target local Mahjong rules, multi-dimensional rule feature encoding is performed on the current game state to construct a multi-dimensional state feature tensor that includes hand distribution features, ready-to-wait features, discard consequences features, local retention strength features, and one-suit tendency features. S3. Construct a mahjong decision model, which includes a global convolutional backbone for extracting global situation features, a local card interaction branch based on dynamic graph attention for dynamically modeling the structured relationship between candidate cards, and a dynamic gating fusion module for adaptively fusing global and local scores. S4. Based on the preprocessed training samples, traverse the legal candidate actions and simulate the post-play state to construct a teacher objective that includes teacher scores and teacher confidence, and perform the first stage of joint training on the mahjong decision model. S5. Based on the high-confidence samples selected by the teacher confidence level, the mahjong decision-making model that has completed the first stage of training is fine-tuned in the second stage. S6. Obtain the game state of the mahjong match to be decided, input the game state of the mahjong match to be decided into the trained mahjong decision model, and after combining the legal action mask to block illegal actions, output the final legal action decision result.
[0006] A storage medium storing instructions and data for implementing a Mahjong AI game method based on the fusion of multi-dimensional rule features and dynamic graph local interaction.
[0007] A mahjong AI playing device based on the fusion of multidimensional rule features and dynamic graph local interaction includes: a processor and the storage medium; the processor loads and executes instructions and data in the storage medium to implement a mahjong AI playing method based on the fusion of multidimensional rule features and dynamic graph local interaction.
[0008] The beneficial effects provided by this invention are as follows: By introducing multi-dimensional rule feature encoding, a joint decision-making model based on a global convolutional backbone, dynamic graph attention local card interaction branches, and a dynamic gating fusion module, a teacher objective construction mechanism based on backward play and multi-factor ranking, a two-stage training strategy, and a legal action mask inference mechanism, this invention enables the model to not only learn the state-action mapping relationship in historical games, but also output more stable and refined action decision results in real mahjong game scenarios with complex rules, strong constraints, and multiple routes. Compared with conventional single-label supervised training, this invention has significant differences in both the training objective construction method and the decision structure modeling method. Attached Figure Description
[0009] Figure 1 This is a schematic diagram of the method flow of the present invention; Figure 2 This is a diagram of the overall structure of the Mahjong decision-making model; Figure 3 This is a schematic diagram of multidimensional state feature construction; Figure 4 This is a flowchart of teacher goal setting and two-stage training; Figure 5 This is a schematic diagram of the hardware device operation according to an embodiment of the present invention. Detailed Implementation
[0010] To make the objectives, technical solutions, and advantages of the present invention clearer, the embodiments of the present invention will be further described below with reference to the accompanying drawings.
[0011] Before formally describing the present invention, a general description of the solution of the present invention will be given first to facilitate understanding.
[0012] Example 1 Please refer to Figure 1 The present invention provides a Mahjong AI playing method based on the fusion of multi-dimensional rule features and dynamic graph local interaction, comprising the following steps: S1. Obtain historical mahjong game data, and perform cleaning and preprocessing on the historical mahjong game data to obtain training samples; It should be noted that, in step S1, the cleaning pretreatment further includes: S11. Analyze player hand information, revealed side information, discard sequences, action tags, and legal action masks from historical game logs; S12. Abnormal samples with abnormal card quantity or action labels inconsistent with the set of legal actions are removed. Conflicting state samples with multiple different reasonable actions corresponding to the same state are screened. Duplicate state samples and near-duplicate state samples are downsampled to build a high-quality training sample library.
[0013] As one embodiment, in this embodiment, historical mahjong game data is first acquired. This historical game data can originate from real platform game logs, offline sampling records, or data generated in an internal gaming environment. After structured parsing of the acquired raw logs, state information related to the current decision-making moment is extracted, including the player's current hand, each player's revealed concealed tiles, each player's discard sequence, current rule parameters, real action tags, and legal action masks.
[0014] To improve the effectiveness of subsequent model training, this embodiment performs preprocessing cleaning on the training samples. The cleaning process includes, but is not limited to: repairing or removing samples with abnormal card counts, missing fields, or action labels inconsistent with the legal action set; filtering conflict state samples; identifying and downsampling repeated and near-repeated states; and, when necessary, performing sample equalization on similar situations to reduce the bias caused by high-frequency situations during model training. Through these steps, a training sample set can be obtained for subsequent multi-dimensional feature construction and teacher objective construction.
[0015] S2. Based on the target local Mahjong rules, multi-dimensional rule feature encoding is performed on the current game state to construct a multi-dimensional state feature tensor that includes hand distribution features, ready-to-wait features, discard consequences features, local retention strength features, and one-suit tendency features. It should be noted that in step S2, constructing the multidimensional state feature tensor further includes: S21. Calculate the potential winning hand number under the following seven hand types: Plain Hand, All Pungs, Pure Hand, All Honors, Seven Pairs, Thirteen Orphans, and Seven Stars Thirteen Orphans. Normalize the result using the following formula to obtain the 7-dimensional potential winning hand number characteristics: in, For the first The number of hands that can be played to complete a hand; S22. Calculate the local retention strength feature of each candidate card in the current hand. The local retention strength feature is used to characterize the value of keeping the card in the current game state. The larger the value, the less likely it is to be played. Specifically, it is calculated using the following multi-source maximum formula: in, For pair indicators, For adjacent stack indicators, Retain the indicator for scattered cards under the Thirteen Orphans or Seven Stars Thirteen Orphans route. Points are reserved for tiles based on their potential to form sequences or pairs; S23. The 7-dimensional listening number feature, the local retention strength feature, the hand distribution feature, the discard consequence feature, the visible card count feature, the security feature, and the one-suit tendency feature are concatenated to form the 186-dimensional multidimensional state feature tensor.
[0016] Please refer to Table 1, which defines the dimensions.
[0017] Table 1 Dimension Definitions
[0018] Among them, dimensions 9 to 15 are 7 types of normalized features for the listening numbers (including the listening numbers for Pinghu, Pengpenghu, Qingyise, Ziyise, Qidui, Shisanlan, and Qixing Shisanlan). Each listening channel takes the same constant value on the entire 4×9 grid and uses linear normalization from 0 to 8:
[0019] in This refers to the number of hands that can be played to complete a winning hand.
[0020] The 17th dimension is the local retention strength feature, used to indicate which cards are more worth keeping at the moment. Pairs usually receive a high value; if a number card has adjacent sequences or belongs to the bad card retention set, it will also be assigned a higher retention score. For honor cards, the potential to form a straight / pair is also considered when assigning a value. For each type of card currently in hand, a "retention strength score" of 0 to 1 is calculated, with a higher value indicating that it should not be played easily. It first calculates pair retention, then processes "honor cards" and "number cards" separately, and finally takes the maximum value among these candidate scores as the final retention strength of the card. It can be summarized as follows:
[0021] in Indicates a card in Position in the grid, This only applies to honor tiles; for number tiles, this value can be considered 0. This "max" structure corresponds to the continuously updated score = max(score, ...) in the code.
[0022] It indicates whether there are at least two copies of the card in the current hand; 1 if there are two copies, 0 otherwise. If the target card forms a pair or multiple duplicates in the current hand, it is considered to have high combination potential, and the corresponding retention strength is set to 1.0.
[0023] (Adjacent Pair Indicator) This applies to tiles other than honor tiles. It checks if there are any tiles of the same suit that differ from the current tile by 1 to 2. If there are adjacent tiles, it takes 1; otherwise, it takes 0. A weight of 0.8 means that although the tile is not a pair, it has the potential to form a local pair with nearby point tiles, such as a two-sided pair, a middle pair, or a edge pair, and is therefore worth keeping.
[0024] It indicates whether the card belongs to the set of sparse, loose cards that should be kept under the Thirteen Orphans / Seven Star Orphans route. Under the current rules, loose cards that are kept simultaneously within the same suit must satisfy the following condition: the difference in points between any two cards must be greater than or equal to 3. This factor has a weight of 0.7. A card that does not have a pair or necessarily a regular sequence, but has retention value under the Thirteen Orphans / Seven Star Orphans route, is given a medium to high retention score.
[0025] This is not a simple 0 / 1 indicator, but a tiered value retrieval function. It only applies to tiles; the code logic is: if the number of tiles of a certain tile... If the tile is a single tile but has the potential to form a sequence, take 1.0; otherwise, if it is just a regular lone tile, take 0.2.
[0026] Here, the reasons for retaining a card are understood as several independent strong reasons for retaining it: pairs (strong retention), having a sequence (worth retaining), thirteen orphans (worth retaining), and honor tiles that can form a straight (worth retaining). Therefore, the final retention strength is:
[0027] As one example, please refer to Figure 3 , Figure 3 This is a schematic diagram illustrating the construction of multidimensional state features in an embodiment of the present invention. In this embodiment, the original situation information is not directly input into the decision model, but is first processed through rule-enhanced feature encoding to form a multidimensional state feature tensor, which is then used by the subsequent model.
[0028] The original situation information includes at least: player hand information, publicly revealed cards information, discard sequence, rule parameters, and legal action mask. After processing by the feature encoding module, a multi-dimensional state feature tensor is obtained, which includes hand distribution features, ready hand count features, discard consequence features, local retention strength features, visible card count features, security features, and one-suit tendency features.
[0029] In one implementation, the feature space can be constructed as 186 dimensions, which includes at least the following types of information: (1) Basic layered features of the hand, used to depict the distribution of cards in the current hand; (2) Multi-type normalization features are used to simultaneously represent the degree of similarity in the formation of hands for different routes such as Ping Hu, Peng Peng Hu, Qing Yi Se, Zi Yi Se, Qi Dui, Shisan Lan and Qi Xing Shisan Lan; (3) The characteristics of the consequences of discarding cards one by one are used to describe the impact of the current card on the progress of the game; (4) Local retention strength characteristics are used to characterize whether the current card is more worth keeping or more suitable to play; (5) Visible number of cards and security features are used to describe the remaining card distribution and the risk from the opponent's perspective; (6) The single-color tendency related features are used to indicate whether the current state is suitable for advancing towards a single color, single color, or other routes.
[0030] In this embodiment, the audio data features are written into the corresponding feature channels using a normalized method. In one embodiment, the first... The characteristics of the hand type corresponding to the number of hands that can be waited for can be represented as follows:
[0031] in, This indicates the number of hands that are ready to win in the current hand type. This represents the position index in the feature plane. Using this representation, the states of different winning strategies can be input into the model on a unified numerical scale, enabling the model to simultaneously perceive the progress of multiple winning strategies when making decisions.
[0032] Furthermore, to highlight explicit hand information such as pairs, sequences, Thirteen Orphans, and honor tiles in local Mahjong, this embodiment constructs a local retention strength feature. For each type of hand currently in hand, considering the retention value of pairs, adjacent sequences, sparse scattered tiles under the Thirteen Orphans / Seven Stars Thirteen Orphans route, and honor tile combination potential, the corresponding retention strength score is calculated, which can be expressed as:
[0033] in, Indicates a pair of indicators. Indicates adjacent stack indicator, This indicates that the scattered cards are retained under the Thirteen Orphans or Seven Stars Thirteen Orphans route. This indicates the score retained for each tile. By introducing this explicit feature, the model can not only learn state-action mappings using statistical data, but also explicitly perceive the local tile structure, thereby improving the stability and interpretability of decisions.
[0034] In one specific embodiment, the present invention also supports modeling "bad cards" and "seven-star bad cards" according to the constraint that the card interval is greater than or equal to 3, and supports constraining the feature construction process by the rule parameters of the character card sequence. Therefore, the feature encoding module can adapt to the rule differences of different regional Mahjong gameplay, enabling subsequent models to handle multiple regional rule scenarios within a unified framework.
[0035] S3. Construct a mahjong decision model, which includes a global convolutional backbone for extracting global situation features, a local card interaction branch based on dynamic graph attention for dynamically modeling the structured relationship between candidate cards, and a dynamic gating fusion module for adaptively fusing global and local scores. It should be noted that in step S3, the dynamic modeling of the structured relationship between candidate cards based on the local card interaction branch of dynamic graph attention further includes: S31. Define 34 types of mahjong tiles as graph nodes, and construct edges between the nodes based on a preset set of mahjong tile rules to form a tile relationship graph; the set of mahjong tile rules includes at least: [the set of rules for connecting nodes with a specified number of nodes]. and The adjacent edges of the same suit card nodes, used to connect the nodes. and Same-suit interval relationship edges for same-suit card nodes, and cross-suit same-rank relationship edges used to connect different suits of cards with the same rank; S32. After concatenating the local feature maps output by the global convolutional backbone with the explicit rule features for each card, project them onto the unified latent space to obtain the initial feature representations of 34 card nodes. ; S33. In at least one graph attention interaction layer, for the first... The layer updates the card node representation through the following steps: S331, Input representation After layer normalization, the linear mapping yields the query. ,key Sum ; S332. Based on the card relationship graph, only for node pairs with existing edges... Calculate attention scores and, based on the original dot product attention, assign them to different relation types. Add learnable relational biases respectively And the relationship gating coefficient generated in combination with the current situation state. Make corrections to obtain the corrected attention score. ; S333. Normalize the corrected attention score to obtain the attention weight. and use it to value vectors Perform weighted aggregation to obtain nodes The update indicates ; S34. Map the 34 node representations output by the final interaction layer through a multilayer perceptron to obtain a 34-dimensional local interaction correction score vector. .
[0036] Furthermore, in step S3, the adaptive fusion of global and local scores by the dynamic gating fusion module further includes: S35. Perform global pooling on the final representations of all card nodes in the local card interaction branch to obtain the sample-level card context. ; S36, Configure the sample-level card context The route probability vector output by the Mahjong decision model and value prediction The vectors are concatenated to obtain the gating context vector. ; S37. Based on the gated context vector Each generates a sample-level gating coefficient to characterize the overall correction strength of the current situation. A card-level gating vector used to represent the corrected weights of each of the 34 card positions. And a route-level gating coefficient vector for adjusting the contributions of multiple route experts. ; S38. Utilizing the aforementioned route-level gating coefficient The corrected scores output by multiple route experts are weighted and fused, and then the sample-level gating coefficients are used. Card-level gating vector The fusion result is modulated element-wise to generate the final local correction term. ; S39. The final local correction item The base card score output by the global convolution backbone Add them together to get the final score for each card played. .
[0037] It should be noted that, in this embodiment, the local card type interaction head is used to model the local structural relationships between the 34 candidate cards to correct the initial score of the macro action branch for the specific card play. Specifically, the local feature map output by the global convolution backbone is first mapped to 34 candidate card tokens through card unrolling operations, and then concatenated with hand card existence features, structural retention strength features, discard priority features, visible card number features, security features, and single-suit correlation features to form the initial feature vector of each card; subsequently, the initial feature vector is projected onto the unified latent space to obtain a 34×128 card token representation matrix.
[0038] Furthermore, at least one card interaction module is used to perform local structural relationship modeling on the 34 card tokens. Unlike the original one-dimensional local message passing based on a fixed convolution kernel, this implementation abstracts the 34 candidate card types into 34 nodes in a graph and constructs a card relationship graph based on Mahjong card priors. The edge relationships in the card relationship graph include at least: node self-connection, adjacent relationships of the same suit, relationships of the same suit with one card skipped, relationships of the same rank across suits, relationships of honor tile sets, relationships of wind tile groups, and relationships of arrow tile groups. The local interaction branches are still composed of two parts of features: The first part is the local convolutional representation extracted from the global backbone feature map. The backbone output features are processed through two 1×3 convolutions to obtain a 64×4×9 local card representation, which is then remapped into convolutional tokens of 34 candidate cards through a card unrolling operation, denoted as:
[0039] The second part consists of explicit rule features directly extracted from the state feature tensor. These card-by-card explicit features include at least: hand presence features, structure retention strength features, discard priority features, visible card count features, global security features, directional security features, single-suit target retention features, single-suit / mixed-suit discard tendency features, pure-suit discard consequence features, and single-suit honor discard consequence features. The convolutional token is concatenated with the above explicit rule features to obtain the initial representation of each candidate card:
[0040] Then, through linear projection, the card representation in the unified latent space is obtained:
[0041] Furthermore, the card interaction module includes a layer normalization layer, a query-key-value mapping layer, a multi-head graph attention layer, a relation bias layer, a relation gating layer, residual connections, and a feedforward network. For the first... The layer interaction module first normalizes the representation of the input cards:
[0042] Then, the query, key, and value vectors are obtained through linear mapping:
[0043] For the Attention head, node With nodes The basic correlation between them is:
[0044] in, This represents the dimension of a single-head feature. Let... Represents a node With nodes Relationship types Indicates the correspondence type in the first position. Learnable relation biases on each attention head; simultaneously, relation gating coefficients are generated based on the current state of the situation. This is used to adaptively adjust the importance of different relationship types. The corrected attention score is then:
[0045] Normalized attention weights are computed only at locations where edges are allowed in the graph:
[0046] Therefore, the first Attention head to node The aggregation result is:
[0047] After concatenating and linearly mapping the outputs of all attention heads, and combining this with residual connections, an intermediate representation is obtained:
[0048] After passing through the feedforward network, the next layer of card representations is obtained:
[0049] Therefore, for hands like 10,000, 20,000, and 30,000 that can form straights, or hands like 3, 4, 5, and 6 of bamboo that have the potential for continuous expansion, the interaction strength is no longer determined by the receptive field of the fixed convolutional kernel, but by the relationship type between candidate hands, the current situation context, and the multi-head attention parameters. For non-straight relationships such as honor tiles, pairs, safe tiles, and tiles of the same rank across suits, their corresponding interaction importance can also be obtained through joint learning of relationship bias, relationship gating, and explicit rule features.
[0050] Following the interaction layer, the code further concatenates three ResidualMLPBlocks for card-by-card refinement, outputting a scalar score for each card, ultimately forming a 34-dimensional local adjustment vector. Let the... The local correction score for each signpost output by the route expert is:
[0051] Furthermore, to avoid local branches consistently applying a fixed intensity to the basic card-playing score, this implementation introduces a dynamic gating fusion mechanism. Specifically, all card representations are first pooled to obtain sample-level card contexts:
[0052] The sample-level card context is concatenated with the route probability vector and value prediction results to obtain the gated context vector:
[0053] Based on the gating context vector, sample-level gating coefficients, card-level gating vectors, and route-level gating coefficients are generated respectively:
[0054]
[0055]
[0056] in, Used to characterize whether the current situation as a whole requires strong local adjustments. This is used to characterize which card positions among the 34 candidate card categories require more focused correction. This is used to adjust the level of involvement of experts from different routes in the local correction results.
[0057] Let the basic route probability output by the route header be... Then, the route fusion weight after introducing route-level gating is:
[0058] Based on this, the local correction score after route fusion is obtained:
[0059] The local correction results are then adaptively adjusted using the sample-level gating coefficients and card-level gating vectors to obtain the final local correction term:
[0060] Let the baseline scores given by the macro branch and the total action branch for the Play interval be... The final score for playing a card is:
[0061] In other words, this local card type interaction head is not a separate card player. Instead, based on the initial Play score given by the macro branch, it performs card-by-card adaptive correction of the score of specific candidate cards according to the dynamic graph relationship between cards and the dynamic gating result generated by the current situation, thereby taking into account the understanding of the overall situation, the local card relationship, and the decision differences under different routes.
[0062] As one embodiment, in this implementation, the local card type interaction head is used to model the local structural relationships between 34 candidate cards to correct the initial score of the macro action branch for specific card plays. Specifically, the local feature map output by the global convolution backbone is first mapped to 34 candidate card tokens through card unrolling operations, and then concatenated with hand card existence features, structural retention strength features, discard priority features, visible card count features, security features, and single-suit correlation features to form an initial feature vector for each card; subsequently, the initial feature vector is projected onto a unified latent space to obtain a 34×128 card token representation matrix.
[0063] Furthermore, at least one card interaction module is used to perform local structural relationship modeling on the 34 card tokens. Unlike the original one-dimensional local message passing based on a fixed convolution kernel, this implementation abstracts the 34 candidate card types into 34 nodes in a graph and constructs a card relationship graph based on Mahjong card priors. The edge relationships in the card relationship graph include at least: node self-connection, adjacent relationships of the same suit, relationships of the same suit with one card skipped, relationships of the same rank across suits, relationships of honor tile sets, relationships of wind tile groups, and relationships of arrow tile groups. The local interaction branches are still composed of two parts of features: The first part is the local convolutional representation extracted from the global backbone feature map. The backbone output features are processed through two 1×3 convolutions to obtain a 64×4×9 local card representation, which is then remapped into convolutional tokens of 34 candidate cards through a card unrolling operation, denoted as:
[0064] The second part consists of explicit rule features directly extracted from the state feature tensor. These card-by-card explicit features include at least: hand presence features, structure retention strength features, discard priority features, visible card count features, global security features, directional security features, single-suit target retention features, single-suit / mixed-suit discard tendency features, pure-suit discard consequence features, and single-suit honor discard consequence features. The convolutional token is concatenated with the above explicit rule features to obtain the initial representation of each candidate card:
[0065] Then, through linear projection, the card representation in the unified latent space is obtained:
[0066] Furthermore, the card interaction module includes a layer normalization layer, a query-key-value mapping layer, a multi-head graph attention layer, a relation bias layer, a relation gating layer, residual connections, and a feedforward network. For the first... The layer interaction module first normalizes the representation of the input cards:
[0067] Then, the query, key, and value vectors are obtained through linear mapping:
[0068] For the Attention head, node With nodes The basic correlation between them is:
[0069] in, This represents the dimension of a single-head feature. Let... Represents a node With nodes Relationship types Indicates the correspondence type in the first position. Learnable relation biases on each attention head; simultaneously, relation gating coefficients are generated based on the current state of the situation. This is used to adaptively adjust the importance of different relationship types. The corrected attention score is then:
[0070] Normalized attention weights are computed only at locations where edges are allowed in the graph:
[0071] Therefore, the first Attention head to node The aggregation result is:
[0072] After concatenating and linearly mapping the outputs of all attention heads, and combining this with residual connections, an intermediate representation is obtained:
[0073] After passing through the feedforward network, the next layer of card representations is obtained:
[0074] Therefore, for hands like 10,000, 20,000, and 30,000 that can form straights, or hands like 3, 4, 5, and 6 of bamboo that have the potential for continuous expansion, the interaction strength is no longer determined by the receptive field of the fixed convolutional kernel, but by the relationship type between candidate hands, the current situation context, and the multi-head attention parameters. For non-straight relationships such as honor tiles, pairs, safe tiles, and tiles of the same rank across suits, their corresponding interaction importance can also be obtained through joint learning of relationship bias, relationship gating, and explicit rule features.
[0075] Following the interaction layer, the code further concatenates three ResidualMLPBlocks for card-by-card refinement, outputting a scalar score for each card, ultimately forming a 34-dimensional local adjustment vector. Let the... The local correction score for each signpost output by the route expert is:
[0076] Furthermore, to avoid local branches consistently applying a fixed intensity to the basic card-playing score, this implementation introduces a dynamic gating fusion mechanism. Specifically, all card representations are first pooled to obtain sample-level card contexts:
[0077] The sample-level card context is concatenated with the route probability vector and value prediction results to obtain the gated context vector:
[0078] Based on the gating context vector, sample-level gating coefficients, card-level gating vectors, and route-level gating coefficients are generated respectively:
[0079]
[0080]
[0081] in, Used to characterize whether the current situation as a whole requires strong local adjustments. This is used to characterize which card positions among the 34 candidate card categories require more focused correction. This is used to adjust the level of involvement of experts from different routes in the local correction results.
[0082] Let the basic route probability output by the route header be... Then, the route fusion weight after introducing route-level gating is:
[0083] Based on this, the local correction score after route fusion is obtained:
[0084] The local correction results are then adaptively adjusted using the sample-level gating coefficients and card-level gating vectors to obtain the final local correction term:
[0085] Let the baseline scores given by the macro branch and the total action branch for the Play interval be... The final score for playing a card is:
[0086] In other words, this local card type interaction head is not a separate card player. Instead, based on the initial Play score given by the macro branch, it performs card-by-card adaptive correction of the score of specific candidate cards according to the dynamic graph relationship between cards and the dynamic gating result generated by the current situation, thereby taking into account the understanding of the overall situation, the local card relationship, and the decision differences under different routes.
[0087] S4. Based on the preprocessed training samples, traverse the legal candidate actions and simulate the post-play state to construct a teacher objective that includes teacher scores and teacher confidence, and perform the first stage of joint training on the mahjong decision model. It should be noted that step S4, in which teacher objectives are constructed, further includes: S41. For training samples involving card play, iterate through all legal candidate card play actions in the current state. ; S42. For each candidate action The simulation removes it from the current hand. Remove from the middle and get a temporary hand card. And based on the publicly disclosed number of subgroups and current rule constraints Calculate the optimal number of sounds after the attack:
[0088] S43. Based on the above, the optimal direction of hearing is as follows The teacher score is generated for each legal candidate action based on factors such as whether to split pairs, card security, and additional benefits from a single-suit route. The score is then used to determine the difference between the top two teacher scores. Construct teacher confidence scores; S44. After temperature normalization of the teacher scores, they are mixed with the real action labels according to a preset ratio to generate a soft label distribution, which is used for soft label loss calculation in the first stage of training.
[0089] Specifically, in step S4, a teacher objective is first constructed for the preprocessed training samples. For card-playing samples, all legally playable candidate cards are iterated through, and each card is simulated for play to obtain a temporary hand. For each training sample, its state is set as follows: The set of legal actions is The real label action is .when When within the range of possible playable cards, iterate through all legal playable cards. Remove the card from your current hand to construct a temporary hand. And calculate the optimal number of sounds after hitting.
[0090]
[0091] in, This indicates the number of currently disclosed subgroups. This indicates the currently open suit constraint or the secondary exposed suit constraint. That is, corresponding to the candidate to play a card. The optimal number of listeners after hitting.
[0092] Further, by combining factors such as whether to split pairs, whether to split triplets, the public visibility of the hand, security, and additional benefits of a single-suit route, a 34-dimensional teacher score vector, teacher_score_34, is generated. For candidate actions that result in the best possible hand after discarding, the number of effective draws is calculated and added to the teacher score as additional ranking information. Subsequently, the teacher score is normalized by temperature and mixed with the real label at a preset ratio to obtain a 34-dimensional soft label. Finally, the teacher confidence score is constructed from the difference between the top two teacher scores.
[0093]
[0094] in, and These are the first and second largest scores among the current sample of valid candidate teacher scores. If there is only one valid candidate action, the confidence level can be set to 1.0.
[0095] In the first stage of offline training, the state feature tensor, the legal action mask, and the teacher's objective are jointly input into the Mahjong decision model. Joint optimization is performed using action classification loss, action type loss, play cross-entropy loss, play ranking loss, soft label loss, teacher preference loss, hand-to-hand preference loss, and effective tile-drawing preference loss. Specifically, the action classification loss constrains the 235-dimensional full action output; the action type loss constrains the 8-dimensional action category output; the play cross-entropy loss and ranking loss constrain the 34-dimensional play scoring; the soft label loss learns the multiple solution distribution given by the teacher; the teacher preference loss learns the priority relationship between different candidate tiles; the hand-to-hand preference loss reinforces the decision-making tendency of "prioritizing reducing or not increasing hand-to-hand"; and the effective tile-drawing preference loss reinforces the selection tendency of "prioritizing actions with richer hand-to-hand" when hand-to-hand is tied after a play, thus obtaining the basic strategy model.
[0096] As one example, please refer to Figure 4 , Figure 4 This is a flowchart illustrating the teacher objective construction and two-stage training process in this embodiment of the invention. A significant difference between this embodiment and conventional single-label supervised learning schemes is that it does not directly use real action labels for hard training of the model. Instead, it first iterates through legal candidate actions for each card-playing training sample, constructs teacher objectives based on post-play state simulation, and then uses the teacher objectives and real labels together to guide model training.
[0097] Specifically, for card-playing samples, all legally playable candidate card actions are iterated through. For any candidate action... First, remove the card from your current hand and construct the corresponding temporary hand state. Then, calculate the optimal post-hit count corresponding to the candidate action. Its expression can be represented as:
[0098] in, Indicates the current hand cards. This indicates the card corresponding to the candidate's play action. This indicates that the number of subgroups has been disclosed. This indicates the current rule constraint or open suit constraint. Through this process, we can obtain the impact of different legal candidate actions on the progression of the hand-making route "after being played", thus avoiding the model merely fitting the original label and ignoring the relative advantages and disadvantages between multiple reasonable actions.
[0099] After obtaining the backward listening value corresponding to each candidate action, the teacher score can be generated by further combining the following information: (1) Whether to split the pairs; (2) Whether the engraving was removed; (3) Public visibility of the cards; (4) Card security; (5) Additional revenue from routes such as "pure suit" and "uniform suit"; (6) The number of effective passes when the optimal direction is parallel after the hit.
[0100] By taking into account the above factors, a teacher score vector can be generated for each legitimate candidate action, which is used to represent the relative priority between different candidate actions.
[0101] Furthermore, the teacher scores are temperature-normalized and mixed with the true labels at a preset ratio to obtain a soft-label distribution; simultaneously, the teacher confidence score is constructed based on the difference between the top two teacher scores, which can be expressed as:
[0102] in, and These represent the first and second largest scores among the teacher scores for the current sample's legal candidate actions. When there is only one legal candidate action, the teacher confidence score can be directly set to 1.0. Through this design, the model can not only learn "what is the optimal action," but also "how big is the gap between other candidate actions and the optimal action," thus better adapting to scenarios in mahjong games where there are multiple reasonable actions for the same state.
[0103] S5. Based on the high-confidence samples selected by the teacher confidence level, the mahjong decision-making model that has completed the first stage of training is fine-tuned in the second stage. It should be noted that in step S5, the second stage of fine-tuning training further includes: S51. Based on the mahjong decision model that has completed the first stage of training, the training samples are filtered in multiple ways to select high-confidence samples. The multiple filtering includes: removing conflict state samples that correspond to multiple different action labels in the same state, removing samples whose teacher confidence is lower than a preset threshold, and performing multi-solution filtering for the direction of play, that is, retaining the candidate action sample with the largest number of effective draws when the optimal direction of play is tied after the play. S52. Freeze some of the underlying network parameters of the Mahjong decision model, and fine-tune the high-level parameters of the local card interaction branch and the dynamic gating fusion module using only the high-confidence samples to reduce label noise interference and improve the stability of the model in multi-route decision scenarios.
[0104] Specifically, the first-stage model is fine-tuned based on high-confidence cleaned samples. Based on the parameters of the first-stage model, the second-stage training is further conducted using high-quality samples that have undergone conflict state filtering, teacher confidence filtering, soft label sharpness filtering, and multi-solution filtering to reduce the interference of label noise on the model and improve the stability of the model's decision boundary, thus obtaining the final deployment model.
[0105] As one example, please refer to Figure 4 After constructing the teacher objectives, this embodiment employs a two-stage training strategy to optimize the mahjong decision-making model. The first stage is the offline joint training stage, and the second stage is the high-confidence sample fine-tuning stage.
[0106] In the first stage of offline training, the state feature tensor, the legal action mask, and the teacher's objective are jointly input into the Mahjong decision model, and optimized using joint losses. The joint losses include at least: action classification loss, action type loss, play cross-entropy loss, play ranking loss, soft label loss, teacher preference loss, hand-to-hand preference loss, and effective draw preference loss. Specifically, the action classification loss constrains the output of all actions, the action type loss constrains the output of major action categories, the play cross-entropy loss and ranking loss constrain the specific play score, the soft label loss learns the multiple solution distribution given by the teacher's objective, the hand-to-hand preference loss reinforces the decision-making tendency of "prioritizing reducing or not increasing hand-to-hand," and the effective draw preference loss prioritizes actions with richer draws when hand-to-hand is the same after a play. Through this joint training, a first-stage model with basic decision-making capabilities can be obtained.
[0107] In the second stage of fine-tuning training, based on the model parameters from the first stage, further fine-tuning is performed using cleaned and screened high-quality samples. The screening of high-quality samples may include: conflict state filtering, teacher confidence filtering, soft label sharpness filtering, and multi-solution filtering. By retaining only samples with clearer teacher preferences and lower label noise for the second stage of training, the interference of ambiguous samples and noisy labels on the model's decision boundary can be reduced, thereby improving the model's stability and generalization ability under complex rule states. Finally, a deployable mahjong decision-making model is obtained through two stages of training.
[0108] S6. Obtain the game state of the mahjong match to be decided, input the game state of the mahjong match to be decided into the trained mahjong decision model, and after combining the legal action mask to block illegal actions, output the final legal action decision result.
[0109] It should be noted that in step S6, the output of the final legal action decision result further includes: S61. Input the real-time hand information, revealed information, discard sequence and rule parameters into the trained mahjong decision model; S62. The Mahjong decision model calculates and outputs the full action space score, action type score, and 34-dimensional play score after local interaction and dynamic gating correction in parallel. S63. Obtain the legal action mask in the current game state, and mask the score of the corresponding illegal action position in the full action space score, action type score and card play score to negative infinity; S64. From the remaining set of legal actions, select the action with the highest score as the final decision result. The final decision result includes eating a card, ponging a card, konging a card, winning a hand, passing a card, or a specific card-playing action.
[0110] Specifically, the system acquires the state of the mahjong game to be decided and outputs the legal action decision result; it inputs the hand information, public information, rule parameters and legal action mask in the real-time game into the trained model, and the model outputs the full action score, macro action bias and card-by-card score result, and then combines the legal action mask to complete the illegal action shielding, and obtains the final decision of eating, ponging, konging, winning or specific card play.
[0111] As one embodiment, during the online game phase, the hand information, public information, rule parameters, and legal action mask from the real-time mahjong game are input into the same feature encoding module as in the training phase to generate a corresponding multi-dimensional state feature tensor. This feature tensor is then fed into the trained mahjong decision model. The model outputs a full action score, an action type score, and a locally corrected play score. Combined with the legal action mask, illegal actions are masked, and only the action with the highest score within the set of legal actions is selected as the final decision result for eating, ponging, konging, winning, passing, or playing.
[0112] Because the online inference phase explicitly incorporates a mask of legal actions, it ensures that the model always meets the constraints of the current rules and the server's candidate action set, avoiding the output of illegal actions. Furthermore, since the model internally utilizes global situational understanding, explicit rule features, and local card interaction information, it can balance basic card logic, route planning, and fine-grained card-playing structure judgment in complex local Mahjong games, achieving more stable performance in real-world scenarios.
[0113] Finally, in an optional implementation, the trained model can also be connected to the internal playing environment and the online platform deployment environment. The model version can be filtered by win rate, winning points, key action performance and real battle feedback, and the feedback data can be fed back to the training sample library to form a closed-loop optimization process of "offline training - internal evaluation - platform deployment - feedback iteration".
[0114] Example 2: Please see Figure 5 , Figure 5This is a schematic diagram of the hardware device in operation according to an embodiment of the present invention. The hardware device specifically includes: a mahjong AI game device 401 based on the fusion of multi-dimensional rule features and dynamic graph local interaction, a processor 402, and a storage medium 403.
[0115] A Mahjong AI Playing Device 401 Based on the Fusion of Multidimensional Rule Features and Dynamic Graph Local Interaction: The Mahjong AI Playing Device 401 implements the Mahjong AI Playing Method Based on the Fusion of Multidimensional Rule Features and Dynamic Graph Local Interaction.
[0116] Processor 402: The processor 402 loads and executes the instructions and data in the storage medium 403 to implement the Mahjong AI game method based on the fusion of multi-dimensional rule features and dynamic graph local interaction.
[0117] Storage medium 403: The storage medium 403 stores instructions and data; the storage medium 403 is used to implement the Mahjong AI game method based on the fusion of multi-dimensional rule features and dynamic graph local interaction.
[0118] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A Mahjong AI-based game-playing method based on the fusion of multi-dimensional rule features and dynamic graph local interaction, characterized in that: Includes the following steps: S1. Obtain historical mahjong game data, and perform cleaning and preprocessing on the historical mahjong game data to obtain training samples; S2. Based on the target local Mahjong rules, multi-dimensional rule feature encoding is performed on the current game state to construct a multi-dimensional state feature tensor that includes hand distribution features, ready-to-wait features, discard consequences features, local retention strength features, and one-suit tendency features. S3. Construct a mahjong decision model, which includes a global convolutional backbone for extracting global situation features, a local card interaction branch based on dynamic graph attention for dynamically modeling the structured relationship between candidate cards, and a dynamic gating fusion module for adaptively fusing global and local scores. S4. Based on the preprocessed training samples, traverse the legal candidate actions and simulate the post-play state to construct a teacher objective that includes teacher scores and teacher confidence, and perform the first stage of joint training on the mahjong decision model. S5. Based on the high-confidence samples selected by the teacher confidence level, the mahjong decision-making model that has completed the first stage of training is fine-tuned in the second stage. S6. Obtain the game state of the mahjong match to be decided, input the game state of the mahjong match to be decided into the trained mahjong decision model, and after combining the legal action mask to block illegal actions, output the final legal action decision result.
2. The Mahjong AI game-playing method based on the fusion of multi-dimensional rule features and dynamic graph local interaction as described in claim 1, characterized in that: In step S1, the cleaning pretreatment further includes: S11. Analyze player hand information, revealed side information, discard sequences, action tags, and legal action masks from historical game logs; S12. Abnormal samples with abnormal card quantity or action labels inconsistent with the set of legal actions are removed. Conflicting state samples with multiple different reasonable actions corresponding to the same state are screened. Duplicate state samples and near-duplicate state samples are downsampled to build a high-quality training sample library.
3. The Mahjong AI game-playing method based on the fusion of multi-dimensional rule features and dynamic graph local interaction as described in claim 1, characterized in that, In step S2, constructing the multidimensional state feature tensor further includes: S21. Calculate the potential winning hand number under the following seven hand types: Plain Hand, All Pungs, Pure Hand, All Honors, Seven Pairs, Thirteen Orphans, and Seven Stars Thirteen Orphans. Normalize the result using the following formula to obtain the 7-dimensional potential winning hand number characteristics: in, For the first The number of hands that can be played to complete a hand; S22. Calculate the local retention strength feature of each candidate card in the current hand. The local retention strength feature is used to characterize the value of keeping the card in the current game state. The larger the value, the less likely it is to be played. Specifically, it is calculated using the following multi-source maximum formula: in, For pair indicators, For adjacent stack indicators, Retain the indicator for scattered cards under the Thirteen Orphans or Seven Stars Thirteen Orphans route. Points are reserved for tiles based on their potential to form sequences or pairs; S23. The 7-dimensional listening number feature, the local retention strength feature, the hand distribution feature, the discard consequence feature, the visible card count feature, the security feature, and the one-suit tendency feature are concatenated to form the 186-dimensional multidimensional state feature tensor.
4. The Mahjong AI game-playing method based on the fusion of multi-dimensional rule features and dynamic graph local interaction as described in claim 1, characterized in that, In step S3, the dynamic modeling of the structured relationships between candidate cards based on the local card interaction branch of dynamic graph attention further includes: S31. Define 34 types of mahjong tiles as graph nodes, and construct edges between the nodes based on a preset set of mahjong tile rules to form a tile relationship graph; the set of mahjong tile rules includes at least: [the set of rules for connecting nodes with a specified number of nodes]. and The adjacent edges of the same suit card nodes, used to connect the nodes. and Same-suit interval relationship edges for same-suit card nodes, and cross-suit same-rank relationship edges used to connect different suits of cards with the same rank; S32. After concatenating the local feature maps output by the global convolutional backbone with the explicit rule features for each card, project them onto the unified latent space to obtain the initial feature representations of 34 card nodes. ; S33. In at least one graph attention interaction layer, for the first... The layer updates the card node representation through the following steps: S331, Input representation After layer normalization, the linear mapping yields the query. ,key Sum ; S332. Based on the card relationship graph, only for node pairs with existing edges... Calculate attention scores and, based on the original dot product attention, assign them to different relation types. Add learnable relational biases respectively And the relationship gating coefficient generated in combination with the current situation state. Make corrections to obtain the corrected attention score. ; S333. Normalize the corrected attention score to obtain the attention weight. and use it to value vectors Perform weighted aggregation to obtain nodes The update indicates ; S34. Map the 34 node representations output by the final interaction layer through a multilayer perceptron to obtain a 34-dimensional local interaction correction score vector. .
5. The Mahjong AI game-playing method based on the fusion of multi-dimensional rule features and dynamic graph local interaction as described in claim 4, characterized in that: In step S3, the adaptive fusion of global and local scores by the dynamic gating fusion module further includes: S35. Perform global pooling on the final representations of all card nodes in the local card interaction branch to obtain the sample-level card context. ; S36, Configure the sample-level card context The route probability vector output by the Mahjong decision model and value prediction The vectors are concatenated to obtain the gating context vector. ; S37. Based on the gated context vector Each generates a sample-level gating coefficient to characterize the overall correction strength of the current situation. A card-level gating vector used to represent the corrected weights of each of the 34 card positions. And a route-level gating coefficient vector for adjusting the contributions of multiple route experts. ; S38. Utilizing the aforementioned route-level gating coefficient The corrected scores output by multiple route experts are weighted and fused, and then the sample-level gating coefficients are used. Card-level gating vector The fusion result is modulated element-wise to generate the final local correction term. ; S39. The final local correction item The base card score output by the global convolution backbone Add them together to get the final score for each card played. .
6. The Mahjong AI game-playing method based on the fusion of multi-dimensional rule features and dynamic graph local interaction as described in claim 1, characterized in that, In step S4, constructing teacher objectives further includes: S41. For training samples involving card play, iterate through all legal candidate card play actions in the current state. ; S42. For each candidate action The simulation removes it from the current hand. Remove from the middle and get a temporary hand card. And based on the publicly disclosed number of subgroups and current rule constraints Calculate the optimal number of sounds after the attack: S43. Based on the above, the optimal direction of hearing is as follows The teacher score is generated for each legal candidate action based on factors such as whether to split pairs, card security, and additional benefits from a single-suit route. The score is then used to determine the difference between the top two teacher scores. Construct teacher confidence scores; S44. After temperature normalization of the teacher scores, they are mixed with the real action labels according to a preset ratio to generate a soft label distribution, which is used for soft label loss calculation in the first stage of training.
7. The Mahjong AI playing method based on the fusion of multi-dimensional rule features and dynamic graph local interaction as described in claim 1, characterized in that, In step S5, the second stage of fine-tuning training further includes: S51. Based on the mahjong decision model that has completed the first stage of training, the training samples are filtered in multiple ways to select high-confidence samples. The multiple filtering includes: removing conflict state samples that correspond to multiple different action labels in the same state, removing samples whose teacher confidence is lower than a preset threshold, and performing multi-solution filtering for the direction of play, that is, retaining the candidate action sample with the largest number of effective draws when the optimal direction of play is tied after the play. S52. Freeze some of the underlying network parameters of the Mahjong decision model, and fine-tune the high-level parameters of the local card interaction branch and the dynamic gating fusion module using only the high-confidence samples to reduce label noise interference and improve the stability of the model in multi-route decision scenarios.
8. The Mahjong AI game-playing method based on the fusion of multi-dimensional rule features and dynamic graph local interaction as described in claim 1, characterized in that, In step S6, the final legal action decision result is further output including: S61. Input the real-time hand information, revealed information, discard sequence and rule parameters into the trained mahjong decision model; S62. The Mahjong decision model calculates and outputs the full action space score, action type score, and 34-dimensional play score after local interaction and dynamic gating correction in parallel. S63. Obtain the legal action mask in the current game state, and mask the score of the corresponding illegal action position in the full action space score, action type score and card play score to negative infinity; S64. From the remaining set of legal actions, select the action with the highest score as the final decision result. The final decision result includes eating a card, ponging a card, konging a card, winning a hand, passing a card, or a specific card-playing action.
9. A storage medium, characterized in that: The storage medium stores instructions and data to implement the Mahjong AI game method based on the fusion of multi-dimensional rule features and dynamic graph local interaction as described in any one of claims 1 to 8.
10. A mahjong AI-powered gaming device based on the fusion of multi-dimensional rule features and dynamic graph local interaction, characterized in that: include: A processor and a storage medium; the processor loads and executes instructions and data in the storage medium to implement the Mahjong AI game method based on the fusion of multi-dimensional rule features and dynamic graph local interaction as described in any one of claims 1 to 8.
