MATLAB code generation method, device, system and medium based on control flow graph matching
Patent Information
- Application Number
- CN202611091735.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-07-22
- Publication Date
- 2026-09-25
AI Technical Summary
[0003]大规模语言模型在Python、Java等主流编程语言的代码生成任务中取得了突破性进展,但针对MATLAB这一低资源编程语言的自动代码生成研究仍相对不足,现有模型在MATLAB代码生成场景中存在逻辑错误率高、结构一致性差的问题,主要原因包括:
[0032](1)本发明提出了基于 CFG-GMN 的结构化奖励机制,利用图匹配网络计算生成代码与专家代码控制流图的结构相似度,突破了传统文本相似度、单纯编译执行结果作为奖励信号的局限,从深层语义层面评估代码正确性,有效引导模型学习程序的逻辑骨架,显著降低代码生成的逻辑错误率。
Smart Images

Figure CN122816612A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and specifically to a MATLAB code generation method, apparatus, system, and medium based on control flow graph matching. Background Technology
[0002] MATLAB, a high-level programming language for matrix computation and engineering simulation, deeply integrates visualization capabilities and hundreds of specialized toolboxes. It is widely used in fields such as automation control, digital signal processing, radar signal processing, and modeling of complex physical systems, and is the standard paradigm for multi-domain system modeling and automatic code generation in industry. With the increasing complexity of engineering systems, manually writing and maintaining large-scale MATLAB code libraries faces efficiency bottlenecks, making the automatic generation of MATLAB code using artificial intelligence technology a research hotspot.
[0003] Large-scale language models have made breakthrough progress in code generation tasks for mainstream programming languages such as Python and Java. However, research on automatic code generation for MATLAB, a low-resource programming language, is still relatively insufficient. Existing models suffer from high logical error rates and poor structural consistency in MATLAB code generation scenarios. The main reasons include:
[0004] First, the MATLAB public code corpus is relatively small and mostly exists in private projects or specific scientific research scenarios, making it difficult for the model to fully model its grammatical distribution.
[0005] Second, MATLAB programs contain complex matrix-dimensional logic, a large number of nested function calls, and a unique partitioned script structure, which places extremely high demands on the logical reasoning ability of the model.
[0006] Third, MATLAB program behavior is deeply bound to specific professional toolboxes, requiring the generated model to not only master the syntax, but also understand the complex physical domain logic and specific API calling specifications.
[0007] Fourth, the common approach to improving code generation models is supervised fine-tuning. This method optimizes the probability distribution only at the lexical level, treating code as a linear text flow and ignoring the nature of programs as highly structured logical entities. Although some studies have introduced reinforcement learning to optimize code generation models, existing reinforcement learning schemes mostly use text similarity and compilation execution results as reward signals, lacking evaluation of the program's logical skeleton. Even if the generated code compiles successfully, its algorithmic logic may deviate significantly from expectations, failing to meet the requirements of consistency and reliability of MATLAB code logic in specialized fields such as radar signal processing.
[0008] Therefore, the existing technologies for automatic MATLAB code generation suffer from problems such as high logical error rate, poor structural consistency, and lack of structural awareness in reinforcement learning reward signals. These are all issues that need to be addressed. Summary of the Invention
[0009] To overcome the shortcomings of the prior art, the present invention provides a MATLAB code generation method, apparatus, system, and computer storage medium based on control flow graph matching.
[0010] In a first aspect, the present invention provides a MATLAB code generation method based on control flow graph matching, comprising the following steps:
[0011] Based on the input natural language instructions, candidate MATLAB code is generated through a pre-trained large language model;
[0012] The candidate MATLAB code and the expert reference MATLAB code are parsed separately to obtain the corresponding candidate control flow graphs. and expert reference control flow diagram ;
[0013] Calculate candidate control flow graphs using graph matching networks. With expert reference control flow graph Structural similarity score S_CFG;
[0014] A multidimensional reward function R is constructed, and the structural similarity score S_CFG is weighted and fused with the compilation reward score R_compile and the test reward score R_test to obtain the reward signal.
[0015] A group-relative strategy optimization algorithm is adopted, and the reward signal is used as feedback to strengthen and fine-tune the large language model to obtain an optimized MATLAB code generation model.
[0016] The natural language instructions to be generated into the MATLAB code are input into the optimized MATLAB code generation model, and the target MATLAB code is output.
[0017] Preferably, the graph matching network is a GMN, and the similarity score is calculated as follows:
[0018] ;
[0019] in, and These represent the number of nodes in the candidate control flow graph and the expert reference control flow graph, respectively. and is the fusion hidden layer embedding vector of the intra-node code extracted by the graph matching network GMN; sim is the similarity calculation function; max is the maximum value function.
[0020] Preferably, the multidimensional reward function R is expressed as: Where α, β, and γ are weight coefficients, and α+β+γ=1, α is the weight of the structural reward, β is the weight of the compilation reward score, and γ is the weight of the test reward score.
[0021] Preferably, the control flow graph is represented in the form G=(V,E); where V is a set of nodes, each node v∈V represents an atomic code block; E is a set of edges, each edge e=(u,v)∈E represents the possibility of program execution right being transferred from atomic code block u to atomic code block v.
[0022] Secondly, the present invention provides a MATLAB code generation device based on control flow graph matching, comprising the following modules:
[0023] The candidate code generation module is used to generate candidate MATLAB code based on the input natural language instructions using a pre-trained large language model.
[0024] The control flow graph extraction module is used to parse the candidate MATLAB code and the expert reference MATLAB code respectively to obtain the corresponding candidate control flow graphs. and expert reference control flow diagram ;
[0025] The graph matching computation module is used to compute candidate control flow graphs through a graph matching network. With expert reference control flow graph Structural similarity score S_CFG;
[0026] The reward signal generation module is used to perform weighted fusion of the structural similarity score S_CFG, compilation reward score R_compile, and test reward score R_test based on the multidimensional reward function R to generate a comprehensive reward signal.
[0027] The enhancement and fine-tuning module is used to enhance and fine-tune the large language model using a group relative strategy optimization algorithm and the reward signal as feedback, so as to obtain an optimized MATLAB code generation model.
[0028] The target code generation module is used to input the natural language instructions to be generated into the optimized MATLAB code generation model and output the target MATLAB code.
[0029] Thirdly, the present invention provides a MATLAB code generation system, including one or more processors; a storage device for storing one or more programs; and when the one or more programs are executed by the one or more processors, causing the one or more processors to implement any of the methods described in the first aspect above.
[0030] Fourthly, the present invention provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements any of the methods described in the first aspect above.
[0031] Compared with the prior art, the beneficial effects of the present invention are as follows:
[0032] (1) This invention proposes a structured reward mechanism based on CFG-GMN, which uses graph matching network to calculate the structural similarity between the generated code and the expert code control flow graph. This breaks through the limitations of traditional text similarity and simply using the compilation execution result as a reward signal. It evaluates the correctness of the code from a deep semantic level, effectively guides the model to learn the logical skeleton of the program, and significantly reduces the logical error rate of code generation.
[0033] (2) This invention constructs an automated static analysis pipeline for MATLAB, which realizes the automated extraction from the original script to the fine-grained control flow graph based on the MATLAB syntax features, solves the problem that the structural features of MATLAB code are difficult to represent effectively, and provides a reliable data foundation for reinforcement learning.
[0034] (3) The present invention uses the GRPO algorithm for model enhancement and fine-tuning. The advantage function is calculated by the relative score within the group. No additional Critic network is required. This effectively solves the problems of difficult training of Critic network and large memory occupation in code generation tasks of traditional PPO algorithm. At the same time, it overcomes the defects of sparse and unstable reward signal and realizes stable training of model.
[0035] (4) The method of this invention performs excellently in MATLAB code generation in professional fields such as radar signal processing. Experiments show that the method achieves a Pass@1 score of 51.2%, a Pass@5 score of 69.4%, and a structural similarity score of 0.89, which is significantly better than the traditional supervised fine-tuning and PPO reinforcement fine-tuning method that only rewards compilation. This greatly improves the accuracy, execution success rate, and structural consistency of MATLAB code generation. Pass@k means that for a programming problem, the model generates k different candidate solutions. As long as at least one of them passes all test cases, the problem is considered to be successfully solved.
[0036] (5) This invention provides a new research paradigm for the generation of large language model codes for low-resource programming languages and domain-specific languages (DSLs), and can be extended to other programming language code generation scenarios with domain characteristics and scarce corpora. Attached Figure Description
[0037] Figure 1 This is a flowchart of a MATLAB code generation method based on control flow graph matching, according to an embodiment of the present invention.
[0038] Figure 2 This is an overall framework diagram of the MATLAB code generation device based on control flow graph matching provided by the present invention. Detailed Implementation
[0039] In the following description, only certain exemplary embodiments are briefly described. As those skilled in the art will recognize, the described embodiments can be modified in various ways without departing from the spirit or scope of the invention. Therefore, the drawings and description are considered to be exemplary in nature and not restrictive.
[0040] This invention provides a MATLAB code generation method based on control flow graph matching. (Reference) Figure 1 The method includes:
[0041] Step S110, Candidate Code Generation: Based on the input natural language instructions, candidate MATLAB code is generated using a pre-trained large language model. The natural language instructions contain information such as the algorithm function to be implemented and parameter requirements, and are suitable for specialized fields such as radar signal processing.
[0042] Step S120, Control Flow Graph Extraction: Construct a MATLAB static analysis pipeline, parse the candidate MATLAB code and expert reference MATLAB code respectively, first convert the code into an abstract syntax tree (AST) using the Abstract Syntax Tree (AST) tool, and then extract the control flow graph (CFG) based on the AST to obtain the control flow graphs of the candidate code. Control flow graph with expert reference code .
[0043] The control flow graph, as understood by those skilled in the art, is a directed graph abstraction of the program execution flow, used to describe the topological structure of all possible execution paths during program runtime. Expert reference code refers to standard MATLAB code that is pre-written by domain experts, is logically correct, and conforms to specifications, for the same natural language programming requirement.
[0044] Specifically, the control flow graph is represented in the form G=(V,E), where V is a set of nodes, and each node v∈V represents an atomic code block, including assignment statements, function declarations, conditional headers, etc.; E is a set of edges, and each edge e=(u,v)∈E represents the possibility of the program execution right being transferred from atomic code block u to atomic code block v.
[0045] The rules for constructing the control flow graph include:
[0046] Sequential structure: Statements S1 to S2 are mapped to directed edges u→v;
[0047] Branching structure (if-else / switch): Multiple outgoing edges are generated from the decision node, each pointing to a node corresponding to a different logical branch;
[0048] Loop structures (for / while): generate a back edge pointing to the starting node of the loop, and generate an exit edge pointing to the node after the loop ends;
[0049] Function call structure: Extract the call dependencies between functions, form cross-function scope call graph features and map them to corresponding directed edges.
[0050] Step S130, Structural similarity calculation: Introduce a graph matching network to calculate candidate control flow graphs. With expert reference control flow graph The structural similarity score S_CFG.
[0051] The graph matching network can capture the topological differences between two graphs through cross-graph attention and interaction mechanisms. During computation, each node is first initialized with features. The initial state of a node is formed by concatenating the node type (e.g., if, loop, call) and the embedding vector of the code within the node. Then, the structural similarity score is obtained through multi-layer interaction computation of the graph matching network. Preferably, the graph matching network is a GMN (Graph Matching Network), and the similarity score is calculated as follows:
[0052] ;
[0053] here and These represent the number of nodes in the candidate control flow graph and the expert reference flow graph, respectively. and Embedding (embedding vector) is the fusion hidden layer for the in-node code extracted from the GMN model; sim is the similarity calculation function, here it is cosine similarity; max is the maximum value function.
[0054] Step S140, Reward Signal Generation: Construct a multidimensional reward function R, and weightedly fuse the structural similarity score S_CFG with the compilation reward score R_compile and the test reward score R_test to obtain the reward signal. The multidimensional reward function R can be expressed as:
[0055] ;
[0056] Wherein, α, β, and γ are weight coefficients and α+β+γ=1. α is the weight of the structural reward, which has the highest weight to ensure that the model prioritizes learning the program logic structure. It is specifically calculated by a graph matching network (e.g., GMN). β is the weight of the compilation reward score; γ is the weight of the test reward score; the compilation reward score R_compile is an indicator function, where R_compile=1 when the candidate MATLAB code passes MATLAB syntax parsing and -1 when it fails; the test reward score R_test is the score of the candidate MATLAB code after passing specific domain test cases in a simulation environment, such as radar waveform parameter verification and pulse compression algorithm verification in the field of radar signal processing.
[0057] Step S150, Enhancement and Fine-tuning: Using the group relative policy optimization (GRPO) algorithm, with the reward signal generated in step S140 as feedback, the large language model is enhanced and fine-tuned to obtain the optimized MATLAB code generation model.
[0058] Specifically, the GRPO algorithm does not rely on the critic network required by the traditional PPO (Proximal Policy Optimization) algorithm. Instead, it directly calculates the advantage function based on the relative performance of samples within the group: For each input natural language instruction, the model generates k candidate MATLAB codes, and calculates the reward signal value of each candidate code based on the multi-dimensional reward function described in step S140. By comparing the relative merits of each candidate code within the group, the advantage function value corresponding to each candidate code is obtained. Subsequently, based on the advantage function, a policy update objective function is constructed, and the parameters of the large language model are iteratively updated to obtain the optimized MATLAB code generation model. Here, the advantage function is the standardized score obtained by performing within-group standardization on the reward signal values of the k candidate codes within the same group, and its value directly reflects the merits of each candidate code relative to the average level of the group.
[0059] Step S160: Target code generation: Input the natural language instructions for generating MATLAB code into the optimized MATLAB code generation large language model, and output the target MATLAB code that meets logical consistency and domain requirements.
[0060] To more clearly illustrate the MATLAB code generation method provided in the embodiments of the present invention, the present invention further provides an example of generating MATLAB code for linear frequency modulation (LFM) signals in the field of radar signal processing, and provides a detailed explanation of each step of the above method.
[0061] Step S210, candidate code generation:
[0062] Input the following natural language command: "Implement a linear frequency modulation (LFM) signal generation algorithm using MATLAB, with a sampling rate fs=20000000.0Hz, bandwidth B=50000000.0Hz, and pulse width T=5e-05s". The pre-trained DeepSeek-Coder-7B large language model will generate candidate MATLAB code.
[0063] Step S220, Control Flow Graph Extraction:
[0064] Select expert reference MATLAB code, parse the candidate MATLAB code and expert reference code into abstract syntax trees (ASTs) respectively, and then extract control flow graphs based on the ASTs to obtain the control flow graphs of the candidate code. Control flow graph with expert reference code In this embodiment, the nodes of the control flow graph include function declaration nodes and assignment statement nodes (assignment of variables t, k, and s), and the edges are directed edges in a sequential structure, connecting the function declaration node, the t assignment node, the k assignment node, and the s assignment node in sequence.
[0065] Step S230, Structural similarity calculation:
[0066] Candidate control flow graphs are analyzed using a graph matching network (GMN). and expert reference control flow diagram Initial feature encoding is performed on nodes. The initial state of a node is formed by concatenating the node type (function declaration, assignment) and the embedding vector of the code within the node. Through cross-graph attention and interaction mechanisms, the structural similarity score is calculated as: S_CFG = 0.92.
[0067] Step S240, Reward signal generation:
[0068] With weighting coefficients set to α=0.7, β=0.1, and γ=0.2, the candidate code is parsed using MATLAB syntax, resulting in a compilation reward score R_compile=1. The candidate code is run in a radar signal simulation environment and passes the LFM signal parameter verification test cases, yielding a test score R_test=0.95. The reward signal is calculated based on the multidimensional reward function R:
[0069] R = 0.7×0.92 + 0.1×1 + 0.2×0.95 = 0.904.
[0070] Step S250, Enhanced Fine-tuning:
[0071] For the natural language instructions generated from the LFM signals, the model generates k=5 candidate MATLAB codes. The reward signal value for each candidate code is calculated, and the advantage function is calculated based on the relative performance of samples within the group. An objective function for policy update is then constructed, and the parameters of the DeepSeek-Coder-7B model are iteratively updated. This process is repeated, using the MATLAB-Radar-Bench dataset from the radar signal processing field for multiple rounds of training until the reward signal converges, completing the model's enhancement and fine-tuning.
[0072] Step S260, Target Code Generation: Input the radar signal processing domain natural language instructions to be generated into the optimized model, and output the target MATLAB code. Verification shows that this code meets the logical consistency requirements, can be directly compiled and executed in the MATLAB environment, and passes the domain test cases.
[0073] On the other hand, embodiments of the present invention provide a MATLAB code generation apparatus. (See reference...) Figure 2 The device includes:
[0074] Candidate code generation module 100: Used to generate candidate MATLAB code based on input natural language instructions using a pre-trained large language model.
[0075] Control flow graph extraction module 200: Used to construct a MATLAB static analysis pipeline, parse the candidate MATLAB code and expert reference MATLAB code respectively, first convert the code into an abstract syntax tree, and then extract the control flow graph based on the abstract syntax tree to obtain the candidate code control flow graph. Control flow graph with expert reference code .
[0076] Graph matching computation module 300: Used to compute candidate control flow graphs through a graph matching network. With expert reference control flow graph The structural similarity score S_CFG.
[0077] Reward signal generation module 400: used to perform weighted fusion of the structural similarity score S_CFG, compilation reward score R_compile and test reward score R_test based on the multidimensional reward function R to generate a comprehensive reward signal.
[0078] The multidimensional reward function R can be expressed as:
[0079] ;
[0080] Where α, β, and γ are weight coefficients, and α+β+γ=1, α is the weight of the structural reward, β is the weight of the compilation reward score, and γ is the weight of the test reward score.
[0081] The reinforcement fine-tuning module 500 is used to perform reinforcement fine-tuning on the pre-trained large language model using the reward signal as feedback. Preferably, the reinforcement fine-tuning employs the group-relative strategy optimization GRPO algorithm.
[0082] Specifically, the GRPO algorithm does not rely on the critic network required by the traditional PPO (Proximal Policy Optimization) algorithm. Instead, it directly calculates the advantage function based on the relative performance of samples within the group: For each input natural language instruction, the large language model generates k candidate MATLAB codes, and calculates the reward signal value of each candidate code based on the multi-dimensional reward function. By comparing the relative merits of each candidate code within the group, the advantage function value corresponding to each candidate code is obtained. Subsequently, an objective function for policy updating is constructed based on the advantage function, and the parameters of the large language model are iteratively updated to obtain the optimized MATLAB code generation model. Here, the advantage function is the standardized score obtained by performing within-group standardization on the reward signal values of the k candidate codes within the same group, and its value directly reflects the merits of each candidate code relative to the average level of the group.
[0083] Target code generation module 600: This module takes the natural language instructions to be generated into the optimized MATLAB code generation large language model and outputs the MATLAB code.
[0084] On the other hand, embodiments of the present invention also provide a MATLAB code generation system, including one or more processors; a storage device for storing one or more programs; and when the one or more programs are executed by the one or more processors, causing the one or more processors to implement any of the methods described above.
[0085] On the other hand, embodiments of the present invention also provide a computer-readable storage medium storing a computer program, characterized in that the program, when executed by a processor, implements any of the methods described above.
[0086] "Computer-readable medium" can be any means that can contain, store, communicate, propagate, or transmit a program for use in or in conjunction with an instruction execution system, apparatus, or device. More specific examples (a non-exhaustive list) of computer-readable media include: an electrical connection having one or more wires (electronic device), a portable computer disk drive (magnetic device), random access memory (RAM), read-only memory (ROM), erasable and editable read-only memory (EPROM or flash memory), fiber optic devices, and portable read-only memory (CDROM). Additionally, a computer-readable medium can even be paper or other suitable media on which the program can be printed, since the program can be obtained electronically, for example, by optical scanning of the paper or other medium, followed by editing, interpreting, or otherwise processing as necessary, and then stored in a computer memory.
[0087] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any person skilled in the art can easily conceive of various variations or substitutions within the technical scope disclosed in the present invention, and these should all be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A MATLAB code generation method based on control flow graph matching, characterized in that, Includes the following steps: Based on the input natural language instructions, candidate MATLAB code is generated through a pre-trained large language model; The candidate MATLAB code and the expert reference MATLAB code are parsed separately to obtain the corresponding candidate control flow graphs. and expert reference control flow diagram ; Calculate candidate control flow graphs using graph matching networks. With expert reference control flow graph Structural similarity score S_CFG; A multidimensional reward function R is constructed, and the structural similarity score S_CFG is weighted and fused with the compilation reward score R_compile and the test reward score R_test to obtain the reward signal. A group-relative strategy optimization algorithm is adopted, and the reward signal is used as feedback to strengthen and fine-tune the large language model to obtain an optimized MATLAB code generation model. The natural language instructions to be generated into the MATLAB code are input into the optimized MATLAB code generation model, and the target MATLAB code is output.
2. The method according to claim 1, characterized in that, The graph matching network is GMN, and the similarity score S_CFG is calculated as follows: , in, and These represent the number of nodes in the candidate control flow graph and the expert reference control flow graph, respectively. and Let be the fused hidden layer embedding vector of the intra-node code extracted by the graph matching network GMN, where sim is the similarity calculation function and max is the maximum value function.
3. The method according to claim 1, characterized in that, The multidimensional reward function R is expressed as: , Where α, β, and γ are weight coefficients, α+β+γ=1, α is the weight of the structural reward, β is the weight of the compilation reward score, and γ is the weight of the test reward score.
4. The method according to claim 1, characterized in that, The control flow graph is represented in the form G=(V,E); where V is a set of nodes, each node v∈V represents an atomic code block; E is a set of edges, each edge e=(u,v)∈E represents the possibility of program execution right being transferred from atomic code block u to atomic code block v.
5. A MATLAB code generation device based on control flow graph matching, characterized in that, include: The candidate code generation module is used to generate candidate MATLAB code based on the input natural language instructions using a pre-trained large language model. The control flow graph extraction module is used to parse the candidate MATLAB code and the expert reference MATLAB code respectively to obtain the corresponding candidate control flow graphs. and expert reference control flow diagram ; The graph matching computation module is used to compute candidate control flow graphs through a graph matching network. With expert reference control flow graph Structural similarity score S_CFG; The reward signal generation module is used to perform weighted fusion of the structural similarity score S_CFG, compilation reward score R_compile, and test reward score R_test based on the multidimensional reward function R to generate a comprehensive reward signal. The enhancement and fine-tuning module is used to enhance and fine-tune the large language model using a group relative strategy optimization algorithm and the reward signal as feedback, so as to obtain an optimized MATLAB code generation model. The target code generation module is used to input the natural language instructions to be generated into the optimized MATLAB code generation model and output the target MATLAB code.
6. The apparatus according to claim 5, wherein the graph matching network is a GMN, and the similarity score is calculated as follows: , in, and These represent the number of nodes in the candidate control flow graph and the expert reference control flow graph, respectively. and Let be the fused hidden layer embedding vector of the intra-node code extracted by the graph matching network GMN, where sim is the similarity calculation function and max is the maximum value function.
7. A MATLAB code generation system, characterized in that, include: One or more processors; Storage device for storing one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in any one of claims 1 to 4.
8. A computer-readable storage medium storing a computer program, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1 to 4.